Setup SDL2 with Visual Studio Code and mingw64 on Windows
Tags: gamedev, sdl2
Published: 2020-09-28 16:52:31

---
title: Setup SDL2 with Visual Studio Code and mingw64 on Windows
published: true
date: 2020-09-28 15:50:18 UTC
tags: GameDev,sdl2
canonical_url: https://giovanni.codes/setup-sdl2-with-visual-studio-code-and-mingw64-on-windows/?utm_source=rss&utm_medium=rss&utm_campaign=setup-sdl2-with-visual-studio-code-and-mingw64-on-windows
---

Recently I changed my setup from macOS to Windows, and I’d like to do some experiments with GameDev in Windows. So one of the first things that I did was try to set up my dev environment and test some SDL2 code.

This is a simple guide to how to setup SDL2 with Visual Studio Code and mingw64, for me worked and I hope that you can start with SDL2 in Windows.

## Installing the tools.

The first thing that we need to install is [Visual Studio Code](https://code.visualstudio.com/download) editor. Download, install, and launch VS Code.

You need to install the **C/C++ plugin from Microsoft**.

![C/C++ plugin](https://giovanni.codes/wp-content/uploads/2020/09/cpp-plugin-editor.png)

Now, you need to install mingw64 to use the compilers that are included like g++, if you want to use the Visual Studio compiler, you can change some of the settings here but for now we continue with mingw64.

For easy installation, use the [MinGW-W64 Online](https://sourceforge.net/projects/mingw-w64/files/) Installer, take note of the path where you installed your compiler, because we’re going to need it in the configuration.

Now, we need to download [SDL2](https://www.libsdl.org/download-2.0.php). For our case, we have to download the Development libraries for MinGW

![](https://giovanni.codes/wp-content/uploads/2020/09/sdl2-library-mingw.png)

And download the Runtime libraries to run our games.

![](https://giovanni.codes/wp-content/uploads/2020/09/sdl2-runtime-libraries.png)

We are going to use the x64 version. Unzip those folders in a location that you can find because we are going to need it.

## Creating the SDL2 project

Now we are ready to start our SDL2 project. Create a folder for your project and open it with VSCode. Inside the folder, create another called **src** , here is where are going to save our code files. For our SDL2 and C++ configuration, create a **.vscode** folder. For the output of our compilation, create a **build** folder. Your editor should be like this

![](https://giovanni.codes/wp-content/uploads/2020/09/sdl2-src-vscode-build-folder.png)

The first thing that we need to create is our Task to compile our code. In the  **.vscode ** folder, create a new file and name it  **tasks.json.** Inside the file, write the following configuration.

```json
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "SDL2",
			"command": "D:\\sdk\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
			"args": [
				"-g",
				"src\\*.cpp",
				"-o",
				"build\\game.exe",
				"-ID:/sdk/sdl2/mingw/x86_64/include",
				"-LD:/sdk/sdl2/mingw/x86_64/lib",
				"-lmingw32",
				"-lSDL2main",
				"-lSDL2",
				"-mwindows"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}
```

**label** option is the name in which we are going to identify our task.

In the  **command** option, we need to put the path to our g++ installation, that is where we install mingw64.

In the  **args** options, we have some arguments that are important to notice. For example, `"build\\game.exe",` is the name of our game and the folder where we’re going to save it.

`"-ID:/sdk/sdl2/mingw/x86_64/include"` is where we save our SDL2 files, this is for SDL2 include libraries, the same as `"-LD:/sdk/sdl2/mingw/x86_64/lib",` is the path to SDL2 libraries.

`"-lmingw32"` is an option to compile with the mingw libraries, besides that said 32, we’re are using the 64 bits compiler. `"-lSDL2main",
				"-lSDL2",
				"-mwindows"` in this case, we are indicating to our compiler that we need to include the SDL2main and SDL2 libraries and we don’t want the command prompt when launching our game.

Now, we need to create a  **launch.json** file to launch our game and debugging.

```json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\build\\game.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\sdk\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enably pretty printing",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "SDL2"
        }
    ]
}
```

` "program": "${workspaceFolder}\\build\\game.exe",` is the executable that we are going to launch, in our case is the same that our game.

`"miDebuggerPath": "D:\\sdk\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe"` is the path where gdb.exe is.

`"preLaunchTask": "SDL2"` before launch our program, we need to run the task “SDL2”, this is the name that we put in the label option in tasks.json.

Now, as the last configuration step, we need to configure our C++ options to have a better Intellisense autocomplete and configuration of our compiler.

In Visual Studio Code, press  **CTRL** + **SHIFT** + **P** , write C/C++ and select **C/C++ Edit configurations (GUI)**. Here we’re are going to change some configurations.

In  **Compiler Path,** you need to put the path to your compiler. Like we put in the tasks.json and change the **IntelliSense mode** to  **gcc\_x64.** In **Include Path,** add the path to your SDL2 includes.

![](https://giovanni.codes/wp-content/uploads/2020/09/cpp-options.png)

Other options that you may change are **C Standard** and  **C++ Standard, ** If you save these configurations, you are going to have a file named  **c\_cpp\_properties.json** in your .vscode folder.

Now, you need to put the **SDL2.dll** file in the  **build** folder, to be able to run our SDL2 game.

Remember to use the version of your game

![](https://giovanni.codes/wp-content/uploads/2020/09/build-sdldll.png)

## Testing our SDL2 game.

Create a  **main.cpp** file in the  **src ** folder to test our environment, and write the next code

```cpp
#include <SDL2/SDL.h>
#include <iostream>

int main(int argv, char** args)
{
	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_Window *window = SDL_CreateWindow("Hello SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

	bool isRunning = true;
	SDL_Event event;

	while (isRunning)
	{
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_QUIT:
				isRunning = false;
				break;

			case SDL_KEYDOWN:
				if (event.key.keysym.sym == SDLK_ESCAPE)
				{
					isRunning = false;
				}
			}
		}

		SDL_RenderClear(renderer);
		SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

		SDL_RenderPresent(renderer);
	}

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}
```

Press  **F5 ** and cross your fingers. You should watch a red window, is you see it, then you are ready to work with SDL2 and VS Code!

![](https://giovanni.codes/wp-content/uploads/2020/09/sdl2-window-1024x754.png)

This is a simple configuration for start with SDL2, if you want to use SDL\_image, you should do the same steps to add the headers and libraries. I will continue with SDL2 and check if with this configuration can be have some problems, but for now, you are able to start working with SDL2.

The post [Setup SDL2 with Visual Studio Code and mingw64 on Windows](https://giovanni.codes/setup-sdl2-with-visual-studio-code-and-mingw64-on-windows/) appeared first on [Giovanni Codes](https://giovanni.codes).

 ![](http://feeds.feedburner.com/~r/giovannicodes/~4/g3DHeT91T-M)