The line: buffer_width = rect.left - rect.right;
=> buffer_width = rect.right- rect.left ;
You should draw in the WM_PAINT
message,
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
unsigned int* pixel = (unsigned int*)buffer_memory;
for (int y = 0; y < buffer_hight; y++)
{
for (int x = 0; x < buffer_width; x++)
{
*pixel++ = 0xff5500;
}
}
// render
StretchDIBits(hdc, 0, 0, buffer_width, buffer_hight, 0, 0, buffer_width, buffer_hight,
buffer_memory, &buffer_bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
EndPaint(hwnd, &ps);
}
break;
Updated:
#include <Windows.h>
//#include "Header.h"
bool running = true;
void* buffer_memory;
int buffer_width;
int buffer_hight;
BITMAPINFO buffer_bitmap_info;
LRESULT CALLBACK windows_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
switch (uMsg)
{
case WM_CLOSE:
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
case WM_SIZE:
{
RECT rect;
GetClientRect(hwnd, &rect);
buffer_width = rect.right - rect.left;
buffer_hight = rect.bottom - rect.top;
int buffer_size = buffer_width * buffer_hight * sizeof(unsigned int);
if (buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
buffer_memory = VirtualAlloc(0, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
buffer_bitmap_info.bmiHeader.biSize = sizeof(buffer_bitmap_info.bmiHeader);
buffer_bitmap_info.bmiHeader.biWidth = buffer_width;
buffer_bitmap_info.bmiHeader.biHeight = buffer_hight;
buffer_bitmap_info.bmiHeader.biPlanes = 1;
buffer_bitmap_info.bmiHeader.biBitCount = 32;
buffer_bitmap_info.bmiHeader.biCompression = BI_RGB;
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
unsigned int* pixel = (unsigned int*)buffer_memory;
for (int y = 0; y < buffer_hight; y++)
{
for (int x = 0; x < buffer_width; x++)
{
*pixel++ = 0xff5500;
}
}
// render
StretchDIBits(hdc, 0, 0, buffer_width, buffer_hight, 0, 0, buffer_width, buffer_hight,
buffer_memory, &buffer_bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
EndPaint(hwnd, &ps);
}
break;
default:
{
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return result;
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//compile window
CHAR clsName[] = "test";
WNDCLASSA window_class = {};
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpszClassName = clsName;
window_class.lpfnWndProc = windows_callback;
//register clases
ATOM atom = RegisterClassA(&window_class);
if (0 == atom)
{
DWORD err = GetLastError();
return 1;
}
// create window
HWND window = CreateWindow(clsName, "game", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
CW_USEDEFAULT, 720, 360, 0, 0, hInstance, 0);
if (NULL == window)
{
DWORD err = GetLastError();
return 1;
}
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}