Drawing a tile map

Started by
6 comments, last by wodinoneeye 16 years, 11 months ago
okay i am having a problem with my first try at making a tile map with mappy for some reason all the graphics have been rotated (not the pictures themselves, but the way they are written to the screen). if anyone can help i would be very grateful: #include <allegro.h> #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 #define NUM_TILES 20 #define TILE_SIZE 32 BITMAP* buffer; BITMAP* sky; BITMAP* ground; BITMAP* grass; BITMAP* player; int game_running = 0; const short ground_map1[20][20] = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }}; void draw_map(); void handle_input(); int main(int argc, char** argv[]) { allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0); buffer = create_bitmap(WINDOW_WIDTH, WINDOW_HEIGHT); sky = load_bitmap("sky.bmp", NULL); ground = load_bitmap("ground.bmp", NULL); grass = load_bitmap("grass.bmp", NULL); player = load_bitmap("player.bmp", NULL); while(game_running == 0) { draw_map(); draw_sprite(screen, buffer, 0, 0); clear_bitmap(buffer); handle_input(); if(key[KEY_ESC]) game_running = 1; } return 0; } END_OF_MAIN(); void draw_map() { for(int i = 0; i <= NUM_TILES; i++) { for(int j = 0; j <= NUM_TILES; j++) { if(ground_map1 [j] == 1) draw_sprite(buffer, grass, i * TILE_SIZE, j * TILE_SIZE); else if(ground_map1 [j] == 0) draw_sprite(buffer, sky, i * TILE_SIZE, j * TILE_SIZE); else if(ground_map1 [j] == 2) draw_sprite(buffer, ground, i * TILE_SIZE, j * TILE_SIZE); else if(ground_map1 [j] == 4) draw_sprite(buffer, player, i * TILE_SIZE, j * TILE_SIZE); } } } void handle_input() { //obviously not yet done }
Advertisement
yay. i fixed it
What did you find was wrong with it? What did you do to fix it?
If the entire tile map was rotated 90 degrees, then he had his x/y indexes backwards somewhere.
yeah that's right Deyja. in my draw_sprite stuff i had y then x instead of x then y.

You probably dont want to have the player as one of the map tiles.

Usually the movable objects are drawn in a seperate loop after you draw the tiles
-- so that they will appear ontop of the scenery (draw the objects with a key color (mode) for its background that is transparent...).

You can then also have the movable objects shift in fractions of a square size increments (per draw frame) to make their motion more fluid (instead of jumping a tile each move).


if(ground_map1 [j] == 1) draw_sprite(buffer, grass, i * TILE_SIZE, j * TILE_SIZE);
else if(ground_map1 [j] == 0) draw_sprite(buffer, sky, i * TILE_SIZE, j * TILE_SIZE);
else if(ground_map1 [j] == 2) draw_sprite(buffer, ground, i * TILE_SIZE, j * TILE_SIZE);
else if(ground_map1 [j] == 4) draw_sprite(buffer, player, i * TILE_SIZE, j * TILE_SIZE);

You could make the tile draw sprites simpler by putting the grass/sky/ground pointers in an array and passing it to the call in one line:

draw_sprite(buffer, pointerarray[ground_map1[j]], i*TILE_SIZE, j*TILE_SIZE);
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
this may sound like a stupid question but if i don't have my character as a tile how do i check if he hits a certain tile?
Quote:Original post by pi_man
this may sound like a stupid question but if i don't have my character as a tile how do i check if he hits a certain tile?



You can check the position on the tile map against the position XY stored in the mobile objects. If you are using the pixels themselves to check collisions, you can still do that without having to actually place the objectonto the pixels themselves.



direction comes from player input

TX = object.x // test position
TY = object.y

TX = TX + dir_x[direction] // shift test position along direction
TY = TY + dir_y[direction]

if (ground_map1[TX][TY] == GRASS) OR (ground_map1[TX][TY] == GROUND) // test
then
move is ok... object.x = TX object.y = TY
else
is blocked....



--------------------------------------------[size="1"]Ratings are Opinion, not Fact

This topic is closed to new replies.

Advertisement