al_wait_for_event Issue (Code execution stuck)
MarcMDE

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

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

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

Chris Katko

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

Eric Johnson

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

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

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.

NiteHackr

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.

Thread #616794. Printed from Allegro.cc