Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problematic timer code

This thread is locked; no one can reply to it. rss feed Print
Problematic timer code
agonvs
Member #15,917
March 2015

Getting the following error message (though it compiles fine):

Unhandled exception at 0x77764c16 in shell.exe: 0xC0000005: Access violation reading
location 0x00000000.

#SelectExpand
1#include<allegro5/allegro.h> 2#include<allegro5/allegro_primitives.h> 3#include<iostream> 4using namespace std; 5ALLEGRO_EVENT_QUEUE* event_queue; 6 7void render() 8{ 9 cout << endl; 10} 11 12enum directions {NONE, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST}; 13enum buttons {ZERO,ONE,EIGHT,NINE}; 14 15int main() 16{ 17 ALLEGRO_TIMER *timer; 18 timer = al_create_timer(1.0/60); 19 float gameTime = 0; 20 int frames = 0; 21 int gameFPS = 0; 22 int button; 23 if(!(al_init())) 24 cout << "Couldn't initialize Allegro." << cout; 25 if(!(al_install_joystick())) 26 cout << "Couldn't install joystick." << endl; 27 event_queue = al_create_event_queue(); 28 if(!event_queue) 29 cout << "al_create_event_queue failed." << endl; 30 31 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 32 al_start_timer(timer); 33 gameTime = al_current_time(); 34 al_register_event_source(event_queue, al_get_joystick_event_source()); 35 ALLEGRO_JOYSTICK* joy = al_get_joystick(0); 36 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 37 38 ALLEGRO_JOYSTICK_STATE jst; 39 ALLEGRO_EVENT event; 40 directions dir; 41 bool render = false; 42 while(true) 43 { 44 ALLEGRO_EVENT ev; 45 al_wait_for_event(event_queue, &ev); 46 al_get_joystick_state(joy, &jst); 47 if (ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS) 48 { 49 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 0)) 50 dir = NONE; 51 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == -1)) 52 dir = NORTH; 53 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == -1)) 54 dir = NORTHEAST; 55 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 0)) 56 dir = EAST; 57 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 1)) 58 dir = SOUTHEAST; 59 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 1)) 60 dir = SOUTH; 61 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 1)) 62 dir = SOUTHWEST; 63 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 0)) 64 dir = WEST; 65 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == -1)) 66 dir = NORTHWEST; 67 68 } 69 if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) 70 { 71 if (jst.button[0]) 72 button = ZERO; 73 if (jst.button[1]) 74 button = ONE; 75 if (jst.button[8]) 76 button = EIGHT; 77 if (jst.button[9]) 78 button = NINE; 79 } 80 else if (ev.type == ALLEGRO_EVENT_TIMER) 81 { 82 render = true; 83 frames++; 84 if (al_current_time() - gameTime >= 1) 85 { 86 gameTime = al_current_time(); 87 gameFPS = frames; 88 frames = 0; 89 } 90 } 91 if (render && al_is_event_queue_empty(event_queue)) 92 { 93 render = false; 94 cout << "FPS: " << gameFPS << endl; 95 switch (dir) 96 { 97 case NORTH: 98 cout << "NORTH" << endl; 99 case NORTHEAST: 100 cout << "NORTHEAST" << endl; 101 case EAST: 102 cout << "EAST" << endl; 103 case SOUTHEAST: 104 cout << "SOUTHEAST" << endl; 105 case SOUTH: 106 cout << "SOUTH" << endl; 107 case SOUTHWEST: 108 cout << "SOUTHWEST" << endl; 109 case WEST: 110 cout << "WEST" << endl; 111 case NORTHWEST: 112 cout << "NORTHWEST" << endl; 113 } 114 switch (button) 115 { 116 case ZERO: 117 cout << "Button 1 pressed." << endl; 118 case ONE: 119 cout << "Button 2 pressed." << endl; 120 case EIGHT: 121 cout << "Button 9 pressed." << endl; 122 case NINE: 123 cout << "Button 10 pressed." << endl; 124 } 125 } 126 } 127 al_destroy_timer(timer); 128 al_destroy_event_queue(event_queue); 129 return 0; 130}

The compiler is pointing to the line timer = al_create_timer(1.0/60).
Any thoughts?
Could it be possible that I'm getting this error because I'm running Allegro 5.0.10 and VC++2010 on Windows 8.1 64-bit edition?

#00JMP00
Member #14,740
November 2012

My guess would be, if timer can be a fpu value.
With old version timer is int and therefore the asignement timer = 1.0/60 would be faulty.

taronĀ 
Member #10,584
January 2009
avatar

You need to call al_init prior to calling al_create_timer

Go to: