Getting Started with SDL2 Graphics
This brief guide provides code to show simple graphics functionality without SplashKit such as how to open a window, keep a window open, draw a rectangle and display a bitmap. These are then combined to create a simple "eye dropper" program to compare with and without SplashKit.
Written by: Simon Rhook and Olivia McKeon
Last updated: December 2024
Getting Started without SplashKit
Installing SDL2 and SDL2_image
If you have not already installed SplashKit, you will need to install SDL2
and SDL2_image
to be able to compile the code shown in this guide.
You can use the following command, which is usually run during the SplashKit installation, which will ensure you have installed the required libraries/dependencies.
Compiling Your Code
With C++ programs, you will need to adjust the compiling command to link to any libraries being used. Below you will see the different commands to compile with and without SplashKit.
Now, assuming the code filename is program.cpp
.
As usual, you will compile the SplashKit C++ code using the following command:
You can use this command to compile the code below:
Note: If you have issues, you can find out the compiling flags needed using these commands:
Note: If you have issues, you can find out the compiling flags needed using these commands:
Note: If you have issues, you can find out the compiling flags needed using these commands:
SDL2 Versions of SplashKit Functions
The following sections show how to create the same functionality given in SplashKit, but without using SplashKit. These code examples are using SDL2 which is actually one of SplashKit’s dependencies, and are quite similar to what is happening behind the scenes when using SplashKit functions.
Open Window
The following code shows how to open a Window with and without SplashKit:
Quit Requested
The following code shows how to open a Window and hold the window open until the user quits, with and without SplashKit:
Draw Rectangle
The following code shows how to draw a rectangle to the screen (Window), with and without SplashKit:
Draw Bitmap
The following code shows how to display a bitmap image on the screen (Window), with and without SplashKit:
Combining Functionality
You can then combine the elements above into a small program such as the eyedropper (colour picker) program shown below.
Eyedropper Program
This program combines the above into a small program that will change the screen (Window) colour based on the colour of the image at the point where you click on it: