15 std::string sdl_error_append(std::string msg) {
16 const char *error = SDL_GetError();
25 std::string net_error_append(std::string msg) {
26 const char *error = SDLNet_GetError();
34 std::string alut_error_append(ALenum num, std::string msg) {
35 const char *error = alutGetErrorString(num);
48 AlutError::AlutError(ALenum num)
49 : std::runtime_error(alutGetErrorString(num)) {
53 AlutError::AlutError(ALenum num, const std::string &msg)
54 : std::runtime_error(alut_error_append(num, msg)) {
60 : std::runtime_error(SDLNet_GetError()) {
64 NetError::NetError(const std::string &msg)
65 : std::runtime_error(net_error_append(msg)) {
71 : std::runtime_error(SDL_GetError()) {
75 SDLError::SDLError(const std::string &msg)
76 : std::runtime_error(sdl_error_append(msg)) {
82 if (SDL_Init(SDL_INIT_EVENTS) != 0) {
83 throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
92 InitVideo::InitVideo() {
93 if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
94 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
96 // SDL seems to start out in text input state
100 InitVideo::~InitVideo() {
101 SDL_QuitSubSystem(SDL_INIT_VIDEO);
106 if (IMG_Init(IMG_INIT_PNG) == 0) {
107 throw SDLError("IMG_Init(IMG_INIT_PNG)");
111 InitIMG::~InitIMG() {
117 if (SDLNet_Init() != 0) {
118 throw SDLError("SDLNet_Init()");
122 InitNet::~InitNet() {
128 if (TTF_Init() != 0) {
129 throw SDLError("TTF_Init()");
133 InitTTF::~InitTTF() {
139 if (!alutInit(nullptr, nullptr)) {
140 throw AlutError(alutGetError(), "alutInit");
146 AlutError e(alutGetError(), "alutExit");
147 std::cerr << "error: " << e.what() << std::endl;
152 InitGL::InitGL(bool double_buffer, int sample_size) {
153 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
154 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
156 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
157 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
159 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
160 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
163 if (!double_buffer) {
164 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) {
165 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
169 if (sample_size > 1) {
170 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
171 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
173 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
174 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
181 : handle(SDL_CreateWindow(
183 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
185 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
188 throw SDLError("SDL_CreateWindow");
193 SDL_DestroyWindow(handle);
196 void Window::GrabInput() {
197 SDL_SetWindowGrab(handle, SDL_TRUE);
200 void Window::ReleaseInput() {
201 SDL_SetWindowGrab(handle, SDL_FALSE);
204 void Window::GrabMouse() {
205 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
206 throw SDLError("SDL_SetRelativeMouseMode");
210 void Window::ReleaseMouse() {
211 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
212 throw SDLError("SDL_SetRelativeMouseMode");
216 void Window::Flip() {
217 SDL_GL_SwapWindow(handle);
221 GLContext::GLContext(SDL_Window *win)
222 : ctx(SDL_GL_CreateContext(win)) {
224 throw SDLError("SDL_GL_CreateContext");
228 GLContext::~GLContext() {
229 SDL_GL_DeleteContext(ctx);
233 InitGLEW::InitGLEW() {
234 glewExperimental = GL_TRUE;
235 GLenum glew_err = glewInit();
236 if (glew_err != GLEW_OK) {
237 std::string msg("glewInit: ");
238 const GLubyte *errBegin = glewGetErrorString(glew_err);
239 const GLubyte *errEnd = errBegin;
240 while (*errEnd != '\0') {
243 msg.append(errBegin, errEnd);
244 throw std::runtime_error(msg);
249 InitHeadless::InitHeadless()
255 Init::Init(bool double_buffer, int sample_size)
259 , init_gl(double_buffer, sample_size)
261 , ctx(window.Handle())