![]() |
|
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: But, very unlucky The situation is: In fact, the speed of the unit is very slow. ======================================== |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
Show your event handling code and your movement code. It only does what you tell it to. :/ 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 |
keprast
Member #16,794
January 2018
|
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
![]() |
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 : 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}
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 |
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 Reynaldo
Major Reynaldo
May 2007
![]() |
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));
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 |
|