Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem using timer

This thread is locked; no one can reply to it. rss feed Print
Problem using timer
Francis Langlais
Member #15,985
June 2015

I'm a second year computer engineering student and I'm fairly new to Allegro 5.

Here my main loop

#SelectExpand
1 2 /*Creating all allegro event soure and the event queue*/ 3 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 4 ALLEGRO_EVENT event; 5 ALLEGRO_TIMER *FramePerSecondTimer; 6 ALLEGRO_TIMER *AddCalcTimer; 7 ALLEGRO_DISPLAY *display = NULL; 8 ALLEGRO_FONT *font = al_create_builtin_font(); 9 10 /*Creating and initialising variable*/ 11 int calcsUntilOrders = 0; 12 int quit = false; 13 int redraw = false; 14 int start = false; 15 char systemMessage[255] = ""; 16 ROBOT *robot; 17 18 /*Initialising allegro event source*/ 19 AddCalcTimer = al_create_timer(SPF); 20 FramePerSecondTimer = al_create_timer(1); 21 event_queue = al_create_event_queue(); 22 al_set_new_display_flags(ALLEGRO_FULLSCREEN); 23 display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT); 24 25 if (display == NULL) 26 AbortOnError("InitGraphics() failed to set graphics mode.\n" 27 "Program will exit."); 28 29 /* Registering the envent source*/ 30 al_register_event_source(event_queue, al_get_keyboard_event_source()); 31 al_register_event_source(event_queue, 32 al_get_timer_event_source(FramePerSecondTimer)); 33 al_register_event_source(event_queue, 34 al_get_timer_event_source(AddCalcTimer)); 35 al_register_event_source(event_queue, al_get_display_event_source(display)); 36 37 /*Initiate display*/ 38 al_set_target_backbuffer(display); 39 al_clear_to_color(al_map_rgb(0, 0, 0)); 40 al_draw_text(font, al_map_rgb(255, 0, 255), SCREEN_WIDTH / 2, 41 SCREEN_HEIGHT / 2 - 5, ALLEGRO_ALIGN_CENTER, "Press SPACE to start"); 42 al_draw_text(font, al_map_rgb(255, 0, 255), SCREEN_WIDTH / 2, 43 SCREEN_HEIGHT / 2 + 5, ALLEGRO_ALIGN_CENTER, "Press ESCAPE to exit"); 44 al_flip_display(); 45 46 /*Starting the game loop*/ 47 while (!quit) { 48 al_start_timer(AddCalcTimer); 49 al_start_timer(FramePerSecondTimer); 50 51 /*Drawing the scene each time a robot take a turn (60 time per second)*/ 52 if (redraw && start) { 53 al_set_target_backbuffer(display); 54 al_clear_to_color(al_map_rgb(0, 0, 0)); 55 sprintf(systemMessage, "FPS: %d CPS: %d", fps, cps); 56 RenderScene(robotList, deadRobotList, weaponList, systemMessage); 57 frameCounter++; 58 al_flip_display(); 59 redraw = false; 60 } 61 62 do { 63 al_wait_for_event(event_queue, &event); 64 65 /*Checking for timers input*/ 66 if (event.type == ALLEGRO_EVENT_TIMER) { 67 if (start) { 68 if (event.timer.source == AddCalcTimer) { 69 calcsUntilOrders++; 70 71 UpdateEnergySystems(robotList); //Do first so we know what 72 //systems are powered. 73 MoveRobots(robotList); 74 DrawRobotBitmaps(robotList); 75 CheckRobotCollisions(&theGame, robotList); 76 77 MoveWeapons(weaponList); 78 CheckWeaponCollisions(&theGame, robotList, weaponList); 79 80 //Now that collisions are 81 ApplyDamage(&theGame, robotList, deadRobotList); //done and weapons have 82 //hit, we apply damage. 83 84 DrawSensorBitmaps(robotList); //Need to draw BEFORE data is 85 //updated because bimaps are 86 //used in collision detection. 87 UpdateSensorData(robotList); 88 89 UpdateParticles(); 90 91 calcsCompleted++; 92 calcCounter++; 93 94 if (calcsUntilOrders == ORDER_FREQ) { 95 calcsUntilOrders = 0; 96 ForeachLL_M(robotList, curRobot) 97 curRobot->ActionsFunction(TURN_TIME); 98 } 99 100 if (theGame.useSounds) 101 PlaySounds(&theGame); 102 103 redraw = true; 104 } 105 if (event.timer.source == FramePerSecondTimer) { 106 fps = frameCounter; 107 frameCounter = 0; 108 cps = calcsCompleted; 109 calcsCompleted = 0; 110 } 111 } else if (!start) { 112 if (event.timer.source == AddCalcTimer){ 113 printf("AddCalcTimer timer as tick\n"); 114 } 115 if (event.timer.source == FramePerSecondTimer){ 116 printf("FramePerSecondTimer timer as tick\n"); 117 } 118 } 119 } 120 121 /*Checking for keyboard input*/ 122 if (event.type == ALLEGRO_EVENT_KEY_DOWN) { 123 printf("A key as been hit\n"); 124 if (event.keyboard.keycode == ALLEGRO_KEY_R && start) { 125 ForeachLL_M(robotList, robot) 126 { 127 ChooseRandomLocation(robot); 128 robot->heading = GetRandomNumber(360); 129 } 130 } 131 if (event.keyboard.keycode == ALLEGRO_KEY_SPACE && !start) { 132 printf("SPACE key as been hit\n"); 133 start = true; 134 theGame.state = GS_FIGHTING; 135 } 136 if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 137 printf("ESCAPE key as been hit\n"); 138 quit = true; 139 } 140 } 141 142 } while (!al_is_event_queue_empty(event_queue)); 143 }

My problem is when I hit the SPACE key, I don't see "SPACE key as been hit" neither "A key as been hit" in the console and my game seems to freeze and when I click beside the window the "your program as crashed" window appear. When I hit ESCAPE, both text show in the console and the program exit. I used the built-in debugger of Eclipse to try to find what happen and basicly everything works fine but when I hit SPACE, the game freeze but the debugger keeps running in an endless loop like if no event source can be registered anymore.

I'm really out of option on this.

Also, is there a better way to debug my program than using built-in debugger from Eclipse or using printf()?

Elias
Member #358
May 2000

You're restarting your timer more than once (inside the loop).

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

Francis Langlais
Member #15,985
June 2015

Thank you for pointing it out but it didn't fix my problem.

After more work I found out that the problem is actually al_draw_filled_circle and this is the way I use it :

al_draw_filled_circle(al_get_bitmap_width(sensorPic) / 2, al_get_bitmap_height(sensorPic) / 2, range, robot->color);

I have put fixed number and I have tried creating an ALLEGRO_COLOR using al_map_rgb but there is nothing to do.

One thing I'm not sure about is that I draw bitmap on bitmap and then on the display. Is there anything wrong with that?

Thomas Fjellstrom
Member #476
June 2000
avatar

One thing I'm not sure about is that I draw bitmap on bitmap and then on the display. Is there anything wrong with that?

Nothing wrong with double buffering, but it's often unnecessary with allegro 5.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Francis Langlais
Member #15,985
June 2015

Should I use sub bitmap instead? Because at the moment my game is running at 3 FPS. I don't know if this kind of thing would improve it.

I'm not familiar with parent/child. Back when I did 3D, the child was affected by all basic transformation (moving, scaling, rotation...) done to the parent. Is this the same in Allegro or is there more to it?

Audric
Member #907
January 2001

I don't see the call to al_init(), and be careful also with addons which need be initialized before you run their functions, such as al_init_font_addon() before font = al_create_builtin_font();

Francis Langlais
Member #15,985
June 2015

Yeah... Sorry about that. This is only the game loop. Everything is done before.

Thomas Fjellstrom
Member #476
June 2000
avatar

Should I use sub bitmap instead? Because at the moment my game is running at 3 FPS. I don't know if this kind of thing would improve it.

Just try drawing everything to the display. It's 3d hardware accelerated. Trying to use clever 2d optimization tricks tends to make things slower when using 3d hardware acceleration.

Quote:

Back when I did 3D, the child was affected by all basic transformation (moving, scaling, rotation...) done to the parent. Is this the same in Allegro or is there more to it?

As Allegro 5 uses 3d hardware acceleration, and has a transform api, you can just use transforms as you would in 3d.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: