12 void sdl_error(std::string msg) {
13 const char *error = SDL_GetError();
19 throw std::runtime_error(msg);
27 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
28 sdl_error("SDL_Init(SDL_INIT_VIDEO)");
38 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
39 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
41 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
42 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
44 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
45 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
48 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
49 sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
59 : handle(SDL_CreateWindow(
61 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
63 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
66 sdl_error("SDL_CreateWindow");
71 SDL_DestroyWindow(handle);
74 GLContext Window::CreateContext() {
75 return GLContext(handle);
79 SDL_GL_SwapWindow(handle);
83 GLContext::GLContext(SDL_Window *win)
84 : handle(SDL_GL_CreateContext(win)) {
86 sdl_error("SDL_GL_CreateContext");
90 GLContext::~GLContext() {
92 SDL_GL_DeleteContext(handle);
97 GLContext::GLContext(GLContext &&other)
98 : handle(other.handle) {
99 other.handle = nullptr;
102 GLContext &GLContext::operator =(GLContext &&other) {
103 std::swap(handle, other.handle);
107 void GLContext::EnableVSync() {
108 if (SDL_GL_SetSwapInterval(1) != 0) {
109 sdl_error("SDL_GL_SetSwapInterval");
114 InitGLEW::InitGLEW() {
115 glewExperimental = GL_TRUE;
116 GLenum glew_err = glewInit();
117 if (glew_err != GLEW_OK) {
118 std::string msg("glewInit: ");
119 const GLubyte *errBegin = glewGetErrorString(glew_err);
120 const GLubyte *errEnd = errBegin;
121 while (*errEnd != '\0') {
124 msg.append(errBegin, errEnd);
125 throw std::runtime_error(msg);
129 InitGLEW::~InitGLEW() {