Most of the programs of this site will use a basic collection of functions based on the SDL.
It is meant to ease the use of of a 2D display where we can set the color of each pixel.
As you will see it will be very convenient to create simple graphic representations to explain most of the
algorithms.
You can download this basic library
here but it is not mandatory as it will be included in most of the
programs on this site.
Here is an explanation of the files it contains.
CSDL.cpp & CSDL.h:
To keep the code of my programs as clear as possible I want to avoid adding too much error checking in the main
code.
So this class is a simple interface to the SDL, where each function call the SDL function of the same name and
take cares of displaying any error that can occur.
By the way these functions are not meant to be called directly by our programs. They will rather be used in the
other modules of the library - Graphics and System.
Graphics.cpp & Graphics.h:
This module contains the core functions of most of the programs you will see:
- Color is a simple structure used to easily define a color by its components - red, green, blue and alpha.
- init() initialize the SDL library, opens a window with the given title and size, and sets up a renderer.
- init2D() allocates a texture and an array of Color structures that represents our 2D screen.
- clearScreen() fills the screen with a given color.
- setPixel() change the color of a pixel in the Color array.
- getPixel() retrieves the color of a pixel.
- render() "displays" all the pixels modified by setPixel() to the window.
- quit() closes everything before you exit the program.
System.cpp & System.h:
These files contains general purpose functions that are not related to graphics.
For example this is where we will handle the inputs.
The basic library only contains 3 functions:
- wait() to wait a given amount of milliseconds.
- processEvents() must be called once in a while to process the user inputs.
- isQuitRequested() returns true if the user clicked on the close button of the window.
You must call processEvents to update this value.