While I am still busy with rewriting everything with the new lib and some RL stuff I will most likely try to explain how I did some stuff with my game, maybe it gets someone some insight, maybe I can get some feedback on how I can do it better.
I will most likely do the lib-specific stuff in pseudo-code and the rest C/C++ style.
So here comes the first one, Tiled maps :
To keep it simple lets say we have the map stored in arrays
int map[49][49]; // map tiles
int obj[49][49]; // object tiles
int pass[49][49]; // shows if the player can walk on the tile
That leaves us with a 50x 50 map.
And lets say we have an array of the images we want to draw:
sometype tiles[100];
sometype objects[100];
As we want the player to be centered in the middle of the map all the time (at least in this example) we have to draw everything corresponding to that and we need to know how big the tiles are.
offset_x= SCREEN_W/2;
offset_y= SCREEN_H/2;
tile_size=32; //size of the tiles in pixel
The next thing we want to do is to ensure, we only draw what´s actually on the screen AND inside the map.
float camera_x_low=0;
float camera_x_high=0;
float camera_y_low=0;
float camera_y_high=0;
The first part of our draw_map() function would look like this then:
if(player.x/tile_size-ceil(offset_x/tile_size)<0)
camera_x_low=0;
else
camera_x_low=(player.x/tile_size-ceil(offset_x/tile_size));
if(player.x/tile_size+ceil(offset_x/tile_size)>49)
camera_x_high=49;
else
camera_x_high=(player.x/tile_size+ceil(offset_x/tile_size));
if(player.y/tile_size-ceil(offset_y/tile_size)<0)
camera_y_low=0;
else
camera_y_low=(player.y/tile_size-ceil(offset_y/tile_size));
if(player.y/tile_size+ceil(offset_y/tile_size)>49)
camera_y_high=49;
else
camera_y_high=(player.y/tile_size+ceil(offset_y/tile_size));
So, if the player_position - half_the_screensize/tile_size < 0 you draw from 0 (so its not outside of the map). if the player_position + half_the_screensize/tile_size > 49 you draw to 49 (so its not outside of the map).
Now we put all the drawing code into a small, but neat for-loop:
for(int i=camera_x_low;i<camera_x_high;++i)
{
for(int t=camera_y_low;t<camera_y_high;++t)
{
// drawing code
}
}
Everything that is just inside that for-loops will only get drawn if it IS actually on the map AND on the screen.
Now we add the basic of drawings:
for(int i=camera_x_low;i<camera_x_high;++i)
{
for(int t=camera_y_low;t<camera_y_high;++t)
{
int tile_number=map[t][i]; // that way its easier to call
if(tile number>0) // so if the tile is actually filled
{
draw(tiles[tile_number],i*tile_size-offset_x,t*tile_size-offset_y); // draws the tile(read out of the map[][]array) and draws it to its position
}
}
}
With just that little bit of actual code, we draw everything that needs to be on the screen and ONLY what should be on the screen.
If this stuff gets some good feedback I might have to consider another way of formating this, because in the preview it looks horrible >.<
Anyway, have a nice day guys ;)
P.S.:screw spellcheck and mistakes in the code. If something seems a bit too off, it´s either a mistake or I srsly should stop writing code :D