17 if (SDL_Init(SDL_INIT_EVENTS) != 0) {
18 throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
27 InitVideo::InitVideo() {
28 if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
29 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
31 // SDL seems to start out in text input state
35 InitVideo::~InitVideo() {
36 SDL_QuitSubSystem(SDL_INIT_VIDEO);
41 if (IMG_Init(IMG_INIT_PNG) == 0) {
42 throw SDLError("IMG_Init(IMG_INIT_PNG)");
52 if (SDLNet_Init() != 0) {
53 throw SDLError("SDLNet_Init()");
63 if (TTF_Init() != 0) {
64 throw SDLError("TTF_Init()");
74 if (!alutInit(nullptr, nullptr)) {
75 throw AlutError(alutGetError(), "alutInit");
79 InitAL::~InitAL() throw(AlutError) {
81 throw AlutError(alutGetError(), "alutExit");
86 InitGL::InitGL(bool double_buffer, int sample_size) {
87 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
88 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
90 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
91 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
93 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
94 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
98 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) {
99 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
103 if (sample_size > 1) {
104 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
105 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
107 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
108 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
115 : handle(SDL_CreateWindow(
117 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
119 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
122 throw SDLError("SDL_CreateWindow");
127 SDL_DestroyWindow(handle);
130 void Window::GrabInput() {
131 SDL_SetWindowGrab(handle, SDL_TRUE);
134 void Window::ReleaseInput() {
135 SDL_SetWindowGrab(handle, SDL_FALSE);
138 void Window::GrabMouse() {
139 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
140 throw SDLError("SDL_SetRelativeMouseMode");
144 void Window::ReleaseMouse() {
145 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
146 throw SDLError("SDL_SetRelativeMouseMode");
150 void Window::Flip() {
151 SDL_GL_SwapWindow(handle);
155 GLContext::GLContext(SDL_Window *win)
156 : ctx(SDL_GL_CreateContext(win)) {
158 throw SDLError("SDL_GL_CreateContext");
162 GLContext::~GLContext() {
163 SDL_GL_DeleteContext(ctx);
167 InitGLEW::InitGLEW() {
168 glewExperimental = GL_TRUE;
169 GLenum glew_err = glewInit();
170 if (glew_err != GLEW_OK) {
171 std::string msg("glewInit: ");
172 const GLubyte *errBegin = glewGetErrorString(glew_err);
173 const GLubyte *errEnd = errBegin;
174 while (*errEnd != '\0') {
177 msg.append(errBegin, errEnd);
178 throw std::runtime_error(msg);
183 InitHeadless::InitHeadless()
189 Init::Init(bool double_buffer, int sample_size)
193 , init_gl(double_buffer, sample_size)
195 , ctx(window.Handle())