Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Can't start multiple timers

This thread is locked; no one can reply to it. rss feed Print
Can't start multiple timers
DragonSpace23
Member #16,945
January 2019

Hi
I'm having some problem starting more than one timer. I've made a class where I create 2 timers, one for game logic and one for graphic. The problem is that when I start the timer only one of them is really starting. What should I do?

Sorry if my inglesh is bad

Audric
Member #907
January 2001

It's difficult to say without seeing your code that declares / sets up / starts these timers.
The error may also be when you obtain the timer events, are you sure that you are correctly finding the source of each timer event (the event's field ".timer.source" should be equal to the first timer OR the second timer)

DragonSpace23
Member #16,945
January 2019

I use 2 different event queues, one for timer.

#SelectExpand
1bool INTERFACE::init(ALLEGRO_DISPLAY* display) { 2 if (int_queue) { 3 queue = al_create_event_queue(); 4 5 al_register_event_source(queue, al_get_keyboard_event_source()); 6 al_register_event_source(queue, al_get_display_event_source(display)); 7 } 8 9 grp.queue = al_create_event_queue(); 10 grp.timer = al_create_timer(1 / setting[SETTING_FPS]); 11 if (!grp.timer) printf("Failed creating timer"); 12 timer = al_create_timer(1 / setting[SETTING_TPS]); 13 if (!timer) printf("Failed creating gtimer"); 14 15 al_register_event_source(grp.queue, al_get_timer_event_source(grp.timer)); 16 al_register_event_source(queue, al_get_timer_event_source(timer)); 17 18 called_init = true; 19 return 0; 20}

#SelectExpand
1void INTERFACE::start() { 2 al_start_timer(grp.timer); 3 al_start_timer(timer); 4}

#SelectExpand
1int INTERFACE::is_running() { 2 int rvalue = 0; 3 4 if (al_get_timer_started(timer)) 5 rvalue += 1; 6 if (al_get_timer_started(grp.timer)) 7 rvalue += 2; 8 9 return rvalue; 10}

I use this function to check if the timers has started and it returns 3 every time

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Audric
Member #907
January 2001

1 / setting[SETTING_FPS]

setting[] is hopefully an array of floats/doubles. If it's an array of integers, the division will be integer division, where 1 / 60 is 0.

One way to foolproof such code is to write "1.0 / x", so that no matter the type of x, it gets 'promoted' to double in order to perform the division.

DragonSpace23
Member #16,945
January 2019

The problem is that only the first I start work. If I print their count the first counts right, but the second stays at zero. I tried starting gps.timer before timer and gps.timer work while timer don't. Event check actualy work.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

DragonSpace23
Member #16,945
January 2019

I changed check_event to get timers count

#SelectExpand
1int main(int argc, char **argv) { 2 al_init(); 3 al_install_keyboard(); 4 5 ALLEGRO_EVENT event; 6 ALLEGRO_DISPLAY* display = al_create_display(1280, 720); 7 8 INTERFACE interface; 9 interface.set_event(&event); 10 interface.init(display); 11 12 unsigned char key[ALLEGRO_KEY_MAX]; 13 memset(key, 0, sizeof(key)); 14 15 unsigned char op1 = 0; 16 17 interface.start(); 18 while (true) { 19 int64_t timer_count, gtimer_count; 20 op1 = interface.check_event(&timer_count, &gtimer_count); 21 system("cls"); 22 switch (interface.is_running()) { 23 case 0: 24 printf("No timer running\n"); 25 break; 26 case 1: 27 printf("Timer running\n"); 28 break; 29 case 2: 30 printf("GTimer running\n"); 31 break; 32 case 3: 33 printf("Both timer running\n"); 34 break; 35 } 36 printf("Timer count: %d\nGTimer count: %d\n", timer_count, gtimer_count); 37 38 op1 = 0; 39 } 40 41 getchar(); 42 return 0; 43}

#SelectExpand
1bool INTERFACE::init(ALLEGRO_DISPLAY* display) { 2 if (int_queue) { 3 queue = al_create_event_queue(); 4 5 al_register_event_source(queue, al_get_keyboard_event_source()); 6 al_register_event_source(queue, al_get_display_event_source(display)); 7 } 8 9 grp.queue = al_create_event_queue(); 10 grp.timer = al_create_timer(1 / setting[SETTING_FPS]); 11 if (!grp.timer) printf("Failed creating timer"); 12 timer = al_create_timer(1 / setting[SETTING_TPS]); 13 if (!timer) printf("Failed creating gtimer"); 14 15 al_register_event_source(grp.queue, al_get_timer_event_source(grp.timer)); 16 al_register_event_source(queue, al_get_timer_event_source(timer)); 17 18 called_init = true; 19 return 0; 20} 21 22bool INTERFACE::is_init() { 23 return called_init; 24} 25 26void INTERFACE::start() { 27 al_start_timer(timer); 28 al_start_timer(grp.timer); 29} 30 31int INTERFACE::is_running() { 32 int rvalue = 0; 33 34 if (al_get_timer_started(timer)) 35 rvalue += 1; 36 if (al_get_timer_started(grp.timer)) 37 rvalue += 2; 38 39 return rvalue; 40} 41 42void INTERFACE::stop() { 43 al_stop_timer(timer); 44 al_stop_timer(grp.timer); 45 46} 47 48unsigned char INTERFACE::check_event(/*int input, unsigned char key[]*/int64_t *timer_count, int64_t *gtimer_count) { 49 unsigned char rvalue = 0; 50 51 *timer_count = al_get_timer_count(timer); 52 *gtimer_count = al_get_timer_count(grp.timer); 53 54return rvalue; 55}

Enough?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

 printf("Timer count: %d\nGTimer count: %d\n", timer_count, gtimer_count);

Both of those variables were declared as int64_t, but you're designating themas %d, which is a signed integer. Your displayed counts will likely be wrong. You need to use %llu instead. See printf reference for details.

Also, you didn't show us where your settings are declared and defined. As Audric said earlier, if settings[SETTINGS_TPS] or settings[SETTINGS_FPS] are integers , then your division will result in zero being passed to al_create_timer.

He did say to change your code to use 1.0 divided by settings[SETTINGS...] but I see you haven't.

DragonSpace23
Member #16,945
January 2019

I changed type of setting from int to float and now %d with %llu

I define setting in the constructor of INTERFACE

#SelectExpand
1INTERFACE::INTERFACE() { 2 int_queue = true; 3 called_init = false; 4 5 setting[SETTING_FPS] = 30; 6 setting[SETTING_TPS] = 100; 7}

Now both of the counts are increasing.

Go to: