How to Create a 1k Mandelbrot Demo in OpenGL / C++ with Source Code

Mandelbrot 1k Demo in OpenGL

Humus has published a small Mandelbrot demo in 1k. The interesting thing is not really the Mandelbrot shader but rather Humus’explanation about how create a 1k demo in C/C++ / OpenGL for Windows.

The first thing you learn when you learn to code C/C++ is that the program execution starts at main(). This is of course a bunch of lies. The program typically starts in a startup function provided by the C runtime, which after doing a whole bunch of initialization eventually calls main(). When main() returns the program doesn’t terminate, instead it just returns to the C runtime which will close down its stuff and eventually ask the OS to terminate the process. If you create a simple project with an empty main() and compile as usual you will likely get an executable that is something like 30KB or so. So you’ve already excluded yourself from the 4K demos, let alone 1K, even before writing any code. That overhead is mostly the C runtime. You can throw it out by specifying a custom entry point and handling process termination yourself. Throwing out the C runtime has the disadvantage of losing a whole bunch of features, including some of the most basic things like printf(), but you probably won’t need so much of that anyway. With this you can easily get down to only a couple of kilobytes, depending on optimization flags used.

Here is a simple version (fully operational) of the code to get a tiny executable:

#define WIN32_EXTRA_LEAN

__declspec(naked) void winmain()
	// Prolog
	__asm enter 0x10, 0;
	__asm pushad;

	{ // Extra scope to make compiler 
    //accept the __decalspec(naked) 
    //with local variables

    // Create window and initialize OpenGL
	  HWND hwnd = CreateWindow("edit", 0, 
                WS_POPUP | WS_VISIBLE | WS_MAXIMIZE, 
                0, 0, 0, 0, 0, 0, 0, 0);
	  HDC hdc = GetDC(hwnd);
    // Init OpenGL stuff
    // ...

	  for (;;)
      // OpenGL Rendering code
      // ...
      if (GetAsyncKeyState(VK_ESCAPE))


Under Visual Studio 2005, I compiled this short code in a 2k (exactly 2048 bytes) executable just by tweaking project properties (the most important thing to compile this code is to set the Entry Point to winmain in the linker properties). Now to reach 1k, you have to add a Crinkler pass in order to compress the link step (see here for more details: Crinkler: Under the Hood of the Best 4k Exe Compressor).

You can download Humus’s code and read details HERE.

Forum thread

3 thoughts on “How to Create a 1k Mandelbrot Demo in OpenGL / C++ with Source Code”

Comments are closed.