Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do demomakers attain ultra small filesizes?

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.

like image 979
devmabbott Avatar asked Feb 09 '14 20:02

devmabbott


People also ask

How does demoscene work?

The demoscene is an international computer art subculture focused on producing demos: self-contained, sometimes extremely small, computer programs that produce audio-visual presentations. The purpose of a demo is to show off programming, visual art, and musical skills.

What is a Demoparty?

A demoparty is an event where demosceners and other computer enthusiasts gather to take part in competitions, nicknamed compos, where they present demos (short audio-visual presentations of computer art) and other works such as digital art and music.


2 Answers

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.

like image 131
Laurent Avatar answered Nov 15 '22 07:11

Laurent


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.

like image 42
Damon Avatar answered Nov 15 '22 07:11

Damon