From b50d18f3f882865a94458c7113aedd7a0cccce80 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Fri, 25 Jun 2010 23:05:47 +0200 Subject: Moved Application to wriggle. --- engine/application.cpp | 233 ------------------------------------------------- engine/application.h | 42 --------- engine/engine.cpp | 159 +++++++++++++++++++++++++++++++++ engine/engine.h | 32 +++++++ main.cpp | 8 +- wriggle | 2 +- 6 files changed, 196 insertions(+), 280 deletions(-) delete mode 100644 engine/application.cpp delete mode 100644 engine/application.h create mode 100644 engine/engine.cpp create mode 100644 engine/engine.h diff --git a/engine/application.cpp b/engine/application.cpp deleted file mode 100644 index ab66bf4..0000000 --- a/engine/application.cpp +++ /dev/null @@ -1,233 +0,0 @@ -#include "application.h" - -#include "config.h" - -#include - -#include - -#include -#include - -#include - -#include - -Application::Application() { - // Initialize SDL - if(SDL_Init(SDL_INIT_VIDEO)) { - throw(std::runtime_error("SDL initialization failed")); - } - // 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(Config::window_w, Config::window_h, 32, flags); - if(!surface) { - throw(std::runtime_error("Video mode set failed")); - } - - // Texturing - glEnable(GL_TEXTURE_2D); - // Blending - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - // Smooth shading - glShadeModel(GL_SMOOTH); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - - glEnable(GL_POINT_SPRITE); - glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); - - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); - - glClearColor(0, 0, 0, 0); - glClearDepth(1); - - please_quit = false; -} - -Application::~Application() { - -} - -void Application::run() { - paused = false; - - font = new FTTextureFont("fonts/VeraMono.ttf"); - font->FaceSize(20); - background = new TextureSDL("textures/background.png"); - - lasttick = SDL_GetTicks(); - - while(1) { - SDL_Event event; - while(SDL_PollEvent(&event)) { - if(event.type == SDL_QUIT) { - break; - } else if(event.type == SDL_KEYDOWN) { - event_keypress(event.key.keysym.sym); - } - } - - if(please_quit) { - return; - } - - unsigned int tick = SDL_GetTicks(); - lasttick = tick; - - main_loop(tick); - - //SDL_Delay(10); - } -} - -void Application::main_loop(unsigned int tick) { - if(!paused) { - stage->update(); - } - - glClearColor(0, 0, 0, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glViewport(0, 0, Config::window_w, Config::window_h); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - float xo = float(Config::window_w)/float(Config::window_h)/2.5 - 0.5; - glOrtho(-xo, 1 + xo, 0, 0.8, 0, 10); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glBindTexture(GL_TEXTURE_2D, background->tex()); - glBegin(GL_QUADS); - glTexCoord2f(0, 1); - glVertex2f(-1.9/9.0, 0); - glTexCoord2f(0, 0); - glVertex2f(-1.9/9.0, 0.8); - glTexCoord2f(1, 0); - glVertex2f(10.9/9.0, 0.8); - glTexCoord2f(1, 1); - glVertex2f(10.9/9.0, 0); - glEnd(); - - glDisable(GL_TEXTURE_2D); - glColor4f(1, 1, 0, 1); - - if(tick - lastframes >= 1000) { - fps = (float)frames * ((float)(tick - lastframes) / 1000.0f); - frames = 1; - lastframes = tick; - } else { - frames++; - } - - glScalef(0.0005, 0.0005, 0.0005); - font->Render((boost::format("FPS: %.2f") % fps).str().c_str()); - - float v_x = Config::window_w * (Config::viewport_x + xo) / (1 + 2 * xo); - float v_y = Config::window_h * Config::viewport_y; - float v_w = Config::window_h * Config::viewport_w; - float v_h = Config::window_h * Config::viewport_h; - - glViewport( - v_x - Config::viewport_overscan, - v_y - Config::viewport_overscan, - v_w + Config::viewport_overscan * 2, - v_h + Config::viewport_overscan * 2); - glScissor(v_x, v_y, v_w, v_h); - glEnable(GL_SCISSOR_TEST); - - glClearColor(0.2, 0.2, 0.2, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(45, (float)660 / (float)740, 1, 100); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - float f = tick * 0.0005; - - gluLookAt( - 5 * sinf(f), 1, 5 * cosf(f), - 0, 0, 0, - 5 * sinf(f), 2, 5 * cosf(f)); - - glBegin(GL_LINES); - for(int i = -10; i < 11; i++) { - if(i % 5 == 0) - glColor3f(1, 1, 1); - else - glColor3f(.5, .5, .5); - glVertex3f(i, 0, -10); - glVertex3f(i, 0, 10); - glVertex3f(-10, 0, i); - glVertex3f(10, 0, i); - } - glEnd(); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho( - -float(Config::viewport_overscan) / float(v_w), - 1.0 + float(Config::viewport_overscan) / float(v_w), - -float(Config::viewport_overscan) / float(v_w), - Config::viewport_aspect + float(Config::viewport_overscan) / float(v_w), - 0, 10); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - stage->draw(); - - glDisable(GL_SCISSOR_TEST); - - SDL_GL_SwapBuffers(); -} - -void Application::quit() { - please_quit = true; -} - -void Application::event_keypress(SDLKey key) { - switch(key) { - case SDLK_ESCAPE: - quit(); - break; - case SDLK_SPACE: - paused = !paused; - lasttick = SDL_GetTicks(); - break; - default: - break; - } -} diff --git a/engine/application.h b/engine/application.h deleted file mode 100644 index b7fc36a..0000000 --- a/engine/application.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef APPLICATION_H -#define APPLICATION_H - -#include -#include -#include - -#include "stage.h" - -#include "bulletpattern.h" -#include -#include - -class Application { - private: - SDL_Surface *surface; - bool please_quit; - - bool paused; - unsigned int lasttick; - unsigned int frames; - unsigned int lastframes; - float fps; - - FTFont* font; - Texture* background; - - public: - Stage* stage; - - Application(); - ~Application(); - void run(); - void quit(); - - protected: - virtual void event_keypress(SDLKey key); - - void main_loop(unsigned int tick); -}; - -#endif diff --git a/engine/engine.cpp b/engine/engine.cpp new file mode 100644 index 0000000..4c7d426 --- /dev/null +++ b/engine/engine.cpp @@ -0,0 +1,159 @@ +#include "engine.h" + +#include "config.h" + +#include + +#include + +#include + +#include + +#include + +void Engine::event_keypress(SDLKey key) { + switch(key) { + case SDLK_SPACE: + paused = !paused; + break; + default: + Application::event_keypress(key); + } +} + +Engine::Engine() { + init_window(Config::window_w, Config::window_h); + + // Texturing + glEnable(GL_TEXTURE_2D); + // Blending + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + // Smooth shading + glShadeModel(GL_SMOOTH); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + + glEnable(GL_POINT_SPRITE); + glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); + + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); + + glClearColor(0, 0, 0, 0); + glClearDepth(1); + + font = new FTTextureFont("fonts/VeraMono.ttf"); + font->FaceSize(20); + background = new TextureSDL("textures/background.png"); + + paused = false; +} + +void Engine::update() { + if(!paused) { + stage->update(); + } + + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glViewport(0, 0, Config::window_w, Config::window_h); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + float xo = float(Config::window_w)/float(Config::window_h)/2.5 - 0.5; + glOrtho(-xo, 1 + xo, 0, 0.8, 0, 10); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glBindTexture(GL_TEXTURE_2D, background->tex()); + glBegin(GL_QUADS); + glTexCoord2f(0, 1); + glVertex2f(-1.9/9.0, 0); + glTexCoord2f(0, 0); + glVertex2f(-1.9/9.0, 0.8); + glTexCoord2f(1, 0); + glVertex2f(10.9/9.0, 0.8); + glTexCoord2f(1, 1); + glVertex2f(10.9/9.0, 0); + glEnd(); + + glDisable(GL_TEXTURE_2D); + glColor4f(1, 1, 0, 1); + + unsigned int tick = SDL_GetTicks(); + if(tick - lastframes >= 1000) { + fps = (float)frames * ((float)(tick - lastframes) / 1000.0f); + frames = 1; + lastframes = tick; + } else { + frames++; + } + + glScalef(0.0005, 0.0005, 0.0005); + font->Render((boost::format("FPS: %.2f") % fps).str().c_str()); + + float v_x = Config::window_w * (Config::viewport_x + xo) / (1 + 2 * xo); + float v_y = Config::window_h * Config::viewport_y; + float v_w = Config::window_h * Config::viewport_w; + float v_h = Config::window_h * Config::viewport_h; + + glViewport( + v_x - Config::viewport_overscan, + v_y - Config::viewport_overscan, + v_w + Config::viewport_overscan * 2, + v_h + Config::viewport_overscan * 2); + glScissor(v_x, v_y, v_w, v_h); + glEnable(GL_SCISSOR_TEST); + + glClearColor(0.2, 0.2, 0.2, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45, (float)660 / (float)740, 1, 100); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + float f = tick * 0.0005; + + gluLookAt( + 5 * sinf(f), 1, 5 * cosf(f), + 0, 0, 0, + 5 * sinf(f), 2, 5 * cosf(f)); + + glBegin(GL_LINES); + for(int i = -10; i < 11; i++) { + if(i % 5 == 0) + glColor3f(1, 1, 1); + else + glColor3f(.5, .5, .5); + glVertex3f(i, 0, -10); + glVertex3f(i, 0, 10); + glVertex3f(-10, 0, i); + glVertex3f(10, 0, i); + } + glEnd(); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho( + -float(Config::viewport_overscan) / float(v_w), + 1.0 + float(Config::viewport_overscan) / float(v_w), + -float(Config::viewport_overscan) / float(v_w), + Config::viewport_aspect + float(Config::viewport_overscan) / float(v_w), + 0, 10); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + stage->draw(); + + glDisable(GL_SCISSOR_TEST); + + SDL_GL_SwapBuffers(); +} diff --git a/engine/engine.h b/engine/engine.h new file mode 100644 index 0000000..894143e --- /dev/null +++ b/engine/engine.h @@ -0,0 +1,32 @@ +#ifndef ENGINE_H +#define ENGINE_H + +#include + +#include + +#include "stage.h" + +class Engine : public Application { + private: + FTFont* font; + Texture* background; + + bool paused; + + unsigned int frames; + unsigned int lastframes; + float fps; + + protected: + virtual void event_keypress(SDLKey key); + + virtual void update(); + + public: + Stage* stage; + + Engine(); +}; + +#endif diff --git a/main.cpp b/main.cpp index 1168949..493950b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,16 +1,16 @@ #include #include -#include "engine/application.h" +#include "engine/engine.h" #include "engine/stage.h" int main(int, char**) { try { - Application* app = new Application(); + Engine engine; - app->stage = new Stage(); + engine.stage = new Stage(); - app->run(); + engine.run(); } catch(std::runtime_error e) { std::cerr << "Exception caught: " << e.what() << std::endl; } diff --git a/wriggle b/wriggle index 95adcb3..f8d8934 160000 --- a/wriggle +++ b/wriggle @@ -1 +1 @@ -Subproject commit 95adcb33f1e5cbc12afe22ed83b3b5c7f30fc4f7 +Subproject commit f8d8934e33ebdb165655297d7eb427fccc19cea8 -- cgit v1.2.3