-
-
Save sharavsambuu/a7c3f5a3142ceabdcd1cb491a3bd3c17 to your computer and use it in GitHub Desktop.
SDL2 Hello World | SDL2 Getting Started | SDL | OpenGL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SDL2 Hello, World! | |
| // This should display a white screen for 2 seconds | |
| // | |
| // sudo apt install libsdl2-dev | |
| // sudo apt install clang | |
| // | |
| // clang main.c -o hello_sdl2_clang -lSDL2 | |
| // clang++ main.cpp -o hello_sdl2_clangpp -lSDL2 | |
| // gcc main.c -o hello_sdl2_gcc -lSDL2 | |
| // g++ main.cpp -o hello_sdl2_gpp -lSDL2 | |
| #include <SDL2/SDL.h> | |
| #include <stdio.h> | |
| #define SCREEN_WIDTH 640 | |
| #define SCREEN_HEIGHT 480 | |
| int main(int argc, char* args[]) { | |
| SDL_Window* window = NULL; | |
| SDL_Surface* screenSurface = NULL; | |
| if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
| fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError()); | |
| return 1; | |
| } | |
| window = SDL_CreateWindow( | |
| "hello_sdl2", | |
| SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
| SCREEN_WIDTH, SCREEN_HEIGHT, | |
| SDL_WINDOW_SHOWN | |
| ); | |
| if (window == NULL) { | |
| fprintf(stderr, "could not create window: %s\n", SDL_GetError()); | |
| return 1; | |
| } | |
| screenSurface = SDL_GetWindowSurface(window); | |
| SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); | |
| SDL_UpdateWindowSurface(window); | |
| SDL_version compiled; | |
| SDL_version linked; | |
| SDL_VERSION(&compiled); | |
| SDL_GetVersion(&linked); | |
| printf("We compiled against SDL version %d.%d.%d ...\n", | |
| compiled.major, compiled.minor, compiled.patch); | |
| printf("But we are linking against SDL version %d.%d.%d.\n", | |
| linked.major, linked.minor, linked.patch); | |
| SDL_Delay(2000); | |
| SDL_DestroyWindow(window); | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment