14 std::string sdl_error_append(std::string msg) {
15 const char *error = SDL_GetError();
24 std::string net_error_append(std::string msg) {
25 const char *error = SDLNet_GetError();
33 std::string alut_error_append(ALenum num, std::string msg) {
34 const char *error = alutGetErrorString(num);
46 AlutError::AlutError(ALenum num)
47 : std::runtime_error(alutGetErrorString(num)) {
51 AlutError::AlutError(ALenum num, const std::string &msg)
52 : std::runtime_error(alut_error_append(num, msg)) {
58 : std::runtime_error(SDLNet_GetError()) {
62 NetError::NetError(const std::string &msg)
63 : std::runtime_error(net_error_append(msg)) {
69 : std::runtime_error(SDL_GetError()) {
73 SDLError::SDLError(const std::string &msg)
74 : std::runtime_error(sdl_error_append(msg)) {
80 if (SDL_Init(0) != 0) {
81 throw SDLError("SDL_Init(0)");
90 InitVideo::InitVideo() {
91 if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
92 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
96 InitVideo::~InitVideo() {
97 SDL_QuitSubSystem(SDL_INIT_VIDEO);
102 if (IMG_Init(IMG_INIT_PNG) == 0) {
103 throw SDLError("IMG_Init(IMG_INIT_PNG)");
107 InitIMG::~InitIMG() {
113 if (SDLNet_Init() != 0) {
114 throw SDLError("SDLNet_Init()");
118 InitNet::~InitNet() {
124 if (TTF_Init() != 0) {
125 throw SDLError("TTF_Init()");
129 InitTTF::~InitTTF() {
135 if (!alutInit(nullptr, nullptr)) {
136 throw AlutError(alutGetError(), "alutInit");
142 throw AlutError(alutGetError(), "alutExit");
147 InitGL::InitGL(bool double_buffer, int sample_size) {
148 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
149 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
151 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
152 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
154 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
155 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
159 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
160 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
164 if (sample_size > 1) {
165 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
166 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
168 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
169 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
176 : handle(SDL_CreateWindow(
178 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
180 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
183 throw SDLError("SDL_CreateWindow");
188 SDL_DestroyWindow(handle);
191 void Window::GrabInput() {
192 SDL_SetWindowGrab(handle, SDL_TRUE);
195 void Window::ReleaseInput() {
196 SDL_SetWindowGrab(handle, SDL_FALSE);
199 void Window::GrabMouse() {
200 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
201 throw SDLError("SDL_SetRelativeMouseMode");
205 void Window::ReleaseMouse() {
206 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
207 throw SDLError("SDL_SetRelativeMouseMode");
211 void Window::Flip() {
212 SDL_GL_SwapWindow(handle);
216 GLContext::GLContext(SDL_Window *win)
217 : ctx(SDL_GL_CreateContext(win)) {
219 throw SDLError("SDL_GL_CreateContext");
223 GLContext::~GLContext() {
224 SDL_GL_DeleteContext(ctx);
228 InitGLEW::InitGLEW() {
229 glewExperimental = GL_TRUE;
230 GLenum glew_err = glewInit();
231 if (glew_err != GLEW_OK) {
232 std::string msg("glewInit: ");
233 const GLubyte *errBegin = glewGetErrorString(glew_err);
234 const GLubyte *errEnd = errBegin;
235 while (*errEnd != '\0') {
238 msg.append(errBegin, errEnd);
239 throw std::runtime_error(msg);
244 InitHeadless::InitHeadless()
250 Init::Init(bool double_buffer, int sample_size)
254 , init_gl(double_buffer, sample_size)
256 , ctx(window.Handle())