Back to Computers & Modding

Timers in allegro 5

vivoOP
#1
Hey
I was looking into Allegro game library and I had a quick question about the timer system. Does it control the rate of change for the whole system? I thought the timer was more for timing things. One other thing: after I read through the tutorial, is this all I need to know in order to make a game? It seemed too easy to me (at least for creating a game) considering it takes 500+ pages to explain DirectX and over 1000 to explain Winapi (granted, it has a wide array of uses) . Maybe less is more. Thanks.
Post Merge: [time]1322531489[/time]
Oh and I almost forgot here is the tutorial I was referring to (Sorry :) ) : http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Bitmaps
Loonylion
#2
timers usually trigger an event every interval, its up to you what happens when the event triggers.Theres a lot of explanation for directx for a reason, it is not a simple API. allegro might make things easy but you would have far more control by using raw directx.It is unlikely you will need to read even 1/10th of the winapi docs.
vivoOP
#3
Okay so the FPS (Frames Per Second) are not determined by the timer, but by itself. And also, is FPS predefined in the allegro header? Thanks.
Loonylion
#4
FPS should not be arbitarily defined, its the outcome of how much work needs doing to render each frame. less work = higher fps
vivoOP
#5
#include <stdio.h>
#include <allegro5/allegro.h>
 
const float FPS = 60;
const int SCREEN_W = 640;
const int SCREEN_H = 480;
const int BOUNCER_SIZE = 32;
 
int main(int argc, char **argv){
   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_EVENT_QUEUE *event_queue = NULL;
   ALLEGRO_TIMER *timer = NULL;
   ALLEGRO_BITMAP *bouncer = NULL;
   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
   float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
   float bouncer_dx = -4.0, bouncer_dy = 4.0;
   bool redraw = true;
 
   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }
 
   timer = al_create_timer(1.0 / FPS);
   if(!timer) {
      fprintf(stderr, "failed to create timer!\n");
      return -1;
   }
 
   display = al_create_display(SCREEN_W, SCREEN_H);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      al_destroy_timer(timer);
      return -1;
   }
 
   bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
   if(!bouncer) {
      fprintf(stderr, "failed to create bouncer bitmap!\n");
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -1;
   }
 
   al_set_target_bitmap(bouncer);
 
   al_clear_to_color(al_map_rgb(255, 0, 255));
 
   al_set_target_bitmap(al_get_backbuffer(display));
 
   event_queue = al_create_event_queue();
   if(!event_queue) {
      fprintf(stderr, "failed to create event_queue!\n");
      al_destroy_bitmap(bouncer);
      al_destroy_display(display);
      al_destroy_timer(timer);
      return -1;
   }
 
   al_register_event_source(event_queue, al_get_display_event_source(display));
 
   al_register_event_source(event_queue, al_get_timer_event_source(timer));
 
   al_clear_to_color(al_map_rgb(0,0,0));
 
   al_flip_display();
 
   al_start_timer(timer);
 
   while(1)
   {
      ALLEGRO_EVENT ev;
      al_wait_for_event(event_queue, &ev);
 
      if(ev.type == ALLEGRO_EVENT_TIMER) {
         if(bouncer_x < 0 || bouncer_x > SCREEN_W - BOUNCER_SIZE) {
            bouncer_dx = -bouncer_dx;
         }
 
         if(bouncer_y < 0 || bouncer_y > SCREEN_H - BOUNCER_SIZE) {
            bouncer_dy = -bouncer_dy;
         }
 
         bouncer_x += bouncer_dx;
         bouncer_y += bouncer_dy;
 
         redraw = true;
      }
      else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
         break;
      }
 
      if(redraw && al_is_event_queue_empty(event_queue)) {
         redraw = false;
 
         al_clear_to_color(al_map_rgb(0,0,0));
 
         al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
 
         al_flip_display();
      }
   }
 
   al_destroy_bitmap(bouncer);
   al_destroy_timer(timer);
   al_destroy_display(display);
   al_destroy_event_queue(event_queue);
 
   return 0;
}
Now here is what I don't get from this code excerpt from the tutorial:
const float FPS = 60;
Ok, so when I play around with this, it changes how fast the bitmap/sprite moves on the screen.
but then here is where they loose me:
 timer = al_create_timer(1.0 / FPS);
I really don't get what they are doing .
Thanks
vivoOP
#6
I think I get it. When you initialize the timer, it becomes the event que (ev) and then the next line tests the condition, and if it is true, it then initializes a certain routine . Thanks again loony
Sorry for not getting right away.
Showing posts 1–6 of 6 · This discussion is archivedBack to top