summaryrefslogtreecommitdiff
path: root/src/sdl_context.cpp
blob: 7601d7294dee83e5aecefde72d0c4508772ea315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "sdl_context.hpp"
#include <fud_assert.hpp>

#include <SDL.h>
#include <cassert>
#include <stdexcept>

namespace bookmouse {

using fud::assertFail;

SdlContext::SdlContext() {
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
        const auto* lastError = SDL_GetError();
        throw std::runtime_error{lastError};
    }

    // Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
    // GL ES 2.0 + GLSL 100
    setAttribute(SDL_GL_CONTEXT_FLAGS, 0);
    setAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    setAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    setAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(__APPLE__)
    // GL 3.2 Core + GLSL 150
    setAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
    setAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    setAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    setAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else
    // GL 3.0 + GLSL 130
    setAttribute(SDL_GL_CONTEXT_FLAGS, 0);
    setAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    setAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    setAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif

    // From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UI
    auto hintSet = SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
    fudAssert(hintSet == SDL_TRUE);
#endif
}

SdlContext::~SdlContext() {
    SDL_Quit();
}

int SdlContext::setAttribute(SDL_GLattr attr, int value) const
{
    auto result = SDL_GL_SetAttribute(attr, value);
    assert(result == 0);
    return result;
}

} // namespace bookmouse