![]() |
|
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. 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
![]() |
Actually, I had this problem before. I did this to tell timers apart: 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: |
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 |
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 ? |
Chris Katko
Member #1,881
January 2002
![]() |
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: |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
Edit 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? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Chris Katko
Member #1,881
January 2002
![]() |
Is Allegro initialized before timers are created and the timer is installed (in a constructor)? Is Allegro init'd twice by accident? -----sig: |
Neil Roy
Member #2,229
April 2002
![]() |
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. --- |
BanditBloodwyn
Member #16,791
January 2018
|
Guys, you are my heroes! Thanks a lot, my friends |
Neil Roy
Member #2,229
April 2002
![]() |
Awesome! Glad you got it working. --- |
BanditBloodwyn
Member #16,791
January 2018
|
One last question off topic: |
Chris Katko
Member #1,881
January 2002
![]() |
Good! -----sig: |
Neil Roy
Member #2,229
April 2002
![]() |
Quote: How was my English?
That was English?!?! You were fine actually. --- |
|