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