We now have an empty project, which links to the SDL library, so it is time to start our SDL development. Click on Source Files
and use the keyboard shortcut Ctrl + Shift + A to add a new item. Create a C++ file called main.cpp
. After creating this file, copy the following code into the source file:
#include<SDL.h> SDL_Window* g_pWindow = 0; SDL_Renderer* g_pRenderer = 0; int main(int argc, char* args[]) { // initialize SDL if(SDL_Init(SDL_INIT_EVERYTHING) >= 0) { // if succeeded create our window g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); // if the window creation succeeded create our renderer if(g_pWindow != 0) { g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0); } } else { return 1; // sdl could not initialize } // everything succeeded lets draw the window // set to black // This function expects Red, Green, Blue and // Alpha as color values SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255); // clear the window to black SDL_RenderClear(g_pRenderer); // show the window SDL_RenderPresent(g_pRenderer); // set a delay before quitting SDL_Delay(5000); // clean up SDL SDL_Quit(); return 0; }
We can now attempt to build our first SDL application. Right-click on the project and choose Build. There will be an error about the SDL.dll
file not being found:
The attempted build should have created a Debug
or Release
folder within the project directory (usually located in your Documents
folder under visual studio and projects). This folder contains the .exe
file from our attempted build; we need to add the SDL.dll
file to this folder. The SDL.dll
file is located at C:SDL2VisualCSDLWin32
(or x64)ReleaseSDL.dll l
). When you want to distribute your game to another computer, you will have to share this file as well as the executable. After you have added the SDL.dll
file to the executable folder, the project will now compile and show an SDL window; wait for 5 seconds and then close.
Let's go through the Hello SDL
code:
SDL.h
header file so that we have access to all of SDL's functions:#include<SDL.h>
SDL_Window
function, which will be set using the SDL_CreateWindow
function. The second is a pointer to an SDL_Renderer
object; set using the SDL_CreateRenderer
function:SDL_Window* g_pWindow = 0; SDL_Renderer* g_pRenderer = 0;
SDL_INIT_EVERYTHING
flag, but this does not always have to be the case (see SDL initialization flags):int main(int argc, char* argv[]) { // initialize SDL if(SDL_Init(SDL_INIT_EVERYTHING) >= 0) {
SDL_CreateWindow
returns a pointer to a window matching the passed parameters. The parameters are the window title, x position of the window, y position of the window, width, height, and any required SDL_flags
(we will cover these later in the chapter). SDL_WINDOWPOS_CENTERED
will center our window relative to the screen:// if succeeded create our window g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
g_pWindow
pointer. The second parameter passed is the index of the rendering driver to initialize; in this case, we use -1
to use the first capable driver. The final parameter is SDL_RendererFlag
(see SDL renderer flags):// if the window creation succeeded create our renderer if(g_pWindow != 0) { g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0); } else { return 1; // sdl could not initialize }
// everything succeeded lets draw the window // set to black SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255); // clear the window to black SDL_RenderClear(g_pRenderer); // show the window SDL_RenderPresent(g_pRenderer); // set a delay before quitting SDL_Delay(5000); // clean up SDL SDL_Quit();
Event handling, file I/O, and threading subsystems are all initialized by default in SDL. Other subsystems can be initialized using the following flags:
Flag |
Initialized subsystem(s) |
---|---|
We can also use bitwise (|
) to initialize more than one subsystem. To initialize only the audio and video subsystems, we can use a call to SDL_Init
, for example:
SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO);
Checking whether a subsystem has been initialized or not can be done with a call to the SDL_WasInit()
function:
if(SDL_WasInit(SDL_INIT_VIDEO) != 0) { cout << "video was initialized"; }
18.118.122.244