Skip to content

SDL

1
find_package(SDL2 QUIET)

1
2
3
4
5
if (SDL2_FOUND)
    if (NOT TARGET glad AND EXPECTED_COMPILER)
        FetchContent_Declare(glad GIT_REPOSITORY https://github.com/Dav1dde/glad.git GIT_TAG df8e9e16110b305479a875399cee13daa0ccadd9)
        FetchContent_MakeAvailable(glad)
    endif ()

1
2
3
4
5
6
    if (SDL2_FOUND AND EXPECTED_COMPILER)
        add_executable(sdl_hello sdl_hello.cpp)
        target_link_libraries(sdl_hello PUBLIC ${SDL2_LIBRARIES} glad)
        target_include_directories(sdl_hello PRIVATE ${SDL2_INCLUDE_DIRS})
    endif ()
endif ()

Share Snippets


1
2
3
4
5
6
7
8
#include <glad/glad.h>
// SDL
#include <SDL.h>
#include <SDL_syswm.h>

#if defined(__APPLE__)
#include "TargetConditionals.h"
#endif

1
2
3
4
5
6
7
8
9
// (Some versions of SDL before <2.0.10 appears to have performance/stalling
// issues on a minority of Windows systems, depending on whether
// SDL_INIT_GAMECONTROLLER is enabled or disabled.. updating to latest
// version of SDL is recommended!)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) !=
    0) {
    printf("Error: %s\n", SDL_GetError());
    return -1;
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#if __APPLE__
    // GL 3.2 Core + GLSL 150
    // const char *glsl_version = "#version 150";
    SDL_GL_SetAttribute(
        SDL_GL_CONTEXT_FLAGS,
        SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
                        SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else
    // GL 3.0 + GLSL 130
    const char *glsl_version = "#version 130";
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
                        SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(
    SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);

SDL_Window *window =
    SDL_CreateWindow("SDL2 Example", SDL_WINDOWPOS_CENTERED,
                     SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync

1
2
3
4
5
bool err = gladLoadGL() == 0;
if (err) {
    fprintf(stderr, "Failed to initialize OpenGL loader!\n");
    return 1;
}

1
2
bool done = false;
while (!done) {

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
SDL_Event event;
while (SDL_PollEvent(&event)) {
    if (event.type == SDL_QUIT) {
        done = true;
    }
    if (event.type == SDL_WINDOWEVENT &&
        event.window.event == SDL_WINDOWEVENT_CLOSE &&
        event.window.windowID == SDL_GetWindowID(window)) {
        done = true;
    }
}

1
2
3
4
5
    glViewport(0, 0, 600, 400);
    glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
    glClear(GL_COLOR_BUFFER_BIT);
    SDL_GL_SwapWindow(window);
}

1
2
3
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();

Share Snippets