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

📄 level.cpp

📁 3D游戏场景编辑器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************************/
/*  level.cpp                                                                           */
/*                                                                                      */
/*  Author:       Jim Mischel, Ken Baird                                                */
/*  Description:  Editor level structure                                                */
/*                                                                                      */
/*                                                                                      */
/*  The contents of this file are subject to the Genesis3D Public License               */
/*  Version 1.01 (the "License"); you may not use this file except in                   */
/*  compliance with the License. You may obtain a copy of the License at                */
/*  http://www.genesis3d.com                                                            */
/*                                                                                      */
/*  Software distributed under the License is distributed on an "AS IS"                 */
/*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
/*  the License for the specific language governing rights and limitations              */
/*  under the License.                                                                  */
/*                                                                                      */
/*  The Original Code is Genesis3D, released March 25, 1999.                            */
/*Genesis3D Version 1.1 released November 15, 1999                            */
/*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
/*                                                                                      */
/*  Modified by Tom Morris for GenEdit-Classic ver. 0.57, Feb. 3, 2001					*/
/****************************************************************************************/
#include "stdafx.h"
#include "level.h"
#include <shlwapi.h>
#include "Globals.h"
/* moved to level.h for g3dc
#include "Parse3dt.h"
#include "EntTypeName.h"
#include <assert.h>
#include "ram.h"
#include "units.h"
#include "util.h"
#include "FilePath.h"

#define NUM_VIEWS (4)
*/
/*	//	moved to level.h for g3dc
struct tag_Level
{
    BrushList *Brushes;
	CEntityArray *Entities;
    char *WadPath;
	char *HeadersDir;
	EntTypeNameList	*EntTypeNames;
	GroupListType *Groups;
	SizeInfo	*WadSizeInfos;
	CWadFile	*WadFile;
	EntityTable	*pEntityDefs;

	ModelInfo_Type	ModelInfo;

	SkyFaceTexture SkyFaces[6];
	geVec3d SkyRotationAxis;
	geFloat SkyRotationSpeed;
	geFloat	SkyTextureScale;
	
	// level edit settings
	CompileParamsType CompileParams;
	int GroupVisSetting;
	EntityViewList *pEntityView;

	GridInfo GridSettings;
	geBoolean BspRebuildFlag;
	ViewStateInfo ViewInfo[NUM_VIEWS];

	BrushTemplate_Arch ArchTemplate;
	BrushTemplate_Box	BoxTemplate;
	BrushTemplate_Cone	ConeTemplate;
	BrushTemplate_Cylinder CylinderTemplate;
	BrushTemplate_Spheroid	SpheroidTemplate;
	BrushTemplate_Staircase StaircaseTemplate;

	geVec3d TemplatePos;

	float DrawScale;		// default draw scale
	float LightmapScale;	// default lightmap scale
};
*/

EntityViewList *Level_GetEntityVisibilityInfo (Level *pLevel)
{
	return pLevel->pEntityView;
}

void Level_SetGroupVisibility (Level *pLevel, int Setting)
{
	assert ((Setting == Group_ShowAll) ||
			(Setting == Group_ShowVisible) ||
			(Setting == Group_ShowCurrent));
	assert (pLevel != NULL);

	pLevel->GroupVisSetting = Setting;
}

int Level_GetGroupVisibility (const Level *pLevel)
{
	assert (pLevel != NULL);

	return pLevel->GroupVisSetting;
}

static void Level_AssignEntityName (Level *pLevel, CEntity *pEnt)
{
	CString EntityClassname;
	CString NewName;
	int Num;

	EntityClassname = pEnt->GetClassname ();
	Num = EntTypeNameList_UpdateCount (pLevel->EntTypeNames, EntityClassname);
	NewName.Format ("%s%d", EntityClassname, Num);
	pEnt->SetKeyValue ("%name%", NewName);
}

static geBoolean Level_LoadEntities
	(
	  Level *pLevel,
	  Parse3dt *Parser,
	  int VersionMajor,
	  int VersionMinor,
	  const char **Expected
	)
{
	int EntityCount, i;
	
	if (!Parse3dt_ScanExpectingText (Parser, (*Expected = "Class"))) return GE_FALSE;
	if (!Parse3dt_ScanExpectingText (Parser, (*Expected = "CEntList"))) return GE_FALSE;
	if (!Parse3dt_GetInt (Parser, (*Expected = "EntCount"), &EntityCount)) return GE_FALSE;
	if (!Parse3dt_GetInt (Parser, (*Expected = "CurEnt"), &i)) return GE_FALSE;	

	for (i=0; i < EntityCount; i++)
	{
		CEntity ent;
		CString Value;

		if (!ent.ReadFromFile (Parser, VersionMajor, VersionMinor, Expected, pLevel->pEntityDefs)) return GE_FALSE;
		ent.DeSelect ();  // don't want it selected on load...
		if (!Parse3dt_ScanExpectingText (Parser, (*Expected = "End"))) return GE_FALSE;
		if (!Parse3dt_ScanExpectingText (Parser, (*Expected = "CEntity"))) return GE_FALSE;

		if( ent.IsCamera() == GE_FALSE )	// Exclude cameras
		{
			if (ent.GetKeyValue ("%name%", Value))
			{
				EntTypeNameList_UpdateCount (pLevel->EntTypeNames, Value);
			}
			else
			{
				Level_AssignEntityName (pLevel, &ent);
			}
			pLevel->Entities->Add (ent);
		}
	}
	return GE_TRUE;
}

static geBoolean Level_SaveEntities (CEntityArray *Entities, FILE *f)
{
	int i;
	int NumEntities;

	assert (Entities != NULL);
	assert (f != NULL);

	NumEntities = Entities->GetSize ();
	if (fprintf(f, "Class CEntList\nEntCount %d\n", NumEntities) < 0) return GE_FALSE;
	if (fprintf(f, "CurEnt 0\n") < 0) return GE_FALSE;

	for(i=0;i < NumEntities;i++)
	{
		if (!(*Entities)[i].SaveToFile (f)) return GE_FALSE;
		if (fprintf(f, "End CEntity\n") < 0) return GE_FALSE;
	}
	return GE_TRUE;
}


static void Level_UnloadEntityDefs (Level *pLevel)
{
	if (pLevel->pEntityDefs != NULL)
	{
		EntityTable_Destroy (&pLevel->pEntityDefs);
	}
	if (pLevel->HeadersDir != NULL)
	{
		geRam_Free (pLevel->HeadersDir);
	}
}

geBoolean Level_LoadEntityDefs (Level *pLevel, const char *HeadersDir)
{
	Level_UnloadEntityDefs (pLevel);
	pLevel->HeadersDir = Util_Strdup (HeadersDir);
	pLevel->pEntityDefs = EntityTable_Create (pLevel->HeadersDir);
	if (pLevel->pEntityDefs == NULL)
	{
		return GE_FALSE;
	}
	return GE_TRUE;
}

const EntityTable *Level_GetEntityDefs (const Level *pLevel)
{
	return pLevel->pEntityDefs;
}

Level *Level_Create (const char *pWadName, const char *HeadersDir)
{
	Level *pLevel;

	pLevel = GE_RAM_ALLOCATE_STRUCT (Level);
	if (pLevel != NULL)
	{
		pLevel->Brushes = BrushList_Create ();
		if (pLevel->Brushes == NULL) goto CreateError;

		pLevel->Entities = new (CEntityArray);
		if (pLevel->Entities == NULL) goto CreateError;

		pLevel->Entities->SetSize (0, 20);

		pLevel->EntTypeNames = EntTypeNameList_Create ();
		if (pLevel->EntTypeNames == NULL) goto CreateError;

		pLevel->Groups = Group_CreateList ();
		if (pLevel->Groups == NULL) goto CreateError;

		{
			// add the default group
			Group *pGroup = Group_Create (0, "Default");
			if (pGroup != NULL)
			{
				GroupList_Add (pLevel->Groups, pGroup);
			}
		}


		pLevel->ModelInfo.CurrentModel = 0;
		pLevel->ModelInfo.Models = ModelList_Create ();
		if (pLevel->ModelInfo.Models == NULL) goto CreateError;

		pLevel->HeadersDir = NULL;
		pLevel->pEntityDefs = NULL;
		if (Level_LoadEntityDefs (pLevel, HeadersDir) == GE_FALSE)
		{
			goto CreateError;
		}

		pLevel->WadPath = Util_Strdup (pWadName);
		pLevel->WadFile = NULL;
		pLevel->WadSizeInfos = NULL;

		// initialize sky
		geVec3d_Set (&pLevel->SkyRotationAxis, 1.0f, 0.0f, 0.0f);
		pLevel->SkyRotationSpeed = 10.0f;
		pLevel->SkyTextureScale = 1.0f;
		for (int i = 0; i < 6; ++i)
		{
			pLevel->SkyFaces[i].TextureName = NULL;
			pLevel->SkyFaces[i].Apply = GE_FALSE;
		}

		// Set default compile dialog params
		pLevel->CompileParams.EntitiesOnly = GE_FALSE;
		pLevel->CompileParams.VisDetailBrushes = GE_FALSE;
		pLevel->CompileParams.DoVis = GE_TRUE;
		pLevel->CompileParams.DoLight = GE_TRUE;
		pLevel->CompileParams.RunBsp = GE_TRUE;
		pLevel->CompileParams.RunPreview = GE_FALSE;
		pLevel->CompileParams.UseMinLight = GE_TRUE;
		pLevel->CompileParams.SuppressHidden = GE_FALSE;
		pLevel->CompileParams.Filename[0] = '\0';

		pLevel->CompileParams.Light.Verbose = GE_FALSE;
		pLevel->CompileParams.Light.ExtraSamples = GE_FALSE;
		pLevel->CompileParams.Light.LightScale = 1.0f;
		pLevel->CompileParams.Light.Radiosity = GE_FALSE;
		pLevel->CompileParams.Light.NumBounce = 10;
		pLevel->CompileParams.Light.PatchSize = 128.0f;
		pLevel->CompileParams.Light.FastPatch = GE_FALSE;
		pLevel->CompileParams.Light.ReflectiveScale = 1.0f;
		geVec3d_Set (&pLevel->CompileParams.Light.MinLight, 128.0f, 128.0f, 128.0f);

		pLevel->CompileParams.Bsp.Verbose = GE_FALSE;
		pLevel->CompileParams.Bsp.EntityVerbose = GE_FALSE;

		pLevel->CompileParams.Vis.Verbose = GE_FALSE;
		pLevel->CompileParams.Vis.FullVis = GE_FALSE;
		pLevel->CompileParams.Vis.SortPortals = GE_TRUE;

		pLevel->GroupVisSetting = Group_ShowVisible;

		pLevel->pEntityView = EntityViewList_Create (pLevel->pEntityDefs);

		// grid settings
		//default to texel grid and snap
		//with rotational snap of 15
		{
			GridInfo *pGridInfo;

			pGridInfo = &pLevel->GridSettings;
			pGridInfo->UseGrid = GE_TRUE;
			pGridInfo->GridType = GridTexel;
			pGridInfo->SnapType = GridTexel;
			pGridInfo->MetricSnapSize = GridSize_Decimeter;
			pGridInfo->TexelSnapSize = 8;
			pGridInfo->RotationSnap = 15;
		}

		pLevel->BspRebuildFlag = GE_TRUE;
		for (int iView = 0; iView < NUM_VIEWS; ++iView)
		{
			ViewStateInfo *pInfo;

			pInfo = &pLevel->ViewInfo[iView];
			pInfo->IsValid = GE_FALSE;
			pInfo->ZoomFactor = 1.0f;
			geVec3d_Clear (&pInfo->PitchRollYaw);
			geVec3d_Set (&pInfo->CameraPos, 0.0f, 0.0f, 0.0f);
		}

		BrushTemplate_ArchDefaults (&pLevel->ArchTemplate);
		BrushTemplate_BoxDefaults (&pLevel->BoxTemplate);
		BrushTemplate_ConeDefaults (&pLevel->ConeTemplate);
		BrushTemplate_CylinderDefaults (&pLevel->CylinderTemplate);
		BrushTemplate_SpheroidDefaults (&pLevel->SpheroidTemplate);
		BrushTemplate_StaircaseDefaults (&pLevel->StaircaseTemplate);

		geVec3d_Clear (&pLevel->TemplatePos);

		pLevel->DrawScale = 1.0f;
		pLevel->LightmapScale = 2.0f;
	}

	return pLevel;

CreateError :
	Level_Destroy (&pLevel);
	return pLevel;
}

geBoolean Level_LoadWad (Level *pLevel)
{
	// get rid of the old wad...
	Level_UnloadWad (pLevel);

	pLevel->WadFile = new CWadFile;
	if (pLevel->WadFile == NULL)
	{
		return GE_FALSE;
	}

	if (pLevel->WadFile->Setup (pLevel->WadPath))
	{
		pLevel->WadSizeInfos = (SizeInfo *)geRam_Allocate(sizeof(SizeInfo)*pLevel->WadFile->mBitmapCount);

		if (pLevel->WadSizeInfos != NULL)
		{
			int i;

			for (i = 0; i < pLevel->WadFile->mBitmapCount;i++)
			{
				SizeInfo *pInfo;
				WadFileEntry *Entry;

				pInfo = &(pLevel->WadSizeInfos[i]);
				Entry = &(pLevel->WadFile->mBitmaps[i]);

				pInfo->TexWidth		=Entry->Width;
				pInfo->TexHeight	=Entry->Height;
				pInfo->TexData		=(uint8 *)Entry->BitsPtr;
			}
		}
	}

	return (pLevel->WadSizeInfos != NULL);
}

void Level_UnloadWad (Level *pLevel)
{
	if (pLevel->WadSizeInfos != NULL)
	{
		geRam_Free (pLevel->WadSizeInfos);
		pLevel->WadSizeInfos = NULL;
	}
	if (pLevel->WadFile != NULL)
	{
		delete pLevel->WadFile;
		pLevel->WadFile = NULL;
	}
}

void Level_Destroy (Level **ppLevel)
{
	Level *pLevel;

	assert (ppLevel != NULL);
	assert (*ppLevel != NULL);

	pLevel = *ppLevel;

	if (pLevel->Brushes != NULL)
	{
		BrushList_Destroy (&pLevel->Brushes);
	}
	if (pLevel->Entities != NULL)
	{
		delete pLevel->Entities;
	}
	if (pLevel->WadPath != NULL)
	{
		geRam_Free (pLevel->WadPath);
	}
	if (pLevel->EntTypeNames != NULL)
	{
		EntTypeNameList_Destroy (&pLevel->EntTypeNames);
	}
	if (pLevel->Groups != NULL)
	{
		Group_DestroyList (&pLevel->Groups);
	}
	Level_UnloadEntityDefs (pLevel);
	Level_UnloadWad (pLevel);
	if (pLevel->ModelInfo.Models != NULL)
	{
		ModelList_Destroy (&pLevel->ModelInfo.Models);
	}
	// destroy sky...
	{
		int i;

		for (i = 0; i < 6; ++i)
		{
			SkyFaceTexture *sft;

			sft = &(pLevel->SkyFaces[i]);
			if (sft->TextureName != NULL)
			{
				geRam_Free (sft->TextureName);
			}
		}
	}

	if (pLevel->pEntityView != NULL)
	{
		EntityViewList_Destroy (&pLevel->pEntityView);
	}

	geRam_Free (pLevel);
	*ppLevel = NULL;
}

static uint16 Level_GetDibIdFromWad (const CWadFile *WadFile, const char *Name)
{
	uint16	i;

	for(i=0;i < WadFile->mBitmapCount;i++)
	{
		if(strcmpi(WadFile->mBitmaps[i].Name, Name) == 0)
		{
			return i;
		}
	}

	return 0xffff;
}

uint16 Level_GetDibId (const Level *pLevel, const char *Name)
{
	return Level_GetDibIdFromWad (pLevel->WadFile, Name);
}


WadFileEntry *Level_GetWadBitmap (Level *pLevel, const char *Name)
{
	uint16 i;

	i = Level_GetDibIdFromWad (pLevel->WadFile, Name);
	if (i != 0xffff)
	{
		return &(pLevel->WadFile->mBitmaps[i]);
	}
	else
	{
		return NULL;
	}
}

CompileParamsType *Level_GetCompileParams (Level *pLevel)
{
	return &(pLevel->CompileParams);
}

⌨️ 快捷键说明

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