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)");
49 InitGL::InitGL(bool double_buffer, int sample_size) {
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)");
61 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
62 sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
66 if (sample_size > 1) {
67 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
68 sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
70 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
71 sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
78 : handle(SDL_CreateWindow(
80 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
82 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
85 sdl_error("SDL_CreateWindow");
90 SDL_DestroyWindow(handle);
93 void Window::GrabInput() {
94 SDL_SetWindowGrab(handle, SDL_TRUE);
97 void Window::ReleaseInput() {
98 SDL_SetWindowGrab(handle, SDL_FALSE);
101 void Window::GrabMouse() {
102 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
103 sdl_error("SDL_SetRelativeMouseMode");
107 void Window::ReleaseMouse() {
108 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
109 sdl_error("SDL_SetRelativeMouseMode");
113 GLContext Window::CreateContext() {
114 return GLContext(handle);
117 void Window::Flip() {
118 SDL_GL_SwapWindow(handle);
122 GLContext::GLContext(SDL_Window *win)
123 : handle(SDL_GL_CreateContext(win)) {
125 sdl_error("SDL_GL_CreateContext");
129 GLContext::~GLContext() {
131 SDL_GL_DeleteContext(handle);
136 GLContext::GLContext(GLContext &&other)
137 : handle(other.handle) {
138 other.handle = nullptr;
141 GLContext &GLContext::operator =(GLContext &&other) {
142 std::swap(handle, other.handle);
146 void GLContext::EnableVSync() {
147 if (SDL_GL_SetSwapInterval(1) != 0) {
148 sdl_error("SDL_GL_SetSwapInterval");
152 void GLContext::EnableDepthTest() {
153 glEnable(GL_DEPTH_TEST);
154 glDepthFunc(GL_LESS);
157 void GLContext::EnableBackfaceCulling() {
158 glEnable(GL_CULL_FACE);
161 void GLContext::Clear() {
162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
165 void GLContext::ClearDepthBuffer() {
166 glClear(GL_DEPTH_BUFFER_BIT);
170 InitGLEW::InitGLEW() {
171 glewExperimental = GL_TRUE;
172 GLenum glew_err = glewInit();
173 if (glew_err != GLEW_OK) {
174 std::string msg("glewInit: ");
175 const GLubyte *errBegin = glewGetErrorString(glew_err);
176 const GLubyte *errEnd = errBegin;
177 while (*errEnd != '\0') {
180 msg.append(errBegin, errEnd);
181 throw std::runtime_error(msg);