Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » images flickering

This thread is locked; no one can reply to it. rss feed Print
images flickering
shadyvillian
Member #12,426
December 2010

When ever an event happens my images flicker except the background and the images that are primitives. I feel like I've had this problem before, but I'm not checking mouse events in a random place only if an axes event happens. I think it could be Buttons[i].IsMouseOver(Event.mouse.x, Event.mouse.y);. Would putting that in parameters produce weird things? I still don't think that would cause it because if it was a weird value over would just stay false and it would draw the up state of the button. Heres some snippets of the code

if(Event.type == ALLEGRO_EVENT_MOUSE_AXES)
{
   for(int i = 0; i < (int)Buttons.size(); i++)
   {
      Buttons[i].IsMouseOver(Event.mouse.x, Event.mouse.y);
   }

if(Event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
     if(Buttons[SEARCH].IsMouseOver() == true)
     {
         //search
      }

the code below is part of a class thats separate to the program that I use for other programs too.

#SelectExpand
1void IsMouseOver(int MouseX, int MouseY) 2 { 3 if((MouseX >= x1) && (MouseX <= x2) && (MouseY >= y1) && (MouseY <= y2)) 4 { 5 Over = true; 6 } 7 8 else 9 { 10 Over = false; 11 } 12 } 13 14 bool IsMouseOver() 15 { 16 return Over; 17 } 18 19 void Draw() 20 { 21 if(UnClickable == true) 22 { 23 al_draw_bitmap(GreyedImage, x1, y1, 0); 24 } 25 26 27 else if(Over == true) 28 { 29 al_draw_bitmap(OverImage, x1, y1, 0); 30 } 31 32 else 33 { 34 al_draw_bitmap(UpImage, x1, y1, 0); 35 } 36 }

Software Engineer by day, hacker by night.

Edgar Reynaldo
Member #8,592
May 2007
avatar

shadyvillian
Member #12,426
December 2010

Do you mean this?

#SelectExpand
1while(Program.Exit == false) 2 { 3 Program.DrawUI(); 4 5 al_wait_for_event(Program.EventQueue, &Program.Event); 6 7 Program.SearchBox.GetInput(Program.Event); 8 9 if(Program.Event.type == ALLEGRO_EVENT_TIMER) 10 { 11 Program.Click = false; 12 al_stop_timer(Program.ClickTimer); 13 } 14 15 Program.GetMouseMovement(); 16 Program.GetMouseClicks(); 17 Program.GetKeyboardInput(); 18 19 if(Program.Event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 20 { 21 int Choice = al_show_native_message_box(Program.Screen, "Warning", NULL, "Are you sure you want to quit?", NULL, ALLEGRO_MESSAGEBOX_YES_NO); 22 23 if(Choice == YES) 24 { 25 Program.Exit = true; 26 } 27 } 28 }

Software Engineer by day, hacker by night.

Edgar Reynaldo
Member #8,592
May 2007
avatar

It's a little odd that you draw once for every event that you receive, although that would just cause lag as the drawing caught up with the mouse events. My only guess is that they flicker because you are behind in your drawing.

Try processing all events in the queue at once, and then if you have received one or more timer events, then draw.

shadyvillian
Member #12,426
December 2010

I Changed it to this:

#SelectExpand
1Program.DrawUI(); 2 al_start_timer(Program.ReDrawTimer); 3 4 while(Program.Exit == false) 5 { 6 al_wait_for_event(Program.EventQueue, &Program.Event); 7 8 Program.SearchBox.GetInput(Program.Event); 9 10 if(Program.Event.type == ALLEGRO_EVENT_TIMER) 11 { 12 if(Program.Event.timer.source == Program.ClickTimer) 13 { 14 Program.Click = false; 15 al_stop_timer(Program.ClickTimer); 16 } 17 18 if(Program.Event.timer.source == Program.ReDrawTimer) 19 { 20 Program.ReDraw = true; 21 } 22 } 23 24 Program.GetMouseMovement(); 25 Program.GetMouseClicks(); 26 Program.GetKeyboardInput(); 27 28 if(Program.Event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 29 { 30 Program.ExitPrompt(); 31 } 32 33 if((Program.ReDraw == true) && (al_is_event_queue_empty(Program.EventQueue) == true)) 34 { 35 Program.DrawUI(); 36 Program.ReDraw = false; 37 } 38 }

It seems it has reduced the number of flickers but it hasn't completely gotten rid of it.

Software Engineer by day, hacker by night.

Edgar Reynaldo
Member #8,592
May 2007
avatar

I don't understand why it is flickering at all. The hover state of your buttons should not be changing back and forth rapidly, and that is the only thing that would cause your buttons to flicker.

What specifically is flickering, and between what two states?

Are you altering your 'Over' (hover) variable anywhere else except inside IsMouseOver(int,int)?

shadyvillian
Member #12,426
December 2010

Well its not just the buttons that are flickering. When I mean flicker all the images disappear then reappear type of thing. But the over states couldn't be changed because there private. Could it be becuase the images are transparent? I made them in a separate program then loaded them in but there 50% transparent.

Software Engineer by day, hacker by night.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Post the code for Program.DrawUI(). It has nothing to do with being transparent. You must be rapidly changing states somehow, because there shouldn't be any flickering at all with a double buffered or page flipping drawing setup like Allegro provides.

shadyvillian
Member #12,426
December 2010

#SelectExpand
1void DrawUI() 2 { 3 if(Help == false) 4 { 5 Backgrounds[BgCounter].Draw(); 6 7 al_draw_text(Font, al_map_rgb(255, 255, 255), 1015, 395, 0, "Minimum cards:"); 8 9 SearchBox.Draw(); 10 11 Pb_DeckSize.Draw(DRAW); 12 13 for(int i = 0; i < (int)UiBoxes.size(); i++) 14 { 15 UiBoxes[i].Draw(); //size is 4 16 } 17 18 if(IsCardSelected == true) 19 { 20 SelectedCard.Draw(); 21 } 22 23 for(int i = 0; i < (int)Buttons.size(); i++) 24 { 25 Buttons[i].Draw(); //size is 2 26 } 27 28 for(int i = 0; i < (int)CardListHeaders.size(); i++) 29 { 30 CardListHeaders[i].Draw(); //size is 13 31 } 32 33 for(int i = 0; i < (int)SetSelectorButtons.size(); i++) 34 { 35 SetSelectorButtons[i].Draw(); //size is 6 36 } 37 38 if(SetSelected != NO_SET) 39 { 40 switch(SetSelected) //this draws a text probably 60 ish times in total 41 { 42 case ZEN: 43 DrawCsText(ZendikarCards); //this function passes a vector of a class that contains 8 variables with a size of anywhere from 150 to 270 in size. 44//I'm passing it by const reference though so it shouldnt make it slow. 45 break; 46 47 case WWK: 48 DrawCsText(WorldwakeCards); 49 break; 50 51 case ROE: 52 DrawCsText(RiseOfEldraziCards); 53 break; 54 55 case M11: 56 DrawCsText(Magic2011Cards); 57 break; 58 59 case SOM: 60 DrawCsText(ScarsOfMirrodinCards); 61 break; 62 63 case MBS: 64 DrawCsText(MirrodinBesiegedCards); 65 break; 66 } 67 } 68 69 if(ShowSearchedCards == true) 70 { 71 DrawCsText(SearchList); 72 } 73 74 if(Deck.size() != 0) 75 { 76 DrawDeckListCards(); //any where from 8 to over a hundred or so different al_draw_text calls in this. 77//note i've had old versions of this program that draw the same way and it didnt flicker 78 } 79 80 Menu.Draw(); 81 82 if((Menu.IsMouseOverItem(SAVE) == true) && (Menu.GetSelectedState() == true)) 83 { 84 SaveInputBox.ChangeActiveState(true); 85 SaveInputBox.Draw(); 86 } 87 88 else 89 { 90 SaveInputBox.ChangeActiveState(false); 91 } 92 93 if((Menu.IsMouseOverItem(LOAD) == true) && (Menu.GetSelectedState() == true)) 94 { 95 LoadInputBox.ChangeActiveState(true); 96 LoadInputBox.Draw(); 97 } 98 99 else 100 { 101 LoadInputBox.ChangeActiveState(false); 102 } 103 104 if(Menu.GetSelectedState() == false) 105 { 106 107 } 108 } 109 110 if(Help == true) 111 { 112 HelpScreen.Draw(); 113 } 114 115 al_flip_display(); 116 }

I'm not drawing that many images. I've got a tile based game that draws over 1000 images and it has no problems.

Software Engineer by day, hacker by night.

Edgar Reynaldo
Member #8,592
May 2007
avatar

The number of images that you draw is not the problem. It's how you're drawing the images. Something must be changing state back and forth, because that's the only thing that explains why images are being drawn on one frame and not on another.

It almost sounds like you're drawing directly to the screen, but I don't think that is even possible with Allegro 5.

Edit
Do any of the draw functions that you call in there call al_flip_display at all? If you're flipping the display more than once that could explain it.

shadyvillian
Member #12,426
December 2010

Your right. I found an extra al_flip_display some where else in the code. After removing it all the flickering is gone. :P

Software Engineer by day, hacker by night.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Go to: