#include "application.h" #include Application::Application() { // Initialize SDL if(SDL_Init(SDL_INIT_VIDEO)) { throw(std::runtime_error("SDL initialization failed")); } please_quit = false; } Application::~Application() { } void Application::init_window(unsigned int w, unsigned int h, bool fs) { // Fetch the video info const SDL_VideoInfo *info = SDL_GetVideoInfo(); if(!info) { throw(std::runtime_error("SDL info query failed")); } // The SDL mode-flags int flags = SDL_OPENGL; // OpenGL in SDL flags |= SDL_GL_DOUBLEBUFFER; // Double buffering flags |= SDL_HWPALETTE; // Hardware palette // Check for hardware surface aviability if(info->hw_available) { flags |= SDL_HWSURFACE; } else { flags |= SDL_SWSURFACE; } // Check for hardware blit ability if(info->blit_hw) { flags |= SDL_HWACCEL; } // Setup double buffering SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); if(fs) { flags |= SDL_FULLSCREEN; } // Get our surface surface = SDL_SetVideoMode(w, h, 32, flags); if(!surface) { throw(std::runtime_error("Video mode set failed")); } } void Application::run() { while(1) { SDL_Event e; while(SDL_PollEvent(&e)) { event(e); } if(please_quit) { return; } update(); } } void Application::quit() { please_quit = true; } void Application::event(const SDL_Event& e) { switch(e.type) { case SDL_QUIT: quit(); break; case SDL_KEYDOWN: event_keypress(e.key.keysym.sym); break; default: break; } } void Application::event_keypress(SDLKey key) { switch(key) { case SDLK_ESCAPE: quit(); break; default: break; } }