Concept of tile management. Opinions wanted.

Started by
6 comments, last by deadimp 18 years, 4 months ago
I was thinking about if its possiable in c++ to create an object from a class without declaring it in hard code. Ok lets say if I knew im my game I wanted to have a grass sprite I would go about it this way.

Tile *grass
grass.Load("data\grass.bmp")

Right, now lets say inside my map editor I could load my tiles and save the information to a .txt file. Would it be possiable to create a Tile object using a name that I declared for it inside the map editor rather than having to hard code the name in? Here is an example of what I think it would look like in psuedo code:

void LoadTiles()
{
     while(there is another tile to load)
     {
          Tile *LoadFromTxtFile(NextTile);
          LoadedTile.Load(LoadFromTxtFile(TilesFileSource);
     }
}

The reason I want to go about this is because I want each tile object to have a variable of type bool that lets me know if its walkable or not. Here is some more pusedo code:

if(g_player.MoveToLoadedTile)
{
    if(LoadedTile.walkable==true)
    {
         g_player.MoveToLoadedTile;
    }
}

Is this even possiable or is there a simpler way to go about doing it?
--brb
Advertisement
Let me get this straight: you want to load each tile, but have it open that each tile is not in your program, but in a text file? Here is how I do it in my game:

I have on "main" tile engine for my game. Everytime I want to load a new tileset, I go through my "environment" class. Each environment class is a text file. At the end of each environment textfile, there are names of other textfiles that can be linked from the one you are in.

Say you are in "blah.txt". If you step on grid 10, 10, you open "foo.txt" and automatically go to x, y grid. That way, the game only needs to know the name of blah.txt right off the top of that bat, and the other ones are added through text files.

My tile engine has a pointer to the array of tiles. They are dynamically allocated each time one loads an "environment" with new, and the pictures are also loaded to a pointer.
What is happening is im trying to design my tile engine/map editor in my head before I start coding it. What im thinking is if im using a class called Tile to declare a new tile I would need to hard code it, ala
Tile *grass;

Now im thinking this is the completly wrong way to do it. If I created a map thats say 10x10 tiles in size it would be saved as an array. Each spot in the array would relate to a number that the map editor gives it. Each number would then relate to a tile that the number is assigned to. Im thinking that each map would consist of two different files, one would be a map file and one would be a tileset file. The map file would hold the array and the tileset would hold the tiles data information (its file location and the number that it relates to in the array). When I wanted to load a map into the game it would first load the tile information, loading each tile into an object of the class Tile.
void LoadMap(){     int numberoftiles = LoadFromFile(totaltilenumber);     if(numberoftiles!=0)     {          Tile *numberoftiles;          numberoftiles.Load(LoadFromFile(FileLocation);          numberoftiles--;     }         }

Then it would load in the map information and draw the map. The numbers in the array would be the same as there Tile name. So say
Tile *1;1.Load(LoadFromFile("Data/grass.bmp");



This way each map would have its own corresponding tileset information so that the tiles themselves wouldnt need to be hard coded, making it easy to add/change tiles. If Say our game had a total of 300 tiles and you had to change one tile on map four, instead of finding which tile that is in the .cpp files you could just load the map, load the tileset, and change the tile. Then when the game runs there is no heavy code changes, and it all works out fine.

Now what im looking for is for some constructive critisim about my concept here. If you notice something that doesent seem possiable, for instance I dont actuially believe I can create an object from a class through a loop, meaning I dont think if I had 25 different tiles in my tileset and I wanted to make them all into Tile objects I dont think I could loop it through to create:
Tile *1;Tile *2;Tile *3;Etc...


Please, feel free to let me know if im on the right path or dead in the center of a busy highway. After checking through the articles here on The Dev (thats GameDev, btw), forum searching, and googling I havent really found anything relating to tile management. I will really appreciate this, thanks!
--brb
I agree with ender7771. You definately need that kind of enviromap system, but if your engine has not much details you can put whole level to one enviromap. In best case I prefer that your tile images are list in few files. Like one to monsters, one to people, one to forest and one to dungeon. With animation you just setup set of tiles and in same file tell how animate them. With file try to add your own stuff in the middle of file. This prevent to copy your data anywhere. What you need is a customiser program, which load file and save it in your data structure. Look for some readymade loaders for imagefile and customice them in your need.
Well, it just so happens this sounds a LOT like a project I'm working on right now. :)
Quote:If I created a map thats say 10x10 tiles in size it would be saved as an array. Each spot in the array would relate to a number that the map editor gives it. Each number would then relate to a tile that the number is assigned to.
That's exactly how I'm doing it. I use a dynamically allocated array of integers to store an array with two layers: one for the floor tile, and one for an object tile. I store the map dimensions with the map save file, and allocate the map during load (with * 2 the dimensions for the two layers)

void LoadMap(){	sprintf(FileLocation,".\\data\\maps\\%d.map",CurrentMap);	pFile = NULL;	pFile = fopen( FileLocation, "r" );	if(pFile == NULL)		return;	else	{		// Load the map number and test it against what we're looking for:		int TestValue = 0;		fscanf( pFile, "%d", &TestValue);		if(TestValue != CurrentMap)		{			Log("Error.  Map id does not match current map id in LoadMap");			fclose( pFile );			return;		}		// Load the map dimensions and test them.		fscanf( pFile, "%d", &MapWidth);		fscanf( pFile, "%d", &MapHeight);		if(MapWidth < 1 || MapHeight < 1 || MapWidth > 160 || MapHeight > 100)		{			Log("Error.  Map dimensions out of bounds in LoadMap");			fclose(pFile);			return;		}		// Allocate the new information and read it from file		Map = new int[MapWidth * MapHeight * 2];		// load all the information.		for(x = 0; x < (MapWidth * MapHeight * 2) ; x++)			fscanf(pFile, "%d ", &Map[x]);	}	fclose(pFile);}

Quote:I dont actuially believe I can create an object from a class through a loop, meaning I dont think if I had 25 different tiles in my tileset and I wanted to make them all into Tile objects I dont think I could loop it through to create:

... but you can create an array of objects from a class, and loop through those easy enough. In my implementation I'm using SDL, and all my graphics are numbered bitmaps- 1.bmp, 112.bmp, 324.bmp ect. I just have an array set to the maximum number of images, and load them all at the start of the program. I know this won't work for most people's needs, but my game fits in quite nicely with some classic nintendo titles (namely Legend of Zelda). Tiny graphics, small memory needs.

#define MAX_IMAGES 100		// Maxiumum # of imagesSDL_Surface *Image[MAX_IMAGES];	// All Images used in this game.for(i = 0; i < MAX_IMAGES; i++){	sprintf(FileLocation, ".\\data\\img\\%d.bmp", i);	Image = SDL_LoadBMP(FileLocation);	// Make pure white transparent with color keying	if(Image)		SDL_SetColorKey(Image, SDL_SRCCOLORKEY, 0x00FFFFFF);}

Quote:the tileset would hold the tiles data information (its file location and the number that it relates to in the array).

If I'm reading that right, your varient would be along the lines of:

for(i = 0; i < TileDataInfoCount; i++){   sprintf(FileLocation, ".\\data\\img\\%d.bmp", TileDataInfo.filenumber);   Image[ TileDataInfo.ImageIndexValue ] = SDL_LoadBMP(FileLocation);}

I'll keep an eye on this thread. Lemme know if any of this helps, or brings up any more questions.
--------------------------~The Feature Creep of the Family~
From the sounds of what you want I've also done something similar.

I was using a text file that would describe a map which would give the name of a file that would contain the tileset data.

eg. Map file

Width = 25;Height = 60;BaseWidth = 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2,2,2,2,2,2,;


eg. TileSet

NumberOfTiles = 5;0 = 64,32,Images\\DifferentColor.tga,1;1 = 64,32,Images\\tile1.bmp,1;2 = 64,32,Images\\tile2.bmp,0;3 = 64,32,Images\\tile3.bmp,0;4 = 64,32,Images\\tile4.bmp,0;


format:
tileIndex = width,height,imageFile,passable;



Then in the program it would just be a case of reading the mapfile and store the tileIndex numbers in an array and then read the tileset stated by the map file and creating an array of tile objects and creating a tile object for each tile using the tileset information placing it in the array using the index.
Thanks for all of the help. Both posts have helped me decide how im going to go about doing it. Thanks alot, I really appreciate it.
--brb
Also, another option, dynamic yet somewhat inefficient in some terms, is to do something similar to that of Game Maker's tiling system. Create a main tile image (a background, in GM's case), and then a tile set (with this background as it's image source), and then individual tiles stored in that tile set. In the tile set, you could have it define: Output: x, y,[Width,Height?]; Input: x, y, Widt, Height; depth, etc. It seems to be pretty good for platform games, but I'm not sure how it will apply with yours.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement