]> git.localhorst.tv Git - blank.git/blob - src/app/init.cpp
split composite model in template and instance
[blank.git] / src / app / init.cpp
1 #include "init.hpp"
2
3 #include <algorithm>
4 #include <alut.h>
5 #include <SDL.h>
6 #include <SDL_image.h>
7 #include <SDL_ttf.h>
8 #include <GL/glew.h>
9
10
11 namespace {
12
13 std::string sdl_error_append(std::string msg) {
14         const char *error = SDL_GetError();
15         if (*error != '\0') {
16                 msg += ": ";
17                 msg += error;
18                 SDL_ClearError();
19         }
20         return msg;
21 }
22
23 std::string alut_error_append(ALenum num, std::string msg) {
24         const char *error = alutGetErrorString(num);
25         if (*error != '\0') {
26                 msg += ": ";
27                 msg += error;
28         }
29         return msg;
30 }
31
32 }
33
34 namespace blank {
35
36 AlutError::AlutError(ALenum num)
37 : std::runtime_error(alutGetErrorString(num)) {
38
39 }
40
41 AlutError::AlutError(ALenum num, const std::string &msg)
42 : std::runtime_error(alut_error_append(num, msg)) {
43
44 }
45
46
47 SDLError::SDLError()
48 : std::runtime_error(SDL_GetError()) {
49
50 }
51
52 SDLError::SDLError(const std::string &msg)
53 : std::runtime_error(sdl_error_append(msg)) {
54
55 }
56
57
58 InitSDL::InitSDL() {
59         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
60                 throw SDLError("SDL_Init(SDL_INIT_VIDEO)");
61         }
62 }
63
64 InitSDL::~InitSDL() {
65         SDL_Quit();
66 }
67
68
69 InitIMG::InitIMG() {
70         if (IMG_Init(IMG_INIT_PNG) == 0) {
71                 throw SDLError("IMG_Init(IMG_INIT_PNG)");
72         }
73 }
74
75 InitIMG::~InitIMG() {
76         IMG_Quit();
77 }
78
79
80 InitTTF::InitTTF() {
81         if (TTF_Init() != 0) {
82                 throw SDLError("TTF_Init()");
83         }
84 }
85
86 InitTTF::~InitTTF() {
87         TTF_Quit();
88 }
89
90
91 InitAL::InitAL() {
92         if (!alutInit(nullptr, nullptr)) {
93                 throw AlutError(alutGetError(), "alutInit");
94         }
95 }
96
97 InitAL::~InitAL() {
98         if (!alutExit()) {
99                 throw AlutError(alutGetError(), "alutExit");
100         }
101 }
102
103
104 InitGL::InitGL(bool double_buffer, int sample_size) {
105         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
106                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
107         }
108         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
109                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
110         }
111         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
112                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
113         }
114
115         if (double_buffer) {
116                 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
117                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
118                 }
119         }
120
121         if (sample_size > 1) {
122                 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
123                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
124                 }
125                 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
126                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
127                 }
128         }
129 }
130
131
132 Window::Window()
133 : handle(SDL_CreateWindow(
134         "blank",
135         SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
136         960, 600,
137         SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
138 )) {
139         if (!handle) {
140                 throw SDLError("SDL_CreateWindow");
141         }
142 }
143
144 Window::~Window() {
145         SDL_DestroyWindow(handle);
146 }
147
148 void Window::GrabInput() {
149         SDL_SetWindowGrab(handle, SDL_TRUE);
150 }
151
152 void Window::ReleaseInput() {
153         SDL_SetWindowGrab(handle, SDL_FALSE);
154 }
155
156 void Window::GrabMouse() {
157         if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
158                 throw SDLError("SDL_SetRelativeMouseMode");
159         }
160 }
161
162 void Window::ReleaseMouse() {
163         if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
164                 throw SDLError("SDL_SetRelativeMouseMode");
165         }
166 }
167
168 void Window::Flip() {
169         SDL_GL_SwapWindow(handle);
170 }
171
172
173 GLContext::GLContext(SDL_Window *win)
174 : ctx(SDL_GL_CreateContext(win)) {
175         if (!ctx) {
176                 throw SDLError("SDL_GL_CreateContext");
177         }
178 }
179
180 GLContext::~GLContext() {
181         SDL_GL_DeleteContext(ctx);
182 }
183
184
185 InitGLEW::InitGLEW() {
186         glewExperimental = GL_TRUE;
187         GLenum glew_err = glewInit();
188         if (glew_err != GLEW_OK) {
189                 std::string msg("glewInit: ");
190                 const GLubyte *errBegin = glewGetErrorString(glew_err);
191                 const GLubyte *errEnd = errBegin;
192                 while (*errEnd != '\0') {
193                         ++errEnd;
194                 }
195                 msg.append(errBegin, errEnd);
196                 throw std::runtime_error(msg);
197         }
198 }
199
200
201 Init::Init(bool double_buffer, int sample_size)
202 : init_sdl()
203 , init_img()
204 , init_ttf()
205 , init_gl(double_buffer, sample_size)
206 , window()
207 , ctx(window.Handle())
208 , init_glew() {
209
210 }
211
212 }