16 if (SDL_Init(SDL_INIT_EVENTS) != 0) {
17 throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
26 InitVideo::InitVideo() {
27 if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
28 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
30 // SDL seems to start out in text input state
34 InitVideo::~InitVideo() {
35 SDL_QuitSubSystem(SDL_INIT_VIDEO);
40 if (IMG_Init(IMG_INIT_PNG) == 0) {
41 throw SDLError("IMG_Init(IMG_INIT_PNG)");
51 if (SDLNet_Init() != 0) {
52 throw SDLError("SDLNet_Init()");
62 if (TTF_Init() != 0) {
63 throw SDLError("TTF_Init()");
73 if (!alutInit(nullptr, nullptr)) {
74 throw AlutError(alutGetError(), "alutInit");
78 InitAL::~InitAL() throw(AlutError) {
80 throw AlutError(alutGetError(), "alutExit");
85 InitGL::InitGL(bool double_buffer, int sample_size) {
86 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
87 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
89 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
90 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
92 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
93 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
97 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) {
98 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
102 if (sample_size > 1) {
103 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
104 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
106 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
107 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
114 : handle(SDL_CreateWindow(
116 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
118 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
121 throw SDLError("SDL_CreateWindow");
126 SDL_DestroyWindow(handle);
129 void Window::GrabInput() {
130 SDL_SetWindowGrab(handle, SDL_TRUE);
133 void Window::ReleaseInput() {
134 SDL_SetWindowGrab(handle, SDL_FALSE);
137 void Window::GrabMouse() {
138 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
139 throw SDLError("SDL_SetRelativeMouseMode");
143 void Window::ReleaseMouse() {
144 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
145 throw SDLError("SDL_SetRelativeMouseMode");
149 void Window::Flip() {
150 SDL_GL_SwapWindow(handle);
154 GLContext::GLContext(SDL_Window *win)
155 : ctx(SDL_GL_CreateContext(win)) {
157 throw SDLError("SDL_GL_CreateContext");
161 GLContext::~GLContext() {
162 SDL_GL_DeleteContext(ctx);
166 InitGLEW::InitGLEW() {
167 glewExperimental = GL_TRUE;
168 GLenum glew_err = glewInit();
169 if (glew_err != GLEW_OK) {
170 std::string msg("glewInit: ");
171 const GLubyte *errBegin = glewGetErrorString(glew_err);
172 const GLubyte *errEnd = errBegin;
173 while (*errEnd != '\0') {
176 msg.append(errBegin, errEnd);
177 throw std::runtime_error(msg);
182 InitHeadless::InitHeadless()
188 Init::Init(bool double_buffer, int sample_size)
192 , init_gl(double_buffer, sample_size)
194 , ctx(window.Handle())