Set up SDL2 in Dev C++
Last updated: September 6, 2021
-
Download SDL2-devel-2.0.16-mingw.tar.gz and extract them to any where you want. For example: C:\dev_lib
-
In Dev C++, create an Empty project: File > new > Project. Select "empty project".
-
In Tools > Compiler Options > Directories > C++ includes tab:
add: C:\dev_lib\SDL2-2.0.16\i686-w64-mingw32\include\SDL2 -
Then in Libraries tab to the left:
add: C:\dev_lib\SDL2-2.0.16\i686-w64-mingw32\lib -
Now in Project > Project options > Parameters Tab > Linker:
add: -lmingw32 -lSDL2main -lSDL2 -
Then in Directories Tab > Include Directories:
add: C:\dev_lib\SDL2-2.0.16\i686-w64-mingw32\include\SDL2 -
In Library Directories: add: C:\dev_lib\SDL2-2.0.16\i686-w64-mingw32\lib
-
Copy the SDL2.dll file from C:\dev_lib\SDL2-2.0.16\i686-w64-mingw32\bin to, where your project executable will run.
Now compile and run this SDL code:
#include <SDL.h>
#include <iostream>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char* args[]){
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if (SDL_Init( SDL_INIT_VIDEO ) < 0){
printf( "SDL initialize error: %s\n", SDL_GetError());
} else {
window = SDL_CreateWindow(
"SDL",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == NULL){
printf("Window could not be created: %s\n", SDL_GetError());
}
else {
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(
screenSurface,
NULL,
SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
SDL_UpdateWindowSurface(window);
SDL_Delay(2000);
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}