]> git.localhorst.tv Git - blank.git/blob - src/init.cpp
moved some stuff around
[blank.git] / src / init.cpp
1 #include "init.hpp"
2
3 #include <algorithm>
4 #include <SDL.h>
5 #include <stdexcept>
6 #include <string>
7 #include <GL/glew.h>
8
9
10 namespace {
11
12 void sdl_error(std::string msg) {
13         const char *error = SDL_GetError();
14         if (*error != '\0') {
15                 msg += ": ";
16                 msg += error;
17                 SDL_ClearError();
18         }
19         throw std::runtime_error(msg);
20 }
21
22 }
23
24 namespace blank {
25
26 InitSDL::InitSDL() {
27         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
28                 sdl_error("SDL_Init(SDL_INIT_VIDEO)");
29         }
30 }
31
32 InitSDL::~InitSDL() {
33         SDL_Quit();
34 }
35
36
37 InitGL::InitGL() {
38         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
39                 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
40         }
41         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
42                 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
43         }
44         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
45                 sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
46         }
47
48         if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
49                 sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
50         }
51 }
52
53 InitGL::~InitGL() {
54
55 }
56
57
58 Window::Window()
59 : handle(SDL_CreateWindow(
60         "blank",
61         SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
62         960, 600,
63         SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
64 )) {
65         if (!handle) {
66                 sdl_error("SDL_CreateWindow");
67         }
68 }
69
70 Window::~Window() {
71         SDL_DestroyWindow(handle);
72 }
73
74 GLContext Window::CreateContext() {
75         return GLContext(handle);
76 }
77
78 void Window::Flip() {
79         SDL_GL_SwapWindow(handle);
80 }
81
82
83 GLContext::GLContext(SDL_Window *win)
84 : handle(SDL_GL_CreateContext(win)) {
85         if (!handle) {
86                 sdl_error("SDL_GL_CreateContext");
87         }
88 }
89
90 GLContext::~GLContext() {
91         if (handle) {
92                 SDL_GL_DeleteContext(handle);
93         }
94 }
95
96
97 GLContext::GLContext(GLContext &&other)
98 : handle(other.handle) {
99         other.handle = nullptr;
100 }
101
102 GLContext &GLContext::operator =(GLContext &&other) {
103         std::swap(handle, other.handle);
104         return *this;
105 }
106
107 void GLContext::EnableVSync() {
108         if (SDL_GL_SetSwapInterval(1) != 0) {
109                 sdl_error("SDL_GL_SetSwapInterval");
110         }
111 }
112
113
114 InitGLEW::InitGLEW() {
115         glewExperimental = GL_TRUE;
116         GLenum glew_err = glewInit();
117         if (glew_err != GLEW_OK) {
118                 std::string msg("glewInit: ");
119                 const GLubyte *errBegin = glewGetErrorString(glew_err);
120                 const GLubyte *errEnd = errBegin;
121                 while (*errEnd != '\0') {
122                         ++errEnd;
123                 }
124                 msg.append(errBegin, errEnd);
125                 throw std::runtime_error(msg);
126         }
127 }
128
129 InitGLEW::~InitGLEW() {
130
131 }
132
133 }