#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <stdint.h>
#include <stdbool.h>
typedef uint32_t u32;
unsigned int client_width = 600;
unsigned int client_height = 600;
void* memory;
BITMAPINFO bitmap_info;
void ClearScreen(u32 color);
void DrawRectangle(int rec_x, int rec_y, int rec_width, int rec_height, u32 color);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS window_class = { 0 };
wchar_t class_name[] = L"WindowClassName";
window_class.lpfnWndProc = WndProc;
window_class.hInstance = hInstance;
window_class.lpszClassName = class_name;
window_class.hCursor = LoadCursor(0, IDC_CROSS);
RECT window_rect;
window_rect.left = 0;
window_rect.top = 0;
window_rect.right = client_width;
window_rect.bottom = client_height;
AdjustWindowRectEx(&window_rect,
WS_OVERLAPPEDWINDOW,
0, 0
);
int window_width = window_rect.right - window_rect.left;
int window_height = window_rect.bottom - window_rect.top;
int screen_width = GetSystemMetrics(SM_CXSCREEN);
int screen_height = GetSystemMetrics(SM_CYSCREEN);
int window_x = (screen_width / 2) - (window_width / 2);
int window_y = (screen_height / 2) - (window_height / 2);
RegisterClass(&window_class);
HWND window = CreateWindowEx(0,
class_name,
L"Draw Rectangle",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
window_x,
window_y,
window_width,
window_height,
0,
0,
hInstance,
0);
memory = VirtualAlloc(0,
client_width * client_height * 4,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE
);
bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader);
bitmap_info.bmiHeader.biWidth = client_width;
bitmap_info.bmiHeader.biHeight = client_height;
bitmap_info.bmiHeader.biPlanes = 1;
bitmap_info.bmiHeader.biBitCount = 32;
bitmap_info.bmiHeader.biCompression = BI_RGB;
HDC hdc = GetDC(window);
bool running = true;
while (running)
{
MSG msg;
while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT) {
running = false;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
ClearScreen(0x999999);
DrawRectangle(200, 300, 100, 100, 0x3389E2); // from bottom left
// The StretchDIBits function copies the color data for a rectangle of pixels
// in a DIB, JPEG, or PNG image to the specified destination rectangle.
StretchDIBits(hdc,
0,
0,
client_width,
client_height,
0,
0,
client_width,
client_height,
memory,
&bitmap_info,
DIB_RGB_COLORS,
SRCCOPY
);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
LRESULT result = 0;
switch (msg)
{
case WM_KEYDOWN:
{
if (wparam == VK_ESCAPE) {
DestroyWindow(hwnd);
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
result = DefWindowProcW(hwnd, msg, wparam, lparam);
}
return result;
}
void ClearScreen(u32 color) {
u32* pixel = (u32*)memory;
for (u32 i = 0; i < client_width * client_height; i++) {
*pixel++ = color;
}
}
void DrawRectangle(int rec_x, int rec_y, int rec_width, int rec_height, u32 color) {
u32* pixel = (u32*)memory;
pixel += rec_y * client_width + rec_x;
for (int y = 0; y < rec_height; y++) {
for (int x = 0; x < rec_width; x++) {
*pixel++ = color;
}
pixel += client_width - rec_width;
}
}
/*
run:
*/