AaronCameron.net
No ads. No Profit. No Master, But Truth.
Not a Member? - Login or Create an Account
Wednesday the 23rd of May 2012 @ 09:23am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cGuiApp.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "gui/cGuiApp.h"
00026 #include "n2l/timing.h"
00027 #include "n2l/gui.h"
00028 #include "n2l/video.h"
00029 
00030 #include "n2l/renderTools.h"
00031 
00032 #include "GL/gl.h"
00033 
00034 namespace n2l
00035 {
00036 
00037     /**************************************************************************/
00038     cGuiApp::cGuiApp( const cAutoPtr<cGuiCanvas> &ioCanvas )
00039     {
00040         if (!ioCanvas.isSet())
00041             throw cBadDataUseException( "cGuiApp::cGuiApp",
00042                 "Provided canvas is null" );
00043 
00044         mCanvas = ioCanvas;
00045         mKeepRunning = new tBool;
00046     }
00047 
00048     /**************************************************************************/
00049     cGuiApp::~cGuiApp()
00050     {
00051     }
00052 
00053     /**************************************************************************/
00054     const cAutoPtr<cGuiACSetVar<tBool> > cGuiApp::newQuitAction()
00055     {
00056         return new cGuiACSetVar<tBool>( mKeepRunning, false, true );
00057     }
00058 
00059     /**************************************************************************/
00060     void cGuiApp::run()
00061     {
00062         mUsingMouseMap = cEventManager::getMouseMapCoordinates( mOldTopLeft,
00063             mOldBottomRight );
00064 
00065         // Set the mapping to the size of the canvas
00066         cEventManager::mouseMapCoordinates( tMousePos(0,0), mCanvas->size() );
00067 
00068         if (mCanvas->size().x()<0.0001f || mCanvas->size().y()<0.0001f)
00069             throw cBadDataUseException( "cGuiApp::run",
00070                 "Base canvas provided has no size from which to derive "
00071                 "running paramters." );
00072 
00073         // Configure the projection.
00074         cROrthoProjection ortho( 0.0, tDouble(mCanvas->size().x()),
00075             tDouble(mCanvas->size().y()), 0.0,
00076             0.0, 1.0 );
00077 
00078         glMatrixMode( GL_PROJECTION );
00079         glPushMatrix();
00080 
00081         glMatrixMode( GL_MODELVIEW );
00082         glPushMatrix();
00083 
00084 
00085         *mKeepRunning = true;
00086         mSystemQuitEvent = false;
00087         tUint now,lastPass = n2lGetTicks();
00088         while (*mKeepRunning) {
00089             now = n2lGetTicks();
00090 
00091             // Update the canvas in time.
00092             const tUint MSecondsPassed = now-lastPass;
00093             mCanvas->update( MSecondsPassed );
00094             // Update other methods that might want them.
00095             for (tUpdateList::const_iterator i = mUpdateList.begin();
00096                 i!=mUpdateList.end(); ++i )
00097             {
00098                 (*i)(MSecondsPassed);
00099             }
00100 
00101             // Handle any outstanding events
00102             cAutoPtr<const cEventInterface> event;
00103             while (cEventManager::popEvent(event)) {
00104                 switch ( event->type() )
00105                 {
00106                     case EventType_Quit:
00107                         *mKeepRunning = false;
00108                         mSystemQuitEvent = true;
00109                         break;
00110 
00111                     default:
00112                         break;
00113                 } // switch
00114 
00115                 // Send the event to the canvas
00116                 mCanvas->systemEvent( event );
00117 
00118                 // Forward events to other methods that might want them.
00119                 for (tSysFuncList::const_iterator i = mSysFuncList.begin();
00120                     i!=mSysFuncList.end(); ++i )
00121                 {
00122                     (*i)(event);
00123                 }
00124             } // while popping events.
00125 
00126             glMatrixMode( GL_PROJECTION );
00127             glLoadIdentity();
00128             ortho.render();
00129             glMatrixMode( GL_MODELVIEW );
00130             glLoadIdentity();
00131 
00132             glClearDepth( 0.0f );
00133 
00134             if (mClearBuffer.isSet()) {
00135                 glClear( GL_DEPTH_BUFFER_BIT );
00136                 glDrawBuffer( GL_BACK );
00137                 mClearBuffer->drawPixels();
00138             } else {
00139                 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
00140                 glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
00141             }
00142 
00143             for (tDrawList::const_iterator i = mPreDrawList.begin();
00144                 i!=mPreDrawList.end(); ++i )
00145             {
00146                 (*i)();
00147             }
00148 
00149             mCanvas->draw();
00150 
00151             for (tDrawList::const_iterator i = mPostDrawList.begin();
00152                 i!=mPostDrawList.end(); ++i )
00153             {
00154                 (*i)();
00155             }
00156 
00157             GLenum error;
00158             while ( (error=glGetError()) != GL_NO_ERROR )
00159                 throw cOpenGLException( "cGuiApp::run","",error );
00160 
00161             cOpenGLDisplay::currentDisplay()->flip();
00162         
00163             lastPass = now;
00164 
00165             n2lDelay(5);
00166         }
00167 
00168         glMatrixMode( GL_PROJECTION );
00169         glPopMatrix();
00170         
00171         glMatrixMode( GL_MODELVIEW );
00172         glPopMatrix();
00173 
00174         if (mUsingMouseMap) 
00175             cEventManager::mouseMapCoordinates( mOldTopLeft, mOldBottomRight );
00176     }
00177 
00178     /**************************************************************************/
00179     void cGuiApp::addSystemEventFunction(
00180         void (*iFunc)(const cAutoPtr<const cEventInterface>&) )
00181     {
00182         mSysFuncList.push_back( iFunc );
00183     }
00184 
00185     /**************************************************************************/
00186     void cGuiApp::addUpdateLoopFunction( void (*iFunc)(const tUint) )
00187     {
00188         mUpdateList.push_back( iFunc );
00189     }
00190 
00191     /**************************************************************************/
00192     void cGuiApp::addPreDrawFunction( void (*iFunc)(void) )
00193     {
00194         mPreDrawList.push_back( iFunc );
00195     }
00196 
00197     /**************************************************************************/    
00198     void cGuiApp::addPostDrawFunction( void (*iFunc)(void) )
00199     {
00200         mPostDrawList.push_back( iFunc );
00201     }
00202 
00203 }
©2012 Aaron Cameron