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(SDL_INIT_EVENTS) != 0) {
81 throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
90 InitVideo::InitVideo() {
91 if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
92 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
94 // SDL seems to start out in text input state
98 InitVideo::~InitVideo() {
99 SDL_QuitSubSystem(SDL_INIT_VIDEO);
104 if (IMG_Init(IMG_INIT_PNG) == 0) {
105 throw SDLError("IMG_Init(IMG_INIT_PNG)");
109 InitIMG::~InitIMG() {
115 if (SDLNet_Init() != 0) {
116 throw SDLError("SDLNet_Init()");
120 InitNet::~InitNet() {
126 if (TTF_Init() != 0) {
127 throw SDLError("TTF_Init()");
131 InitTTF::~InitTTF() {
137 if (!alutInit(nullptr, nullptr)) {
138 throw AlutError(alutGetError(), "alutInit");
144 throw AlutError(alutGetError(), "alutExit");
149 InitGL::InitGL(bool double_buffer, int sample_size) {
150 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
151 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
153 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
154 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
156 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
157 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
161 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
162 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
166 if (sample_size > 1) {
167 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
168 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
170 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
171 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
178 : handle(SDL_CreateWindow(
180 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
182 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
185 throw SDLError("SDL_CreateWindow");
190 SDL_DestroyWindow(handle);
193 void Window::GrabInput() {
194 SDL_SetWindowGrab(handle, SDL_TRUE);
197 void Window::ReleaseInput() {
198 SDL_SetWindowGrab(handle, SDL_FALSE);
201 void Window::GrabMouse() {
202 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
203 throw SDLError("SDL_SetRelativeMouseMode");
207 void Window::ReleaseMouse() {
208 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
209 throw SDLError("SDL_SetRelativeMouseMode");
213 void Window::Flip() {
214 SDL_GL_SwapWindow(handle);
218 GLContext::GLContext(SDL_Window *win)
219 : ctx(SDL_GL_CreateContext(win)) {
221 throw SDLError("SDL_GL_CreateContext");
225 GLContext::~GLContext() {
226 SDL_GL_DeleteContext(ctx);
230 InitGLEW::InitGLEW() {
231 glewExperimental = GL_TRUE;
232 GLenum glew_err = glewInit();
233 if (glew_err != GLEW_OK) {
234 std::string msg("glewInit: ");
235 const GLubyte *errBegin = glewGetErrorString(glew_err);
236 const GLubyte *errEnd = errBegin;
237 while (*errEnd != '\0') {
240 msg.append(errBegin, errEnd);
241 throw std::runtime_error(msg);
246 InitHeadless::InitHeadless()
252 Init::Init(bool double_buffer, int sample_size)
256 , init_gl(double_buffer, sample_size)
258 , ctx(window.Handle())