2 posts
problem with StretchDIBits
Edited by cstpr on Reason: Initial post
Sorry if this post seems confusing. My English is not really good.

I have just started the handmade hero series and been up to day 5. This was when Casey changed the code so that StretchDIBits could stretch the bitmap according to the size and ratio of the window. But my code is failing to do that. StretchDIBits still passes to the window a bitmap of the same size every single time like it is done in day 4. And it ends up like this every single time

Can anyone check and see if I am missing anything. Thanks a lot!

  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 #include #include #define internal static #define local_persist static #define global_variable static typedef int8_t int8; typedef int16_t int16; typedef int32_t int32; typedef int64_t int64; typedef uint8_t uint8; typedef uint16_t uint16; typedef uint32_t uint32; typedef uint64_t uint64; struct win32_offscreen_buffer { BITMAPINFO Info; void *Memory; int Width; int Height; int Pitch; }; struct win32_window_dimension { int Width; int Height; }; global_variable bool GlobalRunning; global_variable win32_offscreen_buffer GlobalBackBuffer; win32_window_dimension Win32GetWindowDimension(HWND Window) { win32_window_dimension Result; RECT ClientRect; GetClientRect(Window, &ClientRect); Result.Width = ClientRect.right - ClientRect.left; Result.Height = ClientRect.bottom - ClientRect.top; return (Result); } internal void RenderWeirdGradient(win32_offscreen_buffer Buffer, int XOffset, int YOffset) { uint8 *Row = (uint8 *)Buffer.Memory; for (int Y = 0; Y < Buffer.Height; ++Y) { uint32 *Pixel = (uint32 *)Row; for (int X = 0; X < Buffer.Width; ++X) { uint8 Blue = (X + XOffset); uint8 Green = (Y + YOffset); *Pixel++ = ((Green << 8) | Blue); } Row += Buffer.Pitch; } } internal void Win32ResizeDIBSection(win32_offscreen_buffer *Buffer, int Width, int Height) { if (Buffer->Memory) { VirtualFree(Buffer->Memory, 0, MEM_RELEASE); } Buffer->Width = Width; Buffer->Height = Height; int BytesPerPixel = 4; Buffer->Info.bmiHeader.biSize = sizeof(Buffer->Info.bmiHeader); Buffer->Info.bmiHeader.biWidth = Buffer->Width; Buffer->Info.bmiHeader.biHeight = -Buffer->Height; Buffer->Info.bmiHeader.biPlanes = 1; Buffer->Info.bmiHeader.biBitCount = 32; Buffer->Info.bmiHeader.biCompression = BI_RGB; int BitmapMemorySize = (Buffer->Width*Buffer->Height)*BytesPerPixel; Buffer->Memory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE); Buffer->Pitch = Width*BytesPerPixel; } internal void Win32DisplayBufferInWindow(HDC DeviceContext, int WindowWidth, int WindowHeight, win32_offscreen_buffer Buffer) { StretchDIBits(DeviceContext, /* X, Y, Width, Height, X, Y, Width, Height, */ 0, 0, WindowWidth, WindowHeight, 0, 0, Buffer.Width, Buffer.Height, Buffer.Memory, &Buffer.Info, DIB_RGB_COLORS, SRCCOPY); } LRESULT CALLBACK Win32MainWindowCallback(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) { LRESULT Result = 0; switch(Message) { case WM_DESTROY: { GlobalRunning = false; } break; case WM_CLOSE: { GlobalRunning = false; } break; case WM_ACTIVATEAPP: { OutputDebugStringA("WM_ACTIVATEAPP\n"); } break; case WM_PAINT: { PAINTSTRUCT Paint; HDC DeviceContext = BeginPaint(Window, &Paint); win32_window_dimension Dimension = Win32GetWindowDimension(Window); Win32DisplayBufferInWindow(DeviceContext, Dimension.Width, Dimension.Height, GlobalBackBuffer); EndPaint(Window, &Paint); } break; default: { Result = DefWindowProc(Window, Message, WParam, LParam); } break; } return(Result); } int CALLBACK WinMain(HINSTANCE Instance, // a handle to our executable HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode) { WNDCLASS WindowClass = {}; Win32ResizeDIBSection(&GlobalBackBuffer, 1280, 720); WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW; WindowClass.lpfnWndProc = Win32MainWindowCallback; WindowClass.hInstance = Instance; //WindowClass.hIcon = ; WindowClass.lpszClassName = "HandmadeHeroWindowClass"; if(RegisterClassA(&WindowClass)) { HWND Window = CreateWindowExA(0, WindowClass.lpszClassName, "Handmade Hero", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, Instance, 0); if(Window) { HDC DeviceContext = GetDC(Window); int XOffset = 0; int YOffset = 0; GlobalRunning = true; while(GlobalRunning) { MSG Message; while (PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) { if (Message.message == WM_QUIT) { GlobalRunning = false; } TranslateMessage(&Message); DispatchMessageA(&Message); } RenderWeirdGradient(GlobalBackBuffer, XOffset, YOffset); win32_window_dimension Dimension = Win32GetWindowDimension(Window); Win32DisplayBufferInWindow(DeviceContext, Dimension.Width, Dimension.Height, GlobalBackBuffer); ++XOffset; YOffset += 2; } } else { // TODO(casey) Logging } } else { // TODO(casey): Logging } return (0); } 
Mārtiņš Možeiko
2201 posts / 1 project
problem with StretchDIBits
The screenshot your are showing is exactly how output should look like. What exactly do you think is wrong?
2 posts
problem with StretchDIBits
Edited by cstpr on
What I mean is the ratio of the animation stays exactly the same even though the window is resized. The only thing changed is the rendering speed. So it turned out like this

But, the correct way should be like this

Anyway, I tried to paste Casey's code over my source code and it works the same way in VS, the animation's ratio just does not change at all. But when I put that same code from my preorder download into a new separate
VS project It worked correctly. What happens?
Mārtiņš Možeiko
2201 posts / 1 project
problem with StretchDIBits
Edited by Mārtiņš Možeiko on
Ah I see what you mean now.
Interest, but I don't see that on my machine. I copy&pasted your code and run it - I see those blue/rectangles being resized for me, stretching & narrowing depending on window size... So not sure what's wrong with it.

Can you resize window and then breakpoint inside Win32DisplayBufferInWindow. And examine argument values - WindowWidth, WindowHeight, Buffer.Width, Buffer.Height. Are they reasonable?