]> git.localhorst.tv Git - blank.git/blobdiff - src/init.cpp
minor optimizations in chunk
[blank.git] / src / init.cpp
index 9010e00ba6b26c50d71514a96d48332f78d94d3c..a56674c309506b5ac8f0876be5338b8241dfebf3 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <algorithm>
 #include <SDL.h>
+#include <SDL_image.h>
 #include <stdexcept>
 #include <string>
 #include <GL/glew.h>
@@ -34,7 +35,18 @@ InitSDL::~InitSDL() {
 }
 
 
-InitGL::InitGL() {
+InitIMG::InitIMG() {
+       if (IMG_Init(IMG_INIT_PNG) == 0) {
+               sdl_error("IMG_Init(IMG_INIT_PNG)");
+       }
+}
+
+InitIMG::~InitIMG() {
+       IMG_Quit();
+}
+
+
+InitGL::InitGL(bool double_buffer, int sample_size) {
        if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
                sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
        }
@@ -45,13 +57,20 @@ InitGL::InitGL() {
                sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
        }
 
-       if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
-               sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
+       if (double_buffer) {
+               if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
+                       sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
+               }
        }
-}
-
-InitGL::~InitGL() {
 
+       if (sample_size > 1) {
+               if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
+                       sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
+               }
+               if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
+                       sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
+               }
+       }
 }
 
 
@@ -71,6 +90,26 @@ Window::~Window() {
        SDL_DestroyWindow(handle);
 }
 
+void Window::GrabInput() {
+       SDL_SetWindowGrab(handle, SDL_TRUE);
+}
+
+void Window::ReleaseInput() {
+       SDL_SetWindowGrab(handle, SDL_FALSE);
+}
+
+void Window::GrabMouse() {
+       if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
+               sdl_error("SDL_SetRelativeMouseMode");
+       }
+}
+
+void Window::ReleaseMouse() {
+       if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
+               sdl_error("SDL_SetRelativeMouseMode");
+       }
+}
+
 GLContext Window::CreateContext() {
        return GLContext(handle);
 }
@@ -110,6 +149,23 @@ void GLContext::EnableVSync() {
        }
 }
 
+void GLContext::EnableDepthTest() noexcept {
+       glEnable(GL_DEPTH_TEST);
+       glDepthFunc(GL_LESS);
+}
+
+void GLContext::EnableBackfaceCulling() noexcept {
+       glEnable(GL_CULL_FACE);
+}
+
+void GLContext::Clear() noexcept {
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+}
+
+void GLContext::ClearDepthBuffer() noexcept {
+       glClear(GL_DEPTH_BUFFER_BIT);
+}
+
 
 InitGLEW::InitGLEW() {
        glewExperimental = GL_TRUE;
@@ -126,8 +182,4 @@ InitGLEW::InitGLEW() {
        }
 }
 
-InitGLEW::~InitGLEW() {
-
-}
-
 }