Page 1 of 1

Re: Source Code

Posted: 29 Sep 2020 12:45
by andythenorth
Not a direct answer, but Maizure did a really nice decoding of OpenTTD from the perspective of someone who has never contributed to OpenTTD. ... index.html

Re: Source Code

Posted: 29 Sep 2020 14:05
by jfs
The rendering is quite spread out and you need many parts to get the full picture.
It's worth noting that this model is very much set in the 1990's world and not designed for hardware accelerated graphics.


Blitter - This is the part that does the low-level putting pixels onto a framebuffer, and has the algorithm to draw lines and paint sprites. There are multiple blitter implementations in OpenTTD for various combinations of colour depth (palette mode/8bpp or true-colour mode/32bpp) and optimization (palette animation on/off, SIMD instruction sets). These are in the src/blitter/ subdirectory.
Video driver - These take the composited image from the blitter and put on the screen.

Generic graphics functions - In the file gfx.cpp several generic blitter-independent algorithms are implemented, such as entry points for drawing lines, rectangles, and text. This is also where the dirty blocks system is implemented (to avoid redrawing the entire screen every frame).

Text formatting and layout - Taking strings and converting to a sequence of sprites that can be painted is done in string.cpp, while the management and formatting of the game's translated text system is in strings.cpp.

Sprite decoding and management - The file spritecache.cpp is where you will find the code to decode sprite bytestreams into the internal representation, and manage the sprite cache and zoom levels of different sprites.

World view rendering - The code to draw the game world is in viewport.cpp, this is also where sprite sorting and such happens. You also need to understand the NewGRF action 1-2-3 system for selecting sprites to draw for things for the full picture, this is very OpenTTD specific.

Generic GUI rendering - The base for GUI rendering and management is in window.cpp
Specific GUI rendering - All the specific GUI is implemented in the various *_gui.cpp files for various windows.