Skip to main content
GameDev.net gamedev.net
🔒 Locked

Optimizing code in a 2d tile engine

Started by Creyx Aug 27, 2011 at 7:04 AM 0 replies 1.7k views
Original Post
Creyx
Creyx
Hello,

over the past time I've learned many ways to set up a simple tile engine for 2d games and I also came across many different solutions to actually
put something up but when it comes to practice I'm still a beginner.

These different solutions, however, raise the question which one of these solutions are actually the best pick and why and I hope somebody can help me out here!

I'll just start with a basic function: loading all the necessary sprites for the game

1st way to do this: Use different for loops and just load them with the basic functions
(these are just basic examples, please don't comment on the name of variables/arrays)
Ex.:

for (int spriteid = 0; spriteid < 2; spriteid++)
{
std::stringstream ss;
ss << spriteid+1000;
if (!sprites[spriteid]].LoadFromFile("c:/example/path"+ss.str()+".png"))
return EXIT_FAILURE;
}


2nd way to do this: Use the vector class and its functions to store the necessary sprites.
No example code here, as I have not yet tried to do something with it, as the example above is working
without any problems.
I see many people use the vector class. Is it recommneded? If so, why?

Next topic: Saving the 2d map

1st way to do this: Simply store long values in a text document indicating which "sprite id" should be used to display.
This sounds simple to me, it works and it should also be quite a fast way to save and load a map as these long values
are the only thing needed to display the map.
I'm using a map array ( map[length][height] ) to read these long values and to maybe save them when using a map editor
Ex.:

// fill the map with some values for a test draw
int map[20][15];
for (int n = 0; n<20; n++)
{
for (int t = 0; t<15; t++)
{
Map[n][t] = Rand_Number(1); // random number between 0 and 1, this would be indicating a sprite with id 0 or 1
}
}

// draw the map
for (int n = 0; n<20; n++)
{
for (int t = 0; t<15; t++)
{
draw(sprites(Map[n][t], n*32, t*32); // 2nd and 3rd parameter are x and y positions, look at the first example to see how this "sprites" array would look like
}
}


Again, are there any better options for drawing the sprites and saving/reading the map? The first for loop is not necessary as soon as the map editor is working; but this map editor would
use the same logic.
When I spent some time in java I came across the way of serializing this in an objectoutput/inputstream. Would this work 'faster' or have any other advantages?
Should I save my maps in some kind of databases?

Next topic: Collision

This topic is actually clear to me but still I'd like to ask if there are better ways for a collision system.
At the moment my collision system works like this: To check if, for example, the player sprite is colliding with an object
I'm just diving the Player coordinates by the tilesize to get the current location on the tilemap.
Then I'm checking the borders of the player sprite, divide these by the tilesize again and check the current tile at this location, if it's an inaccessible tile (this would have to be stored in a bool variable)
the player should not be able to move on it.
I actually can't think of a faster way than this, as there are no graphical checks necessary, only simple checks for coordinates.
Still I'd like to hear about the opnion of of a experienced game programmer!


Ok, so these are some questions I can't answer for myself. I hope somebody can help me here.

Thanks in advance,
Creyx

Topic Locked

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

Sign in to reply to this topic.