20 if (SDL_Init(SDL_INIT_EVENTS) != 0) {
21 throw SDLError("SDL_Init(SDL_INIT_EVENTS)");
30 InitVideo::InitVideo() {
31 if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
32 throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)");
34 // SDL seems to start out in text input state
38 InitVideo::~InitVideo() {
39 SDL_QuitSubSystem(SDL_INIT_VIDEO);
44 if (IMG_Init(IMG_INIT_PNG) == 0) {
45 throw SDLError("IMG_Init(IMG_INIT_PNG)");
55 if (SDLNet_Init() != 0) {
56 throw SDLError("SDLNet_Init()");
66 if (TTF_Init() != 0) {
67 throw SDLError("TTF_Init()");
77 if (!alutInit(nullptr, nullptr)) {
78 throw AlutError(alutGetError(), "alutInit");
84 AlutError e(alutGetError(), "alutExit");
85 std::cerr << "error: " << e.what() << std::endl;
90 InitGL::InitGL(bool double_buffer, int sample_size) {
91 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
92 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
94 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) {
95 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)");
97 if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
98 throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
101 if (!double_buffer) {
102 if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) {
103 throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)");
107 if (sample_size > 1) {
108 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
109 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
111 if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
112 throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
118 Window::Window(int w, int h)
119 : handle(SDL_CreateWindow(
121 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
123 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
126 throw SDLError("SDL_CreateWindow");
131 SDL_DestroyWindow(handle);
134 void Window::GrabInput() {
135 SDL_SetWindowGrab(handle, SDL_TRUE);
138 void Window::ReleaseInput() {
139 SDL_SetWindowGrab(handle, SDL_FALSE);
142 void Window::GrabMouse() {
143 if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) {
144 throw SDLError("SDL_SetRelativeMouseMode");
148 void Window::ReleaseMouse() {
149 if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) {
150 throw SDLError("SDL_SetRelativeMouseMode");
154 void Window::Flip() {
155 SDL_GL_SwapWindow(handle);
159 GLContext::GLContext(SDL_Window *win)
160 : ctx(SDL_GL_CreateContext(win)) {
162 throw SDLError("SDL_GL_CreateContext");
166 GLContext::~GLContext() {
167 SDL_GL_DeleteContext(ctx);
171 InitGLEW::InitGLEW() {
172 glewExperimental = GL_TRUE;
173 GLenum glew_err = glewInit();
174 if (glew_err != GLEW_OK) {
175 std::string msg("glewInit: ");
176 const GLubyte *errBegin = glewGetErrorString(glew_err);
177 const GLubyte *errEnd = errBegin;
178 while (*errEnd != '\0') {
181 msg.append(errBegin, errEnd);
182 throw std::runtime_error(msg);
187 Init::Init(bool double_buffer, int sample_size)
191 , init_gl(double_buffer, sample_size)
193 , ctx(window.Handle())
195 , viewport(960, 540) {