2012-10-30 07:48:12 +01:00
// ==============================================================
2010-03-13 22:10:45 +01:00
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-13 22:10:45 +01:00
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
# include "program.h"
# include "sound.h"
# include "renderer.h"
# include "config.h"
# include "game.h"
# include "main_menu.h"
# include "intro.h"
# include "world.h"
# include "main.h"
# include "sound_renderer.h"
# include "logger.h"
# include "profiler.h"
# include "core_data.h"
# include "metrics.h"
# include "network_manager.h"
# include "menu_state_custom_game.h"
# include "menu_state_join_game.h"
2010-09-01 08:05:28 +02:00
# include "menu_state_scenario.h"
2010-03-13 22:10:45 +01:00
# include "leak_dumper.h"
using namespace Shared : : Util ;
using namespace Shared : : Graphics ;
using namespace Shared : : Graphics : : Gl ;
// =====================================================
// class Program
// =====================================================
namespace Glest { namespace Game {
const int Program : : maxTimes = 10 ;
2010-03-17 07:25:19 +01:00
Program * Program : : singleton = NULL ;
2011-03-27 17:55:55 +02:00
const int SOUND_THREAD_UPDATE_MILLISECONDS = 25 ;
2010-03-17 07:25:19 +01:00
2011-09-27 20:44:01 +02:00
bool Program : : wantShutdownApplicationAfterGame = false ;
2013-11-11 09:28:15 +01:00
const char * ProgramState : : MAIN_PROGRAM_RENDER_KEY = " MEGAGLEST.RENDER " ;
2011-09-27 20:44:01 +02:00
2010-03-17 07:25:19 +01:00
// =====================================================
// class Program::CrashProgramState
// =====================================================
2011-07-01 04:50:20 +02:00
ProgramState : : ProgramState ( Program * program ) {
this - > program = program ;
this - > forceMouseRender = false ;
this - > mouseX = 0 ;
this - > mouseY = 0 ;
this - > mouse2dAnim = 0 ;
this - > fps = 0 ;
this - > lastFps = 0 ;
2011-09-01 01:10:43 +02:00
this - > startX = 0 ;
this - > startY = 0 ;
2011-07-01 04:50:20 +02:00
}
void ProgramState : : incrementFps ( ) {
fps + + ;
}
void ProgramState : : tick ( ) {
lastFps = fps ;
fps = 0 ;
}
bool ProgramState : : canRender ( bool sleepIfCannotRender ) {
2011-11-08 17:27:15 +01:00
int maxFPSCap = Config : : getInstance ( ) . getInt ( " RenderFPSCap " , " 500 " ) ;
int sleepMillis = Config : : getInstance ( ) . getInt ( " RenderFPSCapSleepMillis " , " 1 " ) ;
2012-03-28 08:25:57 +02:00
//Renderer &renderer= Renderer::getInstance();
2011-12-02 23:04:02 +01:00
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = true ) {
2011-11-08 17:27:15 +01:00
maxFPSCap = Config : : getInstance ( ) . getInt ( " RenderFPSCapHeadless " , " 250 " ) ;
sleepMillis = Config : : getInstance ( ) . getInt ( " RenderFPSCapHeadlessSleepMillis " , " 1 " ) ;
2011-11-08 17:16:57 +01:00
}
if ( lastFps > maxFPSCap ) {
2011-07-01 04:50:20 +02:00
if ( sleepIfCannotRender = = true ) {
2011-11-08 17:27:15 +01:00
sleep ( sleepMillis ) ;
2012-03-29 20:14:25 +02:00
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] sleeping because lastFps = %d, maxFPSCap = %d sleepMillis = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,lastFps,maxFPSCap,sleepMillis);
2011-07-01 04:50:20 +02:00
}
return false ;
}
return true ;
}
2011-04-09 03:22:39 +02:00
void ProgramState : : render ( ) {
Renderer & renderer = Renderer : : getInstance ( ) ;
2011-07-01 04:50:20 +02:00
canRender ( ) ;
incrementFps ( ) ;
2011-12-02 23:04:02 +01:00
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = false ) {
2011-09-27 12:16:09 +02:00
renderer . clearBuffers ( ) ;
renderer . reset2d ( ) ;
renderer . renderMessageBox ( program - > getMsgBox ( ) ) ;
renderer . renderMouse2d ( mouseX , mouseY , mouse2dAnim ) ;
renderer . swapBuffers ( ) ;
}
2011-04-09 03:22:39 +02:00
}
void ProgramState : : update ( ) {
mouse2dAnim = ( mouse2dAnim + 1 ) % Renderer : : maxMouse2dAnim ;
}
void ProgramState : : mouseMove ( int x , int y , const MouseState * mouseState ) {
mouseX = x ;
mouseY = y ;
program - > getMsgBox ( ) - > mouseMove ( x , y ) ;
}
2010-03-17 07:25:19 +01:00
Program : : ShowMessageProgramState : : ShowMessageProgramState ( Program * program , const char * msg ) :
ProgramState ( program ) {
2010-03-17 17:27:17 +01:00
userWantsExit = false ;
2010-03-17 07:25:19 +01:00
msgBox . init ( " Ok " ) ;
if ( msg ) {
fprintf ( stderr , " %s \n " , msg ) ;
msgBox . setText ( msg ) ;
2012-04-02 03:45:13 +02:00
}
else {
2010-03-17 07:25:19 +01:00
msgBox . setText ( " Mega-Glest has crashed. " ) ;
}
mouse2dAnim = mouseY = mouseX = 0 ;
this - > msg = ( msg ? msg : " " ) ;
}
void Program : : ShowMessageProgramState : : render ( ) {
Renderer & renderer = Renderer : : getInstance ( ) ;
renderer . clearBuffers ( ) ;
renderer . reset2d ( ) ;
renderer . renderMessageBox ( & msgBox ) ;
renderer . renderMouse2d ( mouseX , mouseY , mouse2dAnim ) ;
renderer . swapBuffers ( ) ;
}
void Program : : ShowMessageProgramState : : mouseDownLeft ( int x , int y ) {
2012-02-19 18:37:53 +01:00
int button = 0 ;
2010-04-12 22:32:47 +02:00
if ( msgBox . mouseClick ( x , y , button ) ) {
2010-03-17 07:25:19 +01:00
program - > exit ( ) ;
2010-03-17 17:27:17 +01:00
userWantsExit = true ;
2010-03-17 07:25:19 +01:00
}
}
2011-06-26 03:55:08 +02:00
void Program : : ShowMessageProgramState : : keyPress ( SDL_KeyboardEvent c ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] c = [%d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , c . keysym . sym ) ;
2010-03-23 09:46:20 +01:00
// if user pressed return we exit
2011-06-26 03:55:08 +02:00
//if(c == 13) {
if ( isKeyPressed ( SDLK_RETURN , c ) = = true ) {
2010-03-23 09:46:20 +01:00
program - > exit ( ) ;
userWantsExit = true ;
}
}
2010-03-17 07:25:19 +01:00
void Program : : ShowMessageProgramState : : mouseMove ( int x , int y , const MouseState & mouseState ) {
mouseX = x ;
mouseY = y ;
msgBox . mouseMove ( x , y ) ;
}
void Program : : ShowMessageProgramState : : update ( ) {
mouse2dAnim = ( mouse2dAnim + 1 ) % Renderer : : maxMouse2dAnim ;
}
2010-03-13 22:10:45 +01:00
// ===================== PUBLIC ========================
2013-10-28 19:27:51 +01:00
bool Program : : rendererInitOk = false ;
bool Program : : tryingRendererInit = false ;
2010-03-17 07:25:19 +01:00
Program : : Program ( ) {
2011-12-02 23:04:02 +01:00
//this->masterserverMode = false;
2012-10-06 04:10:23 +02:00
this - > window = NULL ;
2011-09-27 20:44:01 +02:00
this - > shutdownApplicationEnabled = false ;
this - > skipRenderFrameCount = 0 ;
2012-04-02 03:45:13 +02:00
this - > messageBoxIsSystemError = false ;
this - > programStateOldSystemError = NULL ;
2011-09-27 20:44:01 +02:00
this - > programState = NULL ;
this - > singleton = this ;
this - > soundThreadManager = NULL ;
2010-07-08 10:29:51 +02:00
//mesage box
Lang & lang = Lang : : getInstance ( ) ;
2013-10-29 07:13:38 +01:00
msgBox . init ( lang . getString ( " Ok " ) ) ;
2010-07-08 10:29:51 +02:00
msgBox . setEnabled ( false ) ;
2010-03-13 22:10:45 +01:00
}
2011-12-02 23:04:02 +01:00
//bool Program::isMasterserverMode() const {
// return this->masterserverMode;
//}
2011-09-27 07:29:57 +02:00
2010-03-13 22:10:45 +01:00
void Program : : initNormal ( WindowGl * window ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
init ( window ) ;
2010-03-30 02:25:35 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
setState ( new Intro ( this ) ) ;
2010-03-30 02:25:35 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
}
2012-03-15 16:57:21 +01:00
void Program : : initSavedGame ( WindowGl * window , bool masterserverMode , string saveGameFile ) {
2012-03-13 00:08:22 +01:00
init ( window ) ;
2012-10-06 09:06:40 +02:00
MainMenu * mainMenu = new MainMenu ( this ) ;
2012-03-13 00:08:22 +01:00
setState ( mainMenu ) ;
2012-03-15 16:57:21 +01:00
if ( saveGameFile = = " " ) {
Config & config = Config : : getInstance ( ) ;
saveGameFile = config . getString ( " LastSavedGame " , " " ) ;
if ( saveGameFile = = " " ) {
saveGameFile = GameConstants : : saveGameFileDefault ;
if ( getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) ! = " " ) {
saveGameFile = getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) + saveGameFile ;
}
else {
string userData = Config : : getInstance ( ) . getString ( " UserData_Root " , " " ) ;
if ( userData ! = " " ) {
endPathWithSlash ( userData ) ;
}
saveGameFile = userData + saveGameFile ;
}
}
2012-03-13 00:08:22 +01:00
}
2012-03-15 16:57:21 +01:00
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Loading game from [%s]\n",saveGameFile.c_str());
printf ( " Loading saved game from [%s] \n " , saveGameFile . c_str ( ) ) ;
2012-03-13 00:08:22 +01:00
Game : : loadGame ( saveGameFile , this , masterserverMode ) ;
}
2011-09-24 09:46:56 +02:00
void Program : : initServer ( WindowGl * window , bool autostart , bool openNetworkSlots ,
bool masterserverMode ) {
2011-12-02 23:04:02 +01:00
//this->masterserverMode = masterserverMode;
2010-03-13 22:10:45 +01:00
init ( window ) ;
2012-10-06 09:06:40 +02:00
MainMenu * mainMenu = new MainMenu ( this ) ;
2010-03-13 22:10:45 +01:00
setState ( mainMenu ) ;
2011-10-30 23:23:57 +01:00
mainMenu - > setState ( new MenuStateCustomGame ( this , mainMenu , openNetworkSlots , pNewGame , autostart , NULL , masterserverMode ) ) ;
2010-03-13 22:10:45 +01:00
}
2011-09-16 02:34:14 +02:00
void Program : : initServer ( WindowGl * window , GameSettings * settings ) {
init ( window ) ;
2012-10-06 09:06:40 +02:00
MainMenu * mainMenu = new MainMenu ( this ) ;
2011-09-16 02:34:14 +02:00
setState ( mainMenu ) ;
2011-10-30 23:23:57 +01:00
mainMenu - > setState ( new MenuStateCustomGame ( this , mainMenu , false , pNewGame , true , settings ) ) ;
2011-09-16 02:34:14 +02:00
}
2012-09-25 23:52:23 +02:00
void Program : : initClient ( WindowGl * window , const Ip & serverIp , int portNumber ) {
2010-03-13 22:10:45 +01:00
init ( window ) ;
2012-10-06 09:06:40 +02:00
MainMenu * mainMenu = new MainMenu ( this ) ;
2010-03-13 22:10:45 +01:00
setState ( mainMenu ) ;
2012-09-25 23:52:23 +02:00
mainMenu - > setState ( new MenuStateJoinGame ( this , mainMenu , true , serverIp , portNumber ) ) ;
2010-03-13 22:10:45 +01:00
}
2012-10-06 02:25:13 +02:00
void Program : : initClientAutoFindHost ( WindowGl * window ) {
init ( window ) ;
2012-10-06 09:06:40 +02:00
MainMenu * mainMenu = new MainMenu ( this ) ;
2012-10-06 02:25:13 +02:00
setState ( mainMenu ) ;
bool autoFindHost = true ;
mainMenu - > setState ( new MenuStateJoinGame ( this , mainMenu , & autoFindHost ) ) ;
}
2010-09-01 08:05:28 +02:00
void Program : : initScenario ( WindowGl * window , string autoloadScenarioName ) {
init ( window ) ;
2012-10-06 09:06:40 +02:00
MainMenu * mainMenu = new MainMenu ( this ) ;
2010-09-01 08:05:28 +02:00
setState ( mainMenu ) ;
2012-06-30 21:11:54 +02:00
mainMenu - > setState ( new MenuStateScenario ( this , mainMenu , false ,
Config : : getInstance ( ) . getPathListForType ( ptScenarios ) , autoloadScenarioName ) ) ;
2010-09-01 08:05:28 +02:00
}
2010-03-13 22:10:45 +01:00
Program : : ~ Program ( ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
delete programState ;
2010-03-17 07:25:19 +01:00
programState = NULL ;
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2011-03-28 23:04:47 +02:00
2010-03-13 22:10:45 +01:00
Renderer : : getInstance ( ) . end ( ) ;
2011-12-02 17:07:59 +01:00
CoreData & coreData = CoreData : : getInstance ( ) ;
coreData . cleanup ( ) ;
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2011-03-28 23:04:47 +02:00
2010-03-13 22:10:45 +01:00
//restore video mode
restoreDisplaySettings ( ) ;
2010-03-17 07:25:19 +01:00
singleton = NULL ;
2010-05-01 06:34:23 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2011-03-28 23:04:47 +02:00
2011-03-27 17:55:55 +02:00
if ( soundThreadManager ! = NULL ) {
BaseThread : : shutdownAndWait ( soundThreadManager ) ;
delete soundThreadManager ;
soundThreadManager = NULL ;
}
2011-03-28 23:04:47 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
}
2012-04-02 03:45:13 +02:00
void Program : : restoreStateFromSystemError ( ) {
messageBoxIsSystemError = false ;
if ( this - > programStateOldSystemError = = NULL ) {
setState ( new Intro ( this ) ) ;
}
else {
setState ( this - > programStateOldSystemError ) ;
}
}
2011-06-26 03:55:08 +02:00
void Program : : keyDown ( SDL_KeyboardEvent key ) {
2010-07-08 10:29:51 +02:00
if ( msgBox . getEnabled ( ) ) {
2011-06-26 03:55:08 +02:00
//SDL_keysym keystate = Window::getKeystate();
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2011-06-26 03:55:08 +02:00
//if(key == vkEscape || key == SDLK_ESCAPE ||
// ((key == vkReturn || key == SDLK_RETURN || key == SDLK_KP_ENTER) && !(keystate.mod & (KMOD_LALT | KMOD_RALT)))) {
if ( isKeyPressed ( SDLK_ESCAPE , key ) = = true | | ( ( isKeyPressed ( SDLK_RETURN , key ) = = true ) & & ! ( key . keysym . mod & ( KMOD_LALT | KMOD_RALT ) ) ) ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2011-05-20 22:02:26 +02:00
//printf("---> keystate [%d]\n",keystate);
2010-07-08 10:29:51 +02:00
msgBox . setEnabled ( false ) ;
2012-04-02 03:45:13 +02:00
if ( messageBoxIsSystemError = = true ) {
restoreStateFromSystemError ( ) ;
}
2010-07-08 10:29:51 +02:00
}
}
2010-03-13 22:10:45 +01:00
//delegate event
programState - > keyDown ( key ) ;
}
2011-06-26 03:55:08 +02:00
void Program : : keyUp ( SDL_KeyboardEvent key ) {
2010-03-13 22:10:45 +01:00
programState - > keyUp ( key ) ;
}
2011-06-26 03:55:08 +02:00
void Program : : keyPress ( SDL_KeyboardEvent c ) {
2010-03-13 22:10:45 +01:00
programState - > keyPress ( c ) ;
}
2010-07-08 10:29:51 +02:00
void Program : : mouseDownLeft ( int x , int y ) {
if ( msgBox . getEnabled ( ) ) {
2012-02-19 18:37:53 +01:00
int button = 0 ;
2010-07-08 10:29:51 +02:00
if ( msgBox . mouseClick ( x , y , button ) ) {
2012-03-29 20:14:25 +02:00
//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
2010-07-08 10:29:51 +02:00
//close message box
msgBox . setEnabled ( false ) ;
2012-04-02 03:45:13 +02:00
if ( messageBoxIsSystemError = = true ) {
restoreStateFromSystemError ( ) ;
}
2010-07-08 10:29:51 +02:00
}
}
}
void Program : : eventMouseMove ( int x , int y , const MouseState * ms ) {
if ( msgBox . getEnabled ( ) ) {
msgBox . mouseMove ( x , y ) ;
}
}
2013-11-07 19:39:08 +01:00
void Program : : simpleTask ( BaseThread * callingThread , void * userdata ) {
2010-05-07 08:46:55 +02:00
loopWorker ( ) ;
}
void Program : : loop ( ) {
2010-05-28 07:31:17 +02:00
loopWorker ( ) ;
2010-05-07 08:46:55 +02:00
}
void Program : : loopWorker ( ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] ================================= MAIN LOOP START ================================= \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-25 01:15:33 +02:00
2012-03-28 08:25:57 +02:00
//Renderer &renderer= Renderer::getInstance();
2011-12-02 23:04:02 +01:00
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = false & & window ) {
2011-10-26 00:40:28 +02:00
MainWindow * mainWindow = dynamic_cast < MainWindow * > ( window ) ;
if ( mainWindow ) {
//mainWindow->render();
if ( mainWindow - > getTriggerLanguageToggle ( ) ) {
mainWindow - > toggleLanguage ( mainWindow - > getTriggerLanguage ( ) ) ;
}
}
}
2013-11-11 09:28:15 +01:00
Chrono chronoPerformanceCounts ;
2013-01-03 18:30:59 +01:00
bool showPerfStats = Config : : getInstance ( ) . getBool ( " ShowPerfStats " , " false " ) ;
Chrono chronoPerf ;
char perfBuf [ 8096 ] = " " ;
std : : vector < string > perfList ;
if ( showPerfStats ) chronoPerf . start ( ) ;
2010-08-25 01:15:33 +02:00
Chrono chronoLoop ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chronoLoop . start ( ) ;
2010-05-07 08:46:55 +02:00
2010-05-06 23:29:46 +02:00
Chrono chrono ;
2011-04-11 02:11:52 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-03-23 09:46:20 +01:00
2010-03-13 22:10:45 +01:00
//render
2010-05-03 08:25:54 +02:00
assert ( programState ! = NULL ) ;
2010-03-13 22:10:45 +01:00
2010-08-04 20:49:11 +02:00
if ( this - > programState - > quitTriggered ( ) = = true ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-07 06:49:06 +02:00
Stats endStats = this - > programState - > quitAndToggleState ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-07 06:49:06 +02:00
Game : : exitGameState ( this , endStats ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-07 06:49:06 +02:00
2010-08-04 20:49:11 +02:00
return ;
}
2010-07-08 10:29:51 +02:00
ProgramState * prevState = this - > programState ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2011-04-09 03:22:39 +02:00
assert ( programState ! = NULL ) ;
2013-11-11 09:28:15 +01:00
chronoPerformanceCounts . start ( ) ;
2010-08-26 01:55:59 +02:00
programState - > render ( ) ;
2010-05-03 08:25:54 +02:00
2013-11-11 09:28:15 +01:00
programState - > addPerformanceCount ( ProgramState : : MAIN_PROGRAM_RENDER_KEY , chronoPerformanceCounts . getMillis ( ) ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP RENDERING \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-07-08 10:29:51 +02:00
2010-03-13 22:10:45 +01:00
//update camera
2011-04-11 02:11:52 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2013-11-11 09:28:15 +01:00
chronoPerformanceCounts . start ( ) ;
while ( updateCameraTimer . isTime ( ) ) {
2010-03-13 22:10:45 +01:00
programState - > updateCamera ( ) ;
}
2013-11-11 09:28:15 +01:00
programState - > addPerformanceCount ( " programState->updateCamera() " , chronoPerformanceCounts . getMillis ( ) ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP CAMERA UPDATING \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-03-23 09:46:20 +01:00
2010-03-13 22:10:45 +01:00
//update world
2011-04-11 02:11:52 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-08-25 01:15:33 +02:00
int updateCount = 0 ;
2010-07-08 10:29:51 +02:00
while ( prevState = = this - > programState & & updateTimer . isTime ( ) ) {
2010-09-02 15:43:44 +02:00
Chrono chronoUpdateLoop ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chronoUpdateLoop . start ( ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " updateCount: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) , updateCount ) ;
perfList . push_back ( perfBuf ) ;
}
2010-09-02 15:43:44 +02:00
2010-03-13 22:10:45 +01:00
GraphicComponent : : update ( ) ;
programState - > update ( ) ;
2013-11-11 09:28:15 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chronoUpdateLoop . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] programState->update took msecs: %lld, updateCount = %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoUpdateLoop . getMillis ( ) , updateCount ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chronoUpdateLoop . getMillis ( ) > 0 ) chronoUpdateLoop . start ( ) ;
2010-09-02 15:43:44 +02:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " updateCount: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) , updateCount ) ;
perfList . push_back ( perfBuf ) ;
}
2010-07-08 10:29:51 +02:00
if ( prevState = = this - > programState ) {
2013-11-11 09:28:15 +01:00
chronoPerformanceCounts . start ( ) ;
2011-01-18 02:24:45 +01:00
if ( soundThreadManager = = NULL | | soundThreadManager - > isThreadExecutionLagging ( ) ) {
2011-04-09 03:22:39 +02:00
if ( soundThreadManager ! = NULL ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] ERROR / WARNING soundThreadManager->isThreadExecutionLagging is TRUE \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s %d] ERROR / WARNING soundThreadManager->isThreadExecutionLagging is TRUE \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2011-04-09 03:22:39 +02:00
}
2010-07-08 10:29:51 +02:00
SoundRenderer : : getInstance ( ) . update ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chronoUpdateLoop . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] SoundRenderer::getInstance().update() took msecs: %lld, updateCount = %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoUpdateLoop . getMillis ( ) , updateCount ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chronoUpdateLoop . getMillis ( ) > 0 ) chronoUpdateLoop . start ( ) ;
2010-07-08 10:29:51 +02:00
}
2010-04-11 03:25:06 +02:00
2013-11-11 09:28:15 +01:00
programState - > addPerformanceCount ( " SoundRenderer::getInstance().update() " , chronoPerformanceCounts . getMillis ( ) ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " updateCount: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) , updateCount ) ;
perfList . push_back ( perfBuf ) ;
}
2013-11-11 09:28:15 +01:00
chronoPerformanceCounts . start ( ) ;
2010-07-08 10:29:51 +02:00
NetworkManager : : getInstance ( ) . update ( ) ;
2013-11-11 09:28:15 +01:00
programState - > addPerformanceCount ( " NetworkManager::getInstance().update() " , chronoPerformanceCounts . getMillis ( ) ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chronoUpdateLoop . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] NetworkManager::getInstance().update() took msecs: %lld, updateCount = %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoUpdateLoop . getMillis ( ) , updateCount ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chronoUpdateLoop . getMillis ( ) > 0 ) chronoUpdateLoop . start ( ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " updateCount: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) , updateCount ) ;
perfList . push_back ( perfBuf ) ;
}
2010-07-08 10:29:51 +02:00
}
2010-08-25 01:15:33 +02:00
updateCount + + ;
2010-03-13 22:10:45 +01:00
}
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d AFTER programState->update took msecs: %lld ==============> MAIN LOOP BODY LOGIC, updateCount = %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) , updateCount ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-05-07 08:46:55 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-03-23 09:46:20 +01:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-07-08 10:29:51 +02:00
if ( prevState = = this - > programState ) {
//fps timer
2013-11-11 09:28:15 +01:00
chronoPerformanceCounts . start ( ) ;
2010-07-08 10:29:51 +02:00
chrono . start ( ) ;
while ( fpsTimer . isTime ( ) ) {
programState - > tick ( ) ;
}
2013-11-11 09:28:15 +01:00
programState - > addPerformanceCount ( " programState->tick() " , chronoPerformanceCounts . getMillis ( ) ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP TICKING \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2011-01-28 03:32:55 +01:00
2012-03-29 20:14:25 +02:00
//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP TICKING\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis());
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2011-01-28 03:32:55 +01:00
2010-03-13 22:10:45 +01:00
}
2010-03-23 09:46:20 +01:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats & & chronoPerf . getMillis ( ) > = 100 ) {
for ( unsigned int x = 0 ; x < perfList . size ( ) ; + + x ) {
printf ( " %s " , perfList [ x ] . c_str ( ) ) ;
}
}
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] ------------------------------- MAIN LOOP END, stats: loop took msecs: %lld ------------------------------- \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoLoop . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
void Program : : resize ( SizeState sizeState ) {
switch ( sizeState ) {
case ssMinimized :
//restoreVideoMode();
break ;
case ssMaximized :
case ssRestored :
//setDisplaySettings();
//renderer.reloadResources();
break ;
}
}
// ==================== misc ====================
2010-07-08 10:29:51 +02:00
void Program : : renderProgramMsgBox ( ) {
2011-10-25 20:39:11 +02:00
Renderer & renderer = Renderer : : getInstance ( ) ;
2010-07-08 10:29:51 +02:00
if ( msgBox . getEnabled ( ) ) {
renderer . renderMessageBox ( & msgBox ) ;
}
2011-10-25 20:39:11 +02:00
2011-12-02 23:04:02 +01:00
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = false & & window ) {
2011-10-25 20:39:11 +02:00
MainWindow * mainWindow = dynamic_cast < MainWindow * > ( window ) ;
if ( mainWindow ) {
mainWindow - > render ( ) ;
}
}
2010-07-08 10:29:51 +02:00
}
2010-06-25 07:05:52 +02:00
2012-04-02 03:45:13 +02:00
void Program : : setState ( ProgramState * programStateNew , bool cleanupOldState ) {
2010-04-12 22:32:47 +02:00
try {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2012-04-02 03:45:13 +02:00
this - > programStateOldSystemError = this - > programState ;
2010-07-10 00:16:26 +02:00
bool msgBoxEnabled = msgBox . getEnabled ( ) ;
2010-10-22 18:08:13 +02:00
bool showingOSCursor = isCursorShowing ( ) ;
2011-09-27 12:16:09 +02:00
if ( dynamic_cast < Game * > ( programStateNew ) ! = NULL ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-07-06 07:30:34 +02:00
2010-06-25 07:05:52 +02:00
int X = 0 ;
int Y = 0 ;
SDL_GetMouseState ( & X , & Y ) ;
2011-09-27 12:16:09 +02:00
programStateNew - > setStartXY ( X , Y ) ;
2011-01-09 13:41:08 +01:00
Logger : : getInstance ( ) . setProgress ( 0 ) ;
Logger : : getInstance ( ) . setState ( " " ) ;
2010-06-25 07:05:52 +02:00
SDL_PumpEvents ( ) ;
2010-12-05 00:19:43 +01:00
2010-06-25 07:05:52 +02:00
showCursor ( true ) ;
SDL_PumpEvents ( ) ;
sleep ( 0 ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-06-25 07:05:52 +02:00
}
2010-06-16 08:35:35 +02:00
2010-04-12 22:32:47 +02:00
if ( cleanupOldState = = true ) {
2011-09-27 12:16:09 +02:00
if ( this - > programState ! = programStateNew ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-06-01 21:48:35 +02:00
2012-04-02 03:45:13 +02:00
this - > programStateOldSystemError = NULL ;
2010-06-01 21:48:35 +02:00
delete this - > programState ;
2010-08-04 18:56:24 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-04 18:56:24 +02:00
2010-06-01 21:48:35 +02:00
this - > programState = NULL ;
2010-08-04 18:56:24 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-06-01 21:48:35 +02:00
}
2010-04-12 22:32:47 +02:00
}
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2010-07-08 10:29:51 +02:00
//mesage box
Lang & lang = Lang : : getInstance ( ) ;
2013-10-29 07:13:38 +01:00
msgBox . init ( lang . getString ( " Ok " ) ) ;
2010-07-10 00:16:26 +02:00
msgBox . setEnabled ( msgBoxEnabled ) ;
2010-07-08 10:29:51 +02:00
2010-08-25 20:26:17 +02:00
fpsTimer . init ( 1 , maxTimes ) ;
updateTimer . init ( GameConstants : : updateFps , maxTimes ) ;
updateCameraTimer . init ( GameConstants : : cameraFps , maxTimes ) ;
2011-09-27 12:16:09 +02:00
this - > programState = programStateNew ;
assert ( programStateNew ! = NULL ) ;
2013-06-08 05:40:04 +02:00
if ( programStateNew = = NULL ) {
throw megaglest_runtime_error ( " programStateNew == NULL " ) ;
}
2011-09-27 12:16:09 +02:00
programStateNew - > load ( ) ;
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2011-09-27 12:16:09 +02:00
programStateNew - > init ( ) ;
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2010-04-12 22:32:47 +02:00
updateTimer . reset ( ) ;
updateCameraTimer . reset ( ) ;
fpsTimer . reset ( ) ;
2010-03-13 22:10:45 +01:00
2010-10-22 18:08:13 +02:00
if ( showingOSCursor = = false ) {
Config & config = Config : : getInstance ( ) ;
if ( config . getBool ( " No2DMouseRendering " , " false " ) = = false ) {
showCursor ( false ) ;
}
sleep ( 0 ) ;
2010-06-16 08:35:35 +02:00
2011-09-27 12:16:09 +02:00
if ( dynamic_cast < Intro * > ( programStateNew ) ! = NULL & & msgBoxEnabled = = true ) {
2010-10-22 18:08:13 +02:00
showCursor ( true ) ;
}
2010-07-10 00:16:26 +02:00
}
2012-04-02 03:45:13 +02:00
this - > programStateOldSystemError = NULL ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
}
2012-11-11 00:19:42 +01:00
catch ( megaglest_runtime_error & e ) {
//printf("3333333 ex.wantStackTrace() = %d\n",e.wantStackTrace());
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] \n Error [%s] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , e . what ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugError , szBuf ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , szBuf ) ;
//abort();
//printf("44444444a ex.wantStackTrace() = %d\n",e.wantStackTrace());
messageBoxIsSystemError = true ;
string errorMsg = e . what ( ) ;
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = false ) {
if ( dynamic_cast < Game * > ( programStateNew ) ! = NULL ) {
try {
Game * game = dynamic_cast < Game * > ( programStateNew ) ;
Renderer & renderer = Renderer : : getInstance ( ) ;
renderer . initGame ( game , game - > getGameCameraPtr ( ) ) ;
}
catch ( megaglest_runtime_error & ex2 ) {
errorMsg + = " \n " + string ( ex2 . what ( ) ) ;
}
}
}
//printf("44444444b ex.wantStackTrace() = %d\n",e.wantStackTrace());
this - > showMessage ( errorMsg . c_str ( ) ) ;
//setState(new Intro(this));
}
2010-04-12 22:32:47 +02:00
catch ( const exception & e ) {
2012-04-02 03:45:13 +02:00
char szBuf [ 8096 ] = " " ;
2012-10-19 03:31:20 +02:00
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] \n Error [%s] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , e . what ( ) ) ;
2012-04-02 03:45:13 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , szBuf ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , szBuf ) ;
2012-04-02 17:17:31 +02:00
//abort();
2012-04-02 03:45:13 +02:00
messageBoxIsSystemError = true ;
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = false ) {
if ( dynamic_cast < Game * > ( programStateNew ) ! = NULL ) {
Game * game = dynamic_cast < Game * > ( programStateNew ) ;
Renderer & renderer = Renderer : : getInstance ( ) ;
renderer . initGame ( game , game - > getGameCameraPtr ( ) ) ;
}
}
2010-04-12 22:32:47 +02:00
this - > showMessage ( e . what ( ) ) ;
2012-04-02 03:45:13 +02:00
//setState(new Intro(this));
2010-04-12 22:32:47 +02:00
}
2010-03-13 22:10:45 +01:00
}
2010-03-17 17:27:17 +01:00
void Program : : exit ( ) {
2010-03-13 22:10:45 +01:00
window - > destroy ( ) ;
}
// ==================== PRIVATE ====================
2010-04-01 09:46:14 +02:00
void Program : : init ( WindowGl * window , bool initSound , bool toggleFullScreen ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
this - > window = window ;
Config & config = Config : : getInstance ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//set video mode
2010-04-01 09:46:14 +02:00
if ( toggleFullScreen = = false ) {
setDisplaySettings ( ) ;
}
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//window
2010-12-13 01:32:59 +01:00
window - > setText ( " MegaGlest " ) ;
2010-03-13 22:10:45 +01:00
window - > setStyle ( config . getBool ( " Windowed " ) ? wsWindowedFixed : wsFullscreen ) ;
window - > setPos ( 0 , 0 ) ;
window - > setSize ( config . getInt ( " ScreenWidth " ) , config . getInt ( " ScreenHeight " ) ) ;
window - > create ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//timers
fpsTimer . init ( 1 , maxTimes ) ;
updateTimer . init ( GameConstants : : updateFps , maxTimes ) ;
updateCameraTimer . init ( GameConstants : : cameraFps , maxTimes ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//log start
Logger & logger = Logger : : getInstance ( ) ;
2010-03-22 00:40:35 +01:00
string logFile = " glest.log " ;
2010-12-09 21:41:11 +01:00
if ( getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) ! = " " ) {
logFile = getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) + logFile ;
2011-03-10 18:31:55 +01:00
}
else {
string userData = config . getString ( " UserData_Root " , " " ) ;
if ( userData ! = " " ) {
2011-03-13 09:23:43 +01:00
endPathWithSlash ( userData ) ;
2011-03-10 18:31:55 +01:00
}
logFile = userData + logFile ;
2010-03-22 00:40:35 +01:00
}
logger . setFile ( logFile ) ;
2010-03-13 22:10:45 +01:00
logger . clear ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//lang
2011-09-01 01:10:43 +02:00
//Lang &lang= Lang::getInstance();
Lang : : getInstance ( ) ;
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//render
Renderer & renderer = Renderer : : getInstance ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2011-12-23 09:20:54 +01:00
window - > initGl ( config . getInt ( " ColorBits " ) ,
config . getInt ( " DepthBits " ) ,
config . getInt ( " StencilBits " ) ,
config . getBool ( " HardwareAcceleration " , " false " ) ,
config . getBool ( " FullScreenAntiAliasing " , " false " ) ,
config . getFloat ( " GammaValue " , " 0.0 " ) ) ;
2010-03-30 02:25:35 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
window - > makeCurrentGl ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//coreData, needs renderer, but must load before renderer init
CoreData & coreData = CoreData : : getInstance ( ) ;
coreData . load ( ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//init renderer (load global textures)
2013-10-28 19:27:51 +01:00
tryingRendererInit = true ;
2010-03-13 22:10:45 +01:00
renderer . init ( ) ;
2013-10-28 19:27:51 +01:00
tryingRendererInit = false ;
rendererInitOk = true ;
2010-03-13 22:10:45 +01:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-30 02:25:35 +02:00
2010-03-13 22:10:45 +01:00
//sound
2010-04-01 09:46:14 +02:00
if ( initSound = = true & & toggleFullScreen = = false ) {
2010-03-17 19:04:44 +01:00
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
2010-07-10 00:16:26 +02:00
bool initOk = soundRenderer . init ( window ) ;
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] initOk = %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , initOk ) ;
2010-07-10 00:16:26 +02:00
// Test sound system failed
//initOk = false;
// END
if ( initOk = = false ) {
2013-12-28 07:00:29 +01:00
string sError = " Sound System could not be initialized! " ;
2010-07-10 00:16:26 +02:00
this - > showMessage ( sError . c_str ( ) ) ;
}
2010-05-01 06:34:23 +02:00
2010-05-01 11:10:52 +02:00
// Run sound streaming in a background thread if enabled
2011-01-18 02:24:45 +01:00
if ( SoundRenderer : : getInstance ( ) . runningThreaded ( ) = = true ) {
if ( BaseThread : : shutdownAndWait ( soundThreadManager ) = = true ) {
delete soundThreadManager ;
}
2013-01-23 15:51:28 +01:00
static string mutexOwnerId = string ( extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) ) + string ( " _ " ) + intToStr ( __LINE__ ) ;
2011-03-27 17:55:55 +02:00
soundThreadManager = new SimpleTaskThread ( & SoundRenderer : : getInstance ( ) , 0 , SOUND_THREAD_UPDATE_MILLISECONDS ) ;
2013-01-23 15:51:28 +01:00
soundThreadManager - > setUniqueID ( mutexOwnerId ) ;
2010-05-01 11:10:52 +02:00
soundThreadManager - > start ( ) ;
}
2010-03-17 19:04:44 +01:00
}
2010-03-13 22:10:45 +01:00
2010-05-03 08:25:54 +02:00
NetworkInterface : : setAllowGameDataSynchCheck ( Config : : getInstance ( ) . getBool ( " AllowGameDataSynchCheck " , " false " ) ) ;
NetworkInterface : : setAllowDownloadDataSynch ( Config : : getInstance ( ) . getBool ( " AllowDownloadDataSynch " , " false " ) ) ;
2010-03-30 02:25:35 +02:00
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
}
2011-10-25 20:39:11 +02:00
void Program : : reloadUI ( ) {
if ( programState ) {
programState - > reloadUI ( ) ;
}
}
2010-03-13 22:10:45 +01:00
void Program : : setDisplaySettings ( ) {
Config & config = Config : : getInstance ( ) ;
2011-04-19 00:05:41 +02:00
if ( ! config . getBool ( " Windowed " ) ) {
2010-03-13 22:10:45 +01:00
2013-01-25 03:55:28 +01:00
//int freq= config.getInt("RefreshFrequency");
2010-03-13 22:10:45 +01:00
int colorBits = config . getInt ( " ColorBits " ) ;
int screenWidth = config . getInt ( " ScreenWidth " ) ;
int screenHeight = config . getInt ( " ScreenHeight " ) ;
2010-03-20 08:19:45 +01:00
if ( config . getBool ( " AutoMaxFullScreen " , " false " ) = = true ) {
2011-04-19 00:05:41 +02:00
getFullscreenVideoInfo ( colorBits , screenWidth , screenHeight , ! config . getBool ( " Windowed " ) ) ;
2010-03-20 08:19:45 +01:00
config . setInt ( " ColorBits " , colorBits ) ;
config . setInt ( " ScreenWidth " , screenWidth ) ;
config . setInt ( " ScreenHeight " , screenHeight ) ;
}
2010-03-13 22:10:45 +01:00
}
2013-01-01 03:24:23 +01:00
changeVideoModeFullScreen ( ! config . getBool ( " Windowed " ) ) ;
2010-03-13 22:10:45 +01:00
}
void Program : : restoreDisplaySettings ( ) {
Config & config = Config : : getInstance ( ) ;
if ( ! config . getBool ( " Windowed " ) ) {
restoreVideoMode ( ) ;
}
}
2011-01-02 01:39:13 +01:00
bool Program : : isMessageShowing ( ) {
return msgBox . getEnabled ( ) ;
}
2010-03-17 07:25:19 +01:00
void Program : : showMessage ( const char * msg ) {
2012-03-29 20:14:25 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] msg [%s] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , msg ) ;
2010-07-08 10:29:51 +02:00
msgBox . setText ( msg ) ;
msgBox . setEnabled ( true ) ;
2011-12-05 06:26:48 +01:00
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = true ) {
printf ( " Message: \n %s \n " , msg ) ;
2012-04-02 03:45:13 +02:00
if ( messageBoxIsSystemError = = true ) {
messageBoxIsSystemError = false ;
//setState(new Intro(this));
initServer ( window , false , true , true ) ;
}
2011-12-05 06:26:48 +01:00
}
2010-03-17 07:25:19 +01:00
}
2011-03-27 17:55:55 +02:00
void Program : : stopSoundSystem ( ) {
if ( soundThreadManager ! = NULL ) {
BaseThread : : shutdownAndWait ( soundThreadManager ) ;
delete soundThreadManager ;
soundThreadManager = NULL ;
}
}
void Program : : startSoundSystem ( ) {
stopSoundSystem ( ) ;
if ( SoundRenderer : : getInstance ( ) . runningThreaded ( ) = = true ) {
2013-01-23 15:51:28 +01:00
static string mutexOwnerId = string ( extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) ) + string ( " _ " ) + intToStr ( __LINE__ ) ;
2011-03-27 17:55:55 +02:00
soundThreadManager = new SimpleTaskThread ( & SoundRenderer : : getInstance ( ) , 0 , SOUND_THREAD_UPDATE_MILLISECONDS ) ;
2013-01-23 15:51:28 +01:00
soundThreadManager - > setUniqueID ( mutexOwnerId ) ;
2011-03-27 17:55:55 +02:00
soundThreadManager - > start ( ) ;
}
}
void Program : : resetSoundSystem ( ) {
startSoundSystem ( ) ;
}
2010-10-29 18:27:22 +02:00
void Program : : reInitGl ( ) {
if ( window ! = NULL ) {
Config & config = Config : : getInstance ( ) ;
2011-12-23 09:20:54 +01:00
window - > initGl ( config . getInt ( " ColorBits " ) ,
config . getInt ( " DepthBits " ) ,
config . getInt ( " StencilBits " ) ,
config . getBool ( " HardwareAcceleration " , " false " ) ,
config . getBool ( " FullScreenAntiAliasing " , " false " ) ,
config . getFloat ( " GammaValue " , " 0.0 " ) ) ;
2010-10-29 18:27:22 +02:00
}
}
2011-03-28 23:04:47 +02:00
void Program : : consoleAddLine ( string line ) {
if ( programState ! = NULL ) {
programState - > consoleAddLine ( line ) ;
}
}
2011-10-22 00:07:34 +02:00
SimpleTaskThread * Program : : getSoundThreadManager ( bool takeOwnership ) {
SimpleTaskThread * result = soundThreadManager ;
if ( takeOwnership ) {
soundThreadManager = NULL ;
}
return result ;
}
2010-03-13 22:10:45 +01:00
} } //end namespace