X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Finit.cpp;h=1cc0c2485b50db7e79a5fdd5baf242ef4201a72d;hb=5d2da8a07411ad6417d6ed8d1be997189cf5ce89;hp=9c97c9f4b31432b212149efc1e2d30338eb317d0;hpb=37a1465a83e4ac4363ed0d8e0fa1ce5055dd2db4;p=blank.git diff --git a/src/app/init.cpp b/src/app/init.cpp index 9c97c9f..1cc0c24 100644 --- a/src/app/init.cpp +++ b/src/app/init.cpp @@ -4,30 +4,39 @@ #include #include #include -#include -#include #include 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; } } namespace blank { +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)"); + throw SDLError("SDL_Init(SDL_INIT_VIDEO)"); } } @@ -38,7 +47,7 @@ InitSDL::~InitSDL() { InitIMG::InitIMG() { if (IMG_Init(IMG_INIT_PNG) == 0) { - sdl_error("IMG_Init(IMG_INIT_PNG)"); + throw SDLError("IMG_Init(IMG_INIT_PNG)"); } } @@ -49,7 +58,7 @@ InitIMG::~InitIMG() { InitTTF::InitTTF() { if (TTF_Init() != 0) { - sdl_error("TTF_Init()"); + throw SDLError("TTF_Init()"); } } @@ -60,27 +69,27 @@ InitTTF::~InitTTF() { 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)"); + throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)"); } } 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)"); } } } @@ -94,7 +103,7 @@ Window::Window() SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE )) { if (!handle) { - sdl_error("SDL_CreateWindow"); + throw SDLError("SDL_CreateWindow"); } } @@ -112,84 +121,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::EnableAlphaBlending() noexcept { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -} - -void GLContext::EnableInvertBlending() noexcept { - glEnable(GL_BLEND); - glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); -} - -void GLContext::DisableBlending() noexcept { - glDisable(GL_BLEND); -} - -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); } @@ -208,4 +163,16 @@ InitGLEW::InitGLEW() { } } + +Init::Init(bool double_buffer, int sample_size) +: init_sdl() +, init_img() +, init_ttf() +, init_gl(double_buffer, sample_size) +, window() +, ctx(window.Handle()) +, init_glew() { + +} + }