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