Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Sprite Flickering

This thread is locked; no one can reply to it. rss feed Print
Sprite Flickering
imgregduh
Member #15,241
July 2013

when I run the program my sprite flickers as its moving in any direction but it is showing the animation. I am not sure what is wrong. I'm working with classes so its harder to see all the files, I hope the two I show is enough to see the problem. It definitely seems to do with the rendering but I'm not sure what about it though.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_ttf.h> 6#include <allegro5/allegro_audio.h> 7#include <allegro5/allegro_acodec.h> 8#include <list> 9 10#include "GameObject.h" 11#include "Player.h" 12#include "Global.h" 13#include "TileEngine.h" 14 15bool keys[] = {false, false, false, false, false}; 16enum KEYS{DOWN, LEFT, RIGHT, UP, SPACE}; 17 18//globals 19Player *player; 20std::list<GameObject *> objects; 21std::list<GameObject *>::iterator iter; 22std::list<GameObject *>::iterator iter2; 23 24//prototypes 25 26int main(int argc, char **argv) 27{ 28 //============================================== 29 //SHELL VARIABLES 30 //============================================== 31 bool done = false; 32 bool draw = false; 33 bool render = false; 34 bool active = false; 35 36 float gameTime = 0; 37 int frames = 0; 38 int gameFPS = 0; 39 int direction =0; 40 41 //============================================== 42 //PROJECT VARIABLES 43 //============================================== 44 player = new Player(); 45 int state = -1; 46 47 ALLEGRO_BITMAP *playerImage = NULL; 48 //============================================== 49 //ALLEGRO VARIABLES 50 //============================================== 51 ALLEGRO_DISPLAY *display = NULL; 52 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 53 ALLEGRO_TIMER *timer; 54 ALLEGRO_FONT *font18; 55 56 //============================================== 57 //ALLEGRO INIT FUNCTIONS 58 //============================================== 59 if(!al_init()) //initialize Allegro 60 return -1; 61 62 display = al_create_display(WIDTH, HEIGHT); //create our display object 63 64 if(!display) //test display object 65 return -1; 66 //============================================== 67 //COLOR 68 //============================================== 69 white = al_map_rgb(0,0,0); 70 magenta = al_map_rgb(255,0,255); 71 red = al_map_rgb(255,0,0); 72 blue = al_map_rgb(0,0,255); 73 green = al_map_rgb(0,255,0); 74 75 //============================================== 76 //ADDON INSTALL 77 //============================================== 78 al_install_keyboard(); 79 al_init_image_addon(); 80 al_init_font_addon(); 81 al_init_ttf_addon(); 82 al_init_primitives_addon(); 83 al_install_audio(); 84 al_init_acodec_addon(); 85 86 //============================================== 87 //Object INIT 88 //============================================== 89 90 TileEngine tileEngine; 91 92 //============================================== 93 //PROJECT INIT 94 //============================================== 95 font18 = al_load_font("arial.ttf", 18, 0); 96 97 playerImage = al_load_bitmap("Character01.png"); 98 al_convert_mask_to_alpha(playerImage, white); 99 player->Init(playerImage); 100 objects.push_back(player); 101 102 ALLEGRO_KEYBOARD_STATE keyState; 103 srand(time(NULL)); 104 //============================================== 105 //TIMER INIT AND STARTUP 106 //============================================== 107 event_queue = al_create_event_queue(); 108 timer = al_create_timer(1.0 / 60); 109 110 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 111 al_register_event_source(event_queue, al_get_keyboard_event_source()); 112 113 al_start_timer(timer); 114 gameTime = al_current_time(); 115 player->SetAwake(false); 116 while(!done) 117 { 118 ALLEGRO_EVENT ev; 119 al_wait_for_event(event_queue, &ev); 120 121 //============================================== 122 //INPUT 123 //============================================== 124#pragma region Input 125 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 126 { 127 if(!player->GetAwake()) 128 { 129 switch(ev.keyboard.keycode) 130 { 131 case ALLEGRO_KEY_ESCAPE: 132 done = true; 133 player->SetAwake(true); 134 break; 135 case ALLEGRO_KEY_LEFT: 136 keys[LEFT] = true; 137 player->SetAwake(true); 138 break; 139 case ALLEGRO_KEY_RIGHT: 140 keys[RIGHT] = true; 141 player->SetAwake(true); 142 break; 143 case ALLEGRO_KEY_UP: 144 keys[UP] = true; 145 player->SetAwake(true); 146 break; 147 case ALLEGRO_KEY_DOWN: 148 keys[DOWN] = true; 149 player->SetAwake(true); 150 break; 151 case ALLEGRO_KEY_SPACE: 152 keys[SPACE] = true; 153 break; 154 } 155 } 156 player->SetAwake(true); 157 } 158 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 159 { 160 switch(ev.keyboard.keycode) 161 { 162 case ALLEGRO_KEY_ESCAPE: 163 done = true; 164 break; 165 case ALLEGRO_KEY_LEFT: 166 if(keys[LEFT]) 167 { 168 keys[LEFT] = false; 169 player->SetAwake(false); 170 } 171 break; 172 case ALLEGRO_KEY_RIGHT: 173 if(keys[RIGHT]) 174 { 175 keys[RIGHT] = false; 176 player->SetAwake(false); 177 } 178 break; 179 case ALLEGRO_KEY_UP: 180 if(keys[UP]) 181 { 182 keys[UP] = false; 183 player->SetAwake(false); 184 } 185 break; 186 case ALLEGRO_KEY_DOWN: 187 if(keys[DOWN]) 188 { 189 keys[DOWN] = false; 190 player->SetAwake(false); 191 } 192 break; 193 case ALLEGRO_KEY_SPACE: 194 keys[SPACE] = false; 195 break; 196 } 197 198 } 199 200#pragma endregion 201 //============================================== 202 //GAME UPDATE 203 //============================================== 204#pragma region GameUpdate 205 else if(ev.type == ALLEGRO_EVENT_TIMER) 206 { 207 render = true; 208 209 //UPDATE FPS=========== 210 frames++; 211 if(al_current_time() - gameTime >= 1) 212 { 213 gameTime = al_current_time(); 214 gameFPS = frames; 215 frames = 0; 216 } 217 //===================== 218 219 if(keys[DOWN]) 220 { 221 player->MoveDown(); 222 direction = 0; 223 } 224 else if(keys[LEFT]) 225 { 226 player->MoveLeft(); 227 direction = 1; 228 } 229 else if(keys[RIGHT]) 230 { 231 player->MoveRight(); 232 direction = 2; 233 } 234 else if(keys[UP]) 235 { 236 player->MoveUp(); 237 direction = 3; 238 } 239 else 240 { 241 player->ResetAnimation(direction); 242 } 243 244 //update 245 for(iter = objects.begin(); iter != objects.end(); ++iter) 246 (*iter)->Update(); 247 248 //collisions 249 250 } 251 //cull the dead 252 for(iter = objects.begin(); iter != objects.end(); ) 253 { 254 if(! (*iter)->GetAlive()) 255 { 256 delete (*iter); 257 iter = objects.erase(iter); 258 } 259 else 260 iter++; 261 } 262 263#pragma endregion 264 265 //============================================== 266 //RENDER 267 //============================================== 268 269#pragma region Render 270 if(render && al_is_event_queue_empty(event_queue)) 271 { 272 render =false; 273 al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "FPS: %i", gameFPS); //display FPS on screen 274 al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 20, 0, "X = %f", player->GetX()); 275 al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 35, 0, "Y = %f", player->GetY()); 276 //BEGIN PROJECT RENDER================ 277 278 for(iter = objects.begin(); iter != objects.end(); ++iter) 279 (*iter)->Render(); 280 281 282 283 284 285 //FLIP BUFFERS======================== 286 //tileEngine.DrawMap(); 287 al_flip_display(); 288 al_clear_to_color(al_map_rgb(0,0,0)); 289 }//end render 290 #pragma endregion 291 292 }// end while loop 293 294 295 //============================================== 296 //DESTROY PROJECT OBJECTS 297 //============================================== 298 for(iter = objects.begin(); iter != objects.end(); ) 299 { 300 (*iter)->Destroy(); 301 delete (*iter); 302 iter = objects.erase(iter); 303 } 304 305 306 al_destroy_bitmap(playerImage); 307 308 //SHELL OBJECTS================================= 309 al_destroy_font(font18); 310 al_destroy_timer(timer); 311 al_destroy_event_queue(event_queue); 312 al_destroy_display(display); 313 314 315 return 0; 316} 317 318 319 320 321#include"Player.h" 322 323 324Player::Player() 325{ 326 327} 328void Player::Destroy() 329{ 330 GameObject::Destroy(); 331} 332 333void Player::Init(ALLEGRO_BITMAP *image) 334{ 335 GameObject::Init(20, 200, 4, 4, -1, 0, 10, 12); 336 337 SetID(PLAYER); 338 339 maxFrame = 12; 340 curFrame = 0; 341 frameWidth = 32; 342 frameHeight = 32; 343 animationColumns = 3; 344 animationDirection = 4; 345 346 animationRow = 4; 347 348 if(image != NULL) 349 Player::image = image; 350} 351 352void Player::Update() 353{ 354 GameObject::Update(); 355 if(x < 0) 356 x = 0; 357 else if(x > WIDTH) 358 x = WIDTH; 359 360 if(y < 0) 361 y = 0; 362 else if(y > HEIGHT) 363 y = HEIGHT; 364} 365void Player::Render() 366{ 367 fx += frameWidth; 368 fy = animationRow * frameHeight; 369 timeSinceLastFrameSwap = 0.0f; 370 if(!GetAwake()) 371 { 372 fx = 32; 373 } 374 if(fx > frameWidth * 3) 375 { 376 fx = 0; 377 } 378 al_draw_bitmap_region(image, fx, fy, frameWidth, frameHeight,x , y , 0); 379 380} 381 382void Player::MoveDown() 383{ 384 animationRow = 0; 385 curFrame = 1; 386 dirY = 1; 387 388} 389void Player::MoveLeft() 390{ 391 animationRow = 1; 392 curFrame = 1; 393 dirX = -1; 394 395} 396void Player::MoveRight() 397{ 398 animationRow = 2; 399 curFrame = 1; 400 dirX = 1; 401 402} 403void Player::MoveUp() 404{ 405 animationRow = 3; 406 curFrame = 1; 407 dirY = -1; 408 409} 410 411void Player::ResetAnimation(int position) 412{ 413 switch(position) 414 { 415 case 0: 416 animationRow = 0; 417 dirY = 0; 418 break; 419 case 1: 420 animationRow = 1; 421 dirX = 0; 422 break; 423 case 2: 424 animationRow = 2; 425 dirX = 0; 426 break; 427 case 3: 428 animationRow = 3; 429 dirY = 0; 430 break; 431 } 432 433}

l j
Member #10,584
January 2009
avatar

  if(fx > frameWidth * 3)
  {
    fx = 0;
  }
  al_draw_bitmap_region(image, fx, fy, frameWidth, frameHeight,x , y , 0);

Perhaps you should try.

  if(fx >= frameWidth * 3)
  {
    fx = 0;
  }

I think you might be drawing outside the bounds of the sprite.

imgregduh
Member #15,241
July 2013

taron said:

if(fx > frameWidth * 3)
  {
    fx = 0;
  }
  al_draw_bitmap_region(image, fx, fy, frameWidth, frameHeight,x , y , 0);
Perhaps you should try.

  if(fx >= frameWidth * 3)
  {
    fx = 0;
  }

I think you might be drawing outside the bounds of the sprite.

Yup, that was totally it, thanks !

Go to: