Skip to content

Instantly share code, notes, and snippets.

@Mulperi
Last active July 1, 2021 07:44
Show Gist options
  • Save Mulperi/9b4942442c3757548ea7d7337a9b21e0 to your computer and use it in GitHub Desktop.
Save Mulperi/9b4942442c3757548ea7d7337a9b21e0 to your computer and use it in GitHub Desktop.
SDL2, easy setup on Windows

SDL2 quick and dirty setup on Windows and MinGW

  • Download development library from https://www.libsdl.org/download-2.0.php
  • From extracted SDL2/i686-w64-mingw32/include folder, copy the SDL2 folder to your project/include (so that inside your project you will have include/SDL2).
  • From SDL2/i686-w64-mingw32/bin folder, copy SDL2.dll to your project root.
  • From SDL2/i686-w64-mingw32/lib folder, copy libSDL2main.a to your project root.

In your project root, create main.cpp with this code:

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

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

    SDL_Window *window;                    // Declare a pointer

    SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        "An SDL2 window",                  // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );

    // Check that the window was successfully created
    if (window == NULL) {
        // In the case that the window could not be made...
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    // The window is open: could enter program loop here (see SDL_PollEvent())

    SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

    // Close and destroy the window
    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}

Save the file and run: g++ main.cpp -o main.exe -I include -L . -lmingw32 -lSDL2 -lSDL2main. Now you have main.exe which you can run and see the window open for 3 seconds.

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