Skip to main content

Blog

Win32 C++ pixel playground template

A tiny Win32 starter that cross-compiles from Linux with MinGW-w64 + Meson, opens a window, and pushes pixels through a software framebuffer.

Published13-11-2022
Tags
C++Win32MesonGraphics

Why

I wanted a zero-dependency starting point for Win32 experiments: open a window, draw pixels in software, and measure each frame without pulling in a game engine or DirectX. The template now ships with a Meson-only workflow that cross-compiles on Linux (MinGW-w64) and runs under Wine.

What’s inside

  • Win32 setup: WinMain registers a window class, creates a window titled “TESTING,” and wires a minimal WinProc that handles paint events.
  • Software framebuffer: VirtualAlloc reserves a 32-bit buffer sized to the client area. DrawPixel and ClearScreen write directly into that memory, and StretchDIBits blits it to the window.
  • Timing hooks: QueryPerformanceFrequency/Counter capture microsecond-resolution timings each loop so you can profile any work you add (unused helpers were trimmed to avoid warnings).
  • Meson build: Meson drives the build; the CMake files were removed. Dependencies are user32, gdi32, and kernel32, with C++20 enabled and a WIN32 subsystem binary so no console pops up.
  • Cross-file + tasks: cross/windows-mingw64.txt defines MinGW-w64 binaries and Wine as the exe wrapper. VS Code tasks/launch entries let you hit F5 to build and run under Wine.
  • Default render: The main loop clears to dark gray and fills a simple gradient (x + y) across the client area to prove the pixel path works.

How to build/run (Linux host → Windows target)

Install prerequisites (Debian/Ubuntu example):

sudo apt-get install mingw-w64 ninja-build meson wine64

Meson + Ninja:

meson setup build-mingw --cross-file cross/windows-mingw64.txt --buildtype=release
ninja -C build-mingw
wine build-mingw/cpp_playground.exe

From VS Code: press F5 and choose Run with Wine (Meson); the default build task runs meson:build-mingw and launches the resulting binary via Wine.

What happens when it runs

A window appears, the client area is cleared to dark gray, and a color gradient is drawn via the software framebuffer each frame. WinProc also paints a simple "Hello Team" text on WM_PAINT, giving a spot to extend message handling for input or UI.

Takeaways

  • You can prototype Win32 pixel experiments without DirectX/OpenGL by pushing pixels into a memory buffer and blitting with StretchDIBits.
  • The template keeps the loop explicit, making it easy to insert input handling, physics, or custom rendering code while profiling with high-resolution timers.
  • Cross-compiling from Linux with MinGW-w64 + Meson is a single command, and Wine makes the feedback loop quick without leaving your host OS.

Source

Source