Sign in to follow this  
vipah

Drawing whats viewable by the camera (SDL/C++)

Recommended Posts

Hey guys, Something I'm puzzling my way through, however I may have stumbled upon the answer while searching. Lets say I have a grid of 50x50 tiles and I'd like to draw a 16x12 grid of them on a larger SDL_Surface say 1024x768(my main SDL window). This would be for a map editor, the map (grid of tiles) would show up on the left side of the screen and the rest would be for tile palettes, buttons etc... The issue is that the actual map will be much larger then 16x12..it might be 100x12. I would like to have per pixel scrolling when moving the map right and left. How exactly do I limit the camera to showing only what would exist in it's view? Say the camera is at x = 76... I should then be seeing roughly half of the fourth column of tiles on and half the 20th. The solution I found is to draw the entire map..or maybe just all tiles currently viewable to an SDL_Surface and then blit only that part hat I need to my main display. Is this the way it would be done? I appreciate your time. Thanks!

Share this post


Link to post
Share on other sites
Your camera will define a mobile rectangle that shows a portion of the game map. You then determine which tiles intersect that map, and draw them. For example, say your tiles are 100x100, your game screen is 800x600, and your camera is at (550, 350). Then your view rectangle extends from (150, 50) to (950, 650). All of the tiles from [0][0] through to [9][6] intersect the view rectangle and thus must be drawn.

You will additionally need to offset where the tiles are drawn based on their position relative to the camera. For example, with the aforementioned camera, a tile whose position in the grid is [4][2] would be drawn about halfway across the screen and a bit above center.

Share this post


Link to post
Share on other sites
If the tile width is, say 32, and x=76 then using integer math:


x/32=2;
x%32=12;


therefore x=76 is 12 pixels across the 3rd tile in your grid (assuming we are counting from zero in our grid co-ordinates). This makes sense when you think about it - 32+32+12=76.

The same principle applies on the y axis and this gives you the top-left cell of the grid to draw your cells from (from the divisions) and the pixel offset into that cell (from the modulus) based on a per-pixel camera location.

You can then use these values to step across the map drawing only the cells that are currently visible.


// coding in the browser but general idea:

int tiles_across=20,tiles_down=10;
int map[100][100]; // filled with image indices

void draw_map(int ox,int oy) // ox,oy = camera pos
{
int mx=ox/32,my=oy/32;
int dx=ox%32,dy=oy%32;

for(int y=0;y<tiles_down+1;++y)
{
for(int x=0;x<tiles_across+1;++x)
{
int image=map[x+mx][y+my];

// x or y times the tile width minus the offset is the screen pos of the tile

draw(image,(x*32)-dx,(y*32)-dy);
}
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this