2010-04-05 19:43:04 +02: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-04-05 19:43:04 +02:00
//
2010-12-09 21:41:11 +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
2010-04-05 19:43:04 +02:00
// License, or (at your option) any later version
// ==============================================================
# include "logger.h"
# include "util.h"
# include "renderer.h"
2012-06-18 00:52:30 +02:00
# include "properties.h"
2013-06-26 00:44:57 +02:00
# include "sound_renderer.h"
2010-04-05 19:43:04 +02:00
# include "core_data.h"
# include "metrics.h"
# include "lang.h"
# include "graphics_interface.h"
2010-12-09 21:41:11 +01:00
# include "game_constants.h"
# include "game_util.h"
2011-05-18 23:49:11 +02:00
# include "platform_util.h"
2010-04-05 21:38:50 +02:00
# include "leak_dumper.h"
2010-04-05 19:43:04 +02:00
using namespace std ;
using namespace Shared : : Graphics ;
2010-04-05 21:38:50 +02:00
using namespace Shared : : Util ;
2010-04-05 19:43:04 +02:00
namespace Glest { namespace Game {
// =====================================================
// class Logger
// =====================================================
const int Logger : : logLineCount = 15 ;
2010-12-09 21:41:11 +01:00
// ===================== PUBLIC ========================
2010-04-05 19:43:04 +02:00
2011-01-11 23:09:46 +01:00
Logger : : Logger ( ) {
2012-10-06 04:10:23 +02:00
progress = 0 ;
2010-12-09 21:41:11 +01:00
string logs_path = getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) ;
2011-03-10 18:31:55 +01:00
if ( logs_path ! = " " ) {
fileName = logs_path + " log.txt " ;
}
else {
string userData = Config : : getInstance ( ) . getString ( " UserData_Root " , " " ) ;
if ( userData ! = " " ) {
2011-03-13 09:23:43 +01:00
endPathWithSlash ( userData ) ;
2011-03-10 18:31:55 +01:00
}
fileName = userData + " log.txt " ;
}
2010-04-05 19:43:04 +02:00
loadingTexture = NULL ;
2012-06-18 00:52:30 +02:00
gameHintToShow = " " ;
2011-01-11 23:09:46 +01:00
showProgressBar = false ;
2011-03-31 20:13:02 +02:00
2013-06-19 18:12:00 +02:00
displayColor = Vec4f ( 1.f , 1.f , 1.f , 0.1f ) ;
2011-03-31 20:13:02 +02:00
cancelSelected = false ;
buttonCancel . setEnabled ( false ) ;
2013-06-19 18:12:00 +02:00
buttonNextHint . setEnabled ( false ) ;
2010-04-05 19:43:04 +02:00
}
2011-01-11 23:09:46 +01:00
Logger : : ~ Logger ( ) {
2010-04-05 21:38:50 +02:00
}
2011-01-11 23:09:46 +01:00
Logger & Logger : : getInstance ( ) {
2010-04-05 19:43:04 +02:00
static Logger logger ;
return logger ;
}
2011-03-31 20:13:02 +02:00
void Logger : : add ( const string str , bool renderScreen , const string statusText ) {
2011-05-18 23:49:11 +02:00
# ifdef WIN32
FILE * f = _wfopen ( utf8_decode ( fileName ) . c_str ( ) , L " at+ " ) ;
# else
2011-03-31 20:13:02 +02:00
FILE * f = fopen ( fileName . c_str ( ) , " at+ " ) ;
2011-05-18 23:49:11 +02:00
# endif
2011-03-31 20:13:02 +02:00
if ( f ! = NULL ) {
2010-04-05 19:43:04 +02:00
fprintf ( f , " %s \n " , str . c_str ( ) ) ;
fclose ( f ) ;
}
2011-03-31 20:13:02 +02:00
this - > current = str ;
this - > statusText = statusText ;
2011-12-02 23:04:02 +01:00
if ( renderScreen = = true & & GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = false ) {
2010-04-05 19:43:04 +02:00
renderLoadingScreen ( ) ;
}
}
2011-01-11 23:09:46 +01:00
void Logger : : clear ( ) {
2011-03-31 20:13:02 +02:00
string s = " Log file \n " ;
2010-04-05 19:43:04 +02:00
2011-05-18 23:49:11 +02:00
# ifdef WIN32
FILE * f = _wfopen ( utf8_decode ( fileName ) . c_str ( ) , L " wt+ " ) ;
# else
2010-04-05 19:43:04 +02:00
FILE * f = fopen ( fileName . c_str ( ) , " wt+ " ) ;
2011-05-18 23:49:11 +02:00
# endif
2011-03-31 20:13:02 +02:00
if ( f = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Error opening log file " + fileName ) ;
2010-04-05 19:43:04 +02:00
}
2010-12-09 21:41:11 +01:00
2010-04-05 19:43:04 +02:00
fprintf ( f , " %s " , s . c_str ( ) ) ;
fprintf ( f , " \n " ) ;
fclose ( f ) ;
}
2011-01-11 23:09:46 +01:00
void Logger : : loadLoadingScreen ( string filepath ) {
2010-04-05 21:38:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-03-31 20:13:02 +02:00
if ( filepath = = " " ) {
loadingTexture = NULL ;
2010-04-05 19:43:04 +02:00
}
2011-03-31 20:13:02 +02:00
else {
2010-04-05 21:38:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] filepath = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , filepath . c_str ( ) ) ;
2012-09-25 09:05:52 +02:00
loadingTexture = Renderer : : findTexture ( filepath ) ;
2010-06-24 12:52:58 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-05 19:43:04 +02:00
}
2013-06-19 18:12:00 +02:00
Lang & lang = Lang : : getInstance ( ) ;
2013-10-29 07:13:38 +01:00
buttonCancel . setText ( lang . getString ( " Cancel " ) ) ;
2010-04-05 19:43:04 +02:00
}
2012-06-19 02:32:39 +02:00
void Logger : : loadGameHints ( string filePathEnglish , string filePathTranslation , bool clearList ) {
2012-06-18 00:52:30 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2012-06-19 02:32:39 +02:00
if ( ( filePathEnglish = = " " ) | | ( filePathTranslation = = " " ) ) {
2012-06-18 00:52:30 +02:00
return ;
}
else {
2012-06-19 02:32:39 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] filePathEnglish = [%s] \n filePathTranslation = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , filePathEnglish . c_str ( ) , filePathTranslation . c_str ( ) ) ;
gameHints . load ( filePathEnglish , clearList ) ;
gameHintsTranslation . load ( filePathTranslation , clearList ) ;
2013-06-13 22:08:50 +02:00
showNextHint ( ) ;
2013-06-19 18:12:00 +02:00
2013-06-13 22:08:50 +02:00
GraphicComponent : : applyAllCustomProperties ( " Loading " ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
}
void Logger : : showNextHint ( ) {
string key = gameHints . getRandomKey ( true ) ;
string tmpString = gameHintsTranslation . getString ( key , " " ) ;
if ( tmpString ! = " " ) {
gameHintToShow = tmpString ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] key [%s] not found for [%s] hint translation \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . c_str ( ) , Lang : : getInstance ( ) . getLanguage ( ) . c_str ( ) ) ;
tmpString = gameHints . getString ( key , " " ) ;
2012-06-19 02:32:39 +02:00
if ( tmpString ! = " " ) {
gameHintToShow = tmpString ;
}
else {
2013-06-13 22:08:50 +02:00
gameHintToShow = " Problems to resolve hint key ' " + key + " ' " ;
2012-06-19 02:32:39 +02:00
}
2013-06-13 22:08:50 +02:00
}
replaceAll ( gameHintToShow , " \\ n " , " \n " ) ;
2012-06-19 02:32:39 +02:00
2013-06-13 22:08:50 +02:00
Config & configKeys = Config : : getInstance ( std : : pair < ConfigType , ConfigType > ( cfgMainKeys , cfgUserKeys ) ) ;
2012-06-19 02:32:39 +02:00
2013-06-13 22:08:50 +02:00
vector < pair < string , string > > mergedKeySettings = configKeys . getMergedProperties ( ) ;
for ( unsigned int j = 0 ; j < mergedKeySettings . size ( ) ; + + j ) {
pair < string , string > & property = mergedKeySettings [ j ] ;
replaceAll ( gameHintToShow , " # " + property . first + " # " , property . second ) ;
2012-06-18 00:52:30 +02:00
}
2013-06-13 22:08:50 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2012-06-18 00:52:30 +02:00
}
2012-06-19 02:32:39 +02:00
void Logger : : clearHints ( ) {
gameHintToShow = " " ;
gameHints . clear ( ) ;
gameHintsTranslation . clear ( ) ;
}
2011-03-31 20:13:02 +02:00
void Logger : : handleMouseClick ( int x , int y ) {
if ( buttonCancel . getEnabled ( ) = = true ) {
if ( buttonCancel . mouseClick ( x , y ) ) {
cancelSelected = true ;
}
}
2013-06-19 18:12:00 +02:00
if ( buttonNextHint . getEnabled ( ) = = true & & buttonNextHint . mouseClick ( x , y ) = = true ) {
showNextHint ( ) ;
2013-06-26 00:44:57 +02:00
//buttonNextHint.setLighted(false);
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
CoreData & coreData = CoreData : : getInstance ( ) ;
soundRenderer . playFx ( coreData . getClickSoundC ( ) ) ;
2013-06-19 18:12:00 +02:00
}
2011-03-31 20:13:02 +02:00
}
2010-12-09 21:41:11 +01:00
// ==================== PRIVATE ====================
2010-04-05 19:43:04 +02:00
2011-01-09 11:03:33 +01:00
void Logger : : renderLoadingScreen ( ) {
2010-04-05 19:43:04 +02:00
Renderer & renderer = Renderer : : getInstance ( ) ;
CoreData & coreData = CoreData : : getInstance ( ) ;
const Metrics & metrics = Metrics : : getInstance ( ) ;
2013-06-19 18:12:00 +02:00
//3d
//renderer.reset3d();
//renderer.clearZBuffer();
2010-04-05 19:43:04 +02:00
renderer . reset2d ( ) ;
renderer . clearBuffers ( ) ;
2011-01-26 10:09:59 +01:00
if ( loadingTexture = = NULL ) {
2010-04-05 19:43:04 +02:00
renderer . renderBackground ( CoreData : : getInstance ( ) . getBackgroundTexture ( ) ) ;
}
2011-03-31 20:13:02 +02:00
else {
2010-04-05 19:43:04 +02:00
renderer . renderBackground ( loadingTexture ) ;
2010-12-09 21:41:11 +01:00
}
2011-01-11 23:09:46 +01:00
if ( showProgressBar = = true ) {
2011-06-10 05:09:19 +02:00
if ( Renderer : : renderText3DEnabled ) {
renderer . renderProgressBar3D (
progress ,
metrics . getVirtualW ( ) / 4 ,
59 * metrics . getVirtualH ( ) / 100 ,
coreData . getDisplayFontSmall3D ( ) ,
350 , " " ) ; // no string here, because it has to be language specific and does not give much information
}
else {
renderer . renderProgressBar (
progress ,
metrics . getVirtualW ( ) / 4 ,
59 * metrics . getVirtualH ( ) / 100 ,
coreData . getDisplayFontSmall ( ) ,
350 , " " ) ; // no string here, because it has to be language specific and does not give much information
}
2011-01-11 23:09:46 +01:00
}
2011-01-09 11:03:33 +01:00
2011-06-10 06:34:58 +02:00
int xLocation = metrics . getVirtualW ( ) / 4 ;
2011-06-10 05:09:19 +02:00
if ( Renderer : : renderText3DEnabled ) {
2011-06-10 06:34:58 +02:00
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow3D (
state , coreData . getMenuFontBig3D ( ) , displayColor ,
2011-06-10 06:34:58 +02:00
xLocation ,
65 * metrics . getVirtualH ( ) / 100 , false ) ;
2011-06-10 05:09:19 +02:00
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow3D (
current , coreData . getMenuFontNormal3D ( ) , displayColor ,
2011-06-10 06:34:58 +02:00
xLocation ,
2011-06-10 05:09:19 +02:00
62 * metrics . getVirtualH ( ) / 100 , false ) ;
if ( this - > statusText ! = " " ) {
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow3D (
this - > statusText , coreData . getMenuFontNormal3D ( ) , displayColor ,
2011-06-10 06:34:58 +02:00
xLocation ,
2011-06-10 05:09:19 +02:00
56 * metrics . getVirtualH ( ) / 100 , false ) ;
}
}
else {
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow (
state , coreData . getMenuFontBig ( ) , displayColor ,
2011-06-10 06:34:58 +02:00
xLocation ,
65 * metrics . getVirtualH ( ) / 100 , false ) ;
2011-06-10 05:09:19 +02:00
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow (
current , coreData . getMenuFontNormal ( ) , displayColor ,
2011-06-10 06:34:58 +02:00
xLocation ,
2011-06-10 05:09:19 +02:00
62 * metrics . getVirtualH ( ) / 100 , false ) ;
if ( this - > statusText ! = " " ) {
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow (
this - > statusText , coreData . getMenuFontNormal ( ) , displayColor ,
2011-06-10 06:34:58 +02:00
xLocation ,
2011-06-10 05:09:19 +02:00
56 * metrics . getVirtualH ( ) / 100 , false ) ;
}
}
2012-07-20 21:00:49 +02:00
if ( gameHintToShow ! = " " ) {
2013-06-19 18:12:00 +02:00
Lang & lang = Lang : : getInstance ( ) ;
2013-10-29 07:13:38 +01:00
string hintText = lang . getString ( " Hint " , " " , true ) ;
2012-07-20 21:00:49 +02:00
char szBuf [ 8096 ] = " " ;
2012-10-19 03:31:20 +02:00
snprintf ( szBuf , 8096 , hintText . c_str ( ) , gameHintToShow . c_str ( ) ) ;
2012-07-20 21:00:49 +02:00
hintText = szBuf ;
2012-06-18 00:52:30 +02:00
if ( Renderer : : renderText3DEnabled ) {
2012-07-20 21:00:49 +02:00
int xLocationHint = ( metrics . getVirtualW ( ) / 2 ) - ( coreData . getMenuFontBig3D ( ) - > getMetrics ( ) - > getTextWidth ( hintText ) / 2 ) ;
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow3D (
hintText , coreData . getMenuFontBig3D ( ) , displayColor ,
2012-07-20 21:00:49 +02:00
//xLocation*1.5f,
xLocationHint ,
90 * metrics . getVirtualH ( ) / 100 , false ) ;
2012-06-18 00:52:30 +02:00
}
else {
2012-07-20 21:00:49 +02:00
int xLocationHint = ( metrics . getVirtualW ( ) / 2 ) - ( coreData . getMenuFontBig ( ) - > getMetrics ( ) - > getTextWidth ( hintText ) / 2 ) ;
2013-01-27 21:18:14 +01:00
renderer . renderTextShadow (
hintText , coreData . getMenuFontBig ( ) , displayColor ,
2012-07-20 21:00:49 +02:00
//xLocation*1.5f,
xLocationHint ,
2012-06-18 00:52:30 +02:00
90 * metrics . getVirtualH ( ) / 100 , false ) ;
}
2013-06-13 22:08:50 +02:00
//Show next Hint
2013-06-19 18:12:00 +02:00
if ( buttonNextHint . getEnabled ( ) = = false ) {
2013-07-12 12:28:59 +02:00
buttonNextHint . init ( ( metrics . getVirtualW ( ) / 2 ) - ( 300 / 2 ) , 90 * metrics . getVirtualH ( ) / 100 + 20 , 175 ) ;
2013-10-29 07:13:38 +01:00
buttonNextHint . setText ( lang . getString ( " ShowNextHint " , " " , true ) ) ;
2013-06-19 18:12:00 +02:00
buttonNextHint . setEnabled ( true ) ;
buttonNextHint . setVisible ( true ) ;
buttonNextHint . setEditable ( true ) ;
}
renderer . renderButton ( & buttonNextHint ) ;
/*
2013-06-13 22:08:50 +02:00
if ( Renderer : : renderText3DEnabled ) {
int xLocationHint = ( metrics . getVirtualW ( ) / 2 ) - ( coreData . getMenuFontBig3D ( ) - > getMetrics ( ) - > getTextWidth ( hintText ) / 2 ) ;
2013-06-19 18:12:00 +02:00
renderer . renderText3D (
2013-10-29 07:13:38 +01:00
lang . getString ( " ShowNextHint " , " " , true ) , coreData . getMenuFontNormal3D ( ) , nextHintTitleColor ,
2013-06-13 22:08:50 +02:00
//xLocation*1.5f,
xLocationHint ,
93 * metrics . getVirtualH ( ) / 100 , false ) ;
}
else {
int xLocationHint = ( metrics . getVirtualW ( ) / 2 ) - ( coreData . getMenuFontBig ( ) - > getMetrics ( ) - > getTextWidth ( hintText ) / 2 ) ;
2013-06-19 18:12:00 +02:00
renderer . renderText (
2013-10-29 07:13:38 +01:00
lang . getString ( " ShowNextHint " , " " , true ) , coreData . getMenuFontNormal ( ) , nextHintTitleColor ,
2013-06-13 22:08:50 +02:00
//xLocation*1.5f,
xLocationHint ,
93 * metrics . getVirtualH ( ) / 100 , false ) ;
}
2013-06-19 18:12:00 +02:00
*/
2012-06-18 00:52:30 +02:00
}
2011-03-31 20:13:02 +02:00
if ( buttonCancel . getEnabled ( ) = = true ) {
renderer . renderButton ( & buttonCancel ) ;
}
2010-04-05 19:43:04 +02:00
renderer . swapBuffers ( ) ;
}
2011-03-31 20:13:02 +02:00
void Logger : : setCancelLoadingEnabled ( bool value ) {
Lang & lang = Lang : : getInstance ( ) ;
const Metrics & metrics = Metrics : : getInstance ( ) ;
2012-10-06 09:06:40 +02:00
//string containerName = "logger";
2011-03-31 20:13:02 +02:00
//buttonCancel.registerGraphicComponent(containerName,"buttonCancel");
buttonCancel . init ( ( metrics . getVirtualW ( ) / 2 ) - ( 125 / 2 ) , 50 * metrics . getVirtualH ( ) / 100 , 125 ) ;
2013-10-29 07:13:38 +01:00
buttonCancel . setText ( lang . getString ( " Cancel " ) ) ;
2011-03-31 20:13:02 +02:00
buttonCancel . setEnabled ( value ) ;
//GraphicComponent::applyAllCustomProperties(containerName);
}
2010-04-05 19:43:04 +02:00
} } //end namespace