×

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:

SDL library in C++ with Examples

Related Topics

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ Program to find the largest number formed from an array

Given an array, write a program to find the largest number that will be formed from the elements of the array. Arrangement should be done in such a way that...

4 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.