The 2024 Wheel Reinvention Jam is in 16 days. September 23-29, 2024. More info

Where did DeviceContext get released?

I really wished the title string length is much much longer than it is currently.

Main Question: Where did DeviceContext get released in the Win32 platform layer?

I ask this question because I'm not sure where ReleaseDC() gets called when I was following along with Casey's Win32 platform layer coding. Upon viewing my own code after following Casey's, I noticed my ReleaseDC(DeviceContext) and GetDC(DeviceContext) calls are not aligned by indents, which means they are in different levels of scope.

GetDC(DeviceContext) is outside of the while(GlobalRunning) loop, and ReleaseDC(DeviceContext) is inside the while(GlobalRunning) loop.


Because of that, I'm having a hard time asking myself if GetDC(DeviceContext) and ReleaseDC(DeviceContext) should be within the while(GlobalRunning) loop, or should they be outside of the while(GlobalRunning) loop?

Edited by Thompson Lee on
For latest day win32_handmade.cpp code looks like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int CALLBACK
WinMain(HINSTANCE Instance,
        HINSTANCE PrevInstance,
        LPSTR CommandLine,
        int ShowCode)
{
...
                while(GlobalRunning)
                {
...
                    if(!GlobalPause)
                    {
...
                        HDC DeviceContext = GetDC(Window);
                        Win32DisplayBufferInWindow(&GlobalBackbuffer, DeviceContext,
                                                   Dimension.Width, Dimension.Height);
                        ReleaseDC(Window, DeviceContext);
...
                     }
...
                }
...
}

You probably could move GetDC outside of while(GlobalRunning) - basically do it only once, and never release it.

Edited by Mārtiņš Možeiko on
Ah! That is simple to see why.

And I realized I had two nested

1
2
3
if(WindowHandle){
      ...
}


Gotta clean my own codes up I guess.

Edited by Thompson Lee on