Esempio n. 1
0
void MaratisPlayer::loadGamePlugin(void)
{
	char gameFile[256];
	MWindow * window = MWindow::getInstance();

	#ifdef WIN32
		getGlobalFilename(gameFile, window->getWorkingDirectory(), "Game.dll");
	#elif __APPLE__
		getGlobalFilename(gameFile, window->getWorkingDirectory(), "Game.dylib");
	#elif linux
		getGlobalFilename(gameFile, window->getWorkingDirectory(), "Game.so");
	#endif

	// try to load any other plugins in the game directory first
	// as the game may expect these to be loaded
	vector<string> files;
	readDirectory(window->getWorkingDirectory(), &files);
	for(vector<string>::iterator iFile = files.begin();
		iFile != files.end();
		iFile++)
	{
		if(*iFile == gameFile)
			continue;

		#ifdef WIN32
			if(iFile->find(".dll") != string::npos)
		#elif __APPLE__
			if(iFile->find(".dylib") != string::npos)
		#elif linux
			if(iFile->find(".so") != string::npos)
		#endif
			{
				char pluginPath[256];

				getGlobalFilename(pluginPath, window->getWorkingDirectory(), iFile->c_str());
				MPlugin* plugin = new MPlugin();
				plugin->load(pluginPath);
				if(plugin->getFilename())
					m_plugins.push_back(plugin);
				else
					SAFE_DELETE(plugin);
			}
	}

	// After all other plugins are loaded, we can load the game
	// as we assume all prerequisites are loaded
	SAFE_DELETE(m_gamePlugin);
	m_gamePlugin = new MPlugin();
	m_gamePlugin->load(gameFile);
}
Esempio n. 2
0
void MEditor::init(void)
{
	clear();
	
	// load preferences
	{
		m_preferences.load("Resources/preferences.xml");
		m_preferences.load("Resources/themes/grey/grey.xml");
	}
	
	// load font
	{
		const char * filename = m_preferences.getFontFilename();
		m_guiData.loadFont(filename);
	}
	
	// load gui
	{
		const char * path = m_preferences.getGuiPath();

		char tmp[256];
		vector<string> files;
		readDirectory(path, &files);
		
		unsigned int size = files.size();
		for(unsigned int i=0; i<size; i++)
		{
			getGlobalFilename(tmp, path, files[i].c_str());
			MTextureRef * ref = m_guiData.loadTexture(tmp);
			ref->setFilename(files[i].c_str()); // use only the file name
		}
	}
	
	// load meshes
	{
		const char * path = "Resources/meshes/gui";

		char tmp[256];
		vector<string> files;
		readDirectory(path, &files);
		
		unsigned int size = files.size();
		for(unsigned int i=0; i<size; i++)
		{
			getGlobalFilename(tmp, path, files[i].c_str());
			MMeshRef * ref = m_guiData.loadMesh(tmp);
			ref->setFilename(files[i].c_str()); // use only the file name
		}
	}
}
Esempio n. 3
0
void copySysOSX(const char* projName)
{
#ifdef _DEBUG
	const char * appName = "MaratisPlayerDebug";
#else
	const char * appName = "MaratisPlayer";
#endif

	MWindow * window = MWindow::getInstance();
	MEngine* engine = MEngine::getInstance();
	MSystemContext* system = engine->getSystemContext();

	char filename[256];
	getLocalFilename(filename, system->getWorkingDirectory(), projName);

	if(char* ext = strstr(filename, ".mproj"))
	{
		MProject proj;
		if(proj.loadXML(projName))
		{
			strcpy(ext, ".app");

			char path[256];
			char srcName[256];
			char destName[256];
			char appPath[256];
			char level[256];

			getLocalFilename(level, system->getWorkingDirectory(), proj.startLevel.c_str());
			getGlobalFilename(appPath, getPubDir(), filename);

			sprintf(path, "../../../%s.app", appName);
			getGlobalFilename(srcName, window->getCurrentDirectory(), path);
			copyDirectory(srcName, appPath);

			strcpy(ext, "");
			sprintf(srcName, "%s/Contents/MacOS/%s", appPath, appName);

			strcpy(ext, ".mproj");
			embedProject(srcName, srcName, filename, level, proj.renderer.c_str());
			chmod(srcName, 0777);

			// we need to put all data in app/Contents/Resources/
			sprintf(destName, "%s/Contents/Resources", appPath);
			createDirectory(destName);
			s_dataDir = destName;
		}
	}
}
Esempio n. 4
0
void MaratisPlayer::loadProject(MProject* proj, const char * filename)
{
	MWindow * window = MWindow::getInstance();
	MEngine * engine = MEngine::getInstance();
	
	
	// working directory
	char workingDir[256];
	getRepertory(workingDir, filename);
	window->setWorkingDirectory(workingDir);
	
	// restart
	restart();
	loadGamePlugin();
	
	// renderer
	changeRenderer(proj->renderer.c_str());
	
	// if we have a package manager, try to load the package
	if(MPackageManager* pPackMan = MEngine::getInstance()->getPackageManager())
	{
		char projName[256];
		getLocalFilename(projName, workingDir, filename);
		if(char* ext = strstr(projName, ".mproj"))
		{
			sprintf(ext, ".npk");
			char packageFile[256];
			getGlobalFilename(packageFile, workingDir, projName);
			pPackMan->loadPackage(packageFile);
		}
	}
	
	// load start level
	engine->loadLevel(proj->startLevel.c_str());
}
Esempio n. 5
0
bool MEngine::loadLevel(const char * filename)
{
	if(! filename)
		return false;

	if(! m_level)
		return false;

	if(m_game){
		if(m_game->isRunning())
		{
			m_game->onEndScene();
			m_game->onEndLevel();
		}
	}

	char globalFilename[256];
	getGlobalFilename(globalFilename, m_systemContext->getWorkingDirectory(), filename);
	if(m_levelLoader.loadData(globalFilename, m_level))
	{
		if(m_game){
			if(m_game->isRunning())
			{
				m_game->onBeginLevel();
				m_game->onBeginScene();
			}
		}
		return true;
	}

	return false;
}
Esempio n. 6
0
bool MProject::loadXML(const char * filename)
{
	char rep[256];

	if(! filename)
		return false;

	// get rep
	getRepertory(rep, filename);

	// read document
	TiXmlDocument doc(filename);
	if(! doc.LoadFile())
		return false;
	
	TiXmlHandle hDoc(&doc);
	TiXmlElement * rootNode;
	TiXmlHandle hRoot(0);

	// maratis
	rootNode = hDoc.FirstChildElement().Element();
	if(! rootNode)
		return false;

	if(strcmp(rootNode->Value(), "Maratis") != 0)
		return false;

	hRoot = TiXmlHandle(rootNode);

	// Project
	TiXmlElement * projectNode = rootNode->FirstChildElement("Project");
	if(! projectNode)
		return false;

	// renderer
	TiXmlElement * rendererNode = projectNode->FirstChildElement("renderer");
	if(rendererNode)
	{
		const char * name = rendererNode->Attribute("name");
		if(name)
		{
			renderer = name;
		}
	}
	
	// start
	TiXmlElement * startNode = projectNode->FirstChildElement("start");
	if(startNode)
	{
		const char * file = startNode->Attribute("file");
		if(file)
		{
			char levelFilename[256];
			getGlobalFilename(levelFilename, rep, file);
			startLevel = levelFilename;
		}
	}

	return true;
}
Esempio n. 7
0
bool MEngine::doesLevelExist(const char * filename)
{
    M_PROFILE_SCOPE(MEngine::doesLevelExist);
	if(! filename)
		return false;

	char globalFilename[256];
	getGlobalFilename(globalFilename, m_systemContext->getWorkingDirectory(), filename);
	return isFileExist(globalFilename);
}
Esempio n. 8
0
const char* getPubDir()
{
	MEngine* engine = MEngine::getInstance();
	MSystemContext* system = engine->getSystemContext();

	char dir[256];
	getGlobalFilename(dir, system->getWorkingDirectory(), "published");
	s_pubDir = dir;

	return s_pubDir.c_str();
}
Esempio n. 9
0
static void copyDirFiles(const char * src, const char * dest, const char * filter)
{
	vector<string> files;
	readDirectory(src, &files);
	for(int i = 0; i < files.size(); ++i)
	{
		if(strstr(files[i].c_str(), filter))
		{
			char filename[256];
			getGlobalFilename(filename, dest, files[i].c_str());
			copyFile(files[i].c_str(), filename);
		}
	}
}
Esempio n. 10
0
void MGuiFileBrowser::selectFile(unsigned int id)
{
	unsigned int fSize = m_files.size();
	if(id < fSize)
	{
		const char * name = m_files[id].c_str();
		
		if(id == 0) // go up in the hierarchy
		{
			char filename[256];
			getGlobalFilename(filename, m_currentDirectory.getSafeString(), "../");
			
			m_currentDirectory.set(filename);
			updateMainWin();
		}
		else if(strlen(name) > 0)
		{
			bool isDir = (name[0] == '/');
			if(isDir)
				name = name+1;
			
			char filename[256];
			getGlobalFilename(filename, m_currentDirectory.getSafeString(), name);
			
			if(isDir) // navigate
			{
				m_currentDirectory.set(filename);
				updateMainWin();
			}
			else
			{
				m_currentFile.set(name);
			}
		}
	}
}
Esempio n. 11
0
	void	execute(const char* projName)
	{
		MEngine* engine = MEngine::getInstance();
		MSystemContext* system = engine->getSystemContext();

		char filename[256];
		char destFilename[256];

#ifdef WIN32
		const char* pluginName = "Game.dll";
#elif __APPLE__
		const char* pluginName = "Game.dylib";
#elif linux
		const char* pluginName = "Game.so";
#endif

		getGlobalFilename(filename, system->getWorkingDirectory(), pluginName);
		getGlobalFilename(destFilename, getDataDir(), pluginName);

		if(isFileExist(filename))
		{
			copyFile(filename, destFilename);
		}
	}
Esempio n. 12
0
bool MEngine::loadLevel(const char * filename)
{
    M_PROFILE_SCOPE(MEngine::loadLevel);
	if(! filename)
		return false;

	if(! m_level)
		return false;

	char globalFilename[256];
	getGlobalFilename(globalFilename, m_systemContext->getWorkingDirectory(), filename);

	// Check if there is a level under the given name
	if(! isFileExist(globalFilename))
		return false;

	if(m_game)
	{
		if(m_game->isRunning())
		{
			m_game->onEndScene();
			m_game->onEndLevel();
		}
	}

	if(m_levelLoader.loadData(globalFilename, m_level))
	{
		if(m_game){
			if(m_game->isRunning())
			{
				m_game->onBeginLevel();
				m_game->onBeginScene();
			}
		}
		return true;
	}

	return false;
}
Esempio n. 13
0
MPackage openProjectPackage(const char* projName)
{
	MEngine* engine = MEngine::getInstance();
	MSystemContext* system = engine->getSystemContext();

	char projFile[256];
	getLocalFilename(projFile, system->getWorkingDirectory(), projName);
	char* ext = strstr(projFile, ".mproj");
	if(ext == 0)
	{
		return 0;
	}

	sprintf(ext, ".npk");
	char packFile[256];

	getGlobalFilename(packFile, getDataDir(), projFile);

	MPackageManager* packageManager = engine->getPackageManager();
	MPackage package = packageManager->openPackage(packFile);

	return package;
}
Esempio n. 14
0
void copySysLinux(const char* projName)
{
#ifdef _DEBUG
	const char * appName = "MaratisPlayerDebug";
#else
	const char * appName = "MaratisPlayer";
#endif

	MEngine* engine = MEngine::getInstance();
	MSystemContext* system = engine->getSystemContext();

	char filename[256];
	getLocalFilename(filename, system->getWorkingDirectory(), projName);

	if(char* ext = strstr(filename, ".mproj"))
	{
		*ext = 0;

		MProject proj;
		if(proj.loadXML(projName))
		{
			char destName[256];
			getGlobalFilename(destName, getPubDir(), filename);

			char level[256];
			getLocalFilename(level, system->getWorkingDirectory(), proj.startLevel.c_str());

			strcpy(ext, ".mproj");
			embedProject(appName, destName, filename, level, proj.renderer.c_str());
			chmod(destName, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);

			// find all dynamic libraries
			copyDirFiles(".", getPubDir(), ".so");
		}
	}
}
Esempio n. 15
0
void copySysWindows(const char* projName)
{
#ifdef _DEBUG
	const char * appName = "MaratisPlayerDebug.exe";
#else
	const char * appName = "MaratisPlayer.exe";
#endif

	MEngine* engine = MEngine::getInstance();
	MSystemContext* system = engine->getSystemContext();

	char filename[256];
	getLocalFilename(filename, system->getWorkingDirectory(), projName);

	if(char* ext = strstr(filename, ".mproj"))
	{
		MProject proj;
		if(proj.loadXML(projName))
		{
			strcpy(ext, ".exe");
			char destName[256];
			getGlobalFilename(destName, getPubDir(), filename);

			char level[256];
			getLocalFilename(level, system->getWorkingDirectory(), proj.startLevel.c_str());

			// we need the project "filename" to still be a .mproj for MaratisPlayer to behave
			// correctly
			strcpy(ext, ".mproj");
			embedProject(appName, destName, filename, level, proj.renderer.c_str());

			// find all dynamic libraries
			copyDirFiles(".", getPubDir(), ".dll");
		}
	}
}
Esempio n. 16
0
bool xmlMeshLoad(const char * filename, void * data)
{
    MLOG_DEBUG("xmlMeshLoad " << filename?filename:"NULL");
	
	MLevel * level = MEngine::getInstance()->getLevel();

	// read document
	TiXmlDocument doc(filename);
	if(! doc.LoadFile())
	{
	    MLOG_WARNING("TiXmlDocument load failed : " << doc.ErrorDesc() << " line " << doc.ErrorRow());
	    return false;
	}

	TiXmlHandle hDoc(&doc);
	TiXmlElement * pRootNode;
	TiXmlHandle hRoot(0);

	// Maratis
	pRootNode = hDoc.FirstChildElement().Element();
	if(! pRootNode)
	{
	    MLOG_WARNING("Cannot find any root node");
	    return false;
	}

	if(strcmp(pRootNode->Value(), "Maratis") != 0)
	{
	    MLOG_WARNING("Cannot find Maratis root node");
	    return false;
	}

	hRoot = TiXmlHandle(pRootNode);


	// Mesh
	TiXmlElement * pMeshNode = pRootNode->FirstChildElement("Mesh");
	if(! pMeshNode)
	{
	    MLOG_WARNING("Cannot find a Mesh node");
	    return false;
	}

	// create new mesh
	MMesh * mesh = (MMesh *)data;
	mesh->clear();

	char path[256];
	char meshRep[256];
	char vertShadPath[256];
	char fragShadPath[256];

	// mesh rep
	getRepertory(meshRep, filename);

	// animation
	if(! loadAnim(pMeshNode, meshRep, mesh))
	{
		// load external anim file (depracated)
		char animFilename[256];
		strcpy(animFilename, filename);
		strcpy(animFilename + strlen(animFilename) - 4, "anim");
		loadAnimFile(mesh, animFilename, meshRep);
	}

	// Textures
	TiXmlElement * texturesNode = pMeshNode->FirstChildElement("Textures");
	if(texturesNode)
	{
	    MLOG_DEBUG("entering Textures node");
		
		unsigned int numTextures = 0;
		texturesNode->QueryUIntAttribute("num", &numTextures);
		mesh->allocTextures(numTextures);

		// Texture
		TiXmlElement * textureNode = texturesNode->FirstChildElement("Texture");
		for(textureNode; textureNode; textureNode=textureNode->NextSiblingElement("Texture"))
		{
			const char * file = NULL;
			bool mipmap = true;

			// image
			TiXmlElement * imageNode = textureNode->FirstChildElement("image");
			if(imageNode)
			{
				int value = 1;
				file = imageNode->Attribute("filename");
				imageNode->QueryIntAttribute("mipmap", &value);
				mipmap = (value == 1);
			}

			if(! file)
			{
				mesh->addNewTexture(NULL);
				continue;
			}

			// load texture
			getGlobalFilename(path, meshRep, file);

			MTextureRef * texRef = level->loadTexture(path, mipmap);
			MTexture * texture = mesh->addNewTexture(texRef);

			// tile
			TiXmlElement * tileNode = textureNode->FirstChildElement("tile");
			if(tileNode)
			{
				const char * uTile = tileNode->Attribute("u");
				const char * vTile = tileNode->Attribute("v");
				if(uTile){
					if(strcmp(uTile, "clamp") == 0)
						texture->setUWrapMode(M_WRAP_CLAMP);
					else
						texture->setUWrapMode(M_WRAP_REPEAT);
				}
				if(vTile){
					if(strcmp(vTile, "clamp") == 0)
						texture->setVWrapMode(M_WRAP_CLAMP);
					else
						texture->setVWrapMode(M_WRAP_REPEAT);
				}
			}

			// translate
			TiXmlElement * translateNode = textureNode->FirstChildElement("translate");
			if(translateNode)
			{
				MVector2 translate = texture->getTexTranslate();
				translateNode->QueryFloatAttribute("x", &translate.x);
				translateNode->QueryFloatAttribute("y", &translate.y);
				texture->setTexTranslate(translate);
			}

			// scale
			TiXmlElement * scaleNode = textureNode->FirstChildElement("scale");
			if(scaleNode)
			{
				MVector2 scale = texture->getTexScale();
				scaleNode->QueryFloatAttribute("x", &scale.x);
				scaleNode->QueryFloatAttribute("y", &scale.y);
				texture->setTexScale(scale);
			}

			// rotate
			TiXmlElement * rotateNode = textureNode->FirstChildElement("rotate");
			if(rotateNode)
			{
				float angle = 0;
				rotateNode->QueryFloatAttribute("angle", &angle);
				texture->setTexRotate(angle);
			}
		}
	}


	// Materials
	TiXmlElement * materialsNode = pMeshNode->FirstChildElement("Materials");
	if(materialsNode)
	{
	    MLOG_DEBUG("entering Materials node");
		
		unsigned int numMaterials = 0;
		materialsNode->QueryUIntAttribute("num", &numMaterials);
		mesh->allocMaterials(numMaterials);

		// Material
		TiXmlElement * materialNode = materialsNode->FirstChildElement("Material");
		for(materialNode; materialNode; materialNode=materialNode->NextSiblingElement("Material"))
		{
			MMaterial * material = mesh->addNewMaterial();

			int type = 0;
			materialNode->QueryIntAttribute("type", &type);
			material->setType(type);

			float opacity=1, shininess=0, customValue=0;
			MVector3 diffuseColor;
			MVector3 specularColor;
			MVector3 emitColor;
			MVector3 customColor;

			// blend
			int blendType = 0;
			TiXmlElement * blendNode = materialNode->FirstChildElement("blend");
			if(blendNode)
				blendNode->QueryIntAttribute("type", &blendType);

			switch(blendType)
			{
			case 2:
				material->setBlendMode(M_BLENDING_ALPHA);
				break;
			case 3:
				material->setBlendMode(M_BLENDING_ADD);
				break;
			case 4:
				material->setBlendMode(M_BLENDING_PRODUCT);
				break;
			}

			// opacity
			TiXmlElement * opacityNode = materialNode->FirstChildElement("opacity");
			if(opacityNode)
				opacityNode->QueryFloatAttribute("value", &opacity);

			// shininess
			TiXmlElement * shininessNode = materialNode->FirstChildElement("shininess");
			if(shininessNode)
				shininessNode->QueryFloatAttribute("value", &shininess);

			// customValue
			TiXmlElement * customValueNode = materialNode->FirstChildElement("customValue");
			if(customValueNode)
				customValueNode->QueryFloatAttribute("value", &customValue);

			material->setOpacity(opacity);
			material->setShininess(shininess);
			material->setCustomValue(customValue);

			// diffuseColor
			TiXmlElement * diffuseColorNode = materialNode->FirstChildElement("diffuseColor");
			if(diffuseColorNode){
				diffuseColorNode->QueryFloatAttribute("r", &diffuseColor.x);
				diffuseColorNode->QueryFloatAttribute("g", &diffuseColor.y);
				diffuseColorNode->QueryFloatAttribute("b", &diffuseColor.z);
				material->setDiffuse(diffuseColor);
			}

			// specularColor
			TiXmlElement * specularColorNode = materialNode->FirstChildElement("specularColor");
			if(specularColorNode){
				specularColorNode->QueryFloatAttribute("r", &specularColor.x);
				specularColorNode->QueryFloatAttribute("g", &specularColor.y);
				specularColorNode->QueryFloatAttribute("b", &specularColor.z);
				material->setSpecular(specularColor);
			}

			// emitColor
			TiXmlElement * emitColorNode = materialNode->FirstChildElement("emitColor");
			if(emitColorNode){
				emitColorNode->QueryFloatAttribute("r", &emitColor.x);
				emitColorNode->QueryFloatAttribute("g", &emitColor.y);
				emitColorNode->QueryFloatAttribute("b", &emitColor.z);
				material->setEmit(emitColor);
			}

			// customColor
			TiXmlElement * customColorNode = materialNode->FirstChildElement("customColor");
			if(customColorNode){
				customColorNode->QueryFloatAttribute("r", &customColor.x);
				customColorNode->QueryFloatAttribute("g", &customColor.y);
				customColorNode->QueryFloatAttribute("b", &customColor.z);
				material->setCustomColor(customColor);
			}

			// TexturesPass
			TiXmlElement * texturesPassNode = materialNode->FirstChildElement("TexturesPass");
			if(texturesPassNode)
			{
				unsigned int numTexturesPass = 0;
				texturesPassNode->QueryUIntAttribute("num", &numTexturesPass);
				material->allocTexturesPass(numTexturesPass);

				// texturePass
				TiXmlElement * texturePassNode = texturesPassNode->FirstChildElement("texturePass");
				for(texturePassNode; texturePassNode; texturePassNode=texturePassNode->NextSiblingElement("texturePass"))
				{
					int textureId = -1;
					unsigned int mapChannel = 0;

					const char * mode = texturePassNode->Attribute("mode");
					texturePassNode->QueryIntAttribute("texture", &textureId);

					if(textureId < 0)
					{
						material->addTexturePass(NULL, M_TEX_COMBINE_MODULATE, 0);
						continue;
					}

					texturePassNode->QueryUIntAttribute("mapChannel", &mapChannel);

					// combine mode
					M_TEX_COMBINE_MODES texCombine = M_TEX_COMBINE_MODULATE;

					if(strcmp(mode, "modulate") == 0)
						texCombine = M_TEX_COMBINE_MODULATE;
					else if(strcmp(mode, "replace") == 0)
						texCombine = M_TEX_COMBINE_REPLACE;
					else if(strcmp(mode, "alpha") == 0)
						texCombine = M_TEX_COMBINE_ALPHA;
					else if(strcmp(mode, "dot") == 0)
						texCombine = M_TEX_COMBINE_DOT;
					else if(strcmp(mode, "add") == 0)
						texCombine = M_TEX_COMBINE_ADD;
					else if(strcmp(mode, "sub") == 0)
						texCombine = M_TEX_COMBINE_SUB;

					// add texture pass
					material->addTexturePass(mesh->getTexture(textureId), texCombine, mapChannel);
				}
			}

			// FX
			{
				// vertexShader
				const char * vertShadFile = NULL;
				TiXmlElement * vertexShaderNode = materialNode->FirstChildElement("vertexShader");
				if(vertexShaderNode){
					vertShadFile = vertexShaderNode->Attribute("file");
				}

				// fragmentShader
				const char * fragShadFile = NULL;
				TiXmlElement * fragmentShaderNode = materialNode->FirstChildElement("fragmentShader");
				if(fragmentShaderNode){
					fragShadFile = fragmentShaderNode->Attribute("file");
				}

				// create FX
				if(vertShadFile && fragShadFile)
				{
					getGlobalFilename(vertShadPath, meshRep, vertShadFile);
					getGlobalFilename(fragShadPath, meshRep, fragShadFile);

					MShaderRef * vertShad = level->loadShader(vertShadPath, M_SHADER_VERTEX);
					MShaderRef * pixShad = level->loadShader(fragShadPath, M_SHADER_PIXEL);
					if(vertShad && pixShad)
					{
						MFXRef * FXRef = level->createFX(vertShad, pixShad);
						material->setFXRef(FXRef);
					}
				}
			}

			// ZFX (optional optim)
			{
				// ZVertexShader
				const char * vertShadFile = NULL;
				TiXmlElement * vertexShaderNode = materialNode->FirstChildElement("ZVertexShader");
				if(vertexShaderNode){
					vertShadFile = vertexShaderNode->Attribute("file");
				}

				// ZFragmentShader
				const char * fragShadFile = NULL;
				TiXmlElement * fragmentShaderNode = materialNode->FirstChildElement("ZFragmentShader");
				if(fragmentShaderNode){
					fragShadFile = fragmentShaderNode->Attribute("file");
				}

				// create ZFX
				if(vertShadFile && fragShadFile)
				{
					getGlobalFilename(vertShadPath, meshRep, vertShadFile);
					getGlobalFilename(fragShadPath, meshRep, fragShadFile);

					MShaderRef * vertShad = level->loadShader(vertShadPath, M_SHADER_VERTEX);
					MShaderRef * pixShad = level->loadShader(fragShadPath, M_SHADER_PIXEL);
					if(vertShad && pixShad)
					{
						MFXRef * ZFXRef = level->createFX(vertShad, pixShad);
						material->setZFXRef(ZFXRef);
					}
				}
			}
		}
	}


	// Bones
	TiXmlElement * bonesNode = pMeshNode->FirstChildElement("Bones");
	if(bonesNode)
	{
	    MLOG_DEBUG("entering Bones node");
		
		MArmature * armature = mesh->createArmature();

		unsigned int b, numBones = 0;
		bonesNode->QueryUIntAttribute("num", &numBones);
		armature->allocBones(numBones);

		// add bones
		for(b=0; b<numBones; b++)
			armature->addNewBone();
		b = 0;

		// Bone
		TiXmlElement * boneNode = bonesNode->FirstChildElement("Bone");
		for(boneNode; boneNode; boneNode=boneNode->NextSiblingElement("Bone"))
		{
			if(b >= armature->getBonesNumber())
				break;

			MOBone * bone = armature->getBone(b);

			const char * name = boneNode->Attribute("name");
			if(name)
				bone->setName(name);

			// parent
			TiXmlElement * parentNode = boneNode->FirstChildElement("parent");
			if(parentNode){
				unsigned int boneId = 0;
				parentNode->QueryUIntAttribute("id", &boneId);
				bone->linkTo(armature->getBone(boneId));
			}

			// position
			TiXmlElement * positionNode = boneNode->FirstChildElement("position");
			if(positionNode){
				MVector3 position;
				positionNode->QueryFloatAttribute("x", &position.x);
				positionNode->QueryFloatAttribute("y", &position.y);
				positionNode->QueryFloatAttribute("z", &position.z);
				bone->setPosition(position);
			}

			// rotation
			TiXmlElement * rotationNode = boneNode->FirstChildElement("rotation");
			if(rotationNode){

				MVector3 euler;
				rotationNode->QueryFloatAttribute("x", &euler.x);
				rotationNode->QueryFloatAttribute("y", &euler.y);
				rotationNode->QueryFloatAttribute("z", &euler.z);
				bone->setEulerRotation(euler);
			}

			// scale
			TiXmlElement * scaleNode = boneNode->FirstChildElement("scale");
			if(scaleNode){
				MVector3 scale;
				scaleNode->QueryFloatAttribute("x", &scale.x);
				scaleNode->QueryFloatAttribute("y", &scale.y);
				scaleNode->QueryFloatAttribute("z", &scale.z);
				bone->setScale(scale);
			}

			b++;
		}

		// construct bones inverse pose matrix
		armature->constructBonesInversePoseMatrix();
	}


	// SubMeshs
	TiXmlElement * subMeshsNode = pMeshNode->FirstChildElement("SubMeshs");
	if(! subMeshsNode)
		return true;

	unsigned int numSubMeshs = 0;
	subMeshsNode->QueryUIntAttribute("num", &numSubMeshs);
	if(numSubMeshs == 0)
		return true;

	// alloc subMeshs
	MSubMesh * subMeshs = mesh->allocSubMeshs(numSubMeshs);

	// BoundingBox
	TiXmlElement * boundingBoxNode = pMeshNode->FirstChildElement("BoundingBox");
	if(boundingBoxNode)
	{
		MVector3 * min = &mesh->getBoundingBox()->min;
		MVector3 * max = &mesh->getBoundingBox()->max;

		boundingBoxNode->QueryFloatAttribute("minx", &min->x);
		boundingBoxNode->QueryFloatAttribute("miny", &min->y);
		boundingBoxNode->QueryFloatAttribute("minz", &min->z);

		boundingBoxNode->QueryFloatAttribute("maxx", &max->x);
		boundingBoxNode->QueryFloatAttribute("maxy", &max->y);
		boundingBoxNode->QueryFloatAttribute("maxz", &max->z);
	}

	// SubMesh
	TiXmlElement * SubMeshNode = subMeshsNode->FirstChildElement("SubMesh");
	for(SubMeshNode; SubMeshNode; SubMeshNode=SubMeshNode->NextSiblingElement("SubMesh"))
	{
		MSubMesh * subMesh = subMeshs;

		// BoundingBox
		boundingBoxNode = SubMeshNode->FirstChildElement("BoundingBox");
		if(boundingBoxNode)
		{
			MVector3 * min = &subMesh->getBoundingBox()->min;
			MVector3 * max = &subMesh->getBoundingBox()->max;

			boundingBoxNode->QueryFloatAttribute("minx", &min->x);
			boundingBoxNode->QueryFloatAttribute("miny", &min->y);
			boundingBoxNode->QueryFloatAttribute("minz", &min->z);

			boundingBoxNode->QueryFloatAttribute("maxx", &max->x);
			boundingBoxNode->QueryFloatAttribute("maxy", &max->y);
			boundingBoxNode->QueryFloatAttribute("maxz", &max->z);
		}

		// Vertices
		TiXmlElement * verticesNode = SubMeshNode->FirstChildElement("Vertices");
		if(verticesNode)
		{
			unsigned int numVertices = 0;
			verticesNode->QueryUIntAttribute("num", &numVertices);
			MVector3 * vertices = subMesh->allocVertices(numVertices);

			// vertex
			TiXmlElement * vertexNode = verticesNode->FirstChildElement("vertex");
			for(vertexNode; vertexNode; vertexNode=vertexNode->NextSiblingElement("vertex"))
			{
				vertexNode->QueryFloatAttribute("x", &vertices->x);
				vertexNode->QueryFloatAttribute("y", &vertices->y);
				vertexNode->QueryFloatAttribute("z", &vertices->z);
				vertices++;
			}
		}

		// Normals
		TiXmlElement * normalsNode = SubMeshNode->FirstChildElement("Normals");
		if(normalsNode)
		{
			unsigned int numNormals = 0;
			normalsNode->QueryUIntAttribute("num", &numNormals);
			MVector3 * normals = subMesh->allocNormals(numNormals);

			// normal
			TiXmlElement * normalNode = normalsNode->FirstChildElement("normal");
			for(normalNode; normalNode; normalNode=normalNode->NextSiblingElement("normal"))
			{
				normalNode->QueryFloatAttribute("x", &normals->x);
				normalNode->QueryFloatAttribute("y", &normals->y);
				normalNode->QueryFloatAttribute("z", &normals->z);
				normals->normalize();
				normals++;
			}
		}

		// Tangents
		TiXmlElement * tangentsNode = SubMeshNode->FirstChildElement("Tangents");
		if(tangentsNode)
		{
			unsigned int numTangents = 0;
			tangentsNode->QueryUIntAttribute("num", &numTangents);
			MVector3 * tangents = subMesh->allocTangents(numTangents);

			// tangent
			TiXmlElement * tangentNode = tangentsNode->FirstChildElement("tangent");
			for(tangentNode; tangentNode; tangentNode=tangentNode->NextSiblingElement("tangent"))
			{
				tangentNode->QueryFloatAttribute("x", &tangents->x);
				tangentNode->QueryFloatAttribute("y", &tangents->y);
				tangentNode->QueryFloatAttribute("z", &tangents->z);
				tangents->normalize();
				tangents++;
			}
		}

		// TexCoords
		TiXmlElement * texCoordsNode = SubMeshNode->FirstChildElement("TexCoords");
		if(texCoordsNode)
		{
			// num
			unsigned int numTexCoords = 0;
			texCoordsNode->QueryUIntAttribute("num", &numTexCoords);
			MVector2 * texCoords = subMesh->allocTexCoords(numTexCoords);

			// mapChannels
			unsigned int numVertices = subMesh->getVerticesSize();
			const char * mapChannelsData = texCoordsNode->Attribute("mapChannels");

			// read channels
			if(mapChannelsData)
			{
				char str[256];
				strcpy(str, mapChannelsData);
				char * pch;

				unsigned int offset = 0;
				pch = strtok(str, " ");
				while(pch != NULL)
				{
					unsigned int channel = 0;
					sscanf(pch, "%d", &channel);

					subMesh->setMapChannelOffset(channel, offset);

					pch = strtok(NULL, " ");
					offset += numVertices;
				}
			}
			// create default channels
			else if((numVertices > 0) && (numTexCoords > numVertices))
			{
				unsigned int numChannels = numTexCoords / numVertices;
				for(unsigned int c=0; c<numChannels; c++)
					subMesh->setMapChannelOffset(c, numVertices*c);
			}

			// texCoord
			TiXmlElement * texCoordNode = texCoordsNode->FirstChildElement("texCoord");
			for(texCoordNode; texCoordNode; texCoordNode=texCoordNode->NextSiblingElement("texCoord"))
			{
				texCoordNode->QueryFloatAttribute("x", &texCoords->x);
				texCoordNode->QueryFloatAttribute("y", &texCoords->y);
				texCoords++;
			}
		}

		// Colors
		TiXmlElement * colorsNode = SubMeshNode->FirstChildElement("Colors");
		if(colorsNode)
		{
			unsigned int numColors = 0;
			colorsNode->QueryUIntAttribute("num", &numColors);
			MColor * colors = subMesh->allocColors(numColors);

			// color
			TiXmlElement * colorNode = colorsNode->FirstChildElement("color");
			for(colorNode; colorNode; colorNode=colorNode->NextSiblingElement("color"))
			{
				float x = 1, y = 1, z = 1, w = 1;
				colorNode->QueryFloatAttribute("x", &x);
				colorNode->QueryFloatAttribute("y", &y);
				colorNode->QueryFloatAttribute("z", &z);
				colorNode->QueryFloatAttribute("w", &w);

				colors->r = (unsigned char)x*255;
				colors->g = (unsigned char)y*255;
				colors->b = (unsigned char)z*255;
				colors->a = (unsigned char)w*255;;

				colors++;
			}
		}

		// Indices
		TiXmlElement * indicesNode = SubMeshNode->FirstChildElement("Indices");
		if(indicesNode)
		{
			M_TYPES indicesType;
			unsigned int vSize = subMesh->getVerticesSize();

			if(vSize < 65536){
				indicesType = M_USHORT;
			}
			else{
				indicesType = M_UINT;
			}

			unsigned int numIndices = 0;
			indicesNode->QueryUIntAttribute("num", &numIndices);
			subMesh->allocIndices(numIndices, indicesType);

			// indices
			TiXmlElement * indexNode = indicesNode->FirstChildElement("index");
			switch(indicesType)
			{
			case M_USHORT:
				{
					unsigned short * indices = (unsigned short *)subMesh->getIndices();
					for(indexNode; indexNode; indexNode=indexNode->NextSiblingElement("index"))
					{
						unsigned int id;
						indexNode->QueryUIntAttribute("value", &id);
						*indices = (unsigned short)id;
						indices++;
					}
				}
				break;
			case M_UINT:
				{
					unsigned int * indices = (unsigned int *)subMesh->getIndices();
					for(indexNode; indexNode; indexNode=indexNode->NextSiblingElement("index"))
					{
						indexNode->QueryUIntAttribute("value", indices);
						indices++;
					}
				}
				break;
			}
		}

		// Skins
		TiXmlElement * skinsNode = SubMeshNode->FirstChildElement("Skins");
		if(skinsNode)
		{
			MSkinData * skinData = subMesh->createSkinData();

			unsigned int numSkins = 0;
			skinsNode->QueryUIntAttribute("num", &numSkins);
			MSkinPoint * skinPoints = skinData->allocPoints(numSkins);

			// skin
			TiXmlElement * skinNode = skinsNode->FirstChildElement("skin");
			for(skinNode; skinNode; skinNode=skinNode->NextSiblingElement("skin"))
			{
				unsigned int vertexId = 0;
				unsigned int numBones = 0;

				skinNode->QueryUIntAttribute("vertex", &vertexId);
				skinNode->QueryUIntAttribute("numBones", &numBones);

				if(numBones > 0)
				{
					skinPoints->setVertexId(vertexId);
					skinPoints->allocateBonesLinks(numBones);

					unsigned short * bonesIds = skinPoints->getBonesIds();
					float * bonesWeights = skinPoints->getBonesWeights();

					TiXmlElement * boneNode = skinNode->FirstChildElement("bone");
					for(boneNode; boneNode; boneNode=boneNode->NextSiblingElement("bone"))
					{
						unsigned int id;
						boneNode->QueryUIntAttribute("id", &id);
						boneNode->QueryFloatAttribute("weight", bonesWeights);

						*bonesIds = id;

						bonesIds++;
						bonesWeights++;
					}
				}

				skinPoints++;
			}
		}

		// Displays
		TiXmlElement * displaysNode = SubMeshNode->FirstChildElement("Displays");
		if(displaysNode)
		{
			unsigned int numDisplays = 0;
			displaysNode->QueryUIntAttribute("num", &numDisplays);
			subMesh->allocDisplays(numDisplays);

			// display
			TiXmlElement * displayNode = displaysNode->FirstChildElement("display");
			for(displayNode; displayNode; displayNode=displayNode->NextSiblingElement("display"))
			{
				unsigned int begin, size, material, cullFace = 0;

				displayNode->QueryUIntAttribute("begin", &begin);
				displayNode->QueryUIntAttribute("size", &size);
				displayNode->QueryUIntAttribute("material", &material);
				displayNode->QueryUIntAttribute("cullFace", &cullFace);

				// create display
				MDisplay * display = subMesh->addNewDisplay(M_PRIMITIVE_TRIANGLES, begin, size);

				// set material
				if(material < mesh->getMaterialsNumber())
					display->setMaterial(mesh->getMaterial(material));

				// set cull mode
				M_CULL_MODES cullMode = M_CULL_BACK;
				if(cullFace == 1)
					cullMode = M_CULL_FRONT;
				else if(cullFace == 2)
					cullMode = M_CULL_NONE;
				display->setCullMode(cullMode);
			}
		}

		// generate tangents if needed
		if(! subMesh->getTangents())
			generateTangents(subMesh);


		subMeshs++;
	}

    MLOG_DEBUG("xmlMeshLoad success: "<<numSubMeshs<<" submeshs found");
	
	return true;
}
Esempio n. 17
0
void MGuiFileBrowser::updateMainWin(void)
{
	m_mainWin->clear();
	
	m_files.clear();
	m_files.push_back(string(".."));
	
	if(readDirectory(m_currentDirectory.getSafeString(), &m_files))
	{
		char filename[256];
		
		
		// prepare
		unsigned int f, fSize =  m_files.size();
		for(f=0; f<fSize; f++)
		{
			const char * name = m_files[f].c_str();
			
			if(f > 0)
			{
				getGlobalFilename(filename, m_currentDirectory.getSafeString(), name);
				if(isDirectory(filename))
					m_files[f] = "/" + m_files[f];
			}
		}
		
		// sort
		sort(m_files.begin(), m_files.end(), stringCompare);
		
		
		// scan
		for(f=0; f<fSize; f++)
		{
			float y = m_browserHeight*f;
			const char * name = m_files[f].c_str();
			
			string textName = name;
			
			float grey = 0.24f;
			if((f%2) == 0)
				grey = 0.2f;
			
			MGuiButton * button = new MGuiButton(
				MVector2(0, y),
				MVector2(m_mainWin->getScale().x,
				m_browserHeight),
				MVector3(grey*0.9f, grey*1.1f, grey*1.3f),
				NULL
			);
			
			button->setHighLightColor(MVector3(0.35f));
			button->setPressedColor(MVector3(0.35f));
			
			m_mainWin->addButton(button);
			
			MGuiText * text = new MGuiText(textName.c_str(), MVector2(0, y), 16, m_browserTextColor);
			m_mainWin->addText(text);
		}
	}
	
	m_mainWin->resizeScroll();
}
Esempio n. 18
0
bool loadAnim(TiXmlElement * rootNode, const char * repertory, MMesh * mesh)
{
	MLevel * level = MEngine::getInstance()->getLevel();


	// Animation
	TiXmlElement * animationNode = rootNode->FirstChildElement("Animation");
	if(! animationNode)
		return false;

	// TextureAnim
	TiXmlElement * texAnimNode = animationNode->FirstChildElement("TextureAnim");
	if(texAnimNode)
	{
		const char * file = texAnimNode->Attribute("file");
		if(file)
		{
			// path
			char path[256];
			getGlobalFilename(path, repertory, file);

			// load textures anim
			MTexturesAnimRef * texturesAnim = level->loadTexturesAnim(path);
			mesh->setTexturesAnimRef(texturesAnim);
		}
	}

	// MaterialAnim
	TiXmlElement * matAnimNode = animationNode->FirstChildElement("MaterialAnim");
	if(matAnimNode)
	{
		const char * file = matAnimNode->Attribute("file");
		if(file)
		{
			// path
			char path[256];
			getGlobalFilename(path, repertory, file);

			// load materials anim
			MMaterialsAnimRef * materialsAnim = level->loadMaterialsAnim(path);
			mesh->setMaterialsAnimRef(materialsAnim);
		}
	}

	// ArmatureAnim
	TiXmlElement * armAnimNode = animationNode->FirstChildElement("ArmatureAnim");
	if(armAnimNode)
	{
		const char * file = armAnimNode->Attribute("file");
		if(file)
		{
			// path
			char path[256];
			getGlobalFilename(path, repertory, file);

			// load armature anim
			MArmatureAnimRef * armatureAnim = level->loadArmatureAnim(path);
			mesh->setArmatureAnimRef(armatureAnim);
		}
	}

	// Anims
	TiXmlElement * animsNode = animationNode->FirstChildElement("Anims");
	if(animsNode)
	{
		unsigned int animsNumber = 0;
		animsNode->QueryUIntAttribute("num", &animsNumber);
		if(animsNumber > 0)
		{
			MAnimRange * animsRanges = mesh->allocAnimsRanges(animsNumber);

			TiXmlElement * animNode = animsNode->FirstChildElement("anim");
			for(animNode; animNode; animNode=animNode->NextSiblingElement("anim"))
			{
				animNode->QueryIntAttribute("start", &animsRanges->start);
				animNode->QueryIntAttribute("end", &animsRanges->end);
				animNode->QueryIntAttribute("loops", &animsRanges->loops);

				animsRanges++;
			}
		}
	}

	return true;
}
Esempio n. 19
0
	void execute(const char* projName)
	{
		MEngine* engine = MEngine::getInstance();
		MSystemContext* system = engine->getSystemContext();
		MPackageManager* packageManager = engine->getPackageManager();

		char directory[256], localFilename[256];
		getGlobalFilename(directory, system->getWorkingDirectory(), "meshs");
		vector<string> files;
		readDirectory(directory, &files, 1, 1);

		MLevel* currentLevel = engine->getLevel();
		MLevel* tempLevel = new MLevel();
		engine->setLevel(tempLevel);

		MMesh* mesh = MMesh::getNew();
		MArmatureAnim* armAnim = MArmatureAnim::getNew();
		MTexturesAnim* texAnim = MTexturesAnim::getNew();
		MMaterialsAnim* matAnim = MMaterialsAnim::getNew();

		// open package and scan meshes
		MPackage package = openProjectPackage(projName);
		for(int i = 0; i < files.size(); ++i)
		{
			bool binarized = false;
			
			if(strstr(files[i].c_str(), "._bin") != 0)
				continue;
			
			// export bin
			if(strstr(files[i].c_str(), ".mesh") != 0)
			{
				if(engine->getMeshLoader()->loadData(files[i].c_str(), mesh))
					binarized = exportMeshBin((files[i] + "._bin").c_str(), mesh);
			}
			else if (strstr(files[i].c_str(), ".maa") != 0)
			{
				if(engine->getArmatureAnimLoader()->loadData(files[i].c_str(), armAnim))
					binarized = exportArmatureAnimBin((files[i] + "._bin").c_str(), armAnim);
			}
			else if (strstr(files[i].c_str(), ".mma") != 0)
			{
				if(engine->getMaterialsAnimLoader()->loadData(files[i].c_str(), matAnim))
					binarized = exportMaterialsAnimBin((files[i] + "._bin").c_str(), matAnim);
			}
			else if (strstr(files[i].c_str(), ".mta") != 0)
			{
				if(engine->getTexturesAnimLoader()->loadData(files[i].c_str(), texAnim))
					binarized = exportTexturesAnimBin((files[i] + "._bin").c_str(), texAnim);
			}
			else
			{
				// try to export unknow format
				if(engine->getMeshLoader()->loadData(files[i].c_str(), mesh))
				{
					binarized = exportMeshBin((files[i] + "._bin").c_str(), mesh);
					
					// try to export animation
					MArmatureAnimRef * maaRef = mesh->getArmatureAnimRef();
					if(maaRef)
					{
						exportArmatureAnimBin((files[i] + ".maa._bin").c_str(), maaRef->getArmatureAnim());
						getLocalFilename(localFilename, system->getWorkingDirectory(), (files[i] + ".maa").c_str());
						packageManager->addFileToPackage((files[i] + ".maa._bin").c_str(), package, localFilename);
					}
				}
			}

			tempLevel->clear();

			// pack file
			getLocalFilename(localFilename, system->getWorkingDirectory(), files[i].c_str());
			
			if(binarized)
				packageManager->addFileToPackage((files[i] + "._bin").c_str(), package, localFilename); // pack bin file
			else
				packageManager->addFileToPackage(files[i].c_str(), package, localFilename); // pack original file
		}
		packageManager->closePackage(package);

		
		// clear mesh
		mesh->destroy();
		armAnim->destroy();
		texAnim->destroy();
		matAnim->destroy();

		// remove bin
		for(int i = 0; i < files.size(); ++i)
		{
			remove((files[i] + "._bin").c_str());
			remove((files[i] + ".maa._bin").c_str());
		}

		// restore level
		engine->setLevel(currentLevel);
		SAFE_DELETE(tempLevel);
	}