Code a classic star wars scroller

10 min read

There’s nothing more classic than Star Wars, so Ferenc Deák brushes off his ancient dusty demo code for a scroller celebration.

OUR EXPERT

Ferenc Deák never throws anything away, which is why he still has all his demoscene files.

Last month, we had a short introduction to SDL2, and created a more-or-less usable L coding framework on which we can base our implementation of some of the most notorious effects of the ’90s demoscene. This month, we are continuing the recreation of those effects, and introducing some auxiliary mobility into the not-so-dormant screen we created last time.

The fire effect was one of those quick-and-dirty effects of the ’90s that seized the attention of spectators at a demo party and ensured instant recognition from your peers. Implementing it involves creating a visual simulation of flames on a computer screen. This is typically achieved by representing the fire as a grid of cells, where each cell corresponds to a pixel and holds a value representing the intensity or temperature of the fire at that location. The process begins by initialising the grid, setting up the initial state of the fire, then iteratively updating the values of each cell based on the value of its neighbours. Randomness is often introduced to add a natural and organic appearance, and the intensity values are mapped to a colour palette set up to recreate the colour scheme of those found in real flames. The simulation is animated by continuously updating and rendering the grid.

The fire palette

Due to the nature of the effect, the palette used must be set up in such a way that colours at low indexes are darker (due to them being constantly averaged out, they simulate the extinction of the flame and the death of the fire), while colours at high indexes are bright, representing the new root of the fire, where the flames have the highest intensity. To achieve this effect, we have created the generateFirePalette method, which generates a corresponding set of red, green, blue series, and sets the colours correspondingly.

Ugly and long as it is, it contains the entire implementation of the fire routine. Let’s break it down, section by section. In the first loop, the bottom row of the screen is initialised to a random value between 0 and 255. This will be the new set of values for the fire algorithm that has the tendency to consume the pixels on the screen, thus needs constant feeding.

Then we traverse the screen, row by column, and for each pixel we calculate the new value of the current pixel as the average of the pixels surrounding it. Then we set the current pixel to be the newly calculated average value. The sequence if(rand() % 25