]> git.localhorst.tv Git - blobs.git/blob - src/app/init.cpp
some stolen stuff
[blobs.git] / src / app / init.cpp
1 #include "init.hpp"
2
3 #include <algorithm>
4 #include <iostream>
5 #include <alut.h>
6 #include <SDL.h>
7 #include <SDL_image.h>
8 #include <SDL_net.h>
9 #include <SDL_ttf.h>
10 #include <GL/glew.h>
11
12
13 namespace {
14
15 std::string sdl_error_append(std::string msg) {
16         const char *error = SDL_GetError();
17         if (*error != '\0') {
18                 msg += ": ";
19                 msg += error;
20                 SDL_ClearError();
21         }
22         return msg;
23 }
24
25 std::string net_error_append(std::string msg) {
26         const char *error = SDLNet_GetError();
27         if (*error != '\0') {
28                 msg += ": ";
29                 msg += error;
30         }
31         return msg;
32 }
33
34 std::string alut_error_append(ALenum num, std::string msg) {
35         const char *error = alutGetErrorString(num);
36         if (*error != '\0') {
37                 msg += ": ";
38                 msg += error;
39         }
40         return msg;
41 }
42
43 }
44
45 namespace blobs {
46
47 AlutError::AlutError(ALenum num)
48 : std::runtime_error(alutGetErrorString(num)) {
49
50 }
51
52 AlutError::AlutError(ALenum num, const std::string &msg)
53 : std::runtime_error(alut_error_append(num, msg)) {
54
55 }
56
57
58 NetError::NetError()
59 : std::runtime_error(SDLNet_GetError()) {
60
61 }
62
63 NetError::NetError(const std::string &msg)
64 : std::runtime_error(net_error_append(msg)) {
65
66 }
67
68
69 SDLError::SDLError()
70 : std::runtime_error(SDL_GetError()) {
71
72 }
73
74 SDLError::SDLError(const std::string &msg)
75 : std::runtime_error(sdl_error_append(msg)) {
76
77 }
78
79
80 InitSDL::InitSDL() {
81         if (SDL_Init(SDL_INIT_EVENTS) != 0) {
82                 throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
83         }
84 }
85
86 InitSDL::~InitSDL() {
87         SDL_Quit();
88 }
89
90
91 InitVideo::InitVideo() {
92         if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
93                 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
94         }
95         // SDL seems to start out in text input state
96         SDL_StopTextInput();
97 }
98
99 InitVideo::~InitVideo() {
100         SDL_QuitSubSystem(SDL_INIT_VIDEO);
101 }
102
103
104 InitIMG::InitIMG() {
105         if (IMG_Init(IMG_INIT_PNG) == 0) {
106                 throw SDLError("IMG_Init(IMG_INIT_PNG)");
107         }
108 }
109
110 InitIMG::~InitIMG() {
111         IMG_Quit();
112 }
113
114
115 InitNet::InitNet() {
116         if (SDLNet_Init() != 0) {
117                 throw SDLError("SDLNet_Init()");
118         }
119 }
120
121 InitNet::~InitNet() {
122         SDLNet_Quit();
123 }
124
125
126 InitTTF::InitTTF() {
127         if (TTF_Init() != 0) {
128                 throw SDLError("TTF_Init()");
129         }
130 }
131
132 InitTTF::~InitTTF() {
133         TTF_Quit();
134 }
135
136
137 InitAL::InitAL() {
138         if (!alutInit(nullptr, nullptr)) {
139                 throw AlutError(alutGetError(), "alutInit");
140         }
141 }
142
143 InitAL::~InitAL() {
144         if (!alutExit()) {
145                 AlutError e(alutGetError(), "alutExit");
146                 std::cerr << "error: " << e.what() << std::endl;
147         }
148 }
149
150
151 InitGL::InitGL(bool double_buffer, int sample_size) {
152         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
153                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
154         }
155         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
156                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
157         }
158         if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
159                 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
160         }
161
162         if (!double_buffer) {
163                 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) {
164                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
165                 }
166         }
167
168         if (sample_size > 1) {
169                 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
170                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
171                 }
172                 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
173                         throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
174                 }
175         }
176 }
177
178
179 Window::Window()
180 : handle(SDL_CreateWindow(
181         "blobs",
182         SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
183         960, 600,
184         SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
185 )) {
186         if (!handle) {
187                 throw SDLError("SDL_CreateWindow");
188         }
189 }
190
191 Window::~Window() {
192         SDL_DestroyWindow(handle);
193 }
194
195 void Window::GrabInput() {
196         SDL_SetWindowGrab(handle, SDL_TRUE);
197 }
198
199 void Window::ReleaseInput() {
200         SDL_SetWindowGrab(handle, SDL_FALSE);
201 }
202
203 void Window::GrabMouse() {
204         if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
205                 throw SDLError("SDL_SetRelativeMouseMode");
206         }
207 }
208
209 void Window::ReleaseMouse() {
210         if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
211                 throw SDLError("SDL_SetRelativeMouseMode");
212         }
213 }
214
215 void Window::Flip() {
216         SDL_GL_SwapWindow(handle);
217 }
218
219
220 GLContext::GLContext(SDL_Window *win)
221 : ctx(SDL_GL_CreateContext(win)) {
222         if (!ctx) {
223                 throw SDLError("SDL_GL_CreateContext");
224         }
225 }
226
227 GLContext::~GLContext() {
228         SDL_GL_DeleteContext(ctx);
229 }
230
231
232 InitGLEW::InitGLEW() {
233         glewExperimental = GL_TRUE;
234         GLenum glew_err = glewInit();
235         if (glew_err != GLEW_OK) {
236                 std::string msg("glewInit: ");
237                 const GLubyte *errBegin = glewGetErrorString(glew_err);
238                 const GLubyte *errEnd = errBegin;
239                 while (*errEnd != '\0') {
240                         ++errEnd;
241                 }
242                 msg.append(errBegin, errEnd);
243                 throw std::runtime_error(msg);
244         }
245 }
246
247
248 InitHeadless::InitHeadless()
249 : init_sdl()
250 , init_net() {
251
252 }
253
254 Init::Init(bool double_buffer, int sample_size)
255 : init_video()
256 , init_img()
257 , init_ttf()
258 , init_gl(double_buffer, sample_size)
259 , window()
260 , ctx(window.Handle())
261 , init_glew() {
262
263 }
264
265 }