startdrawing(screenoutput()) not drawing on the backbuffer

Just starting out? Need help? Post your questions and find answers here.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

startdrawing(screenoutput()) not drawing on the backbuffer

Post by bfernhout »

I have some code here and when i tryied to work with it completely by the programming rules. The screen stay blank. This is the whole code

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0 Or InitSprite() = 0
  MessageRequester("Error", "Unable to init system utilities (Possible Direct X7 or higher not installed!)", 0)
  End
EndIf

OpenWindow(0, 0, 0, 640, 480, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0), 0, 0, 640, 480) = 0
  MessageRequester("Error", "Can't open windowed screen!", 0)
  End
EndIf


StartDrawing(ScreenOutput())
Box(5,5,100,100,RGB(255,255,255))
StopDrawing()
FlipBuffers()
ClearScreen(0)

Repeat
  
  ;Exit the program by keyboard or X on window 
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  Event = WindowEvent()
  If event = #PB_Event_CloseWindow
    End
  EndIf
  
ForEver
If I remove 2 lines the program is working. But for me this means that the drawing is done on the front buffer()

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0 Or InitSprite() = 0
  MessageRequester("Error", "Unable to init system utilities (Possible Direct X7 or higher not installed!)", 0)
  End
EndIf

OpenWindow(0, 0, 0, 640, 480, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0), 0, 0, 640, 480) = 0
  MessageRequester("Error", "Can't open windowed screen!", 0)
  End
EndIf


StartDrawing(ScreenOutput())
Box(5,5,100,100,RGB(255,255,255))
StopDrawing()
;Commneting these out so the compiler is not compile them
;FlipBuffers()
;ClearScreen(0)
;My box is showing Strange....
Repeat
  
  ;Exit the program by keyboard or X on window 
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  Event = WindowEvent()
  If event = #PB_Event_CloseWindow
    End
  EndIf
  
ForEver
Is there an error or is there something wrong.

Do i code the lines startdrawing .. Trough clearscreen(0) inside the repeat...Forever loop the code is working to.
But then i think its start drawing on the backbuffer. But why does this happend with only the first drawing command.
From my first self made computer till now I stil like computers.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by #NULL »

FlipBuffers() kinda depends on the event processing IIRC and your screen/window content might get discarded by a window repaint. If you don't want to always redraw you can use a simple flag. Also it's much better if you process all available events in a second/nested loop before doing anything else in the main loop.

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0 Or InitSprite() = 0
  MessageRequester("Error", "Unable to init system utilities (Possible Direct X7 or higher not installed!)", 0)
  End
EndIf

OpenWindow(0, 0, 0, 640, 480, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0), 0, 0, 640, 480) = 0
  MessageRequester("Error", "Can't open windowed screen!", 0)
  End
EndIf

Procedure update()
  ClearScreen(0)
  StartDrawing(ScreenOutput())
  Box(5,5,100,100,RGB(255,255,255))
  StopDrawing()
  FlipBuffers()
EndProcedure

doUpdate = #True

;PostEvent(#PB_Event_Repaint)

Repeat
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  While WindowEvent()
    If Event() = #PB_Event_CloseWindow
      End
    ElseIf Event() = #PB_Event_Repaint
      doUpdate = #True
    EndIf
  Wend
  If doUpdate
    update()
    doUpdate = #False
  EndIf
ForEver
<edit>
the code was misleading. when reseting doUpdate = #False it doesn't work. sorry.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by bfernhout »

Thanks for the relply.

Your code is working. And i think i know how its work now. That was a little bit confusing. When the procedure is using the flipbuffer is working ok. When direct command the flipbuffer is not working correct. As you said its al in windows that discarding the refresh of the screen. But the repaint event is standing. I checked it and it wil alway be a event when the program is starting up. That is a beautifull way of refreshing the screen first before doing anyting on it.

Thanks for the info this give me a little bit more understanding of PB. There is to much to learn of it.
From my first self made computer till now I stil like computers.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 536
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by BasicallyPure »

I noticed that you used InitSprite() two times in your first line of code.
It may not make a difference but I thought I would mention it just in case.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by bfernhout »

Sorry for the double initSprite()

its a typo. In theoriginal program its gone.

bart
From my first self made computer till now I stil like computers.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by Paul »

Your screen looks blank because you clear it after flipping it into view...

Code: Select all

StartDrawing(ScreenOutput())
Box(5,5,100,100,RGB(255,255,255))
StopDrawing()
FlipBuffers()
ClearScreen(0) ;<-----
You should clear your screen, do your drawing operations and then flip into view...

Code: Select all

ClearScreen(0)
StartDrawing(ScreenOutput())
Box(5,5,100,100,RGB(255,255,255))
StopDrawing()
FlipBuffers()
Image Image
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by bfernhout »

Its beautifull replyed. But your missing the code:

The drawing supposed te be on the backbuffer of the screen.
When drawing is finished the flip command is switching the front and back screen.
The backscreen (Not visable) is now the drawing screen.
I need to clear the screen before i can draw again.

What you say is that i am drawing on the front screen and then switch screens. That not good. I take more time to
draw on a screen that is in view than drawing on the background screen.

What my code problem was: That it was drawing the first frame on the frondscreen.
The problem is that windows is not working so wel in this case.

When i do this in full screen mode the filpping andclearen is working fine.

But thaks anyway for you view over it.

bart.
From my first self made computer till now I stil like computers.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by #NULL »

ClearScreen() and drawing operate on the backbuffer. after the flip the new backbuffer is cleared while the previous/drawn buffer is now front.

Code: Select all

clear > draw > flip > clear > draw > flip
is the same as

Code: Select all

        draw > flip > clear > draw > flip > clear
(except for the initial clear)
At least it doesn't seem to make a difference on linux and is also what I remember from windows. I guess it has rather to do with the event processing.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: startdrawing(screenoutput()) not drawing on the backbuff

Post by bfernhout »

Yep you are right.

But for the best result the first option is prefered. But in the most time I do it on the second way which can give some times
a odd responce of the screen. Like a dark blink of the screen if the screen has a different color then black.

Thank for the info.

bart.
From my first self made computer till now I stil like computers.
Post Reply