I've been following HMH with good succes for a few episodes, but I have hit a stumbling block
I'm at this exact spot :
https://youtu.be/hNKU8Jiza2g?t=2120
I should be drawing a window filled with blue
but it remains white, until it is resized, then the resized area is black.
I've gone over my code and can't see here it deviates from what is in the video.
The debugger indicates that all expected values are populating
I think there *might* be an issue with my device context, since the debugger says things like
1 | unused ={???} |
1 | unused: <unable to read memory> |
but from stack overflow posts i also think this might be normal windows behavior -i just don't know.
here is the code:
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | #include <Windows.h> #include <stdint.h> //for accss to unit8_t type // these #defines reuse 'static' with more clarfied intent #define internal static #define local_persist static #define global_variable static // these typedefs redefine types from stdint.h //for easier typing than 'unsigned char' etc 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; internal void Win32ResizeDIBSection(int Width, int Height) { if (BitmapMemory) { VirtualFree(BitmapMemory, 0, MEM_RELEASE); } BitmapWidth = Width; BitmapHeight = Height; BITMAPINFO BitmapInfo; BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader); BitmapInfo.bmiHeader.biWidth = BitmapWidth; BitmapInfo.bmiHeader.biHeight = -BitmapHeight; // negative top yield a 'top down' BitmapInfo.bmiHeader.biPlanes = 1; BitmapInfo.bmiHeader.biBitCount = 32; BitmapInfo.bmiHeader.biCompression = BI_RGB; int BytesPerPixel = 4; int BitmapMemorySize = (BitmapWidth * BitmapHeight) * BytesPerPixel; BitmapMemory = VirtualAlloc( 0, //address 0 = we don't care yet BitmapMemorySize, //size in bytes MEM_COMMIT, // vs MEM_RESERVE PAGE_READWRITE // access mode ); // actually draw pixels! int Pitch = Width * BytesPerPixel; // Pitch is the difference between rows of pixels in Bytes uint8 *Row = (uint8 *)BitmapMemory; // cast the void pointer BitmapMemory to unsigned 8 bit int for (int Y = 0; Y < BitmapHeight; ++Y) { //uint32 *Pixel = (uint32 *)Row; //pointer to first pixel of Row uint8 *Pixel = (uint8 *)Row; //pointer to first byte of first pixel of Row for (int X = 0; X < BitmapWidth; ++X) { /* 32 Pixel in memory : RR GG BB AA (?) (as 1 byte Hex numbers) */ //https://www.youtube.com/watch?v=hNKU8Jiza2g&feature=youtu.be&t=1896 // WHY NO WORK??? *Pixel = 255; ++Pixel; *Pixel = 0; ++Pixel; *Pixel = 0; ++Pixel; *Pixel = 0; ++Pixel; } Row += Pitch; } } internal void Win32UpdateWindow(HDC DeviceContext, RECT *WindowRect, int x, int y, int Width, int Height) { int WindowWidth = WindowRect->right - WindowRect->left; int WindowHeight = WindowRect->bottom - WindowRect->top; StretchDIBits(DeviceContext, 0, 0, BitmapWidth, BitmapHeight, 0, 0, WindowWidth, WindowHeight, BitmapMemory, &BitmapInfo, DIB_RGB_COLORS, SRCCOPY); } LRESULT CALLBACK Win32MainWindowCallback( HWND Window, // handle to a window UINT Message, // window message we want to handle WPARAM WParam, // width LPARAM LParam) //Height { 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_DESTROY: { //handle as error, recreate window Running = false; OutputDebugStringA("WM_DESTROY\n"); } break; case WM_CLOSE: { //todo: handle with message to user Running = false; OutputDebugStringA("WM_CLOSE\n"); } break; case WM_ACTIVATEAPP: { OutputDebugStringA("WM_ACTIVATEAPP\n"); } 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); PatBlt(DeviceContext, X, Y, Width, Height,BLACKNESS); EndPaint(Window,&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: { //OutputDebugStringA("DEFAULT\n"); Result = DefWindowProc(Window, Message, WParam, LParam); } break; } return (Result); } int CALLBACK WinMain( HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR Command, int ShowCode) { WNDCLASS WindowClass = {}; // declares a WNDCLASS instance 'windowClass', with members initialized to 0. WindowClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; // bitfield flags to define windowstyle see MSDN WindowClass.lpfnWndProc = Win32MainWindowCallback; // pointer to a function that defines window's response to events // WindowClass.cbClsExtra = ; // if we want to store extra bytes // WindowClass.cbWndExtra = ; // if we want to store extra bytes WindowClass.hInstance = Instance; // reference to the instance of this window, from WinMain function.(Could also use GetModuleHandle) // WindowClass.hIcon = ; // icon for window // WindowClass.hCursor = ; // cursor position // WindowClass.hbrBackground = ; // background brush // WindowClass.lpszMenuName = ; // menu name WindowClass.lpszClassName = "handmadeHeroWindowClass"; if (RegisterClass(&WindowClass)) { HWND WindowHandle = CreateWindowEx( 0, WindowClass.lpszClassName, "Handmade Hero", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, Instance, 0); if (WindowHandle) { MSG Message; Running = true; while (Running) { BOOL MessageResult = GetMessageA(&Message, 0, 0, 0); if (MessageResult > 0) { TranslateMessage(&Message); DispatchMessageA(&Message); } else { break; } } } else { //todo: logging } } else { //todo: logging } return (0); } |
can anyone help see where my mistake is?
thanks in advance.