Tag Archives: SDL

[ SDL2 – Part 2 ] Your first SDL2 application

Your first SDL2 application


This part will teach you the basic parts of SDL2 namely SDL_Renderer and SDL_Window, what they do and how we use them in order to create a simple window.

SDL2 structures


The SDL2 library has a substantially improved rendering structure from the old SDL. The new structure is very simple and easy to understand. This is a short tutorial / reference guide that will teach you everything you need to know in order to start programming SDL2 applications.

The two major objects you need to know :

SDL_Window


This is the physical window you see on your screen. One SDL_Window represents one physical window on your screen. You can have as many SDL_Windows as you like. The structs holds info about the window like position, size, window state and window style.

SDL_Renderer


The SDL_Renderer is basically what you use to render to the screen. The renderer is usually tied to a window. One renderer can only render within one window. The SDL_Renderer also contains info about the rending itself like hardware acceleration and v-sync prevention.

This tutorial will focus on creating and setting up these so we can start drawing to our screen.

Setting up SDL2


Before you can use SDL2, you must set it up by initializing it and creating a SDL_Window and a SDL_Renderer This section will help you to just that.

Initializing SDL2


The first thing you need to do is to initialize SDL. This is done using the following function :

Parameters :

  • flags – specifies what you want to initialize. Set it to SDL_INIT_EVERYTHING to initialize everything.

Return value

0 on success -1 oterwise

Since the function can fail ( though it probably won’t ) you should add code that handles this. I also recommend you print an error message with SLD_GetError() because it will tell you what went wrong. This tutorial will do this for the most important setup functions.

This makes our code so far look something like this :

Setting up the SDL_Window


Now that we have initialized SDL2, we can can start creating our window, this is done by using the following function :

Parameters :

  • title -the window caption/title that appears on the top of the window
  • posX – x position of the window.
  • posY – y position of the window.
  • sizeW – width of the window
  • sizeH – height of the window
  • flags – specifies window state or properties. More info here

The flags parameters has a few different possible values I will not go through all of them, but you can read about them here

Here are the flags that are most revelant to us now.

  • SDL_WINDOW_FULLSCREEN -the window will start out in fullscreen mode
  • SDL_WINDOW_FULLSCREEN_DESKTOP -the window will start out in fullscreen mode with the same resoltion as your desktop currently has
  • SDL_WINDOW_OPENGL -for use with OpenGL, see my tutorial on OpenGL
  • SDL_WINDOW_SHOWN -the window will start out being visible
  • SDL_WINDOW_HIDDEN -the window will start out being invisible

The window will always start out visible, regardless of whether SDL_WINDOW_SHOWN is set. The only way to make a window start out as hidden is to pass SDL_WINDOW_HIDDEN. Because of this, the SDL_WINDOW_SHOWN flag is ignore by SDL_CreateWindow, and we can just pass 0 here.

Return value

A valid pointer on success. nullptr / NULL on oterwise

We’ll set the flag parameter to 0 for now. This simply means we’ll end up with a standard window that’s not maximized, minimized or fullscreen.

Remember to handle it if the function returns NULL. So we end up with something like this :

Creating the SDL_Renderer


Now for the last object we need to start rendering. This time it’s a little bit more work involved, but nothing too scary. We begin by creating the renderer. This is done using the following function :

Parameters :

  • window – the SDL_Window this renderer will be attached to. In our case, this is the SDL_Window we just created.
  • index - specifies which rendering driver to use. It sounds complicated, but we can just specify -1. This means we’ll use the first driver we can find.
  • flags - species how the rendering should be done. For now, we’ll just use SDL_RENDERER_ACCELERATED which lets us use the graphics card to render quickly. You can read more here.

Return value :

Window a valid pointer on success. Otherwise nullptr / NULL.

As always ; remember to handle any return of 0.

Setting up the renderer


Now that we have created the SDL_Renderer we are technically ready to start rendering. But there are a few more things we should do first…

First of all we should set the resolution of the renderer :

Parameters :

  • renderer – the SDL_Renderer we want to set the resolution on.
  • width - the desired width in pixels
  • height - the desired height in pixels

Return value

0 on success -1 oterwise

This is pretty straight forward, the first argument is the renderer on which to set the resolution on. The second and third is the width and height. Normally these are the same as the width and height of the SDL_Window.

And now the time has come to set the color. Here is the function for doing that :

Parameters :

  • renderer – the SDL_Renderer on which to set render color
  • red - specifies amount of red ( 0 – 255 )
  • green - specifies amount of green ( 0 – 255 )
  • blue - specifies amount of blue ( 0 – 255 )
  • alpha - specifies amount of alpha ( 0 – 255 ) 0 = completely transparent

Return value

0 on success -1 oterwise

That’s it! We’re done with the setup. And now we can start using the renderer for fun stuff!

Rendering something


Now it’s time to get started on the renderng.
The first thing we need to do before drawing something, is to clear our window. This means we fill our window with the color we set using SDL_SetRenderDrawColor()

This is done by calling :

Parameters :

  • renderer – the SDL_Renderer that we’ll clear

Return value

0 on success -1 oterwise

This function should be called at the beginning on every frame to clear the screen and make it ready for more stuff.


But… SDL_RenderClear ( and any other SDL_Render functions ) works behind the scenes. They don’t actually draw anything on the screen. So, in order for he drawing ( including SDL_RenderClear ) to take effect, we need to call another function.

Parameters :

  • renderer – the SDL_Renderer on which to render

Return value

0 on success -1 oterwise

This function takes whatever you have drawn to the screen using SDL_Render* and actually puts it on the screen. So that after calling this, the screen will be all nice and red. And that’s it for now. In the next part I’ll go into rectangles and actually rendering something interesting ( yes, I assume red screens doesn’t catch your interest. )

Conclusion


That’s if for part 2. We now have a nice red window. Next time we’ll be rendering rects and other neat stuff.

Here is all the code so far. You should be able to just copy paste and compile.


Feel free to comment if you have anything to say or ask questions if anything is unclear. I always appreciate getting comments.

You can also email me : olevegard@headerphile.com
Share

[ SDL2 – Part 1 ] Setting up SDL2

Introduction


This tutorial will teach you how to program your own games using C++ and SDL2! This first part will just teach you how to set up SDL2. Don’t worry, it’s very easy. And when you’re done, you can jump ahead to part 2 where we will get something on screen.

This series is a little fast-paced and is mostly aimed at people who knows the basics of programming in C++, but I will try to explain everything. The series will also explain the various types and functions in SDL2, what they do, and what they are for. By the end of the series, you will have a good understanding of SDL2.

During the series we will be making several small games, but I recommend that you play around with the code and have fun. The best way of learning is to experiment.

So what is SDL2?


SDL2 is a cross-platform multimedia development library. You can use SDL2 to access your keyboard, for graphics, play sounds and for communication over Internet. SDl2 can also be used alongside with OpenGL. SDL2 can create the window for OpenGL and be used for text rendering, multiplayer and image loading.

SDL2 brings on a set of changes from SDL1.2. First of all, SDL2 has support for hardware acceleration, which means it’ll be very fast compared to SDL1.2. It also has an improved rendering structure with an object representing the window and an object for dealing with the rendering. I will cover these in more detail in the next post. For now, let’s just install it.

Installation


Installing SDL2 can be a bit tricky, especially on some operating systems where you might have to compile it yourself ( though this is becoming more rare. )

Linux


Linux comes in all shapes and sizes. Below are guides on how to install on most distributions of Linux.

Debian ( Ubuntu, Debian, Mint )


Depnding on your distro and version, you might be able to install SDL2 using the package manager. I.e :

sudo apt-get install libsdl2

If your package manager does have SDL2. it will be installed and you’re done. If not, you’ll have to compile SDL2 yourself Don’t worry, it’s easy. Just follow this excellent guide :

Installing SDL2 on Linux

Arch


Probably the easiest. Simply use pacman :

sudo pacman -S sdl2

And that’s all, SDL2 is installed now.

Fedora


Just as easy as Arch :

sudo yum install sdl2

Note: Since I’m not running Fedora, I haven’t been able to test this. But it should work, and if it doesn’t, feel free to post a comment ( you’ll be the first, you lucky bastard! )

Other distros?


If you are using other distributions, there are three things you can try.

  1. Simply use your package manager and see if it has SDL2/, libsdl2 or lSDL2 or something like that.
  2. Try Google! Yes, I know it’s kinda obvious, but chances are someone else has had the same issue, it’s worth a shot.
  3. If 1 and 2 doesn’t work, you could still try the guide for Debian. You will have to switch sudo apt-get install with the command for install packages on your distribution.

Windows


Setting up SDL2 in Windows is a littel bit more complicated, so I made a seperate post for it. You can find it here.

Mac


This will be the shortest guide and it will only cover homebrew since I don’t have the access to any computer running OS X.

In terminal simply type

brew install SDL2

Note: Since I haven’t actually tested this, I can’t guarantee that it will work.

Testing it


Now we come to the fun part, we get to actually use it and run or SDL2 application. Our end result isn’t terribly exiting this time, it just creates an empty window with a red background. But things will get better, I promise!

Compiling on Linux and Mac using terminal


To compile on Linux, simply add lSDL2 to your compilation string. To compile main.cpp you can do

clang++ main.cpp -lSDL2 -o SDLTest

If you are using GCC, the compilation string is

g++ main.cpp -lSDL2 -o SDLTest

Compiling on Linux and Mac without terminal


If you’re not using the terminal, you need to set up your IDE to use the SDL2 libraries. This should be a simple process, check the documentation for you IDE to find exactly how to do this. I do recommend using the terminal, though. It’s much simpler.

Compiling on Windows in VisualStudio


If you have followed my guide, you shouldn’t have to do anything in order to compile.

If you get any compile errors you need to check your include directory. Remember that you should add the folder that has the SDL2 folder, but not the SDL2 folder itself. This is because the example code uses #include

If you get linker errors, make sure you have added the folder with the SDL2.lib file.

If you get runtime errors, make sure you have all the .dll files in an appropriate directory.

Sample code


To test it, simply replace the content with your main.cpp with the following code snippet. If it displays a window, SDL2 is working properly on your window. Don’t worry about the code just now, I’ll explain it in the next part.


Feel free to comment if you have anything to say or ask questions if anything is unclear. I always appreciate getting comments.

You can also email me : olevegard@headerphile.com
Share