Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with ALLEGRO_FLIP_HORIZONTAL and spritesheet

This thread is locked; no one can reply to it. rss feed Print
Problem with ALLEGRO_FLIP_HORIZONTAL and spritesheet
V T
Member #6,088
August 2005

Hello all,

I'm using a sprite sheet to hold my main players graphics. The last frame is the sprite for when the player is falling / in air. My code checks the direction of the player and flips the bitmap accordingly. However when flipping the frame for falling, the x location where the player is drawn gets flipped as well. Example, the frame on the sprite sheet starts at x = 102, and y = 0. When facing right (unflipped) the sprite draws at the correct location (the players x location). When facing left (the sprite is flipped) the sprite draws at the x location + 102. I feel like I have had this problem before and can't remember how it was fixed. I know I can just make a separate frame, and I will if I need to, but I'm curious what could be causing this. Anyone have any suggestions? My code is below:

#SelectExpand
1 2#define P_FALL_X 102 3#define P_FALL_Y 0 4#define P_FALL_W 133 5#define P_FALL_H 32 6 7 8void player::load_graphics(const char * file_name) 9{ 10 graphics = al_load_bitmap(file_name); 11 12 al_convert_mask_to_alpha(graphics,al_map_rgb(255,0,255)); 13 14 15 g_standing = al_create_sub_bitmap(graphics,P_STAND_X,P_STAND_Y,P_STAND_W,P_STAND_H); 16 g_st = al_create_sub_bitmap(graphics,P_STAND_X,P_STAND_Y,P_STAND_W,P_STAND_H); 17 18 g_falling = al_create_sub_bitmap(graphics,P_FALL_X,P_FALL_Y,P_FALL_W,P_FALL_H); 19 20 g_walking[0] = al_create_sub_bitmap(graphics,P_WALK1_X,P_WALK1_Y,P_WALK1_W,P_WALK1_H); 21 g_walking[1] = al_create_sub_bitmap(graphics,P_WALK2_X,P_WALK2_Y,P_WALK2_W,P_WALK2_H); 22 g_walking[2] = al_create_sub_bitmap(graphics,P_WALK3_X,P_WALK3_Y,P_WALK3_W,P_WALK3_H); 23 g_walking[3] = al_create_sub_bitmap(graphics,P_WALK4_X,P_WALK4_Y,P_WALK4_W,P_WALK4_H); 24 25 curFrame = NULL; 26} 27 28void player::draw_player() 29{ 30 int x_adjust = 0; 31 int y_adjust = 1; 32 33 x_adjust = P_STAND_W - al_get_bitmap_width(curFrame); 34 35 if (x_adjust < 0) 36 x_adjust = 0; 37 38 if(jumping) 39 x_adjust = 0; 40 41 if(d_y > 0) 42 { 43 y_adjust += d_y; 44 } 45 46 if(is_facing == LEFT) 47 { 48 //al_draw_bitmap_region(graphics,d_x,d_y,d_w,d_h,(x + x_adjust),(y+y_adjust),ALLEGRO_FLIP_HORIZONTAL); 49 al_draw_bitmap(curFrame,(x + x_adjust),(y + y_adjust),ALLEGRO_FLIP_HORIZONTAL); 50 51 } 52 else if(is_facing == RIGHT) 53 { 54 //al_draw_bitmap_region(graphics,d_x,d_y,d_w,d_h,(x + x_adjust),(y+y_adjust),0); 55 al_draw_bitmap(curFrame,(x + x_adjust),(y + y_adjust),0); 56 } 57} 58 59void player::set_curFrame() 60{ 61 if(velx == 0) 62 { 63 curFrame = g_st; 64 frame = 0; 65 } 66 else 67 { 68 frame++; 69 if(frame >= 1) 70 { 71 if(frame < 5) 72 { 73 curFrame = g_walking[0]; 74 } 75 else if(frame < 10) 76 { 77 curFrame = g_walking[1]; 78 } 79 else if(frame < 15) 80 { 81 curFrame = g_walking[2]; 82 } 83 else if(frame < 20) 84 { 85 curFrame = g_walking[3]; 86 } 87 else if (frame < 25) 88 { 89 frame = 5; 90 } 91 } 92 } 93 if(jumping || lockjump || is_falling) 94 { 95 curFrame = g_falling; 96 } 97}

Also, I tried removing the parts in the draw function where x_adjust is set, changing it so x_adjust is always = 0, and this didn't change anything. Any suggestions or ideas would be welcome.

beoran
Member #12,636
March 2011

I assume something is wrong with the x coordinate of player? Perhaps debgugging o a few printfs could clarify this.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I didn't look very closely at it, but it seems to me that line 33 should be:

x_adjust = P_STAND_W - al_get_bitmap_width(curFrame)/2;

They all watch too much MSNBC... they get ideas.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: