Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Screen flipping varying speeds

This thread is locked; no one can reply to it. rss feed Print
Screen flipping varying speeds
AceBlkwell
Member #13,038
July 2011
avatar

All,

I'm playing around with getting a bitmap to scroll automatically. I've got it working. In short I draw a 640x480 bitmap at 0,0 and same bitmap at 0,-480. I increase y until it gets to 480 and reset to -480. (give or take depending on actual picture size) It gives the appearance of a rolling picture. I'm using event timer to flip the screen. At first, it works great. However, when I move the mouse over the screen area it speeds up. It slows back down when the mouse is still. Why would this be if the timer is dictating the flipping?

Be kind with the critiquing code below. This was actually different trial I was working on and I altered to test my scrolling idea. It's piece work and the variables probably don't make a lot of sense. I wasn't going to include it but it's the first thing everyone asks about.

#SelectExpand
1int main(int argc, char *argv[]) 2{ 3bool bQuit = false; 4int pos_x = 100; 5int pos_y = 100; 6int y1 = 0; 7int y2 = -478; 8a_init(); 9al_start_timer(timer); 10 11while(!bQuit){ 12 13 14 al_wait_for_event(queue, &event); 15 al_clear_to_color(COLOR::BLACK); 16 17 al_draw_bitmap(city, 0,y1, 0); 18 al_draw_bitmap(city, 0,y2, 0); 19 y1++; 20 y2++; 21 if(y1>478) 22 y1 =-478; 23 if(y2>478) 24 y2 = -478; 25 26 if(event.type == ALLEGRO_EVENT_MOUSE_AXES) 27 { 28 pos_y = event.mouse.y; 29 pos_x = event.mouse.x; 30 } // end mouse axis 31 32 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 33 { 34 bQuit = true; 35 } 36 else if(event.type == ALLEGRO_EVENT_TIMER) 37 { 38 al_flip_display(); 39 } 40 41}// end game while loop 42 43al_destroy_display(disp); 44al_destroy_event_queue(queue); 45 46} //end main

DanielH
Member #934
January 2001
avatar

Do not use else if with your events. If the first triggers, it will ignore all others.

also, where is the setup?

The part where you're changing y should be in the timer event section otherwise ANY EVENT will set it to change. If you want it consistent then you need timers.

Dizzy Egg
Member #10,824
March 2009
avatar

Also, you're drawing every loop, you may only be flipping in the timer, but because you're drawing every loop, any event will cause the draw.

Only draw/flip when the timer ticks (and take into account what Daniel said!)

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

AceBlkwell
Member #13,038
July 2011
avatar

Thanks all,

Makes sense, I'll try it out.

The setup is in a separate file A5_init.cpp

I'll keep you posted. I'm sure I'll be back anyways ;D.

** UPDATE ** It worked like a charm. I was even able to use

al_convert_mask_to_alpha(stars,COLOR::WHITE);

to use a stationary back ground as well. Thanks again.

DanielH
Member #934
January 2001
avatar

The timer should control the logic, not the drawing. Drawing is a result of the logic.

AceBlkwell
Member #13,038
July 2011
avatar

In this program there really isn't any logic. Even the mouse event isn't needed. It was just part of the previous trial program I was working on. The program just updates screen and looks for the X to be clicked. Really the only condition checked for is, has the timer increased by another click.

I could move the actual drawing to it's own function then call the function from within the timer section, but for the sake of this "see if this will work" type program, I didn't see the need to get so structured.

I've got a feeling I'm misunderstanding what you are saying,.

Dizzy Egg
Member #10,824
March 2009
avatar

The logic in this case would just be your xpos/ypos. That should be updated when the timer ticks, the idea being, no matter how fast the computer, things will always move at the same speed, because logic will only update when the timer ticks.

I’d love it if Daniel would post an example of his standard event/drawing loop, because his games always seem really smooth (but I may be hoping for too much!)

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

DanielH
Member #934
January 2001
avatar

I don't "wait" for events, but run through them. As always, I have a standard "framework" of what my applications are based on. Also, added a TicTacToe game to boot.

My Discord

In main loop:
1. process input
2. do logic based on input (set dirty if need to redraw)
3. draw (if dirty)

#SelectExpand
1int32_t App::loop() 2{ 3 while (!this->m_kill) 4 { 5 this->input(); 6 7 while (this->m_counter > 0) 8 { 9 this->logic(); 10 --this->m_counter; 11 } 12 13 if (this->m_dirty) 14 { 15 this->draw(); 16 this->m_dirty = false; 17 } 18 19 al_rest(0.01); 20 } 21 22 return 0; 23} 24 25void App::draw() 26{ 27 al_clear_to_color(al_map_rgb(64, 0, 64)); 28 29 30 31 32 33 al_flip_display(); 34} 35 36void App::logic() 37{ 38} 39 40void App::input() 41{ 42 static ALLEGRO_EVENT event; 43 44 while (!al_event_queue_is_empty(this->m_queue)) 45 { 46 al_get_next_event(this->m_queue, &event); 47 48 switch (event.type) 49 { 50 case ALLEGRO_EVENT_TIMER: 51 { 52 ++this->m_counter; 53 } break; 54 55 case ALLEGRO_EVENT_DISPLAY_CLOSE: 56 { 57 this->m_kill = true; 58 } break; 59 } 60 } 61}

Go to: