Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » al_wait_for_event Issue (Code execution stuck)

This thread is locked; no one can reply to it. rss feed Print
al_wait_for_event Issue (Code execution stuck)
MarcMDE
Member #16,639
February 2017

Hello!
I am developing my first small game using Allegro5, and I am having an issue with the al_wait_for_event instruction. The code gets stuck on the instruction every... 4 seconds for like 0.5-1 secs. And after having stopped 4 or 5 times, it gets permanently stuck until it crashes.

Would really aprecciate some help with this, I am so annoyed.

Thanks!!!

Eric Johnson
Member #14,841
January 2013
avatar

Nothing immediately jumps out at me, so I'm not too sure what the problem is. One thing I would suggest is changing if (draw) to if (draw && al_is_event_queue_empty(event_queue)) though.

Edit
Actually, I see you had al_is_event_queue_empty(), but it's commented out. ???

I can't compile this without all the files. Could you provide GameUtils.h and Game.h, along with its corresponding source files?

Elias
Member #358
May 2000

I didn't look at anything else but you clearly don't want this as 1 / 60 will be identical to 0 in C++: al_create_timer(1 / 60);

--
"Either help out or stop whining" - Evert

Chris Katko
Member #1,881
January 2002
avatar

And to clarify that a little bit.

1 / 60 = 0 because 1 and 60 are both integers.

You'd need 1.0 / 60.0 or 1F / 60F to tell the compiler to treat those constants as floating point numbers.

Sidenote: You might be able to get away with only one of them having .0, but I never tested that and I'm not sure the automatic type promotion rules of C++. If you start the left-side with 1.0 and then divide by 60 (no decimal point), I would imagine it'd be promoted to float. But you might as well leave all of the constants with decimals to clarify you intend the number to be a float, to any human reader. Likewise, I wonder what happens if you start with an integer, and then divide by a floating point constant ala (1 / 60.0).

-----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

Eric Johnson
Member #14,841
January 2013
avatar

Curious... Let me try that...

Okay, here's what I got:

#SelectExpand
1 2#include <iostream> 3 4using std::cout; 5 6int main(void) { 7 8 cout << 1 / 60 << "\n"; 9 cout << 1.0 / 60 << "\n"; 10 cout << 1 / 60.0 << "\n"; 11 cout << 1.0 / 60.0 << "\n"; 12 13 return 0; 14}

Result:
0
0.0166667
0.0166667
0.0166667

So it seems that so long as one of them is a float, the whole thing is treated as a float. I used g++ to compile, by the way.

g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

MarcMDE
Member #16,639
February 2017

Wohaaa, what a mistake... The problem was the al_create_timer. Thanks you all for the answer.

One thing I would suggest is changing if (draw) to if (draw && al_is_event_queue_empty(event_queue)) though.

Not getting why do we need to check if the queue is empty at all. I mean, I understand it, but I'm not understanding it as much as I would. I commented it when I was trying to solve the problem mentioned.

Thanks you all again!

Elias
Member #358
May 2000

If you don't have the al_is_event_queue_empty() but are receiving more than 60 events per second (easy with mouse/joystick events), you would delay your input more and more. You always draw 60 times per second then handle a single event afterwards. The check just makes sure to always handle all events before drawing the next time.

--
"Either help out or stop whining" - Evert

Neil Roy
Member #2,229
April 2002
avatar

Result:
0
0.0166667
0.0166667
0.0166667

So it seems that so long as one of them is a float, the whole thing is treated as a float. I used g++ to compile, by the way.

A C book I was reading said the following;

“Any operation between two values in C is performed as a floating-point operation if either value is a floating-point variable or constant.”

When you have two integers, it is treated as an integer operation and the decimal value is simply dropped with no rounding.

You could have also done something like: 60 / 1f or 60f / 1 etc.

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

Go to: