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