Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save helderc/46210828f19e9e7ceee1962df1edfa2b to your computer and use it in GitHub Desktop.
Save helderc/46210828f19e9e7ceee1962df1edfa2b to your computer and use it in GitHub Desktop.
Setting up SDL2 for development on Windows with MSYS2 and VS Code

Setting up SDL2 for development on Windows with MSYS2 and VS Code

Hello! I made this document for anyone trying to setup an enviroment for SDL2 on Windows using VS Code for building and debugging.

This is just a basic setup to test everything works and compiles correctly. This is adapted from this guide by Adam Richardson, made to work with the UCRT enviroment.

So, let's get to it.

Download and install MSYS2 64bit

You can download from here: https://www.msys2.org/ Then just follow the instructions. Provided on the official website for instalation.

*Tip: When adding the "C:\msys64\ucrt64\bin" to PATH, do so at the top of the list to avoid issues when building and linking the compiler.

Update

After the installation is complete, and you have added your enviroment to PATH, open a terminal, (to integrate MSYS2 with Windows Terminal you can read this), and then update your packages with this command.

  pacman -Syuu

Repeat a few times until everything is up to date, note that your terminal might restart (or straight up close) during this process. Just open it up again and execute the command again to make sure everything is up to date.

Install necesarry build tools on MSYS2

Run the following command on the UCRT shell:

  pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain mingw-w64-ucrt-x86_64-SDL2 mingw-w64-ucrt-x86_64-SDL2_image mingw-w64-ucrt-x86_64-SDL2_sound mingw-w64-ucrt-x86_64-SDL2_mixer mingw-w64-ucrt-x86_64-SDL2_gfx mingw-w64-ucrt-x86_64-SDL2_net mingw-w64-ucrt-x86_64-SDL2_ttf mingw-w64-ucrt-x86_64-cmake make

This will all the packages you might need for building C/C++ programs, and the SDL2 base library and extension libraries you might need for development.

Test that everything works

Open up a terminal and execute the following command:

$ gcc --version
gcc.exe (Rev2, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Note that here the output is included.

You can do the same with, g++ --version and gdb --version, to check if they are working, you should get similar results.

VS Code

For VS Code you need to have the C/C++ Extension installed. After everything is setup, you can create a directory, then create a new file called hello.c and open it in VS Code, then hit Ctrl+Shift+P to open the command palette, and search for edit configurations (UI), there you can select the path to your compiler, and create a configuration for SDL, you can also create one using JSON like this:

c_pp_properties.json (Extract)

...
{
  "name": "SDL2",
  "includePath": [
      "${workspaceFolder}/**",
      "C:\\msys64\\ucrt64\\include"
  ],
  "defines": [
      "_DEBUG",
      "UNICODE",
      "_UNICODE"
  ],
  "windowsSdkVersion": "10.0.22621.0",
  "cStandard": "c17",
  "cppStandard": "c++17",
  "intelliSenseMode": "windows-gcc-x64",
  "compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
  "compilerArgs": [
    "-IC:/msys64/ucrt64/bin/../include",
    "-IC:/msys64/ucrt64/bin/../include/SDL2",
    "-Dmain=SDL_main",
    "-LC:/msys64/ucrt64/bin/../lib",
    "-lmingw32", 
    "-mwindows", 
    "-lSDL2main",
    "-lSDL2"
    ]
}

As you can see, my config has a compilerArgs property, if you are compiling straight from the MSYS2 terminal, you can just use

For C:

gcc hello.c -o hello $(pkg-config --cflags --libs sdl2)

For C++:

g++ hello.c -o hello $(pkg-config --cflags --libs sdl2)

That last bit, executes the pkg-config command and appends everything your linker needs to know what libraries you want to use. The process for VS Code though, it's a bit more manual.

In a power shell terminal execute pkg-config --cflags --libs sdl2

You should see an output similar to this: -IC:/msys64/ucrt64/bin/../include -IC:/msys64/ucrt64/bin/../include/SDL2 -Dmain=SDL_main -LC:/msys64/ucrt64/bin/../lib -lmingw32 -mwindows -lSDL2main -lSDL2

Just copy it, and use it for your compiler args, separating each argument as shown above.

Then paste this into "hello.c"

hello.c

#include <SDL2/SDL.h>
#include <stdio.h>

const int WIN_WIDTH = 800, WIN_HEIGHT = 600;

int main(int argc, char* argv[])
{

  SDL_Window* window = NULL;
  SDL_Renderer* renderer = NULL;

  // Video Initialization
  SDL_InitSubSystem(SDL_INIT_VIDEO);
  if (!SDL_WasInit(SDL_INIT_VIDEO))
  {
    printf("SDL video was not initialized. SDL_Error: %s\n", SDL_GetError());
    return -1;
  }
  printf("Video was initialized succesfully!\n");

  window = SDL_CreateWindow(
    "SDL2 Window", // Window label
    SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, // Position of the window in screen.
    WIN_WIDTH, WIN_HEIGHT,
    0 // Extra flags, defaults SDL_WINDOW_SHOWN to true if 0
  );
  if (!window)
  {
    printf("SDL window was not created. SDL_Error: %s\n", SDL_GetError());
    return -1;
  }

  renderer = SDL_CreateRenderer(window, -1, 0);
  if (!renderer)
  {
    printf("SDL renderer was not created. SDL_Error: %s\n", SDL_GetError());;
    return -1;
  }

  // rendering steps
  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  SDL_RenderClear(renderer); // fills the screen witn draw color
  SDL_RenderPresent(renderer); // updates the screen

  SDL_Event event;

  while (1)
  {
    SDL_PollEvent(&event);
    if (event.type == SDL_QUIT)
    {
      break;
    }
  }

  // Clear everything before quiting.
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

This code just initializes SDL and creates a window with a white background.

Now you should be able to configure a build task like this one

tasks.json

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe build active file",
      "command": "C:/msys64/ucrt64/bin/g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-IC:/msys64/ucrt64/bin/../include",
        "-IC:/msys64/ucrt64/bin/../include/SDL2",
        "-Dmain=SDL_main",
        "-LC:/msys64/ucrt64/bin/../lib",
        "-lmingw32",
        "-mwindows",
        "-lSDL2main",
        "-lSDL2"
      ],
      "options": {
        "cwd": "C:/msys64/ucrt64/bin"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ],
  "version": "2.0.0"
}

And that's it, you should now be able to build and run and debug your project from within VS Code!

Thanks for reading!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment