13 void sdl_error(std::string msg) {
14 const char *error = SDL_GetError();
20 throw std::runtime_error(msg);
28 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
29 sdl_error("SDL_Init(SDL_INIT_VIDEO)");
39 if (IMG_Init(IMG_INIT_PNG) == 0) {
40 sdl_error("IMG_Init(IMG_INIT_PNG)");
50 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
51 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
53 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
54 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
56 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
57 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
60 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
61 sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
71 : handle(SDL_CreateWindow(
73 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
75 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
78 sdl_error("SDL_CreateWindow");
83 SDL_DestroyWindow(handle);
86 GLContext Window::CreateContext() {
87 return GLContext(handle);
91 SDL_GL_SwapWindow(handle);
95 GLContext::GLContext(SDL_Window *win)
96 : handle(SDL_GL_CreateContext(win)) {
98 sdl_error("SDL_GL_CreateContext");
102 GLContext::~GLContext() {
104 SDL_GL_DeleteContext(handle);
109 GLContext::GLContext(GLContext &&other)
110 : handle(other.handle) {
111 other.handle = nullptr;
114 GLContext &GLContext::operator =(GLContext &&other) {
115 std::swap(handle, other.handle);
119 void GLContext::EnableVSync() {
120 if (SDL_GL_SetSwapInterval(1) != 0) {
121 sdl_error("SDL_GL_SetSwapInterval");
126 InitGLEW::InitGLEW() {
127 glewExperimental = GL_TRUE;
128 GLenum glew_err = glewInit();
129 if (glew_err != GLEW_OK) {
130 std::string msg("glewInit: ");
131 const GLubyte *errBegin = glewGetErrorString(glew_err);
132 const GLubyte *errEnd = errBegin;
133 while (*errEnd != '\0') {
136 msg.append(errBegin, errEnd);
137 throw std::runtime_error(msg);
141 InitGLEW::~InitGLEW() {