Timer's tick diferent in diferent computers?
Jarutais

Hello guys, i am new to programming and to allegro, so my question may sound idiot, but i'd be really happy if anyone could help me.

I made a game that runs perfectly on my computer, then i attempted to run it on another computer and everything that depends on timers ran a lot faster.

Then i tried on another computer, and it ran a lot slower.

So... Is there any way to make the timer's ticks the same on any computer?

Thanks!

Matthew Leverton

Timers run at the same speed on every computer. You must be doing something wrong, so show us some code.

Jarutais

The code is far too extense to paste here, but for example, when i want to simulate that the character is walking,

Basically i'm doing this (omiting some declarations)

#SelectExpand
1ALLEGRO_EVENT_QUEUE *event_queue; 2ALLEGRO_EVENT event; 3ALLEGRO_TIMER *timer, 4ALLEGRO_BITMAP *sprite, sprites[5]; 5 6int counter = 0; 7 8event_queue = al_create_event_queue(); 9timer = al_create_timer(0.02); 10 11al_register_event_source(event_queue, al_get_timer_event_source(timer)); 12 13 14while(true) 15{ 16 nextSprite = false; 17 18 doSomeCalculations(); 19 if(al_key_down(&key_state, ALLEGRO_KEY_RIGHT) 20 { 21 nextSprite = true; 22 al_start_timer(timer); 23 } 24 25 doSomeMoreThings(); 26 27 if(al_peek_next_event(event_queue, &event) && nextSprite == true) 28 { 29 counter ++; 30 sprite = sprites[counter]; 31 32 if(counter == 4) counter = 0; 33 } 34 35 if(nextSprite == false) 36 counter = 0; 37 38 al_draw_bitmap(sprite, x, y, 0); 39 al_flip_display(); 40}

In my PC this makes the character move the way i want (i adjusted the ticking time till i`ve got it to 0.02), but if i try to run it on another PC the character sprites change a lot faster... I`m using the same mechanics to make the bitmap move trough the screen from time to time, and i`m having the same issues..

Am i forgeting to do something on the timer routines?

Edgar Reynaldo

Use <code>code goes here...</code> tags when posting code (you can edit your post as well) please.

1) You only want to call al_start_timer once. Calling it more than once does nothing.

2) You only increment your sprite index if the right arrow key is down and there is another event waiting in the queue. The timing of the sprite's frame change should not depend on whether there is an event in the queue.

3) If you increment your sprite index every time you check whether the right key is down, it will go way too fast to see and will just look flickery. You need to use a time based increment for your sprite index. See the animation class below for how to do this.

4) You should only redraw when you get a timer tick or when the state changes. With hardware acceleration, you might as well fully redraw on every timer tick.

5) Your timer should beat at the same rate as your monitor's refresh rate, since that is as often as you can display data to the user.

Here's a basic animation class that you can use with Allegro 5 :

#SelectExpand
1 2class Animation { 3private : 4 ALLEGRO_BITMAP** frames; 5 double duration; 6 double time_passed; 7 int frame_number; 8 int num_frames; 9public : 10 Animation() : 11 frames(0), 12 duration(0.0), 13 time_passed(0.0), 14 frame_number(0), 15 num_frames(0) 16 {} 17 Animation(int framecount , double time_length) : 18 frames(0), 19 duration(0.0), 20 time_passed(0.0), 21 frame_number(0), 22 num_frames(0) 23 { 24 Reallocate(framecount , time_length); 25 } 26 ~Animation() {Free();} 27 void Free() { 28 if (frames) {delete [] frames;frames = 0;} 29 num_frames = 0; 30 } 31 bool Reallocate(int framecount , double time_length) { 32 if (framecount < 1) {return false;} 33 if (time_length <= 0.0) {return false;} 34 Free(); 35 duration = time_length; 36 time_passed = 0.0; 37 frame_number = 0; 38 try { 39 frames = new ALLEGRO_BITMAP*[framecount]; 40 } 41 catch(...) { 42 frames = 0; 43 return false; 44 } 45 num_frames = framecount; 46 return true; 47 } 48 49 void SetBitmap(int index , ALLEGRO_BITMAP* bmp) { 50 assert((index >= 0) && (index < num_frames)); 51 frames[index] = bmp; 52 } 53 54 void AdvanceFrameTime(double delta_time) { 55 SetFrameTime(time_passed + delta_time); 56 } 57 void SetFrameTime(double frame_time) { 58 // simple forward repeating animation 59 time_passed = frame_time; 60 while (time_passed >= duration) {time_passed -= duration;} 61 while (time_passed < 0.0) {time_passed += duration;} 62 frame_number = (int)((float)num_frames*(time_passed / duration)); 63 // shouldn't be necessary, but just in case 64 if (frame_number < 0) {frame_number = 0;} 65 if (frame_number >= num_frames) {frame_number = num_frames - 1;} 66 } 67 68 void Draw(int x , int y) { 69 al_draw_bitmap(frames[frame_number] , x , y , 0); 70 } 71 72 double FrameTime() {return time_passed;} 73 int FrameNum() {return frame_number;} 74 75};

Basic usage :

#SelectExpand
1 /// Setup code... 2 3 Animation anime(3 , 1.0); 4 ALLEGRO_BITMAP* f1 = al_load_bitmap("frame1.bmp"); 5 ALLEGRO_BITMAP* f2 = al_load_bitmap("frame2.bmp"); 6 ALLEGRO_BITMAP* f3 = al_load_bitmap("frame3.bmp"); 7 8 anime.SetBitmap(0 , f1); 9 anime.SetBitmap(1 , f2); 10 anime.SetBitmap(2 , f3); 11 12 bool redraw = true; 13 while (!quit) { 14 if (redraw) { 15 al_clear_to_color(al_map_rgb(0,0,0)); 16 anime.Draw(400,300); 17 redraw = false; 18 } 19 do { 20 ALLEGRO_EVENT ev; 21 al_wait_for_event(event_queue , &ev); 22 if (ev.type == ALLEGRO_EVENT_TIMER) { 23 redraw = true; 24 anime.AdvanceFrameTime(1.0/FRAMES_PER_SECOND); 25 } 26 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 27 quit = true; 28 break; 29 } 30 } while (!al_is_event_queue_empty()); 31 } 32 33 /// Cleanup code...

van_houtte

Timers run at the same speed on every computer. You must be doing something wrong, so show us some code.

False, this depends on your position relative to the center of gravity of the earth

Goalie Ca

False, this depends on your position relative to the center of gravity of the earth

It also depends if the computer simulation is being executed on a server in high orbit and then displayed on your desktop.

Tobias Dammers
Goalie Ca said:

It also depends if the computer simulation is being executed on a server in high orbit and then displayed on your desktop.

It doesn't have to be a high orbit, as long as the relative velocity between the client and the server is sufficiently large. If the server moves away from the client at the speed of light, the timer will stop ticking altogether.

verthex

If the server moves away from the client at the speed of light, the timer will stop ticking altogether.

As long as the server is made of photons.

Thread #607845. Printed from Allegro.cc