Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » There is a bug that prevents the game from working properly.

This thread is locked; no one can reply to it. rss feed Print
There is a bug that prevents the game from working properly.
keprast
Member #16,794
January 2018

Hello.

My game met a problem.

Very simple function:
Use 4 keyboard buttons to move the unit.

But, very unlucky :( ,It doesn't work properly.

The situation is:
Press the ( any button, this is an example ) up button and the unit will go up.
1. the unit will not move.
2. do not move, wait a little time, move like a rocket(It's like having superpowers).

In fact, the speed of the unit is very slow.
??? What happened?

========================================
Add:
It looks like a game caton. Just like some games, network delay, instant movement.
I guess it's my quadtree that consumes too much time?(Map data)
========================================
Add:
The code is below.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

keprast
Member #16,794
January 2018

#SelectExpand
1while(open_game) 2{ 3 al_wait_for_event(event,&ev); 4 5 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 6 //something 7 if (ev.type == ALLEGRO_EVENT_KEY_UP) 8 //something 9 10 get_operation(); 11 12 if (start_geme) { 13 quad_tree(); 14 15 //a function that takes a lot of time 16 //something 17 18 } 19 //It takes a lot of time to get here 20 //something 21 22 if (ev.type == ALLEGRO_EVENT_TIMER) { 23 if (ev.timer.soure == animation_timer) 24 //something 25 if (ev.timer.soure == fps_timer) { 26 al_flip_display(); 27 } 28}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You need to process all events as soon as they occur. This means you can't be blocking in 'quad_tree()' while you're processing events.

Try restructuring your code so that QuadTree() is called less often. Process every event as soon as you get it. Try something like this :

#SelectExpand
1 2int main(int argc , char** argv) { 3 4 Init(); 5 6 bool quit = false; 7 bool redraw = true; 8 9 int ticks = 1; 10 11 /// Begin processing loop 12 while (!quit) { 13 if (redraw) { 14 Draw(); 15 FlipDisplay(); 16 redraw = false; 17 } 18 ALLEGRO_EVENT ev; 19 while (al_get_next_event(event_queue , &ev)) { 20 if (ev.type == KEYBOARD) { 21 /// Handle keypresses 22 } 23 if (ev.type == TIMER) { 24 if (ev.timer.source == LOGICTIMER) {ticks++;} 25 if (ev.timer.source == FPSTIMER) {redraw = true;} 26 } 27 } 28 while (ticks > 0) { 29 Logic(); 30 --ticks; 31 } 32 QuadTree();/// This is the problem, not sure where to put this, 33 /// but you can't block with QuadTree on every event 34 /// Also, try not to call it every time Logic() is run 35 } 36 37 Cleanup(); 38 39 return 0; 40}

Peter Hull
Member #1,136
March 2001

I wish Allegro were a bit more helpful with the main loop (for example, providing a higher level function for the common case). It's difficult to get right and, last time I looked, the Allegro example games all do it a bit differently.
Edgar - in your example I think it will spin around the loop even if there are no events. Not a big deal but it might impact power use adversely (and have the fans running etc.) Although, in this case, if quad_tree is doing a lot of work the CPU will be 100% anyway.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Peter - yeah, I figured he would want to use 100% CPU in this case, because his QuadTree function would be taking a lot of time. I thought he wouldn't want to waste time waiting for an event if he needed to be processing quad_tree.

It can easily be altered to wait for an event.

do {
   ALLEGRO_EVENT ev;
   al_wait_for_event(q , &ev);
   
} while (!al_is_event_queue_empty(q));

Go to: