Handmade Network»Forums
Banehallow
4 posts
None
Day 004 screen black when resizing window
Hi everyone,

I'm current on Day 4 and I ran into an issue. Sorry if I am posting in the wrong forum -- please feel free to move this to the appropriate section.

When I move the window, the animation stops. This is expected, and Casey's does the same.

However, upon resize, everything goes black until the resize operation is done. I don't see this exhibited by Casey's executable (around 52:16 in the video for day 4). It looks like the painted area "expands" as he drags to make the window larger.

I've looked over the code a couple times and the video as well, but I can't seem to figure it out.

Do you guys see anything? Thanks!

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <windows.h>
#include <stdint.h>

#define internal static
#define local_persist static
#define global_variable static

typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;

typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;

global_variable bool Running;

global_variable BITMAPINFO BitmapInfo;
global_variable void* BitmapMemory;
global_variable int BitmapWidth;
global_variable int BitmapHeight;
global_variable int BytesPerPixel = 4;

internal void RenderWeirdGradient(int XOffset, int YOffset)
{
    int Width = BitmapWidth;
    int Height = BitmapHeight;

    int Pitch = Width * BytesPerPixel;
    uint8* Row = (uint8*)BitmapMemory;
    for (int Y = 0; Y < BitmapHeight; ++Y) 
    {
        uint8* Pixel = (uint8*)Row;
        for (int X = 0; X < BitmapWidth; ++X)
        {
            *Pixel = (uint8)(X + XOffset);
            ++Pixel;

            *Pixel = (uint8)(Y + YOffset);
            ++Pixel;

            *Pixel = 0;
            ++Pixel;

            *Pixel = 0;
            ++Pixel;
        }
        Row += Pitch;
    }
}

internal void Win32ResizeDIBSection(int Width, int Height)
{
    // TODO: bulletproof this
    // maybe don't free first, free after, then free first if that fails

    if (BitmapMemory) 
    {
        VirtualFree(BitmapMemory, 0, MEM_RELEASE);
    }

    BitmapWidth = Width;
    BitmapHeight = Height;

    BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
    BitmapInfo.bmiHeader.biWidth = BitmapWidth;
    BitmapInfo.bmiHeader.biHeight = -BitmapHeight;
    BitmapInfo.bmiHeader.biPlanes = 1;
    BitmapInfo.bmiHeader.biBitCount = 32;
    BitmapInfo.bmiHeader.biCompression = BI_RGB;

    int BitmapMemorySize = BitmapWidth * BitmapHeight * BytesPerPixel;
    BitmapMemory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);
    // TODO: clear to black
}

internal void Win32UpdateWindow(HDC DeviceContext, RECT* ClientRect, int X, int Y, int Width, int Height)
{
    int WindowWidth = ClientRect->right - ClientRect->left;
    int WindowHeight = ClientRect->bottom - ClientRect->top;
    StretchDIBits(DeviceContext, 
                  /*X, Y, Width, Height,
                  X, Y, Width, Height, */
                  0, 0, BitmapWidth, BitmapHeight, 
                  0, 0, WindowWidth, WindowHeight,
                  BitmapMemory,
                  &BitmapInfo,
                  DIB_RGB_COLORS,
                  SRCCOPY);
}

LRESULT CALLBACK Win32MainWindowCallBack(_In_ HWND   Window,
                                    _In_ UINT   Message,
                                    _In_ WPARAM WParam,
                                    _In_ LPARAM LParam) 
{
    LRESULT Result = 0;
    switch(Message) 
    {
        case WM_SIZE:
        {
            RECT ClientRect;
            GetClientRect(Window, &ClientRect);
            int Width = ClientRect.right - ClientRect.left;
            int Height = ClientRect.bottom - ClientRect.top;
            Win32ResizeDIBSection(Width, Height);
            OutputDebugStringA("WM_SIZE\n");
        } break;

        case WM_CLOSE:
        {
            // TODO: handle this with msg to user?
            Running = false;
            OutputDebugStringA("WM_CLOSE\n");
        } break;

        case WM_ACTIVATEAPP:
        {
            OutputDebugStringA("WM_ACTIVATEAPP\n");
        } break;

        case WM_DESTROY:
        {
            // TODO: handle this as error - recreate window?
            Running = false;
        } break;

        case WM_PAINT:
        {
            PAINTSTRUCT Paint;
            HDC DeviceContext = BeginPaint(Window, &Paint);
            int X = Paint.rcPaint.left;
            int Y = Paint.rcPaint.top;
            int Width = Paint.rcPaint.right - Paint.rcPaint.left;
            int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;

            RECT ClientRect;
            GetClientRect(Window, &ClientRect);

            Win32UpdateWindow(DeviceContext, &ClientRect, X, Y, Width, Height);
            EndPaint(Window, &Paint);
        } break;

        default:
        {
            Result = DefWindowProc(Window, Message, WParam, LParam);
        } break;
    }
    return Result;
}

int CALLBACK WinMain(
    _In_ HINSTANCE Instance,
    _In_ HINSTANCE PrevInstance,
    _In_ LPSTR     CommandLine,
    _In_ int       ShowCode)
{
    WNDCLASS windowClass{};
    windowClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    windowClass.lpfnWndProc = Win32MainWindowCallBack;
    windowClass.hInstance = Instance;
    windowClass.lpszClassName = "HandmadeHeroWindowClass";
    if (RegisterClass(&windowClass)) 
    {
        HWND Window = CreateWindowEx(0,
                                           windowClass.lpszClassName,
                                           "Handmade Hero",
                                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                                           CW_USEDEFAULT, 
                                           CW_USEDEFAULT, 
                                           CW_USEDEFAULT, 
                                           CW_USEDEFAULT, 
                                           0, 
                                           0, 
                                           Instance,
                                           0);
        if (Window) 
        {
            int XOffset = 0;
            int YOffset = 0;
            Running = true;
            while (Running)
            {
                MSG Message;
                while (PeekMessage(&Message, 0, 0, 0, PM_REMOVE))
                {
                    if (Message.message == WM_QUIT)
                    {
                        Running = false;
                    }

                    TranslateMessage(&Message);
                    DispatchMessageA(&Message);
                }

                RenderWeirdGradient(XOffset, YOffset);

                HDC DeviceContext = GetDC(Window);
                RECT ClientRect;
                GetClientRect(Window, &ClientRect);
                int WindowWidth = ClientRect.right - ClientRect.left;
                int WindowHeight = ClientRect.bottom - ClientRect.top;
                Win32UpdateWindow(DeviceContext, &ClientRect, 0, 0, WindowWidth, WindowHeight);
                ReleaseDC(Window, DeviceContext);

                ++XOffset;
            }
        }
        else 
        {
            // TODO: logging
        }
    }
    else 
    {
        // TODO: logging
    }
    return 0;
}

Connor
52 posts
It's been so long ;)
Day 004 screen black when resizing window
Edited by Connor on
I don't think that this is a problem with the code. The same thing had me confused but when I copied the source from day four I got the same results as you. Maybe it's a windows thing. But I doubt it's the code. I wouldn't worry about it in the end though :P
Mārtiņš Možeiko
2559 posts / 2 projects
Day 004 screen black when resizing window
On Windows moving or resizing window happens inside internal modal loop: https://msdn.microsoft.com/en-us/...ary/windows/desktop/ms632622.aspx

DefWindowProc doesn't return until resize/move operation is done. That means your game code in WinMain doesn't run at this time, so it cannot render anything.

In Casey's video area "expands", because window size is smaller than bitmap he renders into. So whatever was put in bitmap will remain unchanged. But if he'll resize window large enough, then you'll still see black borders around.
Banehallow
4 posts
None
Day 004 screen black when resizing window
Okay, that's good to hear. Yea, must be some strange bug. I'm on Windows 10, so I thought maybe that was the issue? So I tried the code on Windows 7 and got the same thing. I think I'll just keep proceeding on. Thanks!
Banehallow
4 posts
None
Day 004 screen black when resizing window
mmozeiko
On Windows moving or resizing window happens inside internal modal loop: https://msdn.microsoft.com/en-us/...ary/windows/desktop/ms632622.aspx

DefWindowProc doesn't return until resize/move operation is done. That means your game code in WinMain doesn't run at this time, so it cannot render anything.

In Casey's video area "expands", because window size is smaller than bitmap he renders into. So whatever was put in bitmap will remain unchanged. But if he'll resize window large enough, then you'll still see black borders around.


Hey mmozeiko, thanks for the link.

Right -- that's what was so weird. I expected that behavior too, but I didn't see it. What I saw was that the entire screen goes black. To me, it makes sense that it goes black because in Win32ResizeDIBSection, I'm freeing the previous chunk. It looks like the whole memory block is getting zeroed out as well. Strangely enough, it seems like Casey's copies the contents of the previous buffer. I don't remember him writing any special code for that though....


In any case, I shouldn't get hung up on this and should just push forward. Thanks all for your help! This community is great.
Mārtiņš Možeiko
2559 posts / 2 projects
Day 004 screen black when resizing window
Edited by Mārtiņš Možeiko on
Oh, I see what is the issue - CS_HREDRAW and CS_VREDRAW flags for WNDCLASS style member. These flags means that window will redrawn during resize or move if some part of window becomes visible because of moving. Without these flags OS simply doesn't update window on WM_PAINT message while resizing. If these flags are set (that's what you have) then OS will display actual content on resizing - which as you correctly say is black because of memory allocation in WM_SIZE.
Banehallow
4 posts
None
Day 004 screen black when resizing window
mmozeiko
Oh, I see what is the issue - CS_HREDRAW and CS_VREDRAW flags for WNDCLASS style member. These flags means that window will redrawn during resize or move if some part of window becomes visible because of moving. Without these flags OS simply doesn't update window on WM_PAINT message while resizing. If these flags are set (that's what you have) then OS will display actual content on resizing - which as you correctly say is black because of memory allocation in WM_SIZE.


Wow. I just rewatched the video again, and at 43:00 I noticed that Casey does not have anything for the style. After removing that line, my program now works just like Casey's! Thanks mmozeiko!

I feel like I am following everything line by line, but clearly I miss things here and there :D

Thanks again!
Simon
5 posts
Day 004 screen black when resizing window
Edited by Simon on
Hi guys, I know I'm late to the party (about a year and a half), but I have questions:

If I wanted to keep rerendering the scene while inside the modal loop, how would I do that in the best way? I went a bit into the direction of just doing all the calls inside the WM_PAINT message handler, but that would basically mean that I would duplicate my code. Would it be viable to have my whole game logic and rendering in a function, that I could call from inside the message handler?

I am currently doing a classical GUI application, and the expected behaviour for them is to repaint while resizing.

I hope there are people that will see this reply, if not, I'm just going to post a seperate thread.
Mārtiņš Možeiko
2559 posts / 2 projects
Day 004 screen black when resizing window
Most common way is to create window timer when modal loop is entered, and remove it when it exits. And inside WM_TIMER handler call your game rendering function.

There are more options, but they are more complex.
Simon
5 posts
Day 004 screen black when resizing window
I did use a timer now, after trying some different things like WM_USER messages or PostQuitmessage. I create a timer when I first receive WM_ENTERSIZEMOVE and in every WM_TIMER that I get until a WM_EXITSIZEMOVE comes in. I was really confused about PeekMessage not returning while one resizes the window, which feels very counterintuitive (I always thought that all messages went through PeekMessage -> TranslateMessage -> Dispatch Message). Since timers have a minimum duration of 10ms I am still interested how to solve that "properly", in a way that wouldn't limit the framerate while dragging. That said: The timer solution is working like a charm currently, so this would be a purely academic endeavour - who needs >100fps while dragging anyway? So thank you very much for posting the timer solution here!