Jump to content
  • Advertisement
Sign in to follow this  
ToRuk MaKto

Tilemap Help!

This topic is 3061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, im writing a simple plattform game with c++/sdl. Its a school project:) Well anyways i got most of it down but the map. I tried to search for tutorials in google and stuff but couldnt find any decent good tutorial on how to do it. All i know is that i want to use a bidimentional array,2 for loops to do this job. Just a simple code for setting like 32*32 tiles on the map. Hope i could explain it well.

Share this post


Link to post
Share on other sites
Advertisement
You mean like this?


const int width = 32;
const int height = 32;

int map[width][height]; //Replace int with your map tile type. And I think I got
//the width/height the right way round.
for(int i = 0; i <= width; i++)
{
for(int j = 0; j <= height; j++)
{
map[j] = /* whatever you read in to set it to. */
}
}


Share this post


Link to post
Share on other sites
Yes just like that, i had gotten that far to, lets just say for an example that i got: SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
SCREEN_BPP = 32;
and i just want to put 32*32 tiles arount the map, just on the edges like a wall around the screen. How do i do it?

Ty for ur help

Share this post


Link to post
Share on other sites
Quote:
Original post by ToRuk MaKto
Yes just like that, i had gotten that far to, lets just say for an example that i got: SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
SCREEN_BPP = 32;
and i just want to put 32*32 tiles arount the map, just on the edges like a wall around the screen. How do i do it?

Ty for ur help


So the question is, how to draw a 32x32 section of the map? Given:

M, the map with R rows, and C columns
Z, the size of a tile in pixels
W, H, the screen resolution
X, Y, the center of the camera

To find how many tiles fit on the screen: W/Z by H/Z tiles
To find out the tile that is at the center of the camera: At (X/Z, Y/Z)
To find the top-left most tile that is visible on screen: At (X/Z-W/(2Z), Y/Z-H/(2Z)

Combined you get something like this:

// Draws a tile on the screen whose top left corner resides at x, and y
void drawTile( int x, int y, int idx );

int visibleTilesW = W / Z; // Number of tiles that fit on the length of the screen
int visibleTilesH = H / Z; // Number of tiles that fit on the height of the screen
int startX = X/Z - visibleTilesW/2; // The tile at the top left of the screen when camera is at (X,Y)
int startY = Y/Z - visibleTilesH/2;

for ( int row = 0; row < visibleTilesH; ++row )
for ( int col = 0; col < visibleTilesW; ++col ) {
// Todo: Be sure to check for out-of-bounds!
drawTile( col*Z, row*Z, M[row+startY][col+startX] );
}



Does this make sense?

Share this post


Link to post
Share on other sites
Think of it in steps. I wont put code since its a school deal.

You need to create some variables to hold your tiles.
Load your tiles into those variables.
create some sort of backbuffer
now draw the tiles to the backbuffer using your loops
draw the backbuffer to the screen

main loop
erase screen
redraw tiles to backbuffer
draw backbuffer to screen


I have never used SDL but i assume this would be similar to the process you would use to draw a tile map. Its what i did in other languages to tile a map. Hope it helps. If this doesnt help hopefully someone can make this better without reeming me too bad =O

Share this post


Link to post
Share on other sites
Oh no. Next you'll be asking how to pass around 2d arrays.. Just go straight with objects:

class level_map
{
public:
static const int width = 32;
static const int height = 32;
typedef int tile_type;

tile_type& tile_at(int x, int y);

private:
// data...
};




But ofcourse if your job is to learn the arrays, this wont help :)

Share this post


Link to post
Share on other sites
google lazyfoo for SDL examples he shows how to do tileing with SDL really easy to understand.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!