Sign in to follow this  

Tilemap Help!

This topic is 2846 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
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[i][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

This topic is 2846 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.

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