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