]> git.localhorst.tv Git - blank.git/blobdiff - src/app/init.cpp
fix direct rendering
[blank.git] / src / app / init.cpp
index a56674c309506b5ac8f0876be5338b8241dfebf3..fea5653cf1cf6499bb378b22286169cf2c15fff8 100644 (file)
@@ -1,32 +1,84 @@
 #include "init.hpp"
 
 #include <algorithm>
+#include <alut.h>
 #include <SDL.h>
 #include <SDL_image.h>
-#include <stdexcept>
-#include <string>
+#include <SDL_net.h>
+#include <SDL_ttf.h>
 #include <GL/glew.h>
 
 
 namespace {
 
-void sdl_error(std::string msg) {
+std::string sdl_error_append(std::string msg) {
        const char *error = SDL_GetError();
        if (*error != '\0') {
                msg += ": ";
                msg += error;
                SDL_ClearError();
        }
-       throw std::runtime_error(msg);
+       return msg;
+}
+
+std::string net_error_append(std::string msg) {
+       const char *error = SDLNet_GetError();
+       if (*error != '\0') {
+               msg += ": ";
+               msg += error;
+       }
+       return msg;
+}
+
+std::string alut_error_append(ALenum num, std::string msg) {
+       const char *error = alutGetErrorString(num);
+       if (*error != '\0') {
+               msg += ": ";
+               msg += error;
+       }
+       return msg;
 }
 
 }
 
 namespace blank {
 
+AlutError::AlutError(ALenum num)
+: std::runtime_error(alutGetErrorString(num)) {
+
+}
+
+AlutError::AlutError(ALenum num, const std::string &msg)
+: std::runtime_error(alut_error_append(num, msg)) {
+
+}
+
+
+NetError::NetError()
+: std::runtime_error(SDLNet_GetError()) {
+
+}
+
+NetError::NetError(const std::string &msg)
+: std::runtime_error(net_error_append(msg)) {
+
+}
+
+
+SDLError::SDLError()
+: std::runtime_error(SDL_GetError()) {
+
+}
+
+SDLError::SDLError(const std::string &msg)
+: std::runtime_error(sdl_error_append(msg)) {
+
+}
+
+
 InitSDL::InitSDL() {
-       if (SDL_Init(SDL_INIT_VIDEO) != 0) {
-               sdl_error("SDL_Init(SDL_INIT_VIDEO)");
+       if (SDL_Init(SDL_INIT_EVENTS) != 0) {
+               throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
        }
 }
 
@@ -35,9 +87,22 @@ InitSDL::~InitSDL() {
 }
 
 
+InitVideo::InitVideo() {
+       if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
+               throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
+       }
+       // SDL seems to start out in text input state
+       SDL_StopTextInput();
+}
+
+InitVideo::~InitVideo() {
+       SDL_QuitSubSystem(SDL_INIT_VIDEO);
+}
+
+
 InitIMG::InitIMG() {
        if (IMG_Init(IMG_INIT_PNG) == 0) {
-               sdl_error("IMG_Init(IMG_INIT_PNG)");
+               throw SDLError("IMG_Init(IMG_INIT_PNG)");
        }
 }
 
@@ -46,29 +111,64 @@ InitIMG::~InitIMG() {
 }
 
 
+InitNet::InitNet() {
+       if (SDLNet_Init() != 0) {
+               throw SDLError("SDLNet_Init()");
+       }
+}
+
+InitNet::~InitNet() {
+       SDLNet_Quit();
+}
+
+
+InitTTF::InitTTF() {
+       if (TTF_Init() != 0) {
+               throw SDLError("TTF_Init()");
+       }
+}
+
+InitTTF::~InitTTF() {
+       TTF_Quit();
+}
+
+
+InitAL::InitAL() {
+       if (!alutInit(nullptr, nullptr)) {
+               throw AlutError(alutGetError(), "alutInit");
+       }
+}
+
+InitAL::~InitAL() {
+       if (!alutExit()) {
+               throw AlutError(alutGetError(), "alutExit");
+       }
+}
+
+
 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)");
+               throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
        }
        if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
-               sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
+               throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
        }
        if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
-               sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
+               throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
        }
 
-       if (double_buffer) {
-               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, 0) != 0) {
+                       throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
                }
        }
 
        if (sample_size > 1) {
                if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
-                       sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
+                       throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
                }
                if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
-                       sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
+                       throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
                }
        }
 }
@@ -82,7 +182,7 @@ Window::Window()
        SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
 )) {
        if (!handle) {
-               sdl_error("SDL_CreateWindow");
+               throw SDLError("SDL_CreateWindow");
        }
 }
 
@@ -100,70 +200,30 @@ void Window::ReleaseInput() {
 
 void Window::GrabMouse() {
        if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
-               sdl_error("SDL_SetRelativeMouseMode");
+               throw SDLError("SDL_SetRelativeMouseMode");
        }
 }
 
 void Window::ReleaseMouse() {
        if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
-               sdl_error("SDL_SetRelativeMouseMode");
+               throw SDLError("SDL_SetRelativeMouseMode");
        }
 }
 
-GLContext Window::CreateContext() {
-       return GLContext(handle);
-}
-
 void Window::Flip() {
        SDL_GL_SwapWindow(handle);
 }
 
 
 GLContext::GLContext(SDL_Window *win)
-: handle(SDL_GL_CreateContext(win)) {
-       if (!handle) {
-               sdl_error("SDL_GL_CreateContext");
+: ctx(SDL_GL_CreateContext(win)) {
+       if (!ctx) {
+               throw SDLError("SDL_GL_CreateContext");
        }
 }
 
 GLContext::~GLContext() {
-       if (handle) {
-               SDL_GL_DeleteContext(handle);
-       }
-}
-
-
-GLContext::GLContext(GLContext &&other)
-: handle(other.handle) {
-       other.handle = nullptr;
-}
-
-GLContext &GLContext::operator =(GLContext &&other) {
-       std::swap(handle, other.handle);
-       return *this;
-}
-
-void GLContext::EnableVSync() {
-       if (SDL_GL_SetSwapInterval(1) != 0) {
-               sdl_error("SDL_GL_SetSwapInterval");
-       }
-}
-
-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);
+       SDL_GL_DeleteContext(ctx);
 }
 
 
@@ -182,4 +242,22 @@ InitGLEW::InitGLEW() {
        }
 }
 
+
+InitHeadless::InitHeadless()
+: init_sdl()
+, init_net() {
+
+}
+
+Init::Init(bool double_buffer, int sample_size)
+: init_video()
+, init_img()
+, init_ttf()
+, init_gl(double_buffer, sample_size)
+, window()
+, ctx(window.Handle())
+, init_glew() {
+
+}
+
 }