12 std::string sdl_error_append(std::string msg) {
13 const char *error = SDL_GetError();
27 : std::runtime_error(SDL_GetError()) {
31 SDLError::SDLError(const std::string &msg)
32 : std::runtime_error(sdl_error_append(msg)) {
38 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
39 throw SDLError("SDL_Init(SDL_INIT_VIDEO)");
49 if (IMG_Init(IMG_INIT_PNG) == 0) {
50 throw SDLError("IMG_Init(IMG_INIT_PNG)");
60 if (TTF_Init() != 0) {
61 throw SDLError("TTF_Init()");
70 InitGL::InitGL(bool double_buffer, int sample_size) {
71 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
72 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
74 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
75 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
77 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
78 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
82 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
83 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
87 if (sample_size > 1) {
88 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
89 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
91 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
92 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
99 : handle(SDL_CreateWindow(
101 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
103 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
106 throw SDLError("SDL_CreateWindow");
111 SDL_DestroyWindow(handle);
114 void Window::GrabInput() {
115 SDL_SetWindowGrab(handle, SDL_TRUE);
118 void Window::ReleaseInput() {
119 SDL_SetWindowGrab(handle, SDL_FALSE);
122 void Window::GrabMouse() {
123 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
124 throw SDLError("SDL_SetRelativeMouseMode");
128 void Window::ReleaseMouse() {
129 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
130 throw SDLError("SDL_SetRelativeMouseMode");
134 void Window::Flip() {
135 SDL_GL_SwapWindow(handle);
139 GLContext::GLContext(SDL_Window *win)
140 : ctx(SDL_GL_CreateContext(win)) {
142 throw SDLError("SDL_GL_CreateContext");
146 GLContext::~GLContext() {
147 SDL_GL_DeleteContext(ctx);
151 InitGLEW::InitGLEW() {
152 glewExperimental = GL_TRUE;
153 GLenum glew_err = glewInit();
154 if (glew_err != GLEW_OK) {
155 std::string msg("glewInit: ");
156 const GLubyte *errBegin = glewGetErrorString(glew_err);
157 const GLubyte *errEnd = errBegin;
158 while (*errEnd != '\0') {
161 msg.append(errBegin, errEnd);
162 throw std::runtime_error(msg);
167 Init::Init(bool double_buffer, int sample_size)
171 , init_gl(double_buffer, sample_size)
173 , ctx(window.Handle())