2010-03-13 22:10:45 +01:00
// This file is part of Glest (www.glest.org)
//
2010-04-11 03:25:06 +02:00
// Copyright (C) 2001-2008 Martio 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"
# 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 ;
// =====================================================
// class Program::CrashProgramState
// =====================================================
Program : : ShowMessageProgramState : : ShowMessageProgramState ( Program * program , const char * msg ) :
ProgramState ( program ) {
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 17:27:17 +01:00
userWantsExit = false ;
2010-03-17 07:25:19 +01:00
msgBox . init ( " Ok " ) ;
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 07:25:19 +01:00
if ( msg ) {
fprintf ( stderr , " %s \n " , msg ) ;
msgBox . setText ( msg ) ;
} else {
msgBox . setText ( " Mega-Glest has crashed. " ) ;
}
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 07:25:19 +01:00
mouse2dAnim = mouseY = mouseX = 0 ;
this - > msg = ( msg ? msg : " " ) ;
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 07:25:19 +01:00
}
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 ) {
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 07:25:19 +01:00
2010-04-12 22:32:47 +02:00
int button = 1 ;
if ( msgBox . mouseClick ( x , y , button ) ) {
2010-03-17 07:25:19 +01:00
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
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
}
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 07:25:19 +01:00
}
2010-03-23 09:46:20 +01:00
void Program : : ShowMessageProgramState : : keyPress ( char c ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] c = [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , c ) ;
// if user pressed return we exit
if ( c = = 13 ) {
program - > exit ( ) ;
userWantsExit = true ;
}
else {
//msgBox.keyPress(c);
}
}
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 ========================
2010-03-17 07:25:19 +01:00
Program : : Program ( ) {
2010-08-25 01:15:33 +02:00
skipRenderFrameCount = 0 ;
2010-03-13 22:10:45 +01:00
programState = NULL ;
2010-03-17 07:25:19 +01:00
singleton = this ;
2010-05-01 06:34:23 +02:00
soundThreadManager = NULL ;
2010-07-08 10:29:51 +02:00
//mesage box
Lang & lang = Lang : : getInstance ( ) ;
msgBox . init ( lang . get ( " Ok " ) ) ;
msgBox . setEnabled ( false ) ;
2010-03-13 22:10:45 +01:00
}
void Program : : initNormal ( WindowGl * window ) {
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
init ( window ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
setState ( new Intro ( this ) ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
}
void Program : : initServer ( WindowGl * window ) {
MainMenu * mainMenu = NULL ;
init ( window ) ;
mainMenu = new MainMenu ( this ) ;
setState ( mainMenu ) ;
mainMenu - > setState ( new MenuStateCustomGame ( this , mainMenu , true ) ) ;
}
void Program : : initClient ( WindowGl * window , const Ip & serverIp ) {
MainMenu * mainMenu = NULL ;
init ( window ) ;
mainMenu = new MainMenu ( this ) ;
setState ( mainMenu ) ;
mainMenu - > setState ( new MenuStateJoinGame ( this , mainMenu , true , serverIp ) ) ;
}
Program : : ~ Program ( ) {
delete programState ;
2010-03-17 07:25:19 +01:00
programState = NULL ;
2010-03-13 22:10:45 +01:00
Renderer : : getInstance ( ) . end ( ) ;
//restore video mode
restoreDisplaySettings ( ) ;
2010-03-17 07:25:19 +01:00
singleton = NULL ;
2010-05-01 06:34:23 +02:00
BaseThread : : shutdownAndWait ( soundThreadManager ) ;
2010-05-01 11:10:52 +02:00
delete soundThreadManager ;
2010-05-01 06:34:23 +02:00
soundThreadManager = NULL ;
2010-03-13 22:10:45 +01:00
}
void Program : : keyDown ( char key ) {
2010-07-08 10:29:51 +02:00
if ( msgBox . getEnabled ( ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( key = = vkEscape | | key = = vkReturn ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
msgBox . setEnabled ( false ) ;
}
}
2010-03-13 22:10:45 +01:00
//delegate event
programState - > keyDown ( key ) ;
}
void Program : : keyUp ( char key ) {
programState - > keyUp ( key ) ;
}
void Program : : keyPress ( char c ) {
programState - > keyPress ( c ) ;
}
2010-07-08 10:29:51 +02:00
void Program : : mouseDownLeft ( int x , int y ) {
if ( msgBox . getEnabled ( ) ) {
int button = 1 ;
if ( msgBox . mouseClick ( x , y , button ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//close message box
msgBox . setEnabled ( false ) ;
}
}
}
void Program : : eventMouseMove ( int x , int y , const MouseState * ms ) {
if ( msgBox . getEnabled ( ) ) {
msgBox . mouseMove ( x , y ) ;
}
}
2010-05-07 08:46:55 +02:00
void Program : : simpleTask ( ) {
loopWorker ( ) ;
}
void Program : : loop ( ) {
2010-05-28 07:31:17 +02:00
loopWorker ( ) ;
2010-05-07 08:46:55 +02:00
}
void Program : : loopWorker ( ) {
2010-08-25 01:15:33 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] ================================= MAIN LOOP START ================================= \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
Chrono chronoLoop ;
chronoLoop . start ( ) ;
2010-05-07 08:46:55 +02:00
2010-05-06 23:29:46 +02:00
Chrono chrono ;
chrono . start ( ) ;
2010-03-23 09:46:20 +01:00
2010-05-08 11:06:30 +02:00
//getWindow()->makeCurrentGl();
2010-07-08 10:29:51 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-07 08:46:55 +02: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 ) {
2010-08-06 05:55:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-07 06:49:06 +02:00
Stats endStats = this - > programState - > quitAndToggleState ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
Game : : exitGameState ( this , endStats ) ;
2010-08-06 05:55:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __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 ;
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-08-26 01:55:59 +02:00
//if(skipRenderFrameCount <= 0) {
programState - > render ( ) ;
//}
//else {
// skipRenderFrameCount--;
//}
2010-05-07 08:46:55 +02:00
2010-05-11 01:58:07 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-03 08:25:54 +02:00
2010-08-25 01:15:33 +02:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP RENDERING \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
if ( chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-07-08 10:29:51 +02:00
2010-03-13 22:10:45 +01:00
//update camera
2010-05-06 23:29:46 +02:00
chrono . start ( ) ;
2010-03-13 22:10:45 +01:00
while ( updateCameraTimer . isTime ( ) ) {
programState - > updateCamera ( ) ;
}
2010-05-11 01:58:07 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-07 08:46:55 +02:00
2010-08-25 01:15:33 +02:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP CAMERA UPDATING \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
if ( chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-03-23 09:46:20 +01:00
2010-03-13 22:10:45 +01:00
//update world
2010-05-06 23:29:46 +02:00
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-04-11 03:25:06 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-13 22:10:45 +01:00
GraphicComponent : : update ( ) ;
programState - > update ( ) ;
2010-07-08 10:29:51 +02:00
if ( prevState = = this - > programState ) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-04-11 03:25:06 +02:00
2010-07-08 10:29:51 +02:00
if ( soundThreadManager = = NULL ) {
SoundRenderer : : getInstance ( ) . update ( ) ;
}
2010-04-11 03:25:06 +02:00
2010-07-08 10:29:51 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-04-11 03:25:06 +02:00
2010-07-08 10:29:51 +02:00
NetworkManager : : getInstance ( ) . update ( ) ;
2010-04-11 03:25:06 +02:00
2010-07-08 10:29:51 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
2010-08-25 01:15:33 +02:00
updateCount + + ;
2010-03-13 22:10:45 +01:00
}
2010-08-25 01:15:33 +02:00
//if(chrono.getMillis() >= 100) {
// skipRenderFrameCount = (chrono.getMillis() / 100);
//}
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP BODY LOGIC, updateCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) , updateCount ) ;
if ( chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-05-07 08:46:55 +02:00
2010-05-11 01:58:07 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-23 09:46:20 +01:00
2010-07-08 10:29:51 +02:00
if ( prevState = = this - > programState ) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//fps timer
chrono . start ( ) ;
while ( fpsTimer . isTime ( ) ) {
programState - > tick ( ) ;
}
2010-08-25 01:15:33 +02:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP TICKING \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
if ( chrono . getMillis ( ) > 0 ) chrono . start ( ) ;
2010-03-13 22:10:45 +01:00
}
2010-03-23 09:46:20 +01:00
2010-07-08 10:29:51 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-08-25 01:15:33 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] ------------------------------- MAIN LOOP END, stats: loop took msecs: %lld ------------------------------- \n " , __FILE__ , __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 ( ) {
if ( msgBox . getEnabled ( ) ) {
Renderer & renderer = Renderer : : getInstance ( ) ;
renderer . renderMessageBox ( & msgBox ) ;
}
}
2010-06-25 07:05:52 +02:00
2010-04-01 09:46:14 +02:00
void Program : : setState ( ProgramState * programState , bool cleanupOldState )
2010-03-13 22:10:45 +01:00
{
2010-04-12 22:32:47 +02:00
try {
2010-06-05 17:48:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2010-07-10 00:16:26 +02:00
bool msgBoxEnabled = msgBox . getEnabled ( ) ;
2010-06-25 07:05:52 +02:00
if ( dynamic_cast < Game * > ( programState ) ! = NULL ) {
2010-07-06 07:30:34 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-25 07:05:52 +02:00
int X = 0 ;
int Y = 0 ;
SDL_GetMouseState ( & X , & Y ) ;
programState - > setStartXY ( X , Y ) ;
SDL_PumpEvents ( ) ;
showCursor ( true ) ;
SDL_PumpEvents ( ) ;
sleep ( 0 ) ;
2010-07-06 07:30:34 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __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 ) {
2010-06-01 21:48:35 +02:00
if ( this - > programState ! = programState ) {
2010-06-05 17:48:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-01 21:48:35 +02:00
delete this - > programState ;
2010-08-04 18:56:24 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-01 21:48:35 +02:00
this - > programState = NULL ;
2010-08-04 18:56:24 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __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
2010-06-05 17:48:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2010-07-08 10:29:51 +02:00
//mesage box
Lang & lang = Lang : : getInstance ( ) ;
msgBox . init ( lang . get ( " 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 ) ;
2010-04-12 22:32:47 +02:00
this - > programState = programState ;
programState - > load ( ) ;
2010-03-13 22:10:45 +01:00
2010-06-05 17:48:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2010-04-12 22:32:47 +02:00
programState - > init ( ) ;
2010-03-13 22:10:45 +01:00
2010-06-05 17:48:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __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-06-24 03:23:18 +02:00
Config & config = Config : : getInstance ( ) ;
if ( config . getBool ( " No2DMouseRendering " , " false " ) = = false ) {
showCursor ( false ) ;
}
2010-06-16 08:35:35 +02:00
sleep ( 0 ) ;
2010-07-10 00:16:26 +02:00
if ( dynamic_cast < Intro * > ( programState ) ! = NULL & & msgBoxEnabled = = true ) {
showCursor ( true ) ;
}
2010-06-05 17:48:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
}
catch ( const exception & e ) {
//exceptionMessage(e);
//throw runtime_error(e.what());
2010-06-05 17:48:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
this - > showMessage ( e . what ( ) ) ;
setState ( new Intro ( this ) ) ;
}
2010-03-13 22:10:45 +01:00
}
2010-03-17 17:27:17 +01:00
void Program : : exit ( ) {
2010-03-17 07:25:19 +01:00
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 07:25:19 +01:00
2010-03-13 22:10:45 +01:00
window - > destroy ( ) ;
2010-03-17 07:25:19 +01:00
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-13 22:10:45 +01:00
}
// ==================== PRIVATE ====================
2010-04-01 09:46:14 +02:00
void Program : : init ( WindowGl * window , bool initSound , bool toggleFullScreen ) {
2010-03-13 22:10:45 +01:00
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
this - > window = window ;
Config & config = Config : : getInstance ( ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
//window
window - > setText ( " Glest " ) ;
window - > setStyle ( config . getBool ( " Windowed " ) ? wsWindowedFixed : wsFullscreen ) ;
window - > setPos ( 0 , 0 ) ;
window - > setSize ( config . getInt ( " ScreenWidth " ) , config . getInt ( " ScreenHeight " ) ) ;
window - > create ( ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
//timers
fpsTimer . init ( 1 , maxTimes ) ;
updateTimer . init ( GameConstants : : updateFps , maxTimes ) ;
updateCameraTimer . init ( GameConstants : : cameraFps , maxTimes ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 " ;
if ( getGameReadWritePath ( ) ! = " " ) {
logFile = getGameReadWritePath ( ) + logFile ;
}
logger . setFile ( logFile ) ;
2010-03-13 22:10:45 +01:00
logger . clear ( ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
//lang
Lang & lang = Lang : : getInstance ( ) ;
lang . loadStrings ( config . getString ( " Lang " ) ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
//render
Renderer & renderer = Renderer : : getInstance ( ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-11 09:38:08 +02:00
window - > initGl ( config . getInt ( " ColorBits " ) , config . getInt ( " DepthBits " ) , config . getInt ( " StencilBits " ) , config . getBool ( " HardwareAcceleration " , " false " ) , config . getBool ( " FullScreenAntiAliasing " , " false " ) ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
window - > makeCurrentGl ( ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
//coreData, needs renderer, but must load before renderer init
CoreData & coreData = CoreData : : getInstance ( ) ;
coreData . load ( ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
//init renderer (load global textures)
renderer . init ( ) ;
2010-03-30 02:25:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] initOk = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , initOk ) ;
// Test sound system failed
//initOk = false;
// END
if ( initOk = = false ) {
string sError = " Sound System could not be initialzed! " ;
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
2010-05-03 08:25:54 +02:00
if ( config . getBool ( " ThreadedSoundStream " , " false " ) = = true ) {
2010-05-01 11:10:52 +02:00
BaseThread : : shutdownAndWait ( soundThreadManager ) ;
delete soundThreadManager ;
soundThreadManager = new SimpleTaskThread ( & SoundRenderer : : getInstance ( ) , 0 , 50 ) ;
2010-06-18 22:04:05 +02:00
soundThreadManager - > setUniqueID ( __FILE__ ) ;
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
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
}
void Program : : setDisplaySettings ( ) {
Config & config = Config : : getInstance ( ) ;
if ( ! config . getBool ( " Windowed " ) ) {
int freq = config . getInt ( " RefreshFrequency " ) ;
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 ) {
getFullscreenVideoInfo ( colorBits , screenWidth , screenHeight ) ;
config . setInt ( " ColorBits " , colorBits ) ;
config . setInt ( " ScreenWidth " , screenWidth ) ;
config . setInt ( " ScreenHeight " , screenHeight ) ;
}
2010-03-13 22:10:45 +01:00
if ( ! ( changeVideoMode ( screenWidth , screenHeight , colorBits , freq ) | |
changeVideoMode ( screenWidth , screenHeight , colorBits , 0 ) ) )
{
throw runtime_error (
" Error setting video mode: " +
intToStr ( screenWidth ) + " x " + intToStr ( screenHeight ) + " x " + intToStr ( colorBits ) ) ;
}
}
}
void Program : : restoreDisplaySettings ( ) {
Config & config = Config : : getInstance ( ) ;
if ( ! config . getBool ( " Windowed " ) ) {
restoreVideoMode ( ) ;
}
}
2010-03-17 07:25:19 +01:00
void Program : : showMessage ( const char * msg ) {
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] msg [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , msg ) ;
msgBox . setText ( msg ) ;
//msgBox.setHeader(header);
msgBox . setEnabled ( true ) ;
/*
int showMouseState = SDL_ShowCursor ( SDL_QUERY ) ;
2010-03-17 07:25:19 +01:00
ProgramState * originalState = NULL ;
2010-03-17 08:22:04 +01:00
if ( this - > programState ) {
2010-03-17 07:25:19 +01:00
//delete programState;
2010-03-17 08:22:04 +01:00
originalState = this - > programState ;
2010-03-17 07:25:19 +01:00
}
2010-03-23 09:46:20 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2010-03-17 17:27:17 +01:00
ShowMessageProgramState * showMsg = new ShowMessageProgramState ( this , msg ) ;
2010-03-17 18:24:12 +01:00
this - > programState = NULL ;
setState ( showMsg ) ;
2010-03-17 07:25:19 +01:00
2010-03-23 09:46:20 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2010-07-08 10:29:51 +02:00
showCursor ( true ) ;
2010-03-17 17:27:17 +01:00
while ( Window : : handleEvent ( ) & & showMsg - > wantExit ( ) = = false ) {
2010-03-17 07:25:19 +01:00
loop ( ) ;
}
2010-03-23 09:46:20 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2010-03-17 08:22:04 +01:00
delete this - > programState ;
this - > programState = NULL ;
2010-03-17 07:25:19 +01:00
2010-03-23 09:46:20 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2010-06-24 03:23:18 +02:00
Config & config = Config : : getInstance ( ) ;
2010-07-08 10:29:51 +02:00
//if(config.getBool("No2DMouseRendering","false") == false) {
showCursor ( ( showMouseState = = SDL_ENABLE ) ) ;
//}
2010-03-17 18:24:12 +01:00
2010-03-17 19:04:44 +01:00
init ( this - > window , false ) ;
2010-03-17 21:40:06 +01:00
2010-03-23 09:46:20 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] START \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 21:40:06 +01:00
delete this - > programState ;
2010-03-23 09:46:20 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 21:40:06 +01:00
this - > programState = originalState ;
2010-03-23 09:46:20 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
*/
2010-03-17 07:25:19 +01:00
2010-03-23 03:35:55 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-17 07:25:19 +01:00
}
2010-03-13 22:10:45 +01:00
} } //end namespace