Print
View Comments
Setting up Visual Studio for SDL2Welcome to setting up Visual Studio 2015, a prerequisite of my SDL2 Guide series! In this guide you will learn how to create a new C/C++ Win32 Project and configure it to use SDL2. The prerequisites for this course include the following:
 
SDL2 Extracted Location
Extracted Location (Click to Enlarge)
SOMETHING TO DOWNLOAD & EXTRACT (SDL2 Development Libraries) ...
For this guide, or any SDL project, you need to download SDL's development libraries. At the time I was writing this guide the current version is 2.0.4, 2.0.4 is needed for Visual Studio 2015, but it will also work for 2012 and 2013. Earlier versions of SDL2 does not work with Visual Studio 2015 without modfication.
  1. You need to download 2.0.4 for Visual Studio 2015: SDL2-devel-2.0.4-VC.zip
  2. Open the archive and extract the folder "SDL2-2.0.4" to "C:\Development\" so that you end up with "C:\Development\SDL2-2.0.4".
    • TIP: Where you put this on your Windows machine is not as important for Windows users as it is for Linux users. You can put it where you like, but you will need to keep that path in mind for the rest of the guide. It's common to use the folder name "SDL" rather than "SDL2-2.0.4", but I have not changed what comes in the archive for the sake of simplicity.
    • ADVANCED TIP: I'll be honest. I often build SDL myself rather than using these pre-build libraries! This is not only so I can make make sure SDL is using the same C++ runtime as my end-product but I also can make debug versions of the libraries.
 
NEW PROJECT & APP WIZARD
Now we need to make a new Visual Studio project. Some of my initial screen shots are from Visual Studio 2013, but nothing has changed in 2015; although keep in mind my screen shots might show 2.0.3 in the paths. You need to change the paths to point to 2.0.4!
Follow the procedure below:
  1. After launching Visual Studio, open the "File" menu, select "New", and then "Project..."
  2. In the dialog that pops up, in the left tree view, find "Visual C++" and select "Win32" below it.
  3. In the listbox next to the tree view, choose "Win32 Project".
  4. In the "Name:" edit box below, choose the name of your project. I'm just using the default, "Win32Project<#>".
  5. In the "Location:" edit box, choose the location for your project. Again, I'm using the default (though my data drive is D:), but it can be anywhere you find suitable.
  6. (Optional) You might want to uncheck the "Create directory for solution" option. With it checked your project path will look like "Win32Project\Win32Project\" which I often dislike.
  7. Click "OK".
  8. In the next dialog, go to "Application Settings", check "Empty project", and uncheck "Security Development Lifecycle (SDL) checks" (unless you want it).
  9. Click "Finish".
Visual Studio 2013 - New Project
Visual Studio 2015 (Windows 10): New Project
Visual Studio 2013 - Win32 Application Wizard
Visual Studio 2013 (Windows 7): Win32 Application Wizard
 
 
ADD A SOURCE FILE
Now we need to create a new source file. Follow the procedure below:
  1. Right-click your project's name in the Solution Explorer and hover over "Add" and then click "New Item ..."
  2. Ensure "C++ File (.cpp)" is selected in the list and in the "Name:" edit control type "main.c"
    • You can use main.cpp (without issue) if you prefer. The guides here are strictly C so I am using a C file extension.
  3. Click the "Add" button.
Visual Studio 2013 - Add Menu
Visual Studio 2015: Add Context Menu
Visual Studio 2013 - Ad New Item Dialog
Visual Studio 2015: Add New Item Dialog

 

START CONFIGURING THE PROJECT

Now we need to do some initial project configuration and select some items before we can get to the important stuff:
  1. Right-click your project's name in the Solution Explorer and and then click "Properties"
    Don't be clever and use the Keyboard Shortcut (Alt+Enter) as that would not bring up the right window!
  2. IMPORTANT: Ensure Configuration: is set to "All Configurations" and Platform: is set to "All Platforms"
    The goal here is to affect changes with any configuration (debug and release) as well as any platform you might have (Win32, x64, etc.).
  3. (Optional) If you need Windows XP compatibility with your end product change the Platform Toolset to "Visual Studio 20XX - Windows XP (v1X_xp)"
    Replace XX with what is relevant to your version of Visual Studio. For example, in 2015 it's "Visual Studio 2015 - Windows XP (v140_xp)." It's a drop-down so what to choose will be very obvious.
  4. Do not click OK just yet, we're not done yet! Keep the Property Pages window open.
    TIP: You can click "Apply" when that button is available to you. It may not enable until you change the focus from the edit box you last changed.
Visual Studio 2015 - Properties in Project's Context Menu
Open Project Properties
Visual Studio 2015 - Project Property Pages
Visual Studio 2015: Select All Config., All Platforms, & Optionally Choose XP Support

 INCLUDES & LIBRARIES THEN FINAL CONFIGURATION

In order for SDL to build properly using our project, the include and library directories need to be configured. If you skip this the compiler / linker will not know where SDL exists and errors will result. Hopefully you didn't close the Project's Properties Window, but if you did, open it back up.
    1. Select "C/C++" in the left Pane below "Configuration Properties"
      This is actually C/C++ > General, but General should be selected for you.
    2. For "Additional Include Directories" type "C:\Development\SDL2-2.0.4\include"
      This is where you extracted the SDL archive, but if you put it elsewhere, use that base path instead.
    3. Select "Linker" in the left Pane below "Configuration Properties." General should select by default.
    4. For "Additional Library Directories" type "C:\Development\SDL2-2.0.4\lib\$(PlatformTarget)"
      TIP (Visual Studio): Notice I used a macro named PlatformTarget. This will turn into x86 or x64 depending on what platform you build for. It just so happens SDL's VC archive's library folders match perfectly with that. Maybe they knew what they were doing?
    5. Do not click OK just yet, we're not done yet! Keep the Property Pages window open. You can click "Apply" whenever it becomes enabled.
Visual Studio 2015 - Include Directory
Visual Studio 2015: Add SDL's Include Directory
Visual Studio 2015 - Library Directory
Visual Studio 2015: Add SDL's Library Directory (Tip: Use a Macro)
  1. Select "Linker" > "Input" in the left Pane below "Configuration Properties."
  2. For "Additional Dependencies" and at the VERY BEGINNING of what it contains type "SDL2main.lib;SDL2.lib;"
    The goal is not to remove any of the libraries Visual Studio has already put in there for us. Our Windows Application needs most of those libraries, but  you can decide more on that when you become or if you already are a pro. It is possible to remove some if you know what you're doing.
  3. OK, now you can click OK to save all of our property changes.
    The project is virtually setup now to build with SDL2, but there is still more to do. There isn't any code yet!
Visual Studio 2015 - Library Files
Visual Studio 2015: Add SDL's Library Files
 
OPTIONAL: AUTO COPY & CUSTOM BUILD STEPS
One important thing to know is that after you build your project your executable can't run without SDL2.dll. The correct platform version of this file must be copied into the same folder as your executable or run-time errors will result. Well, it just will not run at all. Visual Studio does give us a way to automatically copy this DLL or really any other file you want from some location to your project's output directory after you build. This way you could totally clean out (delete) your build's output folders, build again, and the proper DLLs will still be where they need to be.
Here's how to do that:
  1. Select "Custom Build Step" in the left Pane below "Configuration Properties." General should be the default when you get there.
  2. For "Command Line" type this: copy /Y "C:\Development\SDL2-2.0.4\lib\$(PlatformTarget)\SDL2.dll" "$(OutDir)"
    TIP: See the "OutDir" macro there? Nifty. Also if you were to select the drop-down arrow for the "Command Line" box and use "Edit" you could use multiple lines. The script will be the same commands as what is used at the Windows Command-Prompt (Essentially the same as a Batch File).
  3. For the "Description" it's up to you, but I used "Copy SDL2 Libraries to Build Output Folder"
  4. For the "Outputs" type "SDL2.dll" as that's the only file we're copying right now.
    TIP: If you are copying more than one file that ends up in your output folder, put a semi-colon between file names.
  5. For "Treat Output As Content" select "Yes" so Visual Studio knows it's Deployable Content.
  6. Finally, so that Visual Studio knows when to run this script, for "Execute After" select "Build"
  7. Click the OK button to save your custom build step.
Visual Studio 2015 - Custom Build Step
Visual Studio 2015: Configure a Custom Build Step
 
THE TEST SOURCE CODESDL2 w/ Visual Studio Run Screenshot
The image on the right is a screen shot of the end result and what your application should look like (on Windows 10). That's all there is to it and I hope you found my guide as a good explanation on how to use SDL2 with Visual Studio as a beginner. I've tried to be detailed and concise enough to give you a good start. If you have any suggestions on revising this guide please let me know in the comments or by This email address is being protected from spambots. You need JavaScript enabled to view it..
 
The source code is mostly taken from my SDL Guide Part 1. To exit just press ESCAPE if the window is in the foreground. The code itself should also be platform independent thanks to SDL_main, but that's invisible to you as it should be.

Here is your completed source code (you should be able to build and run):

// XEEKWORX.COM
// SDL2 Guides, Setting Up Visual Studio
// Copyright (C) 2016 John A. Tullos
//
// FILE: main.c
// LAST UPDATED: 10.11.2015
#include <SDL.h>
#ifdef __cplusplus
extern "C"
#endif
int main(int argc, char *argv[])
{
	int screen_width = 350, screen_height = 128;
	SDL_Window* main_window = NULL;
	SDL_Renderer* renderer = NULL;
	SDL_Event event = { 0 };
	SDL_Color draw_color = { 0, 0, 255, 255 };
	int should_quit = 0;
	
	if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		SDL_Log("Failed to initialize SDL (\"%s\").\n", SDL_GetError());
		goto exit_app;
	}
	
	if((main_window = SDL_CreateWindow("XeekWorx SDL2 w/ Visual Studio", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, screen_height, 0)) == 0) {
		SDL_Log("Failed to create a window (\"%s\").\n", SDL_GetError());
		goto exit_app;
	}
	
	if((renderer = SDL_CreateRenderer(main_window, -1, 0)) == 0) {
		SDL_Log("Failed to create the renderer (\"%s\").\n", SDL_GetError());
		goto exit_app;
	}
	
	while(!should_quit) {
		while(SDL_PollEvent(&event)) {
			switch(event.type) {
			case SDL_QUIT:
				should_quit = 1;
				break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym) {
				case SDLK_ESCAPE:
					should_quit = 1;
					break;
				}
				break;
			}
		}
		
		SDL_SetRenderDrawColor(renderer, draw_color.r, draw_color.g, draw_color.b, draw_color.a);
		SDL_RenderClear(renderer);
		
		SDL_RenderPresent(renderer);
	}
	
exit_app:
	
	if(renderer) SDL_DestroyRenderer(renderer);
	if(main_window) SDL_DestroyWindow(main_window);
	SDL_Quit();
	
	return 0;
}