From f8d8934e33ebdb165655297d7eb427fccc19cea8 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Fri, 25 Jun 2010 23:04:23 +0200 Subject: Added Application-class. --- application.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ application.h | 27 +++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 application.cpp create mode 100644 application.h diff --git a/application.cpp b/application.cpp new file mode 100644 index 0000000..890ac0f --- /dev/null +++ b/application.cpp @@ -0,0 +1,92 @@ +#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); + + // 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; + } +} \ No newline at end of file diff --git a/application.h b/application.h new file mode 100644 index 0000000..ea8d899 --- /dev/null +++ b/application.h @@ -0,0 +1,27 @@ +#ifndef APPLICATION_H +#define APPLICATION_H + +#include + +class Application { + private: + SDL_Surface *surface; + bool please_quit; + + protected: + void init_window(unsigned int w, unsigned int h, bool fs = false); + + virtual void event(const SDL_Event& e); + virtual void event_keypress(SDLKey key); + + virtual void update() = 0; + + public: + Application(); + virtual ~Application(); + + void run(); + void quit(); +}; + +#endif -- cgit v1.2.3