summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-06-25 23:04:23 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-06-25 23:04:23 +0200
commitf8d8934e33ebdb165655297d7eb427fccc19cea8 (patch)
tree1addf317bd1019def959e56469fb3c3bf3dc03ea
parent95adcb33f1e5cbc12afe22ed83b3b5c7f30fc4f7 (diff)
Added Application-class.
-rw-r--r--application.cpp92
-rw-r--r--application.h27
2 files changed, 119 insertions, 0 deletions
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 <stdexcept>
+
+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 <SDL/SDL.h>
+
+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