Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

Main.cpp

Go to the documentation of this file.
00001 
00007 #include "Main.h"
00008 
00009 Application *app = new MainApp();
00010 
00011 unsigned int g_selectedScene;                           // extern in Game/Globals.h
00012 SPlayer         g_players[SPLAYER_MAX_PLAYERS];
00013 int                     g_numPlayers = 2;
00014 MilkShape       g_models[SPLAYER_MAX_PLAYERS];
00015 OpenGLConsole console;
00016 int                     g_myPlayerID = 0;
00017 Gne                     *gne = NULL;
00018 SGameInfo       g_gameInfo;
00019 SGalaxyInfo             g_galaxies[JOIN_GALAXIES];
00020 SModelInfo              g_allModels[MODELS];
00021 Music                   g_music;
00022 
00023 /********************************************************************
00024  * Constructor
00025  */
00026 MainApp::MainApp() {
00027         //m_selectedScene = SCENE_SELECTPROFILE;
00028         g_selectedScene = SCENE_MENU;
00029         //g_selectedScene = SCENE_PLAY;
00030         //g_selectedScene = SCENE_DEMO;
00031         m_currentScene = SCENE_UNDEFINED;
00032 
00033         m_currScene                             = NULL;
00034         m_sceneMenu                             = NULL;
00035         m_sceneDemo                             = NULL;
00036         m_scenePlay                             = NULL;
00037         m_sceneJoin                             = NULL;
00038         m_sceneScores                   = NULL;
00039 
00040 }
00041 
00042 /********************************************************************
00043  * Destructor
00044  */
00045 MainApp::~MainApp() {
00046 }
00047 
00048 /********************************************************************
00049  * Draw frame
00050  */
00051 bool MainApp::drawFrame() {
00052 
00053         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
00054         glLoadIdentity();
00055 
00056         if (m_currScene)
00057                 m_currScene->drawFrame();
00058 
00059         console.draw(defaultFont);
00060 
00061         //renderer->apply();    // hmm, need this to make everything clear properly.. (when using masks)
00062 
00063         if (g_showFPS) {
00064                 defaultFont->startTextMode();
00065                 {
00066                         defaultFont->setColor(1.0f, 1.0f, 1.0f, 1.0f);
00067                         defaultFont->print(20.0f, (g_fullscreen ? 20.0f : 50.0f), "FPS: %d", (int)g_fps);
00068                         defaultFont->print(20.0f, (g_fullscreen ? 40.0f : 70.0f), "Time: %d", (int)g_time);
00069                         defaultFont->print(20.0f, (g_fullscreen ? 60.0f : 90.0f), "Scene: %d", m_currentScene);
00070                 }
00071                 defaultFont->endTextMode();
00072         }
00073 
00074         if (m_currentScene != SCENE_DEMO) {
00075                 flush();
00076         }
00077 
00078         return true;
00079 }
00080 
00081 /********************************************************************
00082  * Initialize
00083  */
00084 bool MainApp::update() {
00085 
00086         if (g_selectedScene != m_currentScene) {
00087                 drawLoading();
00088 
00089                 if (m_currScene)
00090                         m_currScene->unload();
00091 
00092                 //LOG_MISC(("Byter till:%d",g_selectedScene));
00093 
00094                 switch(g_selectedScene) {
00095                         case SCENE_MENU:
00096                                 m_currScene = m_sceneMenu;
00097                                 break;
00098                         case SCENE_PLAY:
00099                                 m_currScene = m_scenePlay;
00100                                 break;
00101                         case SCENE_JOIN:
00102                                 m_currScene = m_sceneJoin;
00103                                 break;
00104                         case SCENE_SCORES:
00105                                 m_currScene = m_sceneScores;
00106                                 break;
00107                         case SCENE_DEMO:
00108                                 m_currScene = m_sceneDemo;
00109                                 break;
00110                         default:
00111                                 m_currScene = NULL;
00112                                 break;
00113                 }
00114 
00115                 m_currentScene = g_selectedScene;
00116 
00117                 if (m_currScene) m_currScene->load(app);
00118         }
00119 
00120         if (m_currScene)
00121                 m_currScene->update();
00122 
00123 
00124         // Chat
00125         if (gne && gne->isRunning() && gne->isMessagePending()) {
00126                 msgstruct msg = gne->getNextMessage();
00136                 Player *p = gne->getPlayerById(msg.playerid);
00137 
00138                 Vec3 clr;
00139                 clr.x = g_players[msg.playerid].chatClr.x;
00140                 clr.y = g_players[msg.playerid].chatClr.y;
00141                 clr.z = g_players[msg.playerid].chatClr.z;
00142 
00143                 console.addEx(clr, "%s > %s", p->getName(), msg.msg);
00144         }
00145 
00146         return true;
00147 }
00148 
00149 /********************************************************************
00150  * Initialize
00151  */
00152 bool MainApp::initialize() {
00153         LOG_NEWLINE;
00154         camera.setPosition(Vec3(0.0f, 0.0f, 15.0f));
00155 
00156         soundMedia.initialize();
00157         //music.load("data\\music\\Aa_outli.it");
00158 
00159         m_sceneMenu = new Menu();
00160         m_sceneDemo = new Demo();
00161         m_scenePlay = new Play();
00162         m_sceneJoin     = new Join();
00163         m_sceneScores = new Scores();
00164 
00165         gne = NULL;
00166         //gne = new Gne();
00167 
00168         LOG_SUCCESS(("Application initialized"));
00169         return true;
00170 }
00171 
00172 /********************************************************************
00173  * Deinitialize
00174  */
00175 bool MainApp::deInitialize() {
00176         LOG_NEWLINE;
00177 
00178         if (m_sceneMenu) delete m_sceneMenu;
00179         m_sceneMenu = NULL;
00180 
00181         if (m_sceneDemo) delete m_sceneDemo;
00182         m_sceneDemo = NULL;
00183 
00184         if (m_scenePlay) delete m_scenePlay;
00185         m_scenePlay = NULL;
00186 
00187         if (m_sceneJoin) delete m_sceneJoin;
00188         m_sceneJoin = NULL;
00189 
00190         if (m_sceneScores) delete m_sceneScores;
00191         m_sceneScores = NULL;
00192 
00193         m_currScene = NULL;
00194 
00195         //music.unload();
00196         soundMedia.deInitialize();
00197 
00198         if (gne) {
00199                 if (gne->isRunning()) gne->disconnect();
00200                 delete gne;
00201         }
00202         gne = NULL;
00203 
00204         LOG_SUCCESS(("Application deinitialized"));
00205         return true;
00206 }
00207 
00208 /********************************************************************
00209  * Load
00210  */
00211 bool MainApp::load() {
00212         LOG_NEWLINE;
00213 
00214         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
00215 
00216         if (g_fullscreen)
00217                 app->showCursor(false);
00218         else
00219                 app->showCursor(true);
00220 
00221         if (!GL_ARB_shader_objects_supported || !GL_ARB_vertex_shader_supported || !GL_ARB_fragment_shader_supported/* || !GL_ARB_shading_language_100_supported*/) {
00222                 renderer->lock(LF_SHADING);
00223                 LOG_ERROR(("Your computer cant handle GLSL vertex/fragment shaders - the game will run, but will it look cool?"));
00224         }
00225 
00226         // create font
00227         // this is realy realy realy ugly (but it works (for now)) -->
00228         HFONT tmpFont = CreateFont(     -14,                                                    // height
00229                                                                 0,                                                              // default width,
00230                                                                 0, 0,                                                   // angles
00231                                                                 FW_BOLD,                                                // bold
00232                                                                 false,                                                  // italic
00233                                                                 false,                                                  // underline
00234                                                                 false,                                                  // strikeout
00235                                                                 ANSI_CHARSET,                                   // character set
00236                                                                 OUT_TT_PRECIS,                                  // ouput precision
00237                                                                 CLIP_DEFAULT_PRECIS,                    // clipping precision
00238                                                                 ANTIALIASED_QUALITY,                    // quality
00239                                                                 FF_DONTCARE | DEFAULT_PITCH,    // family and pitch
00240                                                                 "Courier New");                                 // font name
00241 
00242         defaultFont = new OpenGLFont(tmpFont, (OpenGLRenderer *)renderer, hDC);
00243         DeleteObject(tmpFont);
00244         // <--
00245 
00246         g_music.load("data\\music\\Aa_outli.it");
00247 
00248         if (m_currScene) m_currScene->load(app);
00249 
00250         //if ((m_texLoading = renderer->addTexture("data\\textures\\common\\loading.bmp")) == TEXTURE_NONE) return false;
00251 
00252         LOG_SUCCESS(("Application loaded"));
00253         return true;
00254 }
00255 
00256 /********************************************************************
00257  * Unload
00258  */
00259 bool MainApp::unload() {
00260         LOG_NEWLINE;
00261 
00262         // make sure everything is unloaded
00263         if (m_sceneMenu) m_sceneMenu->unload();
00264         if (m_sceneDemo) m_sceneDemo->unload();
00265         if (m_scenePlay) m_scenePlay->unload();
00266         if (m_sceneJoin) m_sceneJoin->unload();
00267         if (m_sceneScores) m_sceneScores->unload();
00268 
00269         //renderer->deleteTexture(m_texLoading);
00270 
00271         g_music.stop();
00272         g_music.unload();
00273 
00274         if (defaultFont) delete defaultFont;
00275         defaultFont = NULL;
00276 
00277         LOG_SUCCESS(("Application unloaded"));
00278         return true;
00279 }
00280 
00281 /********************************************************************
00282  * Draw loading
00283  */
00284 void MainApp::drawLoading(float percent) {
00285         glClear(GL_COLOR_BUFFER_BIT);
00286         defaultFont->startTextMode();
00287         {
00288                 defaultFont->setColor(1.0f, 1.0f, 1.0f, 1.0f);
00289                 defaultFont->print(g_width/2.0f-20.0f, g_height/2.0f, "Loading...");
00319         }
00320         defaultFont->endTextMode();
00321 
00322         flush();
00323 }
00324 
00325 /********************************************************************
00326  * Handle char
00327  */
00328 void MainApp::handleChar(char ch) {
00329         if (g_console)
00330                 console.handleChar(ch);
00331         else if (m_currentScene == SCENE_MENU && m_currScene)
00332                 ((Menu *)m_currScene)->handleChar(ch);
00333 }
00334 
00335 /********************************************************************
00336  * Key down
00337  */
00338 void MainApp::keyDown(unsigned int key) {
00339         if (g_console) {
00340                 console.handleKey(key);
00341         }
00342         else {
00343                 switch(key & 0xffff) {
00344                         //case VK_ESCAPE:
00345                         //      PostMessage(hWnd, WM_CLOSE, 0, 0);
00346                         //      return;
00347 
00348                         case VK_F1:
00349                                 toggleFullscreen();
00350                                 return;
00351 
00352                         //case VK_F2:
00353                         //      camera.shake(0.5f, 80, 1.0f);
00354                         //      return;
00380                         // enter
00381                         case 13:
00382                                 if (g_drawConsole)
00383                                         g_console = true;
00384                                 else
00385                                         setKey(key, true);
00386                                 break;
00387 
00388                         default:
00389                                 setKey(key, true);
00390                                 break;
00391                 }
00392         }
00393 }

Generated on Sun Jun 5 15:47:04 2005 for Defacto by  doxygen 1.4.3