The SDL2, natively, image formats png
and jpg/jpeg
are not supported, only ` bmp`.
However, there is an additional package made available by the SDL2 development team itself that allows you to use PNG and JPG in your games.
In this article we will see how to use these formats in SDL2 on Windows.
First, first of all, you must have GCC MinGW properly installed on your Windows, see the article below to see how to do this:
After properly installed on your system and working correctly, download SDL2 at this link, choose version SDL2-devel-X.XX.X -mingw.zip
, where X.XX.X
is the latest version number.
According to the publication date of this article, the latest version is 2.26.4
, if you want to download it directly from here, just click the button below:
Once downloaded, right-click and select Extract here
(needs Winrar installed).
I renamed the folder that was unzipped to SDL2
only, it’s easier when we use it.
Now cut or copy this folder to your project.
Now let’s download the SDL2_image
. Access this address: https://github.com/libsdl-org/SDL_image/releases/ and select the latest version of SDL2_image, according to the publication date of this article is 2.6.3
.
Download the package: SDL2_image-devel-X.X.X-mingw.zip
, where X.X.X
is the version, if you want the one according to this article click on the button below:
SDL2_image-devel-2.6.3-mingw.zip
Unzip the downloaded file (right mouse button: Extract here), enter the unzipped folder and copy the SDL_image.h
file and paste it in the path corresponding to the SDL2 folder you have pasted in your project.
SDL2_image\x86_64-w64-mingw32\include\SDL2\SDL2_image.h
and paste it in SDL2\x86_64-w64-mingw32\include\SDL2\
, where there will be other files.Do the same, but this time for ALL FILES in the bin
and lib
folder in SDL2_image
and paste them in the corresponding paths in SDL2
, example:
bin
folder: SDL2_image\x86_64-w64-mingw32\bin
for ``SDL2\x86_64-w64-mingw32\bin`lib
folder: SDL2_image\x86_64-w64-mingw32\lib
for ``SDL2\x86_64-w64-mingw32\lib`To finish this step of the copies, also copy the file: SDL2_image.dll
which is in SDL2_image\x86_64-w64-mingw32\bin\SDL2_image.dll
to the “root” of your project.
TIP: If you want, make a backup of this SDL2 folder that already has all the SDL2_image files somewhere else to always use them in possible new projects and save time.
To run our example we will use this PNG image below:
Save it inside your project!
Now copy the code below and paste it inside a main.cpp
file (Do not use Notepad to create it, use a code editor: VS Code, for example).
Of course, in the same place as the
SDL2
folder (with the files copied from SDL2_image) and the image.
Note that in this code we have 4 fundamental lines to read and display the image correctly, they are the lines:
To compile the command is similar to what we did in the video, there is only one difference, add the flag: -lSDL2_image
at the end, example:
Now just run with the command:
If it doesn’t work it’s because you must have ′′ skipped ′′ some step, review all of them!
Makefile
To speed it up and type less when compiling, you can create a file (no extension, again don’t use Notepad) named Makefile
and insert this content below:
Remember: A Makefile must respect the
tabs
as the code is here, otherwise it won’t work.
After saving, still inside your project folder, such as PowerShell or CMD
, run the command:
Also remember to have previously renamed the
mingw32-make
file to justmake
in the path:C:\mingw64\bin\
The cool thing about using a script
from some scripting language, is that you can check if files exist and with that it helps you debug which step you forgot.
If you want to use a script: Python, Batch
, … feel free!
In this example I used Lua, to find out how to install Lua on Windows see the file: How to install Lua on Windows 10(Article in Portuguese)
Create a file named build.lua
still inside your project and insert the code below:
This script checks if you didn’t forget to copy the
SDL2.dll
file into your project, you can still improve and also include theSDL2_image.dll
file. In Lua you need to escape the backslash with another backslash, eg\\
.
And to run:
Any questions comment on the SDL2 video on the channel: .