When I watch demoscene videos on youtube the author's often boast of how their filesizes are 64kb or less, some as few as just 4kb. When I compile even a very basic program in C++ the executable is always at least 90kb or so. Are these demos written entirely in assembly? it was my understanding that demomakers used c/c++ as well.
I'm one of the coder of Felix's Workshop and Immersion (64k intros by Ctrl-Alt-Test). Most 64k intros nowadays use C++ (exception: Logicoma uses Rust). Assembly may make sense for 4k intros (although most of them actually use C++), but not for 64k intros.
Here are the two most important things:
- Compile without the standard library (in particular, the STL could make the binary quite large).
- Compress your binary (kkrunchy for 64k intros on Windows, Crinkler for 4k intros on Windows).
Now, you can write a ton of code before filling the 64kB. How to use them? Procedural generation.
- For music, music sheet is compressed. Instruments are generated with a soft synth. A popular option, although a bit outdated, is to use v2 by Farbrausch.
- If you need textures, generate them.
- If you need 3d models, generate them.
- Animations and effects are procedural.
- For the camera, save some key positions and interpolate.
- Shaders are heavily used in modern graphics. Minifying the shaders can save quite a lot of space.
Want to hear more about procedural generation and other techniques? Check IQ's articles.
If you want to further optimise your code, here are some additional tricks:
- You probably use lots of floats. Try to truncate the mantissa of your floats (it can save many kB).
- Disable function inlining (it saved me 2kB).
- Try the fastcall calling convention (it saved me 0.7kB).
- Disable support for exceptions. You don't need them.
- If you use classes, avoid inheritance.
- Be careful if you use templates.
In a typical 4k intro, the C++ code is used for the music and the initialisation. Graphics are done in a shader.
Those demos do not use the standard library (not C++ and not even the C standard lib), nor do they link with standard libraries (to avoid import table sizes). They dynamically link only the absolute minimum necessary.
The demo's "main function" is usually identical with the entry point (unlike in a normal program where the entry point is a CRT init function which does some OS-specific setup, initializes globals, runs constructors, and eventually calls main
).
Usually the demo executables are not compliant with the specifications (omitting minimum section sizes and alignments) of the executable format and are compressed with an exe-packer. Technically, these are "broken" programs, but they are just "broken" so much that they still run successfully.
Also, such demos rely heavily on procedurally generated content.
These ultra-small programs typically don't depend on any libraries or frameworks, as is typical with traditional application development. These programs typically accesses graphics/io, etc. directly.
OpenGL
or DirectX
. –
Verdha .com
demos typically directly access VGA memory in the 16-bit virtual machine they run in. But otherwise no, Windows .exe or Linux executable demos would use DLL or shared library calls, because there is no portable hardware present on every system, and Windows wouldn't let you access it anyway. The only kind of "directly" possible would be manually inlining syscall
or Windows int 2Eh
/ Linux int 0x80
system calls, but that probably takes more code size than calling into a shared library. (Especially under Linux to implement the X11 protocol over a socket.) –
Spidery I can't comment yet because I don't have 50 rep points, so I'm answering.
One way to create a smaller program is to use an older compiler, such as Microsoft Visual C/C++ 4.0, which produces a smaller .exe file than say Microsoft Visual Studio 2005.
It really depends on your environment, but if you don't instantiate any templates, and you link everything dynamically, it's fairly easy to achieve a very small size for your executable, since none of the code you actually execute will be in the executable.
© 2022 - 2024 — McMap. All rights reserved.
.exe
compressors, as well tricks they pick up along the way to keep the file size down. Many (all?) of them are tied to a particular compiler (mostly MSVC). No templates here. – Verdhaprintf
you are toasted. :) Of ocurse, getting below a certain minimum requires assembly. – Commensurate