Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » very basic bitmap animation

This thread is locked; no one can reply to it. rss feed Print
very basic bitmap animation
DuncanShine
Member #17,479
March 2020

I have created two images that are supposed to be part of a character's animation cycle, and I want to rapidly switch between the two images, like a run cycle. However, just doing this makes the image change way too quickly, and adding a delay with Sleep() would slow down the whole program so i'm stuck as to how i would do it

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

The key is measuring time without delaying the program. A timer will work for this, as will al_wait_for_vsync, or simply al_get_time.

Decide the duration of the animation in seconds, determine how many frames you have and then to get the current animation frame, use fmod.

#SelectExpand
1#include <allegro5/allegro.h> 2... 3const double FPS = 60.0; 4const double SPF = 1.0/FPS; 5 6ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 7ALLEGRO_TIMER* timer = al_create_timer(SPF); 8if (!timer) {Fail();} 9al_register_event_source(queue , al_get_timer_event_source(timer)); 10 11double duration = 2.0;/// 2 seconds for 2 frames 12int nframes = 2; 13double elapsed_time; 14 15al_start_timer(timer); 16 17/// In event loop 18while (!quit) { 19 if (redraw) { 20 al_clear_to_color(al_map_rgb(255,255,255)); 21 int FRAME_NUM = (int)nframes*fmod(elapsed_time , duration)/duration; 22 al_draw_bitmap(bitmaps[FRAME_NUM] , 0 , 0); 23 al_flip_display(); 24 redraw = false; 25 } 26 27 do { 28 ALLEGRO_EVENT ev; 29 al_wait_for_event(queue , &ev); 30 if (ev.type == ALLEGRO_EVENT_TIMER) { 31 elapsed_time += SPF; 32 redraw = true; 33 } 34 } while (!al_is_event_queue_empty(queue)); 35}; 36return 0; 37}

Niunio
Member #1,975
March 2002
avatar

[edit]I was distracted and Edgar send it before me, but I think mine is simpler.[/edit]

Keep track about how many time the animation frame is on screen:

#SelectExpand
1 typedef struct { 2 int frame, cnt 3 } ANIMATION_STATE; 4 5 ANIMATION_STATE MySpriteState; 6 7... 8 9/* Initialization */ 10 MySpriteState.frame = FIRST_FRAME_NDX; MySpriteState.cnt = 0; 11 12.... 13/* At updating. */ 14 if (++MySpriteState.cnt > MAX_TIME_FRAME) { 15 MySpriteState.cnt = 0; 16 if (++MySpriteState.frame >= NUM_FRAMES) MySpriteState.frame = FIRST_FRAME_NDX; 17 } 18...

-----------------
Current projects: Allegro.pas | MinGRo

DuncanShine
Member #17,479
March 2020

This all works, how would i modify it to happen when the character is moving in a specific direction?

Niunio
Member #1,975
March 2002
avatar

Not sure what do you mean...

-----------------
Current projects: Allegro.pas | MinGRo

DuncanShine
Member #17,479
March 2020

well i've made it so that when you change direction, the character does the opposite animation but the problem is a huge delay between pressing the button and the character actually moving.

Niunio
Member #1,975
March 2002
avatar

Without actual code is hard to say. Maybe you should take a look to the Allegro Vivace tutorial.

-----------------
Current projects: Allegro.pas | MinGRo

Go to: