Exemplo n.º 1
0
MF_API MFModel* MFModel_Create(const char *pFilename)
{
	const char* pOriginalFilename = pFilename;

	// see if it's already loaded
	MFModelPool::Iterator it = gModelBank.Get(pOriginalFilename);
	MFModelTemplate *pTemplate = it ? *it : NULL;

	if(!pTemplate)
	{
		char *pTemplateData = NULL;

		MFFile *hFile = MFFileSystem_Open(MFStr("%s.mdl", pFilename), MFOF_Read|MFOF_Binary);
		if(hFile)
		{
			int64 size = MFFile_GetSize(hFile);

			if(size > 0)
			{
				// allocate memory and load file
				pTemplateData = (char*)MFHeap_Alloc((size_t)size + MFString_Length(pFilename) + 1);
				MFFile_Read(hFile, pTemplateData, (size_t)size);
				MFFile_Close(hFile);

				MFString_Copy(&pTemplateData[size], pFilename);
				pFilename = &pTemplateData[size];
			}
		}
		else
		{
#if defined(ALLOW_LOAD_FROM_SOURCE_DATA)
			// try to load from source data
			const char * const pExt[] = { ".f3d", ".dae", ".x", ".ase", ".obj", ".md2", ".md3", ".me2", NULL };
			const char * const *ppExt = pExt;
			MFIntModel *pIM = NULL;
			while(!pIM && *ppExt)
			{
				MFString tempFilename = MFString::Format("%s%s", pFilename, *ppExt);
				pIM = MFIntModel_CreateFromFile(tempFilename.CStr());
				if(pIM)
				{
					pFilename = MFString_Copy((char*)MFHeap_Alloc(tempFilename.NumBytes()+1), tempFilename.CStr());
					break;
				}
				++ppExt;
			}

			if(pIM)
			{
				MFIntModel_Optimise(pIM);

				size_t size;
				MFIntModel_CreateRuntimeData(pIM, (void**)&pTemplateData, &size, MFSystem_GetCurrentPlatform());

				MFFile *pFile = MFFileSystem_Open(MFStr("cache:%s.mdl", pOriginalFilename), MFOF_Write | MFOF_Binary);
				if(pFile)
				{
					MFFile_Write(pFile, pTemplateData, size, false);
					MFFile_Close(pFile);
				}

				MFIntModel_Destroy(pIM);
			}
#endif
		}

		if(!pTemplateData)
			return NULL;

		// check ID string
		MFDebug_Assert(*(uint32*)pTemplateData == MFMAKEFOURCC('M', 'D', 'L', '2'), "Incorrect MFModel version.");

		// store filename for later reference
		pTemplate = (MFModelTemplate*)pTemplateData;
		pTemplate->pFilename = pFilename;

		gModelBank.Add(pOriginalFilename, pTemplate);

		MFModel_FixUp(pTemplate, true);

		MFModelDataChunk *pChunk = MFModel_GetDataChunk(pTemplate, MFChunkType_SubObjects);

		if(pChunk)
		{
			MFModelSubObject *pSubobjects = (MFModelSubObject*)pChunk->pData;

			for(int a=0; a<pChunk->count; a++)
			{
//				pSubobjects[a].pMaterial = MFMaterial_Create((char*)pSubobjects[a].pMaterial);

				for(int b=0; b<pSubobjects[a].numMeshChunks; b++)
				{
					MFModel_CreateMeshChunk(MFModel_GetMeshChunkInternal(pTemplate, a, b));
				}
			}
		}
	}

	MFModel *pModel;
	pModel = (MFModel*)MFHeap_Alloc(sizeof(MFModel));

	pModel->worldMatrix = MFMatrix::identity;
	pModel->modelColour = MFVector::one;
	pModel->pTemplate = pTemplate;
	pModel->pAnimation = NULL;

	++pTemplate->refCount;

	return pModel;
}
Exemplo n.º 2
0
int MFString::Parse(const char *pFormat, ...)
{
	if(!pData || !pFormat)
		return 0;

	va_list arglist;
	va_start(arglist, pFormat);

	const char *pS = pData->pMemory;
	int numArgs = 0;

	MFString format = GetNextBit(pFormat);
	int numChars = Match(pS, format.CStr());

	while(*pFormat && numChars >= 0)
	{
		pS += numChars;
		++pFormat;

		int length = -1;

		// gather format options...
		while(*pFormat)
		{
			int c = MFToLower(*pFormat++);
			if(c == 's')
			{
				MFString *pStr = va_arg(arglist, MFString*);
				++numArgs;

				format = GetNextBit(pFormat);

				if(length >= 0)
				{
					MFString s(pS, (size_t)length);
					*pStr = s;

					numChars = Match(pS, format.CStr());
				}
				else if(format.NumBytes() == 0)
				{
					*pStr = pS;
				}
				else
				{
					const char *pEnd = pS;
					while(*pEnd && (numChars = Match(pEnd, format.CStr())) < 0)
						++pEnd;

					MFString s(pS, (size_t)(pEnd - pS));
					*pStr = s;
				}

				pS += pStr->NumBytes();
				break;
			}
			else if(c == 'd' || c == 'i')
			{
				int *pInt = va_arg(arglist, int*);
				++numArgs;

				bool bNeg = *pS == '-';
				if(*pS == '-' || *pS == '+')
					++pS;

				*pInt = 0;
				while(MFIsNumeric(*pS) && ((uint32&)length)-- > 0)
					*pInt = *pInt*10 + *pS++ - '0';

				if(bNeg)
					*pInt = -*pInt;

				format = GetNextBit(pFormat);
				numChars = Match(pS, format.CStr());
				break;
			}