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);
47 AlutError::AlutError(ALenum num)
48 : std::runtime_error(alutGetErrorString(num)) {
52 AlutError::AlutError(ALenum num, const std::string &msg)
53 : std::runtime_error(alut_error_append(num, msg)) {
59 : std::runtime_error(SDLNet_GetError()) {
63 NetError::NetError(const std::string &msg)
64 : std::runtime_error(net_error_append(msg)) {
70 : std::runtime_error(SDL_GetError()) {
74 SDLError::SDLError(const std::string &msg)
75 : std::runtime_error(sdl_error_append(msg)) {
81 if (SDL_Init(SDL_INIT_EVENTS) != 0) {
82 throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
91 InitVideo::InitVideo() {
92 if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
93 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
95 // SDL seems to start out in text input state
99 InitVideo::~InitVideo() {
100 SDL_QuitSubSystem(SDL_INIT_VIDEO);
105 if (IMG_Init(IMG_INIT_PNG) == 0) {
106 throw SDLError("IMG_Init(IMG_INIT_PNG)");
110 InitIMG::~InitIMG() {
116 if (SDLNet_Init() != 0) {
117 throw SDLError("SDLNet_Init()");
121 InitNet::~InitNet() {
127 if (TTF_Init() != 0) {
128 throw SDLError("TTF_Init()");
132 InitTTF::~InitTTF() {
138 if (!alutInit(nullptr, nullptr)) {
139 throw AlutError(alutGetError(), "alutInit");
145 AlutError e(alutGetError(), "alutExit");
146 std::cerr << "error: " << e.what() << std::endl;
151 InitGL::InitGL(bool double_buffer, int sample_size) {
152 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
153 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
155 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
156 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
158 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
159 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
162 if (!double_buffer) {
163 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) {
164 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
168 if (sample_size > 1) {
169 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
170 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
172 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
173 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
180 : handle(SDL_CreateWindow(
182 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
184 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
187 throw SDLError("SDL_CreateWindow");
192 SDL_DestroyWindow(handle);
195 void Window::GrabInput() {
196 SDL_SetWindowGrab(handle, SDL_TRUE);
199 void Window::ReleaseInput() {
200 SDL_SetWindowGrab(handle, SDL_FALSE);
203 void Window::GrabMouse() {
204 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
205 throw SDLError("SDL_SetRelativeMouseMode");
209 void Window::ReleaseMouse() {
210 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
211 throw SDLError("SDL_SetRelativeMouseMode");
215 void Window::Flip() {
216 SDL_GL_SwapWindow(handle);
220 GLContext::GLContext(SDL_Window *win)
221 : ctx(SDL_GL_CreateContext(win)) {
223 throw SDLError("SDL_GL_CreateContext");
227 GLContext::~GLContext() {
228 SDL_GL_DeleteContext(ctx);
232 InitGLEW::InitGLEW() {
233 glewExperimental = GL_TRUE;
234 GLenum glew_err = glewInit();
235 if (glew_err != GLEW_OK) {
236 std::string msg("glewInit: ");
237 const GLubyte *errBegin = glewGetErrorString(glew_err);
238 const GLubyte *errEnd = errBegin;
239 while (*errEnd != '\0') {
242 msg.append(errBegin, errEnd);
243 throw std::runtime_error(msg);
248 InitHeadless::InitHeadless()
254 Init::Init(bool double_buffer, int sample_size)
258 , init_gl(double_buffer, sample_size)
260 , ctx(window.Handle())