Text flickering in game loop

Just starting out? Need help? Post your questions and find answers here.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Text flickering in game loop

Post by coco2 »

I'm very new to programming a game loop (or any main loop) and I'm writing text directly to the Windowed Screen and it flickers a small amount. I don't get any flickering when using full screen. Can someone explain if it is OK to draw text directly to the windowed screen?

Code: Select all

  If InitSprite() = 0
    MessageRequester("Error", "Can't open screen & sprite environment!", 0)
    End
  EndIf
  
  If OpenWindow(0, 0, 0, 220, 160, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(0, 170, 135, 45, 20, "Quit")

    If OpenWindowedScreen(WindowID(0), 0, 0, 160, 160)
      CreateSprite(0, 20, 20)
      If StartDrawing(SpriteOutput(0))
        Box(0, 0, 20, 20, RGB(255, 0, 155))
        Box(5, 5, 10, 10, RGB(155, 0, 255))
        StopDrawing()
      EndIf
    Else
      MessageRequester("Error", "Can't open windowed screen!", 0)
      End
    EndIf
  EndIf
  
  direction = 2
  Repeat
    ; It's very important to process all the events remaining in the queue at each frame
    ;
    Repeat
      Event = WindowEvent()
      
      Select Event 
        Case #PB_Event_Gadget
          If EventGadget() = 0
            End
          EndIf
        
        Case #PB_Event_CloseWindow
          End 
      EndSelect
    Until Event = 0
  
    FlipBuffers() 
    ClearScreen(RGB(0, 0, 0))
    
    ; *******************************
    ; The following text will flicker
    ;********************************
    StartDrawing(WindowOutput(0))
    DrawText(0, 0, "Test text")    
    StopDrawing()
    
    DisplaySprite(0, x, x)
    x + direction
    If x > 140 : direction = -2 : EndIf
    If x < 0   : direction =  2 : EndIf
    Delay(1)
  ForEver
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Text flickering in game loop

Post by netmaestro »

It flickers because you are drawing to WindowOutput(). Draw instead to ScreenOutput() and your flickering will stop.
BERESHEIT
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Text flickering in game loop

Post by coco2 »

Thank you that fixed it :)
User avatar
_aNdy_
User
User
Posts: 40
Joined: Fri Jun 17, 2016 12:06 am
Contact:

Re: Text flickering in game loop

Post by _aNdy_ »

Apologies for resurrecting this older thread, but only a quick curiosity! I've coded applications before, but haven't really looked at the screen/sound/sprite handling side of PureBasic yet. I decided to remedy this by coding a very simple intro/demo in the old Amiga cracktro style.

As in the post above, due to rushing on my part, I'd 'accidentally' coded a WindowOutput instead of ScreenOutput in one of my procedures when drawing to a OpenWindowedScreen.

Even with a draw to WindowOutput by accident, when testing on my machine (64bit Win10) it runs fine with no flicker, but when a friend tested on an older machine (32bit Win7) it flickered bad apparently. There are obvious other differences between machines (mine has more RAM, better gfx card, etc.)

I take it that more powerful machines can sometimes 'overcome' these flickering problems by sheer brute force? I assume that if even more was going on during the screen writes, even my machine would struggle eventually?

Just wondered if any old PureBasic hacks can confirm that and/or explain a bit more?

Cheers!

aNdy/AL/Cosine
Post Reply