Skip to main content
gamedev.net
🔒 Locked

level loading code

Started by Laroche 3 replies 250+ views
Original Post
Laroche
Laroche
I just cant seem to get this to work, ive been looking at it for a while and I'm not sure what the problem is. Here is what the map file looks like:
    
nick
C:\Nick\Projects\HSLevelEditor\Debug\TileSet.bmp
5
5
C:\Nick\Projects\HSLevelEditor\Debug\Desc.txt
C:\Nick\Projects\HSLevelEditor\Debug\Psy.txt
C:\Nick\Projects\HSLevelEditor\Debug\Mob.txt
25
0:0,1:1,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,1:24,0:24,E,
  

and this is the level loading code
      
CMap * Load(char* Filename, CTileset *pTileset) // Uses RLE Compression

{
	using namespace std;
	CMap * Temp;
	string Title;
	string TilesetPath;
	string Height, Width, D, P, M, TS, buf;
	int W, H, TilesetSize;
	CTile * Struct;
	int * StateMap;
	int * TileIDMap;

	ifstream File;
	File.open(Filename, ios::in);
	Title = GetNextWord(File, '\n');
	TilesetPath = GetNextWord(File, '\n');
	Width = GetNextWord(File, '\n');
	Height = GetNextWord(File, '\n');
	D = GetNextWord(File, '\n');
	P = GetNextWord(File, '\n');
	M = GetNextWord(File, '\n');
	TS = GetNextWord(File, '\n');

	MessageBox(NULL, TS.c_str(), TS.c_str(), MB_OK);

	TilesetSize = atoi(TS.c_str());

	W = atoi(Width.c_str());
	H = atoi(Height.c_str());

	StateMap = new int[TilesetSize];
	TileIDMap = new int[TilesetSize];

	for (int h = 0; h < TilesetSize; ++h)
	{
		TileIDMap[h] = h;
	}

	for (int q = 0; q < TilesetSize; ++q)
	{
		StateMap[q] = 0;
	}
//////////// START OF TILE-STATE LOADING CODE ///////////

	// Set All Effects for the tiles

	for (int m = 0; m < TilesetSize; ++m)
	{
		buf = GetNextWord(File, ',');
		int Ps = buf.find(':',0);
		if (Ps != std::string::npos)
		{
			int Test, Pest;
			std::string before, after;
			before = buf.substr(Ps);
			after = buf.substr(0, Ps);
			Test = atoi(before.c_str());
			Pest = atoi(after.c_str());
			StateMap[Test] = Pest;
		}
	}
////////////END OF TILE-STATE LOADING CODE ///////////


	if (pMap)
	{
		delete pMap;
		pMap = NULL;
		MapExist = FALSE;
	}

	if (pTileset)
	{
		delete pTileset;
		pTileset = NULL;
		TilesetExist = FALSE;
	}

	Temp = new CMap(W, H, Title.c_str());
	Temp->SetDescription(D);
	Temp->SetMob(M);
	Temp->SetPhysics(P);
	Struct = new CTile[W * H * 2];

	int * Effect = new int[TilesetSize];

	string Buf, buft;
	int Size = W*H*2;
	bool HandleReps = FALSE;
	int Rep = 0;

//////////// START OF TILE LOADING CODE ///////////

	// decompress with RLE , set it to temporary structure

	for (int X = 0; X < Size; ++X)
	{
		if (!HandleReps)
		{
			Buf = GetNextWord(File, ',');
		}
		
		int test = Buf.find('E',0);
		if (test != std::string::npos)
		{
			// End of File, E is terminating Char.

			break;
		}

		int Pos = Buf.find(':',0);
		if (Pos != std::string::npos)
		{
			buft = Buf.substr(0, Pos);
			int ID = atoi(buft.c_str());
			buft = Buf.substr(Pos);			

			if (!HandleReps)
			{
				Rep = atoi(buft.c_str());
			}
					
			CTile t;
			t.TileID = ID;
			t.Layer = 1;
			if (X > W*H)
				t.Layer = 2;
			Struct[X] = t;
			Rep--;
			HandleReps = TRUE;
			if (Rep < 0)
			{
				HandleReps = FALSE;
				Rep = 0;
			}
			continue;
			
		}
		if (Pos == std::string::npos)
		{
			CTile Goo;
			Goo.TileID = atoi(Buf.c_str());
			Goo.Layer = 1;
			if (X > W*H)
				Goo.Layer = 2;
			Struct[X] = Goo;
		}
	}
//////////// END OF TILE LOADING CODE ///////////


	// Copy temp structure to real class	

	for (int i = 0; i < Size; ++i)
	{
		Temp->SetTile(i, Struct[i]);
	}

	//Set the effects for the tiles

	for (int l = 0; l < TilesetSize; ++l)
	{
		Temp->SetTileEffect(TileIDMap[l], StateMap[l]);
	}

	delete [] Struct;
	delete [] StateMap;
	delete [] TileIDMap;
	File.close();
	return Temp;
}
    
I think the problem might have to do with my manipulation of strings, especially the use of substr(), but im not sure. Sorry just to dump code, but im really having difficulty figuring this out, ive been scanning through it for quite a while now and im just note sure what the problem is. [edited by - laroche on December 20, 2002 8:48:26 PM]
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
granat
granat
You should make some code to log stuff to a text file. That will help you find errors in your code.
Maybe you can use this:



    

void CLog::OpenLog(char *filename)
{
fp = fopen(filename, "w");

Filename[0] = 0;
strcpy(Filename, filename);
fclose(fp);
}

void CLog::CloseLog()
{
fclose(fp);
}

void CLog::Log(char *szText, ...)
{
char buffer[1024];
buffer[0] = 0;

va_list marker;
va_start(marker, szText);
vsprintf(buffer,szText,marker);
strcat(buffer,"\n");

//add string to file.

fp = fopen(Filename, "a");
fwrite(buffer, (strlen(buffer)+1), 1, fp);
fclose(fp);
}



Use it like this: (%d = integer, %f = float)

OpenLog("Log.txt");
Log("Value in TilesetSize is: %d",TilesetSize);
CloseLog();




[edited by - granat on December 20, 2002 2:59:32 AM]
-------------Ban KalvinB !
Laroche
Laroche
Before I put in my tile-state system, everything was working fine, levels loading and saving perfectly. Then I wanted to save the effects on the tiles, such as Open, Closed, Slow, DamagePlayerWhenSteppedOn, etc. So i saved the values into the tiles, then during save, would write those values directly after the amount of different tiles there were. In this case, that would be 25. You can see under that it progresses linearly (1:0, 2:0, etc) eventually finishing at 24:0.

Now somehow, my algorithm is screwing up my tile loading code, and the tiles are not loading properly at all. Ive edited the original source to show where I use the state-loading code and where i use level loading. Im not too sure if my use of substr() is right to get everything upto a certain character in a string. I hope this explains a bit more. btw, sorry for the ugly code
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
Laroche
Laroche
ok i got it working! I was right, it was my use of substr(), i should use Ps+1, not Ps. Ps game me, for example, ":48" and things like that, where as I only wanted the 48. Ill be more careful in the future, and will try not to dump code.
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.