Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

/Users/blackie/Documents/myRepository/phobosengine-vc2005/phobosengine/phobosengine/CoreEngine.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "CoreEngine.h"
00004 
00005 #include "FontRenderer.h"
00006 #include "GLApplication.h"
00007 #include "Renderer.h"
00008 #include "TextureDatabase.h"
00009 #include "World.h"
00010 
00011 
00012 namespace pge {
00013 
00014 
00015         //****************************************************************************
00016         //
00017         //
00018         //
00019         //****************************************************************************
00020         CoreEngine::CoreEngine(void) {
00021                 m_owner = NULL;
00022                 m_resX = 0;
00023                 m_resY = 0;
00024 
00025                 // Frames per second
00026                 m_fps = 0;
00027                 m_fpsLoop = 0;
00028                 m_fpsResult = 0;
00029 
00030                 m_lastTime = m_owner->getTicks();
00031                 m_delay = 15;
00032 
00033                 m_engineRunning = false;
00034 
00035                 m_initDone = false;
00036 
00037                 m_currentWorld = NULL;
00038         }
00039 
00040 
00041         //****************************************************************************
00042         //
00043         // ~pCoreEngine()
00044         //
00045         //****************************************************************************  
00046         CoreEngine::~CoreEngine(void) {
00047                 if(m_currentWorld != NULL) {
00048                         delete m_currentWorld;
00049                         m_currentWorld = NULL;
00050                 }
00051 
00052                 TextureDatabase::getInstance()->destroy();
00053         }
00054 
00055 
00056         //****************************************************************************
00057         //
00058         //
00059         //
00060         //****************************************************************************
00061         CoreEngine* CoreEngine::getInstance() {
00062                 static CoreEngine instance;
00063                 return &instance;
00064         }
00065 
00066 
00067         //****************************************************************************
00068         //
00069         //
00070         //
00071         //****************************************************************************
00072         void CoreEngine::init(GLApplication *owner, int resX, int resY) {
00073                 // Set owner
00074                 m_owner = owner;
00075 
00076                 // Apply resolution
00077                 m_resX = resX;
00078                 m_resY = resY;
00079 
00080 
00081                 // TODO: reshape has to be called here at startup since it seems that SDL
00082                 //       does not do this
00083                 // Call reshape to set video modes to OpenGL
00084                 reshape(m_resX, m_resY);
00085 
00086                 // Set cull face.
00087                 glDisable(GL_CULL_FACE);
00088 
00089                 // Set a smooth shading which looks much better than flat.
00090                 glShadeModel(GL_SMOOTH);
00091 
00092                 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
00093 
00094                 // Settings for the depth buffer
00095                 glClearDepth(1.0f);
00096                 glEnable(GL_DEPTH_TEST);
00097                 glDepthFunc(GL_LEQUAL);
00098 
00099                 // Nice perspective.
00100                 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00101 
00102                 glEnable(GL_POLYGON_SMOOTH);
00103 
00104                 // TODO: init?
00105                 TextureDatabase::getInstance();
00106 
00107                 FontRenderer::getInstance()->init();
00108         }
00109 
00110 
00111         //****************************************************************************
00112         //
00113         //
00114         //
00115         //****************************************************************************
00116         bool CoreEngine::init(void) {
00117                 if(!m_initDone) {
00118                         if(m_currentWorld != NULL) {
00119                                 m_currentWorld->init();
00120                         }
00121                         m_initDone = true;
00122                         return true;
00123                 } else {
00124                         return false;
00125                 }
00126         }
00127 
00128 
00129         //****************************************************************************
00130         //
00131         //
00132         //
00133         //****************************************************************************
00134         void CoreEngine::render(void) {
00135                 if(m_initDone) {
00136                         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00137                         glMatrixMode(GL_MODELVIEW);
00138                         glLoadIdentity();
00139 
00140                         if(m_currentWorld != NULL) {
00141                                 m_currentWorld->render();
00142                         }
00143 
00144                         m_fps++;
00145                 }
00146         }
00147 
00148 
00149         //****************************************************************************
00150         //
00151         //
00152         //
00153         //****************************************************************************
00154         void CoreEngine::reshape(GLsizei width, GLsizei height) {
00155                 // Check for division by zero.
00156                 if(height == 0) {
00157                         height = 1;
00158                 }
00159                 m_resX = width;
00160                 m_resY = height;
00161 
00162                 renderer::enter3DMode();
00163         }
00164 
00165 
00166         //****************************************************************************
00167         //
00168         //
00169         //
00170         //****************************************************************************
00171         void CoreEngine::timer(unsigned int delay) {
00172                 if(m_initDone) {
00173                         InputSystem::getInstance()->timer(delay);
00174 
00175 
00176                         if(m_currentWorld != NULL) {
00177                                 m_currentWorld->timer(delay);
00178                         }
00179 
00180                         // Frames per second
00181                         if(m_fpsLoop >= 1000) {
00182                                 m_fpsResult = m_fps;
00183                                 m_fps = 0;
00184                                 m_fpsLoop = 0;
00185                         }
00186                         m_fpsLoop += delay;
00187                 }
00188         }
00189 
00190 
00191         //****************************************************************************
00192         //
00193         //
00194         //
00195         //****************************************************************************
00196         void CoreEngine::shutdown(void) {
00197                 m_owner->shutdown();
00198         }
00199 
00200 
00201         //****************************************************************************
00202         //
00203         //
00204         //
00205         //****************************************************************************
00206         void CoreEngine::mainLoop(void) {
00207                 static unsigned int temp = 0;
00208                 static GLApplication::MouseCoordinate m;
00209 
00210 
00211                 render();
00212 
00213                 // Swap the OpenGL buffer.
00214                 m_owner->swapBuffer();
00215 
00216                 temp = m_owner->getTicks();
00217 
00218                 // Timer.
00219                 if(temp >= (m_lastTime + m_delay)) {
00220                         timer(temp - m_lastTime);
00221 
00222                         // TODO: this works
00223                         //POINT p;
00224                         //GetCursorPos(&p);
00225                         //SetCursorPos(m_owner->getResolutionWidth() / 2, m_owner->getResolutionHeight() / 2);
00226                         //InputSystem::getInstance()->mouseMotion(temp - m_lastTime, p.x, p.y);
00227 
00228                         // Generate mousemotion events here.
00229                         m = m_owner->getMousePointer();
00230                         InputSystem::getInstance()->mouseMotion(temp - m_lastTime, m.x, m.y);
00231                         // TODO: only if in gamemode, not in menumode or similar
00232                         // Set mouse pointer to the middle of the screen
00233                         m_owner->setMousePointer(m_resX / 2, m_resY / 2);
00234 
00235                         m_lastTime = temp;
00236                 }
00237         }
00238 
00239 
00240         //****************************************************************************
00241         //
00242         //
00243         //
00244         //****************************************************************************
00245         void CoreEngine::action(InputSystem::InputAction type, int param0, int param1) {
00246                 if(m_currentWorld != NULL) {
00247                         m_currentWorld->action(type, param0, param1);
00248                 }
00249         }
00250 
00251 
00252         //****************************************************************************
00253         //
00254         //
00255         //
00256         //****************************************************************************
00257         int CoreEngine::getResolutionWidth(void) {
00258                 return this->m_resX;
00259         }
00260 
00261 
00262         //****************************************************************************
00263         //
00264         //
00265         //
00266         //****************************************************************************
00267         int CoreEngine::getResolutionHeight(void) {
00268                 return this->m_resY;
00269         }
00270 
00271 
00272         //****************************************************************************
00273         //
00274         //
00275         //
00276         //****************************************************************************
00277         int CoreEngine::getFPS(void) {
00278                 return m_fpsResult;
00279         }
00280 
00281 
00282         //****************************************************************************
00283         //
00284         //
00285         //
00286         //****************************************************************************
00287         void CoreEngine::setCurrentWorld(World *world) {
00288                 m_currentWorld = world;
00289         }
00290 
00291 
00292         //****************************************************************************
00293         //
00294         //
00295         //
00296         //****************************************************************************
00297         World* CoreEngine::getCurrentWorld(void) {
00298                 return m_currentWorld;
00299         }
00300 
00301 
00302         //****************************************************************************
00303         //
00304         //
00305         //
00306         //****************************************************************************
00307         bool CoreEngine::hasActiveWorld(void) {
00308                 return (m_currentWorld != NULL);
00309         }
00310 };

Generated on Mon Oct 16 12:08:10 2006 for Phobosengine by doxygen 1.3.4