Confusion about WindowEvent and FlipBuffers
Posted: Mon Oct 07, 2024 9:46 am
Hello, all. I'm a new user of PB on Windows (only 1 or 2 weeks), but an experienced programmer. The program below is a toy version of something I've been working on. The FOR loop simulates the fact that my real program will have a need, every so often, to draw graphics that will take some time, say several seconds or even tens of seconds. The first version of this code did not contain either LINE A or LINE B, and it works fine. But while the drawing is being drawn the cursor turns into a spinning circle, suggesting that the For loop doesn't "yield" to Windows. Not surprising. To try and fix that I put in LINE A and that solved that problem, but the result is that the graphic doesn't appear - the window is just black. After several hours of trying things I put in LINE B on a whim, and voila, the graphics are there again. Trying all 4 combinations of LINE A and LINE B, the result is that LINE A and LINE B have to either both be used, or both be not used, in order to have the graphics show up at the end. I'm confused. Is this the expected behavior? It seems odd that WindowEvent and FlipBuffers are interrelated in this way. Since I really want LINE A (by processing the event I can allow the user to quit while it's drawing, for one thing) it seems I have to have LINE B also. Is there a different/better way to "yield" when drawing something that takes several seconds?
Code: Select all
#xres = 800: #yres = 600
InitSprite()
OpenWindow(0, 0, 0, #xres, #yres, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #xres, #yres)
LoadFont(0, "Verdana", 14)
StartDrawing(ScreenOutput())
DrawingFont(FontID(0))
t = ElapsedMilliseconds()
For i=1 To 2000000
event = WindowEvent() ; ====== LINE A ======
x = Random(#xres-1)
y = Random(#yres-1)
color = Random($ffffff)
Plot(x, y, color)
Next
DrawText(5, 5, Str(ElapsedMilliseconds() - t), RGB(255, 255, 255))
StopDrawing()
FlipBuffers() ; ====== LINE B ======
Repeat
event = WindowEvent()
Until event = #PB_Event_CloseWindow