Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Multiple timers in Allegro 5

This thread is locked; no one can reply to it. rss feed Print
Multiple timers in Allegro 5
BanditBloodwyn
Member #16,791
January 2018

Hey guys,

First I want to say, that I already posted my question on Stackoverflow. So I will link to it, so that I don't have to write novels here. ;) But to describe my problem, here some introducing words.

I watched multiple tutorials about using multiple timers in Allegro, but that way of programming doesn't work for me. The problem is that the source adress never matches to the timer adress i want to watch.

I use multiple classes/instances to encapsulate my code, because it will be very complex in the future. The main loop of my game is located in the Game class/instance. The timers and the events are encapsulated in an Engine class/instance, that is a member of the Game instance.

You can see the code by following the link to my Stackoverflow-question.

Thanks in advance for your help :)

https://stackoverflow.com/questions/47993289/using-multiple-timers-in-allegro-5

P.S.: If this linking is not welcome here, i can recreate my thread.

Chris Katko
Member #1,881
January 2002
avatar

Actually, I had this problem before. I did this to tell timers apart:

#SelectExpand
1 2ALLEGRO_TIMER *fps_timer; 3ALLEGRO_TIMER *other_timer; 4 5//... 6 while(!time_to_exit ) 7 { 8 ALLEGRO_EVENT ev; 9 10 if(!al_is_event_queue_empty(queue)) 11 { 12 al_get_next_event(queue, &ev); 13 14// important part: 15if(ev.type == ALLEGRO_EVENT_TIMER) 16 { 17 if(ev.timer.source == fps_timer){} 18 if(ev.timer.source == other_timer){} 19 } 20 21//end 22 } 23 }

[edit] I didn't realize your SO post was so long. There may be something else going on...

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

BanditBloodwyn
Member #16,791
January 2018

Thanks Chris for your quick answer, but yeah, as you see I tried it exactly like you did, but the adresses don't match and I don't know why.

Sorry that my SO question is so long, but I wanted to describe my situation as detailed as possible/necessary

Audric
Member #907
January 2001

I don't see an evident mistake, but you need to be sure that you have only one instance of Engine. Are there any compilation warnings ? These may point to the real issue.

You should probably pull LoopTimerEvent off your class : Such piece of data is normally only used inside the function that performs al_wait_for_event(), and you don't need it afterwards, so a local variable is fine.

BanditBloodwyn
Member #16,791
January 2018

@Audric:

No, I don't get any compiler warnings and I'm sure I have only one instance of Engine (I will change it to be a singleton to be 110% sure, but it already is unique).

I will try to solve it by pull out my events to the gameloop itself, whereby I would prefer not to have to do that, because I like to encapsulate elements that belong to each other. But maybe it's the only way without creating a completely own timer-event-concept.

Btw: sorry if my English is not the best. I'm not a native speaker ;D

Audric
Member #907
January 2001

I don't see how a rewrite / reorganization would fix it.

Wait, maybe the timer event you're getting is from something else, and you only have to ignore these events when you find them in the queue : maybe a keyboard handler, or music system ?
To quickly test, "don't start" your 3 timers, and see if you still find such events.

Chris Katko
Member #1,881
January 2002
avatar

I thought that too, but somehow late last night it didn't occur to me to just remove the three timers completely and check. Good idea.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Edit
Didn't read close enough.

Quote:
TimerEvent: 031A0D80 0326AF30 0326A380 0326B090

The second three addresses look right - they are all close together, like you would expect from a sequential memory assignment. The first ALLEGRO_TIMER* looks like it was allocated from an earlier part of the program.

Can you post a github link or a zip file?

Chris Katko
Member #1,881
January 2002
avatar

Is Allegro initialized before timers are created and the timer is installed (in a constructor)? Is Allegro init'd twice by accident?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Neil Roy
Member #2,229
April 2002
avatar

I have multiple timers in my Deluxe Pacman 2 game (below) and after I initialize allegro, I call:

pacman.timer = al_create_timer(1.0f / pacman.ts);

pacman.ts being the speed I want for that timer. I have a global struct for my main character, Pacman that has ALLEGRO_TIMER *timer;

I do the same for my ghosts and I even have a third redraw timer. All works well, but then, I program in C, not C++, so no hassles with classes and junk. ;)

In my events loop I check with if(event.timer.source == pacman.timer) { as usual. Works just fine.

Follow your code, put a watch on your timer variables and check when they change. Maybe do a search for all instances of your timer variables being assigned anything and maybe do a simple console printout of the the line number and what the variable equals when done etc.

---
“I love you too.” - last words of Wanda Roy

BanditBloodwyn
Member #16,791
January 2018

Guys, you are my heroes! :-*:P I found the mistake:
I accidentally initialized the timers instance twice, which created new timers with (of course) different adresses. But in the event queue the OLD ones were registered, because of my stupid organization. I fixed that by putting the queue registration to the InitTimer function and fixed the bug with the double timer initialization.
Now everything works perfectly!

Thanks a lot, my friends :):):)

Neil Roy
Member #2,229
April 2002
avatar

Awesome! Glad you got it working.

---
“I love you too.” - last words of Wanda Roy

BanditBloodwyn
Member #16,791
January 2018

One last question off topic:
How was my English? ;D

Chris Katko
Member #1,881
January 2002
avatar

Good! :)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Neil Roy
Member #2,229
April 2002
avatar

Quote:

How was my English? ;D

That was English?!?! :o

You were fine actually. ;)

---
“I love you too.” - last words of Wanda Roy

Go to: