⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entitytable.cpp

📁 3D游戏场景编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	
    FieldIter	fi;
	int				res;
	int				published;
	TopType			tt;
	const char *	typeName;
	const char *	fieldName;
	const char *	defaultValue;

	res = CParser_GetFirstField(&fi, pEntityType, &tt, &typeName, &fieldName, &published, &defaultValue);
	while (res)
	{
        if (strcmp (pPropertyName, fieldName) == 0)
        {
            return typeName;
        }
		res = CParser_GetNextField(&fi, &tt, &typeName, &fieldName, &published, &defaultValue);
	}

    assert (0);    // doesn't even exist...
    return "Invalid";  // really need an invalid return here....
}

static int __cdecl PropCompareFcn
    (
      const void *elem1,
      const void *elem2
    )
{
    EntityProperty const *p1;
    EntityProperty const *p2;

	p1 = static_cast<EntityProperty const *>(elem1);
	p2 = static_cast<EntityProperty const *>(elem2);

	return stricmp (p1->pKey, p2->pKey);
}

static void EntityTable_FillProperty
    (
	  EntityProperty *p,
	  char const *Keyname,
	  TopType tt,
	  int Keynum,
	  char const *Value,
	  int published
	)
{
	static char * DefaultTypeValues[] =
	{
	    "0",			// T_INT
		"0.0",			// T_FLOAT
		"255 255 255",  // T_COLOR
		"0 0 0",		// T_POINT,
		"<null>",		// T_STRING
		"<null>",		// T_MODEL
		"<null>",		// T_PTR
		"<null>"		// T_STRUCT
	};

    p->pKey = Util_Strdup (Keyname);
	if ((tt < T_INT) || (tt > T_STRUCT))
	{
	    tt = T_STRING;
	}
    p->type = tt;
    p->KeyNum = Keynum;
	if (Value == NULL)
	{
		// if the value is NULL, then set it to the default
		// for this property type
		p->pValue = Util_Strdup (DefaultTypeValues[tt]);
	}
	else
	{
		p->pValue = Util_Strdup (Value);
	}
	p->published = published;
}

EntityPropertiesList *EntityTable_GetEntityPropertiesFromName
    (
	  const EntityTable *pTable,
	  CString const Classname,
	  EntityTablePropType WhichProps
	)
{
    EntityPropertiesList *pProps;
    int propNo;
    Type *pEntityType;

    // Now obtain this class type's type entry
    pEntityType = EntityTable_GetEntityTypeFromName (pTable, Classname);
    if (pEntityType == NULL)
    {
		// if we get here, we've been passed an entity type that wasn't
		// parsed.  It's an error, and the caller will have to handle
		// it somehow.
        return NULL;
    }

    // The stuff exists, so let's create the properties array.
	// Since there's no API to count the number of entries in the type array,
	// I'll loop through and count them...
    FieldIter	fi;
	int				res;
	int				published;
	TopType			tt;
	const char *	typeName;
	const char *	fieldName;
	const char *	defaultValue;

	int nEntries = 1;  // FIRST entry is for the class name....
    // should GetFirstField take a const for tp??
	res = CParser_GetFirstField(&fi, pEntityType, &tt, &typeName, &fieldName, &published, &defaultValue);
	while (res)
	{
		++nEntries;
		res = CParser_GetNextField(&fi, &tt, &typeName, &fieldName, &published, &defaultValue);
	}

    pProps = (EntityPropertiesList *)geRam_Allocate
        (sizeof (EntityPropertiesList) + nEntries * sizeof (EntityProperty));

    if (pProps == NULL)
    {
        return NULL;
    }

    pProps->NumProps = 0;
    // initialize the structure to all NULLs
    for (propNo = 0; propNo < nEntries; propNo++)
    {
        EntityProperty *p;

        p = &(pProps->Props[propNo]);

        p->KeyNum = -1;
        p->pKey = NULL;
        p->pValue = NULL;
    }


    // fill structure with the properties...
    propNo = 0;

//	EntityProperty *p = &(pProps->Props[propNo]);
	EntityTable_FillProperty (&(pProps->Props[0]), ClassnameKey, tt, 0, Classname, 0  /*not published*/);

	propNo = 1;
	res = CParser_GetFirstField (&fi, pEntityType, &tt, &typeName, &fieldName, &published, &defaultValue);
	while (res)
	{
		if ((WhichProps == ET_ALL) || (published != 0))
		{
			EntityTable_FillProperty
				(
				  &(pProps->Props[propNo]),
				  fieldName,
				  tt,
				  0,
				  defaultValue,
				  published
				);

            propNo++;
        }        
		res = CParser_GetNextField(&fi, &tt, &typeName, &fieldName, &published, &defaultValue);
    }
    pProps->NumProps = propNo;
    // Sort the list...
    qsort (pProps->Props, propNo, sizeof (EntityProperty), PropCompareFcn);

    // structure is built...return it.
    return pProps;
}

void EntityTable_ReleaseEntityProperties
    (
      EntityPropertiesList const *pcProps
    )
{
    int propNo;
	EntityPropertiesList *pProps = const_cast<EntityPropertiesList *>(pcProps);

    for (propNo = 0; propNo < pProps->NumProps; propNo++)
    {
        EntityProperty *p;

        p = &(pProps->Props[propNo]);
        if (p->pKey != NULL)
        {
            geRam_Free (p->pKey);
        }
        if (p->pValue != NULL)
        {
            geRam_Free (p->pValue);
        }
        p->KeyNum = -1;
    }

    geRam_Free (pProps);
}

EntityTypeList *EntityTable_GetAvailableEntityTypes
	(
	  const EntityTable *pTable
	)
{
    StructIter si;
    Type *tp;
	int TypeCount;
	EntityTypeList *pList;


	TypeCount = EntityTable_GetTypeCount (pTable);


	// allocate memory for the structure
	pList = (EntityTypeList *)geRam_Allocate (sizeof (EntityTypeList) + (TypeCount*sizeof (char *)));
	if (pList == NULL)
	{
	    return NULL;
	}
	pList->nTypes = 0;

	// now get the type names and put them in the list
	tp = CParser_GetFirstStruct (&si, pTable->EntityInfo);
	while (tp != NULL)
	{	
        const char *typeName;

        typeName = CParser_GetTypeName (tp);
		pList->TypeNames[pList->nTypes] = Util_Strdup (typeName);
		++(pList->nTypes);
        tp = CParser_GetNextStruct (&si);
	}


    return pList;

}

void EntityTable_ReleaseEntityTypes
	(
	  EntityTypeList *pList
	)
{
	assert (pList != NULL);

	// deallocate all of the strings...
	while (pList->nTypes)
	{
		char *c;

		c = pList->TypeNames[pList->nTypes-1];
		if (c != NULL)
		{
		    geRam_Free (c);
		}
		--(pList->nTypes);
	}

	// and free the structure
	geRam_Free (pList);
}

int EntityTable_WriteTypesToMap
	(
	  const EntityTable *pTable,
	  FILE *f
	)
{
	assert (pTable->EntityInfo != NULL);
	assert (f != NULL);

	return CParser_WriteTypesToMap (pTable->EntityInfo, f);
}

// GetTypeCount added 02/20/98 by Jim
int EntityTable_GetTypeCount
	(
	  const EntityTable *pTable
	)
{
    StructIter si;
    Type *tp;
	int TypeCount;

	TypeCount = 0;
    tp = CParser_GetFirstStruct (&si, pTable->EntityInfo);
    while (tp != NULL)
    {
		++TypeCount;
        tp = CParser_GetNextStruct (&si);
    }

	return TypeCount;
}

// This function assumes that the passed bitmap is a Windows 16-bit 555.
static TypeBmpEntry *CreateEntryFromLoadedBitmap (TypeBmpArray *pBitmapCache, Type *pType, HBITMAP bmpHandle)
{
	BITMAPINFO *bmi;
	BITMAPINFOHEADER *bmih;
	void *bits;
	geBitmap *pBitmap;
	HDC dcMem;
	int retval;

	dcMem = CreateCompatibleDC (NULL);

	bmi = (BITMAPINFO *)geRam_Allocate (sizeof (BITMAPINFO) + (255 * sizeof (RGBQUAD)));
	bmih = &bmi->bmiHeader;

	bmih->biSize = sizeof (BITMAPINFOHEADER);
	bmih->biBitCount = 0;

	// get bitmap information
	retval = ::GetDIBits (dcMem, bmpHandle, 0, 0, NULL, bmi, DIB_RGB_COLORS);

	// and then get the bits...
	bmih->biBitCount = 32;
	bmih->biCompression = BI_BITFIELDS;
	bits = geRam_Allocate((bmih->biWidth * bmih->biHeight) * 4);
	retval = ::GetDIBits (dcMem, bmpHandle, 0, bmih->biHeight, bits, bmi, DIB_RGB_COLORS);

	// now create our internal-format bitmap
	pBitmap = geBitmap_Create (bmih->biWidth, bmih->biHeight, 1, GE_PIXELFORMAT_32BIT_XRGB);

	// copy the bits into it
	geBitmap *OutputBitmap;
	geBitmap_LockForWrite (pBitmap, &OutputBitmap, 0, 0);

	void *NewBits = geBitmap_GetBits (OutputBitmap);

	const int CopySize = bmih->biSizeImage;

	memcpy (NewBits, bits, CopySize);

	geBitmap_UnLock (OutputBitmap);
	
	// get rid of old bits pointer and bitmap header and Windows bitmap
	geRam_Free (bits);
	geRam_Free (bmi);
	DeleteObject (bmpHandle);
	DeleteDC (dcMem);

	//convert to 555
	geBitmap_SetFormatMin(pBitmap, GE_PIXELFORMAT_16BIT_555_RGB);

	TypeBmpEntry *pEntry;

	pEntry = TypeBmpArray_Add (pBitmapCache, pType, pBitmap);

	return pEntry;
}

static TypeBmpEntry *LoadResourceBitmap (TypeBmpArray *pBitmapCache, Type *pType, const char *BmpResourceName)
{
	HBITMAP bmpHandle;

	bmpHandle = ::LoadBitmap (AfxGetInstanceHandle (), BmpResourceName);

	if (bmpHandle == NULL)
	{
		return NULL;
	}

	return CreateEntryFromLoadedBitmap (pBitmapCache, pType, bmpHandle);
}

static TypeBmpEntry *LoadDefaultEntityBitmap (TypeBmpArray *pBitmapCache)
{
	HBITMAP bmpHandle;
	
	bmpHandle = ::LoadBitmap (AfxGetInstanceHandle (), "BMP_DEFAULT");
	assert (bmpHandle != NULL);  // this would be bad!
	
	return CreateEntryFromLoadedBitmap (pBitmapCache, NULL, bmpHandle);
}

static TypeBmpEntry *LoadBitmapFromFile (TypeBmpArray *pBitmapCache, Type *pType, const char *BmpFilename)
{
	geBitmap *pBitmap = NULL;
	geVFile *vfs;

	vfs = geVFile_OpenNewSystem (NULL, GE_VFILE_TYPE_DOS, BmpFilename, NULL, GE_VFILE_OPEN_READONLY);
	if (vfs != NULL)
	{
		pBitmap = geBitmap_CreateFromFile (vfs);
		geBitmap_SetFormat (pBitmap, GE_PIXELFORMAT_16BIT_555_RGB, GE_FALSE, 0, NULL);
		geVFile_Close (vfs);
	}
	if (pBitmap == NULL)
	{
		return NULL;
	}

	TypeBmpEntry *pEntry;

	pEntry = TypeBmpArray_Add (pBitmapCache, pType, pBitmap);
	return pEntry;
}

static TypeBmpEntry *EntityTable_GetBitmap
	(
	  const EntityTable *pTable, 
	  const char *EntityClassname
	)
{
	Type *pType;
	TypeBmpEntry *pEntry;

	pType = EntityTable_GetEntityTypeFromName (pTable, EntityClassname);

	if (pType == NULL)
	{
		return NULL;
	}

	// have we loaded this bitmap yet?
	pEntry = TypeBmpArray_Find (pTable->BitmapCache, pType);
	if (pEntry == NULL)
	{
		const char *BmpFilename;

		BmpFilename = CParser_GetIconName (pType);
		if (BmpFilename != NULL)
		{
			if ((strcmp (EntityClassname, "light") == 0) ||
				(strcmp (EntityClassname, "spotlight") == 0) ||
				(strcmp (EntityClassname, "ModelOrigin") == 0) ||
				(strcmp (EntityClassname, "Camera") == 0))
			{
				pEntry = LoadResourceBitmap (pTable->BitmapCache, pType, BmpFilename);
			}
			else
			{
				pEntry = LoadBitmapFromFile (pTable->BitmapCache, pType, BmpFilename);
			}
		}
	}
	return pEntry;
}


const geBitmap *EntityTable_GetBitmapPtr
	(
	  const EntityTable *pTable, 
	  const char *EntityClassname
	)
{
	TypeBmpEntry *pEntry = EntityTable_GetBitmap (pTable, EntityClassname);

	if (pEntry == NULL)
	{
		pEntry = LoadDefaultEntityBitmap (pTable->BitmapCache);
		assert (pEntry != NULL);
	}

	return pEntry->pBitmap;
}

ContentsTable *EntityTable_GetContentsList
	(
	  const EntityTable *pTable
	)
{
	int nEntries;
	ContentsTable *pContentsTable;
	int StructSize;

	assert (pTable->EntityInfo != NULL);

	nEntries = CParser_GetContentsCount (pTable->EntityInfo);
	// I allocate one more than necessary...oh well
	StructSize = sizeof (ContentsTable) + (nEntries * sizeof (ContentsTableEntry));
	pContentsTable = (ContentsTable *)geRam_Allocate (StructSize);
	if (pContentsTable == NULL)
	{
		return NULL;
	}
	pContentsTable->nEntries = nEntries;
	for (int i = 0; i < nEntries; ++i)
	{
		ContentsTableEntry *pEntry = &pContentsTable->Entries[i];
		CParser_GetContentsNameAndValue (pTable->EntityInfo, i, &pEntry->Name, &pEntry->Value);
	}
	return pContentsTable;
}


void EntityTable_FreeContentsList
	(
	  ContentsTable **ppContents
	)
{
	geRam_Free (*ppContents);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -