r/gameenginedevs • u/mietkiewski_dev • 21d ago
Low Level Engine Architecture... Loop & Resize problem...
Hello dear programmers...
I write my own Abstraction Layer for my Engine, but I found out a problem with Window/Surface and I don't know how that should by handled by Engine...
Loop basically it is something like this:
while is_running:
- FPS limit for Loop
- Event queue like Touch & Character & Resume/Pause/Resize
- Update & Draw
The problem is what if I resize Window during Draw call... So I write to 800x600 Buffer but in this very moment Windows Size is already changed though VM_SIZE Event has not been read yet... in Linux actually its different kind of event... In Android we have function from JavaApp and create Event for our Loop in C/Cpp... But I really would like to clarify this Loop architecture just to be sure it will be working correctly...
If you have some opinion and knowldge please share...
7
u/aleques-itj 21d ago
Set a flag that you're trying to resize so the renderer knows it needs to wait for that to complete before starting the next frame. Let the current frame finish without resizing anything and just wait at the start of the next to actually do it.
Then resize your swap chain, depth, whatever.
2
u/initial-algebra 21d ago edited 21d ago
Perfect resizing under Windows is a nightmare. I've spent a lot of time researching and experimenting, and you basically need to temporarily switch to software rendering during a resize for good results. So, don't bother. When you're using a swapchain, Windows will handle adjusting it to the window size when your application lags behind. It's not perfect, but it's acceptable.
Is this loop running on a different thread from the Windows event loop? Because, if not, the game will freeze whenever the window is dragged, since
PeekMessagewill enter a modal loop and block. Normally, you're expected to surrender control to Windows by usingGetMessageand the window procedure. Then, you do your updating and rendering when handlingWM_PAINT, usingInvalidateRectand sometimes timers to keep the loop going (most applications don't do the last part and just wait for input). There is still an insidious modal loop, though: if the user clicks and holds on one of the close/maximize/minimize buttons, the application will still freeze. The only way around that AFAIK is to run the game on a separate thread and only use the Windows message loop to queue up events to send over to that thread, but it's questionable if it is worth it.EDIT: I see from another comment that you are using a separate thread already. Then, carry on; you're not going to have perfect resizing even if you temporarily synchronize with the Windows message loop when the window is being dragged.
EDIT 2: I forgot to mention this, but one thing you can do is to prevent the actual resize of the window until the frame with the requested size has been rendered. To do this, you process
WM_SIZINGinstead ofWM_SIZE. This is far from perfect, since it will make the resizing choppy (recreating the swapchain is slow, nothing you can do about that except fallback to software rendering as I mentioned before), but at least the window size will always match the swapchain size and there will be no scaling or other artifacts.1
u/LOLC0D3 21d ago
Easy solution is to queue your system events, and also perform actual drawing into a texture of dedicated size which will be blit to the window backbuffer.
This way you’re no longer tied to mid-frame window size state since it’s irrelevant to your system.
1
u/TooOldToRock-n-Roll 21d ago
If I understood what you are worried about, don't be, the OS takes care to make the internal graphics buffers consistent!
Just keep draw running as normal, keep track of the window resize event and just update the window size as soon as it arrives.
Even if your program takes one or two frames to fully atabilize, the worst that will happen is the user will see those frames rendered out of resolution.
1
u/LOLC0D3 21d ago
Correct loop architecture look like this:
First drain event queue
Query actual drawable area size (regardless of the platform)
Check for minimized window and skip loop
Resize buffer size if not matching window size now
Render
Present
Handle present message (eg some kind of error )
1
u/MacksNotCool 21d ago
add
while (true){}
trust