tile based game engine?

Pages: 12
i want to eventually make a game engine/map editor for a 2d rpg game. but i want it to be tile based, but i am wondering how to do this? i have been looking at youtube videos of stuff like this and it seems people use som sort of array?
maybe multidimensional array?
Yes, it's something like this:
1
2
3
4
5
struct Tile {
	Texture Tex;
	bool Solid;
}
Tile Map[SizeOfTiles_Horizontal][SizeOfTiles_Vertical];

and you can get a tile like this:
 
Map[0][0] // First Horizontal Tile, First Vertical Tile 

Just be careful not to do something like this:
 
Map[SizeOfTiles_Horizontal][SizeOfTiles_Vertical];

The last tile is (SizeOfTiles...)-1.
"Texture" is a pseudocode Texture that you may want to load
You have to set SizeOfTiles_Horizontal and SizeOfTiles_Vertical too to:
Horizontal Size / Tile Size
Vertical Size / Tile Size
but remember to use constants, and not variables, like...
1
2
3
4
5
const unsigned int ScreenSize_X = 640;
const unsigned int ScreenSize_Y = 480;
const unsigned int TileSize = 32;
const unsigned int SizeOfTiles_Horizontal = ScreenSize_X / TileSize;
const unsigned int SizeOfTiles_Vertical = ScreenSize_Y / TileSize;
Yes you can use an array or a vector.

The number of tiles in the map will be width * height so make sure the array/vector has that size.
vector: std::vector<Tile> tiles(width * height);
array: Tile* tiles = new Tile[width * height];

To access a tile you can do tiles[y * width + y]. You probably want to create a map class that hides these details so that the tile access through the map object looks something like map.at(x, y) or map(x, y).
If you plan on using
array: Tile* tiles = new Tile[width * height];

remember to
delete[] tiles;
before you close your program, like:
1
2
3
4
5
6
7
8
9
int main() {
	Tile * tiles = new Tile[width * height];
	while(Run())
	{
		Draw();
	}
	delete[] tiles;
	return 0;
}
so i can use arrays, but if i do i have to delete them, to not cause memory leaks (or something like that)?

i guess il have to study a lot of array stuff then, cause i only know my basics atm :(
thanks for all the help :D
so i can use arrays, but if i do i have to delete them, to not cause memory leaks (or something like that)?

Exactly, if you don't delete a Dynamically Stored Array (if you use new it's a Dynamically stored object/array) you will get a Memory Leak.
Another thing:
If you use
 
Tile * tiles = new Tile[width * height]; // Dynamically stored ARRAY 

you will have to call
 
delete[] tiles; // Delete ARRAY 

but if you use
 
int * Integer = new int; // Dynamically stored OBJECT 

you will have to call
 
delete Integer; // Delete OBJECT 
holy, this all looks quite complicated, but all the more reason to learn it, i really need a tought challange right about now ;)

if i were to assign a texture to a certain tile, say grass for instance how would i do it, if i were to do it in the code?

something like this i guess:

tile [5][2] = grassTile //where grassTile is surface for the grass (using sdl btw)
With a struct like the one i made, it will become like:
tile[5 + (2 * width)].Tex = grassTile
But instead of
1
2
3
4
struct Tile {
	Texture Tex;
	bool Solid;
}

use
1
2
3
4
struct Tile {
	SDL_Surface * Tex;
	bool Solid;
}

But if you use
 
SDL_Surface ** tile = new tile[width * height]

you can easily do this:
 
tile[5 + (2 * width)] = grassTile;

You may want to do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SDL_Surface ** tile = 0;
SDL_Surface * GetTile(unsigned int x, unsigned int y) {
	return tile[ x + (y * width)] /* Add .Tex if using the structure */ ;
}
void SetTile(unsigned int x, unsigned int y, SDL_Surface * Tile) {
	tile[ x + (y * width)] /* Add .Tex if using structure */ = Tile;
}
int main() {
	tile = new tile[width * height];
	//...
	SDL_Surface * Surface = GetTile(5,2);
	SetTile(5,2,NewSurface);
	delete[] tile;
}
Last edited on
ah ok, thanks :) think ima stick with the struct method, seemed to be the less complicated one.

i guess il start studying multidimensional arrays and structs etc then :)
I edited the post, check it out again.
holy jesus thats complicated :O
might be cause my brain is tired from school though.

could i do something like this?

1
2
3
4
5
int map [5][5] = {{1,1,1,1,1}
				    ,{1,0,0,0,1}
				    ,{1,0,0,0,1}
				    ,{1,0,0,0,1}
				    ,{1,1,1,1,1}};


and then write some code that defined the size of each tile, and make it so that every 0 would be a certain tile, and every 1 would be another one?
or am i completely wrong now?
Last edited on
You can, but you should then "Bind" a number to a specific Tile.
Personally, i'm making a tile engine too, and this is how i'm doing it (But with Dynamic Arrays), and i simply
 
switch(map[x][y])

and depending on the value, i change the Texture
1
2
3
4
5
6
7
8
9
10
11
12
switch(map[x][y])
{
	case 0:
		Texture = NullSurface;
		break;
	case 1:
		Texture = GrassSurface;
		break;
	case 2:
		Texture = StoneSurface;
		break;
}

and so on.
Last edited on
ye i was thinking of something like that, ofc i would have each number constantly equal to one texture, like 1 would be grass, 2 would be water etc.

but thanks, im going to look into dynamic arrays then. mind explaining a little bit of what they are and how to use them?
It's quite complex to explain it on the fly, but i'll show you the main difference:
Static array:
 
char Variable[8] = "Test.";

Variable is a "char [8]", also "char *".
In fact, you can define values to create a Array like this.
1
2
const unsigned int Size = 8;
char Variable[Size] = "Test.";

This is the same.
Why did i use "const" ?
Try compiling it without "const".
It says it needs a constant value to declare an static array.
This is why we use Dynamic Arrays:
1
2
char * Variable = new char[8];
strcpy(Variable,"Test.");

Variable is a "char *", but not a "char [8]".
To define its value you cannot include it after "new char[...]" but must use strcpy, or similar.
You still can access it like Variable[0], Variable[1]...
Now...
The difference:
1
2
3
unsigned int RandomValue = 8 + rand() % (512-8); // Random Number Between 8 and 512
char * Variable = new char[RandomValue];
strcpy(Variable,"Test.");

It works!
You can use variables to create arrays of unlimited sizes, just like std::string or std::vector.
Remember to delete them anyways.
OT: For today i have to go, good luck with your game! ;)
Just a not guys, a memory leak will only last as long as the application is loaded into RAM.
computergeek, what do you mean? that these methods will cause memory leaks, or?
He means, when your program closes, even if you forgot to delete what you new'd, it gets deleted from the OS.
ah ok. btw i have been thinking about this for a bit now, but cant seem to figure out one thing.

if i use a multidimensional array, and every number corresponds to one specific tile (like 0 would be empty, 1 would be grass etc) but how would i tell the program where to place the different, tiles? like say i have an array called map. how would the program know that "map[0][1]" would start at x = 32, and y = 0? (assumng i make each tile 32x32)
i dont know if that makes sense but i dont know any other way to explain it at the moment.
Well...
Here is a way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void DrawImages()
{
	for(int x = 0; x < Tiles_X; x++)
	{
		for(int y = 0; y < Tiles_Y; y++)
		{
			//Get Your Surface
			// Your TopLeft Angle is now:
			// x * 32, y * 32
			// Your TopRight Angle is now:
			// (x+1) * 32, y * 32
			// Your BottomLeft Angle is now:
			// x * 32, (y+1) * 32
			// Your BottomRight Angle is now:
			// (x+1) * 32, (y+1) * 32
			// Your Center is now:
			// (TopLeft + BottomRight) / 2
			// Change eventually every '32' into your tile size
		}
	}
}
Last edited on
ah ok, apreciate the help. :D

i would have to make the tiles themself seperate from my array then?

like my map[][] array would decide what a tile should display (water, grass, dirt etc), but the tiles themself would already have a class or function that defines their size and placement? or am i wrong?
Last edited on
Pages: 12