SDL library in C++ with Examples
SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware. Video games and animations can be created using it. You can view the SDL Documentation Wiki and the SDL Homepage for API documentation. Windows, macOS, Linux, iOS, and Android are all platforms that SDL formally supports. The source code might contain support for different systems. SDL 2.0 is distributed under the Zlib license. With this license, SDL can be freely used in any software.
SDL2 is used by programs like playback software, emulators, and well-known games from Valve. Depending on your platform, SDL2 uses Direct3D or OpenGL. You may create a lovely overlay on top of Direct3D or OpenGL rather than utilizing them directly to assist prevent touching them.
You must download a few files from http://www.libSDL.org/download-1.2.php to start using SDL. All you require for Windows is the development package. That file's name as of when this guide was written was SDL-devel-1.2.9-VC6.zip. Use "C: Program FilesSDL-1.2.9" as the place to which you unzip this file after downloading it. After installing the development package, go to "C: Program FilesSDL-1.2.9docsindex.html" to access the documentation.
- It offers a collection of APIs to communicate with diverse hardware, including audio, video, keyboard etc.
- It was created using the C programming language and is compatible with Python, C#, and C++.
Installation on Linux (For operating systems which use the apt package manager, such as Ubuntu)
Go to your terminal, and run the following command one by one:
- sudo apt-get update
- sudo apt-get install clang
- sudo apt-get install libsdl2-2.0-0 libsdl2-dbg libsdl2-dev libsdl2-image-2.0-0 libsdl2-image-dbg libsdl2-image-dev
- You need to make a Makefile. So open any text editor and write the following code.
Your initial SDL programs
Here is a summary of what occurs in the example program.
- First, we use SDL Init to initialize the SDL video system().
- The title of our application window is then established.
- Then, using SDL SetVideoMode, we build the window and obtain a pointer to the surface ().
- A temporary surface is loaded with the backdrop image.
- The temporary surface is duplicated by SDL Display Format() using the same bit-depth as the screen surface. Rendering will proceed more quickly.
- A call to SDL Free Surface releases the memory for the temporary surface().
- The "main loop" of the program is now activated. Here, we scan for events before updating the display.
- We terminate the "main loop" when the user clicks ESC or "q."
- With SDL Free Surface, we release memory for the backdrop surface before the program finishes, and SDL is cleaned up with SDL Quit().
Save this file as either detest.cpp or detest.c. If you prefer to use C++ instead of pure C, the bitmap file included with this software is also required.
It would be best if you carried out the following actions in Visual C++
- Make a brand-new, blank Windows Application Project.
- Include "SDLtest.cpp" in your Project'sProject's file list.
- Click ProjectProject, then Properties, from the menu.
- "Configuration Properties" must be clicked twice.
- Click "C/C++" twice.
- Then, select "Code Generation."
- "Runtime Library" should be changed to "Multi-threaded DLL."
- Click "Linker" twice.
- Select "Input."
- Type "SDL.lib domain. lib" next to "Additional Dependencies."
Press F7 to build the program.
You must place the "SDL.dll" file where Windows can find it before we can execute the program. Transfer the file from "C: Program FilesSDL-1.2.9" to the folder for your new Visual C++ Project Project. You can now press F5 to run the application if it is compiled without issues.
Header document:
// for shutting down and initializing operations
#include <SDL2/SDL.h>
// to produce images and graphics for use on screens
#include <SDL2/SDL_image.h>
//to use the SDL Delay() functions
#include <SDL2/SDL_timer.h>
Initialization:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
int main(intargc, char *argv[])
{
// returns 0 if successful, 0 otherwise
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("Error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("GAME",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1000, 1000, 0);
while (1)
;
return 0;
}
Your screen will display an empty window as a result.
Output:
