14 void sdl_error(std::string msg) {
15 const char *error = SDL_GetError();
21 throw std::runtime_error(msg);
29 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
30 sdl_error("SDL_Init(SDL_INIT_VIDEO)");
40 if (IMG_Init(IMG_INIT_PNG) == 0) {
41 sdl_error("IMG_Init(IMG_INIT_PNG)");
51 if (TTF_Init() != 0) {
52 sdl_error("TTF_Init()");
61 InitGL::InitGL(bool double_buffer, int sample_size) {
62 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
63 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
65 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
66 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
68 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
69 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
73 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
74 sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
78 if (sample_size > 1) {
79 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
80 sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
82 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
83 sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
90 : handle(SDL_CreateWindow(
92 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
94 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
97 sdl_error("SDL_CreateWindow");
102 SDL_DestroyWindow(handle);
105 void Window::GrabInput() {
106 SDL_SetWindowGrab(handle, SDL_TRUE);
109 void Window::ReleaseInput() {
110 SDL_SetWindowGrab(handle, SDL_FALSE);
113 void Window::GrabMouse() {
114 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
115 sdl_error("SDL_SetRelativeMouseMode");
119 void Window::ReleaseMouse() {
120 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
121 sdl_error("SDL_SetRelativeMouseMode");
125 GLContext Window::CreateContext() {
126 return GLContext(handle);
129 void Window::Flip() {
130 SDL_GL_SwapWindow(handle);
134 GLContext::GLContext(SDL_Window *win)
135 : handle(SDL_GL_CreateContext(win)) {
137 sdl_error("SDL_GL_CreateContext");
141 GLContext::~GLContext() {
143 SDL_GL_DeleteContext(handle);
148 GLContext::GLContext(GLContext &&other)
149 : handle(other.handle) {
150 other.handle = nullptr;
153 GLContext &GLContext::operator =(GLContext &&other) {
154 std::swap(handle, other.handle);
158 void GLContext::EnableVSync() {
159 if (SDL_GL_SetSwapInterval(1) != 0) {
160 sdl_error("SDL_GL_SetSwapInterval");
164 void GLContext::EnableDepthTest() noexcept {
165 glEnable(GL_DEPTH_TEST);
166 glDepthFunc(GL_LESS);
169 void GLContext::EnableBackfaceCulling() noexcept {
170 glEnable(GL_CULL_FACE);
173 void GLContext::EnableAlphaBlending() noexcept {
175 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
178 void GLContext::DisableAlphaBlending() noexcept {
182 void GLContext::Clear() noexcept {
183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
186 void GLContext::ClearDepthBuffer() noexcept {
187 glClear(GL_DEPTH_BUFFER_BIT);
191 InitGLEW::InitGLEW() {
192 glewExperimental = GL_TRUE;
193 GLenum glew_err = glewInit();
194 if (glew_err != GLEW_OK) {
195 std::string msg("glewInit: ");
196 const GLubyte *errBegin = glewGetErrorString(glew_err);
197 const GLubyte *errEnd = errBegin;
198 while (*errEnd != '\0') {
201 msg.append(errBegin, errEnd);
202 throw std::runtime_error(msg);