SDL - Installation (Windows) (With C)

How to setup SDL and create a window.

Updated December 29, 2022

Making Money with Django and AI: How to Build SaaS Services Using Python

Learn how to build AI-driven websites with the Django web framework and monetize your services.
Read More
You will receive the book in PDF and ePub formats.

Table of contents

Step-by-step

Create a project directory:

mkdir project
cd project

Create libs folder in the project root:

mkdir libs

Download SDL2-devel-2.26.1-VC.zip and extract it to the libs folder (it has a folder called SDL2-2.26.1 in it).

  • This package allows us to build the project using cl.exe (a command line tool for the Microsoft Visual C++ Compiler [MSVC]).
  • How to access cl.exe? Install Visual Studio, select C++ workload and launch Developer Command Prompt.

Copy SDL2.dll to the project root:

copy libs\SDL2-2.26.1\lib\x64\SDL2.dll .

You should now have this kind of folder structure:

project
│ SDL2.dll
└─libs
	└───SDL2-2.26.1
		├─── ...
		├───include
		└───lib
			├───x64
			└───x86

Create main.c in the project root and add these lines to it:

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

int main(int argc, char *args[]) {
	SDL_Init(SDL_INIT_VIDEO);
	return 0;
}
  • SDL_Init() initializes subsystems. Some systems are enabled by default. Some of them you have to initialize manually (e.g. video and audio). SDL functions might also enable uninitialized systems if needed.

Create build.bat and add these lines to it:

@echo off
cl main.c ^
/Fea.exe /Zi /nologo ^
/Ilibs\SDL2-2.26.1\include ^
/link ^
/SUBSYSTEM:console ^
/LIBPATH:libs\SDL2-2.26.1\lib\x64 ^
SDL2.lib SDL2main.lib shell32.lib
  • include folder contains the SDL.h header.
  • /subsystem:console provides a console. This allows us to printf() to terminal from the app.
  • lib\x64 folder contains the two library files we will use: SDL2.lib and SDL2main.lib.
  • SDL2main.lib allows us to use the program entry point int main(int argc, char* argv[]) in all platforms. SDL redefines your main() to SDL_main(), runs its own platform specific initialization and calls SDL_main().
  • SDL2main.lib needs shell32.lib in Windows (for CommandLineToArgvW()). This is probably added automatically if you are using Visual Studio.

Compile and run the program:

build.bat && a.exe

It should compile and run but we don't have a window yet.

Add these lines:

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

int main(int argc, char *args[]) {
	
	SDL_Init(SDL_INIT_VIDEO);

	// Create a window
	
	SDL_Window *Window =
		SDL_CreateWindow(
			"Title",
			SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
			640, 640, 0
			);

        
	// Add a loop

	int Running = 1;
	SDL_Event Event;
    
	while(Running) {
        while(SDL_PollEvent(&Event)) {
            switch(Event.type) {
                case SDL_QUIT: {
                    Running = 0;
                } break;
                case SDL_KEYDOWN: {
                    switch(Event.key.keysym.sym) {
                        case SDLK_o: {
                            Running = 0;
                        } break;
                    }
                } break;
            }
        }
    }

	SDL_Quit();
	
	return 0;
}
  • SDL_Window is a struct that holds information about a window.
  • SDL_CreateWindow() creates a window and returns a pointer to it.
  • SDL_Event is used to store data about events. It's a union of all event types.
  • SDL_PollEvent gets events from the event queue.
  • SDL_QUIT event happens for multiple reasons (e.g. when you click the window close button).
  • SDL_Quit() cleans up all subsystems.

Compile and run. You should see a window.

main.c

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

int main(int argc, char *argv[]) {
	
    SDL_Init(SDL_INIT_VIDEO);
    
    SDL_Window *Window = 
        SDL_CreateWindow("Title", 
                         SDL_WINDOWPOS_CENTERED,
                         SDL_WINDOWPOS_CENTERED,
                         640, 640, 0);
    
    int Running = 1;
    
    SDL_Event Event = {0};
    
    while(Running) {
        while(SDL_PollEvent(&Event)) {
            switch(Event.type) {
                case SDL_QUIT: {
                    Running = 0;
                } break;
                case SDL_KEYDOWN: {
                    switch(Event.key.keysym.sym) {
                        case SDLK_o: {
                            Running = 0;
                        } break;
                    }
                } break;
            }
        }
    }
    
    SDL_Quit();
    
	return 0;
}

Leave a comment

You can use Markdown to format your comment.