src/Unuk/Game.cpp
Go to the documentation of this file.

    00001 #ifdef WIN32
    00002 #include <windows.h>
    00003 #endif
    00004
    00005 #include <X11/Xlib.h>
    00006 #include <GL/gl.h>
    00007 #include <GL/glu.h>
    00008 #include <GL/glut.h>
    00009 #include <cstdlib>
    00010
    00011 #include "SDL/SDL.h"
    00012 #include "Game.h"
    00013 #include "Player.h"
    00014 #include "../libUnuk/Input.h"
    00015 #include "../libUnuk/Sprite.h"
    00016 #include "../libUnuk/Debug.h"
    00017
    00018 Game::Game(void) {
    00019   m_assets = false;
    00020   //m_player = new Player();
    00021   //m_player->SetSprite();
    00022   m_rotationAngle = 0.0f;
    00023 }
    00024
    00025 Game::~Game(void) {
    00026   DeleteAssets();
    00027 }
    00028
    00029 bool Game::Init(void) {
    00030   glEnable(GL_DEPTH_TEST);
    00031   glDepthFunc(GL_LEQUAL);
    00032
    00033   LoadAssets();
    00034   m_assets = true;
    00035
    00036   return true;
    00037 }
    00038
    00039 void Game::Prepare(float dt) {
    00040   glEnable(GL_BLEND);
    00041   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    00042   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    00043   glShadeModel(GL_FLAT);
    00044   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    00045
    00046   Sprite::Enable2D();
    00047
    00048   m_player->Prepare();
    00049
    00050   const float SPEED = 15.0f;
    00051   m_rotationAngle += SPEED * dt;
    00052   if(m_rotationAngle > 360.0f) {
    00053     m_rotationAngle -= 360.0f;
    00054   }
    00055 }
    00056
    00057 void Game::Render(void) {
    00058   static GLint T0       = 0;
    00059   static GLint frames   = 0;
    00060
    00061   glClear(GL_COLOR_BUFFER_BIT);
    00062   glRasterPos2i(0, 0);
    00063
    00064   // Draw the test image.
    00065   if(m_assets) {
    00066     m_player->Render();
    00067   }
    00068
    00069   glFlush();
    00070
    00071   glDisable(GL_TEXTURE_2D);
    00072
    00073   // Get frames per second.
    00074   frames++;
    00075   {
    00076     GLint t = SDL_GetTicks();
    00077     if (t - T0 >= 5000) {
    00078       GLfloat seconds = (t - T0) / 1000.0f;
    00079       GLfloat fps = frames / seconds;
    00080       Debug::logger->message("\n%d frames in %g seconds = %g FPS", frames, seconds, fps);
    00081       T0 = t;
    00082       frames = 0;
    00083     }
    00084   }
    00085 }
    00086
    00087 void Game::Shutdown(void) {
    00088   Debug::logger->message("\n\n-----Cleaning Up-----");
    00089   m_player->CleanUp();
    00090   delete m_player;
    00091   Debug::logger->message("\nPlayer Deleted.");
    00092   Debug::closeLog();
    00093 }
    00094
    00095 void Game::UpdateProjection(void) {
    00096   GLint iViewport[4];
    00097
    00098   // Get a copy of the viewport.
    00099   glGetIntegerv(GL_VIEWPORT, iViewport);
    00100   glPushMatrix();
    00101   glLoadIdentity();
    00102
    00103   // Save a copy of the projection matrix so that we can restore
    00104   // it when it's time to do 3D rendering again.
    00105   glMatrixMode(GL_PROJECTION);
    00106   glPushMatrix();
    00107   glLoadIdentity();
    00108
    00109   // Set up the orthographic projection.
    00110   glOrtho( iViewport[0], iViewport[0] + iViewport[2],
    00111       iViewport[1] + iViewport[3], iViewport[1], -1, 1);
    00112   glMatrixMode(GL_MODELVIEW);
    00113   glPushMatrix();
    00114   glLoadIdentity();
    00115
    00116   // Make sure depth testing and lighting are disabled for 2D rendering
    00117   //until we are finished rendering in 2D.
    00118   glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT);
    00119   glDisable(GL_DEPTH_TEST);
    00120   glDisable(GL_LIGHTING);
    00121
    00122 //  glMatrixMode(GL_PROJECTION);
    00123 //  glLoadIdentity();
    00124 //
    00125 //  // Set up the orthographic projection.
    00126 //  glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 1000.0);
    00127 //
    00128 //  glMatrixMode(GL_MODELVIEW);
    00129 //  glLoadIdentity();
    00130 }
    00131
    00132 void Game::OnResize(int width, int height) {
    00133   // Let's see you divide by zero now!
    00134   if(height == 0) { height = 1; }
    00135
    00136   // Set the viewport to the window size.
    00137   glViewport(0, 0, width, height);
    00138
    00139   // Set the projection.
    00140   UpdateProjection();
    00141 }
    00142
    00143 void Game::LoadAssets(void) {
    00144   m_player = new Player();
    00145   m_player->SetSprite();
    00146 }
    00147
    00148 void Game::DeleteAssets(void) {
    00149   delete m_player;
    00150 }

All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines