From 1d0836f0baa9c02c32be00408e362f390a15932f Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 5 Jun 2010 15:48:50 +0000 Subject: [PATCH] -added another mutex release to fix a bug found from a segfault -added a safer debug buffer output method -added ability to specify precsion for floating point conversion --- source/glest_game/game/game.cpp | 4 +- source/glest_game/main/program.cpp | 14 +-- .../menu/menu_state_custom_game.cpp | 3 + source/glest_game/world/time_flow.cpp | 98 +++++++++++++++++++ source/shared_lib/include/util/conversion.h | 37 +++++++ source/shared_lib/sources/util/conversion.cpp | 10 +- source/shared_lib/sources/util/util.cpp | 5 +- 7 files changed, 155 insertions(+), 16 deletions(-) create mode 100644 source/glest_game/world/time_flow.cpp create mode 100644 source/shared_lib/include/util/conversion.h diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 7f2447c3..9ea7e8de 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -1085,8 +1085,8 @@ void Game::render2d(){ str+= "Render FPS: "+intToStr(lastRenderFps)+"\n"; str+= "Update FPS: "+intToStr(lastUpdateFps)+"\n"; str+= "GameCamera pos: "+floatToStr(gameCamera.getPos().x)+","+floatToStr(gameCamera.getPos().y)+","+floatToStr(gameCamera.getPos().z)+"\n"; - str+= "Time: "+floatToStr(world.getTimeFlow()->getTime())+"\n"; - str+= "Time Increment: "+floatToStr(world.getTimeFlow()->getTimeInc())+"\n"; + str+= "Time: "+floatToStr(world.getTimeFlow()->getTime(),8)+"\n"; + str+= "Time Increment: "+floatToStr(world.getTimeFlow()->getTimeInc(),8)+"\n"; str+= "Triangle count: "+intToStr(renderer.getTriangleCount())+"\n"; str+= "Vertex count: "+intToStr(renderer.getPointCount())+"\n"; str+= "Frame count:"+intToStr(world.getFrameCount())+"\n"; diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 9b393bf4..59856727 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -268,38 +268,38 @@ void Program::resize(SizeState sizeState){ void Program::setState(ProgramState *programState, bool cleanupOldState) { try { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s LineL: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(cleanupOldState == true) { if(this->programState != programState) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s LineL: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); delete this->programState; this->programState = NULL; } } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s LineL: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); this->programState= programState; programState->load(); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s LineL: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); programState->init(); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s LineL: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); updateTimer.reset(); updateCameraTimer.reset(); fpsTimer.reset(); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s LineL: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } catch(const exception &e){ //exceptionMessage(e); //throw runtime_error(e.what()); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s LineL: %d]\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); this->showMessage(e.what()); setState(new Intro(this)); } diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index bc8b696d..e4dd8f7e 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -354,6 +354,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ needToRepublishToMasterserver = false; BaseThread::shutdownAndWait(publishToMasterserverThread); + safeMutex.ReleaseLock(); returnToParentMenu(); } else if(buttonPlayNow.mouseClick(x,y) && buttonPlayNow.getEnabled()) { @@ -414,8 +415,10 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + safeMutex.ReleaseLock(); program->setState(new Game(program, &gameSettings)); } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } else if(buttonRestoreLastSettings.mouseClick(x,y) && buttonRestoreLastSettings.getEnabled()) { diff --git a/source/glest_game/world/time_flow.cpp b/source/glest_game/world/time_flow.cpp new file mode 100644 index 00000000..40626832 --- /dev/null +++ b/source/glest_game/world/time_flow.cpp @@ -0,0 +1,98 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// 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 "time_flow.h" + +#include "sound_renderer.h" +#include "config.h" +#include "game_constants.h" +#include "util.h" +#include "leak_dumper.h" + +using namespace Shared::Util; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class TimeFlow +// ===================================================== + +const float TimeFlow::dusk= 18.f; +const float TimeFlow::dawn= 6.f; + +void TimeFlow::init(Tileset *tileset){ + firstTime= true; + this->tileset= tileset; + time= dawn+1.5f; + lastTime= time; + Config &config= Config::getInstance(); + timeInc= 24.f * (1.f / config.getFloat("DayTime")) / GameConstants::updateFps; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] timeInc = %f\n",__FILE__,__FUNCTION__,__LINE__,timeInc); +} + +void TimeFlow::update() { + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] timeInc = %f, time = %f\n",__FILE__,__FUNCTION__,__LINE__,timeInc,time); + + //update time + time += isDay()? timeInc: timeInc*2; + if(time > 24.f){ + time -= 24.f; + } + + //sounds + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + AmbientSounds *ambientSounds= tileset->getAmbientSounds(); + + //day + if(lastTime=dawn){ + soundRenderer.stopAmbient(ambientSounds->getNight()); + } + + if((lastTime=dawn) || firstTime){ + + //day sound + if(ambientSounds->isEnabledDayStart() && !firstTime){ + soundRenderer.playFx(ambientSounds->getDayStart()); + } + if(ambientSounds->isEnabledDay()){ + if(ambientSounds->getAlwaysPlayDay() || tileset->getWeather()==wSunny){ + soundRenderer.playAmbient(ambientSounds->getDay()); + } + } + firstTime= false; + } + + //night + if(lastTime=dusk){ + soundRenderer.stopAmbient(ambientSounds->getDay()); + } + + if(lastTime=dusk){ + //night + if(ambientSounds->isEnabledNightStart()){ + soundRenderer.playFx(ambientSounds->getNightStart()); + } + if(ambientSounds->isEnabledNight()){ + if(ambientSounds->getAlwaysPlayNight() || tileset->getWeather()==wSunny){ + soundRenderer.playAmbient(ambientSounds->getNight()); + } + } + } + lastTime= time; +} + +bool TimeFlow::isAproxTime(float time){ + return (this->time>=time) && (this->time + +using std::string; + +namespace Shared{ namespace Util{ + +bool strToBool(const string &s); +int strToInt(const string &s); +float strToFloat(const string &s); + +bool strToBool(const string &s, bool *b); +bool strToInt(const string &s, int *i); +bool strToFloat(const string &s, float *f); + +string boolToStr(bool b); +string intToStr(int i); +string intToHex(int i); +string floatToStr(float f,int precsion=2); +string doubleToStr(double f,int precsion=2); + +}}//end namespace + +#endif diff --git a/source/shared_lib/sources/util/conversion.cpp b/source/shared_lib/sources/util/conversion.cpp index be145388..c0da92ae 100644 --- a/source/shared_lib/sources/util/conversion.cpp +++ b/source/shared_lib/sources/util/conversion.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest Shared Library (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -116,15 +116,15 @@ string intToHex(int i){ return str; } -string floatToStr(float f){ +string floatToStr(float f,int precsion){ char str[strSize]; - sprintf(str, "%.2f", f); + sprintf(str, "%.*f", precsion,f); return str; } -string doubleToStr(double d){ +string doubleToStr(double d,int precsion){ char str[strSize]; - sprintf(str, "%.2f", d); + sprintf(str, "%.*f", precsion,d); return str; } diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index 67cacb68..f5e8e588 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -195,8 +195,9 @@ void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) { va_list argList; va_start(argList, fmt); - char szBuf[4096]=""; - vsprintf(szBuf,fmt, argList); + const int max_debug_buffer_size = 8096; + char szBuf[max_debug_buffer_size]=""; + vsnprintf(szBuf,max_debug_buffer_size-1,fmt, argList); // Either output to a logfile or if(currentDebugLog.debugLogFileName != "") {