Mercury.cpp

Go to the documentation of this file.
00001 #ifdef _LIB
00002 //SR Content should be added here, not in the project itself.
00003 #include "ImageLoaders.cpp"
00004 #include "ImageLoaderTGA.cpp"
00005 #include "ScreenDebugOverlay.cpp"
00006 #include "ScreenCopper.cpp"
00007 #include "CopperWindow.cpp"
00008 #include "CopperPrimitives.cpp"
00009 #endif
00010 #include "global.h"
00011 #include "Mercury.h"
00012 #include "MercuryDisplay.h"
00013 #include "MercuryTextureManager.h"
00014 #include "Crash/archCrash.h"
00015 #include "MercuryTimer.h"
00016 #include "MercuryInput.h"
00017 #include "MercuryMessages.h"
00018 #include "MercuryScreenManager.h"
00019 #include "MercuryTheme.h"
00020 #include "MercuryLog.h"
00021 #include "MercuryObjectFactory.h"
00022 #include "MercurySoundDriver.h"
00023 
00024 #if defined (_EE)
00025 #include "MercuryDisplayEE.h"
00026 #endif
00027 MercuryDisplay* DISPLAY = NULL;
00028 MercuryTextureManager* TEXTMAN = NULL;
00029 MercuryScreenManager* SCREENMAN = NULL;
00030 MercuryINI* PREFSMAN = NULL;
00031 MercuryTimer gameTimer;
00032 
00033 //ClbkManager CLBKMAN;
00034 volatile float DTIME;
00035 
00036 KeyMappingWithCode( button_Pause, "0-19" );
00037 
00038 
00039 
00040 REGISTER_STATEMENT_TO_MESSAGE( Pause,mappedinput,
00041                               InputMessageStruct c(args);
00042                             if (c.code == button_Pause && c.type == IET_DOWN )
00043                                 gameTimer.Pause();
00044 )
00045 
00046 int HGEXPORT StartMercury()
00047 {
00048     char ** C;
00049     C = new (char*);
00050     (*C) = "nothing";
00051     int ret = InitializeMercury( 1, C );
00052     delete C;
00053     return ret;
00054 }
00055 
00056 int HGEXPORT InitializeMercury( int argc, char**argv )
00057 {
00058     StartCrashHandler( argv, argc );
00059     LOG.Info("Initializing Mercury()");
00060     CheckPREFSMANSetup();
00061     if ( !MESSAGEMAN )
00062         MESSAGEMAN = new MercuryMessageManager;
00063     INPUTMAN = new MercuryInputManager;
00064     DTIME = 0;
00065     DISPLAY = MakeDisplayDriver();
00066 
00067 //#if !defined( NO_THREADS )
00068 //  DISPLAY->CreateThread();
00069 //#else
00070     if ( !DISPLAY->Init(
00071         PREFSMAN->GetValueS( "Window", "Name", InitialWindowProperties::ProductName, true ).c_str(),
00072         PREFSMAN->GetValueI( "Window", "Width", InitialWindowProperties::WindowWidth, true ),
00073         PREFSMAN->GetValueI( "Window", "Height", InitialWindowProperties::WindowHeight, true ),
00074         PREFSMAN->GetValueI( "Window", "BPP", InitialWindowProperties::WindowBPP, true ),
00075         PREFSMAN->GetValueB( "Window", "Fullscreen", InitialWindowProperties::WindowFullscreen, true )
00076         ) )
00077     {
00078         LOG.Warn("Failed to initalize display.");
00079         return -1;
00080     }
00081 //#endif
00082 
00083     SCREENMAN = new MercuryScreenManager;
00084     TEXTMAN = new MercuryTextureManager;
00085 
00086     MercuryObjectFactory::GetInstance().FinalizeCallbacks();
00087 
00088 #ifdef _DEBUG_MEMORY
00089     ResetAllLeakInfo();
00090     SetPrintOnEnd(true);
00091 #endif
00092 
00093     /* The above section sets up all the required things for running
00094         mercury. Do not put any new variables above this point,
00095         unless it is a manager.
00096     */
00097     TEXTMAN->CreateThread();
00098     SCREENMAN->SetPrimaryScreen( THEME.GetMetricS( "Common", "InitialScreen" ) );
00099     
00100     //Load any overlay screens
00101     MString sOverlay = THEME.GetMetricS( "Common", "OverlayScreen" );
00102     while ( sOverlay.size() > 0 )
00103     {
00104         int i = BytesUntil( sOverlay.c_str(), ",", 0, sOverlay.length(), 1 );   //Find next comma
00105         if ( i == (int)sOverlay.size() )
00106         {
00107             SCREENMAN->AddScreen( sOverlay );   //Load the last screen
00108             break;
00109         }
00110         SCREENMAN->AddScreen( sOverlay.substr( 0, i ) );    //Load current screen
00111         sOverlay = sOverlay.substr( i+1 );                  //and change the remaining screens
00112     }
00113     gameTimer.Touch();
00114     return 0;
00115 }
00116 
00117 int HGEXPORT UpdateMercury( float &dTime )
00118 {
00119 
00120     if ( dTime <= 0 )
00121     {
00122 #ifndef _EE
00123         DTIME = dTime = float(gameTimer.Age());
00124 #else
00125         dTime = .033333f;
00126 #endif
00127     }
00128 
00129     gameTimer.Touch();
00130     if (SCREENMAN)
00131     {
00132         DISPLAY->EndFrame(); //End right before we begin a frame so that we can keep the pipe busy
00133         DISPLAY->BeginFrame();
00134         SCREENMAN->Update(dTime);
00135         TEXTMAN->Update(dTime);
00136 
00137     }
00138     MESSAGEMAN->Update(dTime);
00139     INPUTMAN->Update();
00140     return DISPLAY->Update(dTime);
00141 }
00142 
00143 void HGEXPORT CloseMercury()
00144 {
00145     //Screens are deleted first.
00146     SAFE_DELETE(SCREENMAN);
00147 
00148     FONTHASH.Destroy();
00149     SAFE_DELETE(TEXTMAN);
00150     SAFE_DELETE(DISPLAY);
00151     SAFE_DELETE(INPUTMAN);
00152     SAFE_DELETE(PREFSMAN);
00153     SAFE_DELETE(MESSAGEMAN);
00154     LOG.Info( "CloseMercury done." );
00155     OBJECTREGISTER.ShutDown();
00156     BENCHMARK.Print();
00157 }
00158 
00159 #if !defined( MAKE_DLL )
00160 int main(int argc, char* argv[]) {
00161     if ( InitializeMercury( argc, argv ) != 0 )
00162     {
00163         printf( "Mercury Initialization failed.\n" );
00164         return -1;
00165     }
00166 
00167     float dTime = -1;
00168     do {
00169         //Do Stuff
00170         dTime = -1; //Reset
00171     } while ( UpdateMercury( dTime ) );
00172 
00173     CloseMercury();
00174     return 0;
00175 }
00176 #else
00177 bool HGEXPORT DllMain( HANDLE hModule, 
00178                        DWORD  ul_reason_for_call, 
00179                        LPVOID lpReserved
00180                      )
00181 {
00182     switch (ul_reason_for_call)
00183     {
00184     case DLL_PROCESS_ATTACH:
00185     case DLL_THREAD_ATTACH:
00186     case DLL_THREAD_DETACH:
00187     case DLL_PROCESS_DETACH:
00188         break;
00189     }
00190     return TRUE;
00191 }
00192 
00193 #endif
00194 
00195 /* 
00196  * Copyright (c) 2005-2006, Joshua Allen
00197  * All rights reserved.
00198  *
00199  * Redistribution and use in source and binary forms, with or
00200  * without modification, are permitted provided that the following
00201  * conditions are met:
00202  *  -   Redistributions of source code must retain the above
00203  *      copyright notice, this list of conditions and the following disclaimer.
00204  *  -   Redistributions in binary form must reproduce the above copyright
00205  *      notice, this list of conditions and the following disclaimer in
00206  *      the documentation and/or other materials provided with the distribution.
00207  *  -   Neither the name of the Mercury Engine nor the names of its
00208  *      contributors may be used to endorse or promote products derived from
00209  *      this software without specific prior written permission.
00210  *
00211  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00212  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00213  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00214  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00215  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00216  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00217  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00218  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00219  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00220  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00221  */

Hosted by SourceForge.net Logo