13 std::string sdl_error_append(std::string msg) {
14 const char *error = SDL_GetError();
23 std::string alut_error_append(ALenum num, std::string msg) {
24 const char *error = alutGetErrorString(num);
36 AlutError::AlutError(ALenum num)
37 : std::runtime_error(alutGetErrorString(num)) {
41 AlutError::AlutError(ALenum num, const std::string &msg)
42 : std::runtime_error(alut_error_append(num, msg)) {
48 : std::runtime_error(SDL_GetError()) {
52 SDLError::SDLError(const std::string &msg)
53 : std::runtime_error(sdl_error_append(msg)) {
59 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
60 throw SDLError("SDL_Init(SDL_INIT_VIDEO)");
70 if (IMG_Init(IMG_INIT_PNG) == 0) {
71 throw SDLError("IMG_Init(IMG_INIT_PNG)");
81 if (TTF_Init() != 0) {
82 throw SDLError("TTF_Init()");
92 if (!alutInit(nullptr, nullptr)) {
93 throw AlutError(alutGetError(), "alutInit");
99 throw AlutError(alutGetError(), "alutExit");
104 InitGL::InitGL(bool double_buffer, int sample_size) {
105 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
106 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
108 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
109 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
111 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
112 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
116 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
117 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
121 if (sample_size > 1) {
122 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
123 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
125 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
126 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
133 : handle(SDL_CreateWindow(
135 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
137 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
140 throw SDLError("SDL_CreateWindow");
145 SDL_DestroyWindow(handle);
148 void Window::GrabInput() {
149 SDL_SetWindowGrab(handle, SDL_TRUE);
152 void Window::ReleaseInput() {
153 SDL_SetWindowGrab(handle, SDL_FALSE);
156 void Window::GrabMouse() {
157 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
158 throw SDLError("SDL_SetRelativeMouseMode");
162 void Window::ReleaseMouse() {
163 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
164 throw SDLError("SDL_SetRelativeMouseMode");
168 void Window::Flip() {
169 SDL_GL_SwapWindow(handle);
173 GLContext::GLContext(SDL_Window *win)
174 : ctx(SDL_GL_CreateContext(win)) {
176 throw SDLError("SDL_GL_CreateContext");
180 GLContext::~GLContext() {
181 SDL_GL_DeleteContext(ctx);
185 InitGLEW::InitGLEW() {
186 glewExperimental = GL_TRUE;
187 GLenum glew_err = glewInit();
188 if (glew_err != GLEW_OK) {
189 std::string msg("glewInit: ");
190 const GLubyte *errBegin = glewGetErrorString(glew_err);
191 const GLubyte *errEnd = errBegin;
192 while (*errEnd != '\0') {
195 msg.append(errBegin, errEnd);
196 throw std::runtime_error(msg);
201 Init::Init(bool double_buffer, int sample_size)
205 , init_gl(double_buffer, sample_size)
207 , ctx(window.Handle())