-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
This commit is contained in:
Mark Vejvoda 2010-06-05 15:48:50 +00:00
parent 50ca413310
commit 1d0836f0ba
7 changed files with 155 additions and 16 deletions

View File

@ -1085,8 +1085,8 @@ void Game::render2d(){
str+= "Render FPS: "+intToStr(lastRenderFps)+"\n"; str+= "Render FPS: "+intToStr(lastRenderFps)+"\n";
str+= "Update FPS: "+intToStr(lastUpdateFps)+"\n"; str+= "Update FPS: "+intToStr(lastUpdateFps)+"\n";
str+= "GameCamera pos: "+floatToStr(gameCamera.getPos().x)+","+floatToStr(gameCamera.getPos().y)+","+floatToStr(gameCamera.getPos().z)+"\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: "+floatToStr(world.getTimeFlow()->getTime(),8)+"\n";
str+= "Time Increment: "+floatToStr(world.getTimeFlow()->getTimeInc())+"\n"; str+= "Time Increment: "+floatToStr(world.getTimeFlow()->getTimeInc(),8)+"\n";
str+= "Triangle count: "+intToStr(renderer.getTriangleCount())+"\n"; str+= "Triangle count: "+intToStr(renderer.getTriangleCount())+"\n";
str+= "Vertex count: "+intToStr(renderer.getPointCount())+"\n"; str+= "Vertex count: "+intToStr(renderer.getPointCount())+"\n";
str+= "Frame count:"+intToStr(world.getFrameCount())+"\n"; str+= "Frame count:"+intToStr(world.getFrameCount())+"\n";

View File

@ -268,38 +268,38 @@ void Program::resize(SizeState sizeState){
void Program::setState(ProgramState *programState, bool cleanupOldState) void Program::setState(ProgramState *programState, bool cleanupOldState)
{ {
try { 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(cleanupOldState == true) {
if(this->programState != programState) { 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; delete this->programState;
this->programState = NULL; 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; this->programState= programState;
programState->load(); 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(); 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(); updateTimer.reset();
updateCameraTimer.reset(); updateCameraTimer.reset();
fpsTimer.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){ catch(const exception &e){
//exceptionMessage(e); //exceptionMessage(e);
//throw runtime_error(e.what()); //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()); this->showMessage(e.what());
setState(new Intro(this)); setState(new Intro(this));
} }

View File

@ -354,6 +354,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){
needToRepublishToMasterserver = false; needToRepublishToMasterserver = false;
BaseThread::shutdownAndWait(publishToMasterserverThread); BaseThread::shutdownAndWait(publishToMasterserverThread);
safeMutex.ReleaseLock();
returnToParentMenu(); returnToParentMenu();
} }
else if(buttonPlayNow.mouseClick(x,y) && buttonPlayNow.getEnabled()) { 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__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
safeMutex.ReleaseLock();
program->setState(new Game(program, &gameSettings)); program->setState(new Game(program, &gameSettings));
} }
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
} }
else if(buttonRestoreLastSettings.mouseClick(x,y) && buttonRestoreLastSettings.getEnabled()) { else if(buttonRestoreLastSettings.mouseClick(x,y) && buttonRestoreLastSettings.getEnabled()) {

View File

@ -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 && time>=dawn){
soundRenderer.stopAmbient(ambientSounds->getNight());
}
if((lastTime<dawn && time>=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 && time>=dusk){
soundRenderer.stopAmbient(ambientSounds->getDay());
}
if(lastTime<dusk && time>=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<time+timeInc);
}
}}//end namespace

View File

@ -0,0 +1,37 @@
// ==============================================================
// This file is part of Glest Shared Library (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
// ==============================================================
#ifndef _SHARED_UTIL_CONVERSION_H_
#define _SHARED_UTIL_CONVERSION_H_
#include <string>
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

View File

@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest Shared Library (www.glest.org) // 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 // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
@ -116,15 +116,15 @@ string intToHex(int i){
return str; return str;
} }
string floatToStr(float f){ string floatToStr(float f,int precsion){
char str[strSize]; char str[strSize];
sprintf(str, "%.2f", f); sprintf(str, "%.*f", precsion,f);
return str; return str;
} }
string doubleToStr(double d){ string doubleToStr(double d,int precsion){
char str[strSize]; char str[strSize];
sprintf(str, "%.2f", d); sprintf(str, "%.*f", precsion,d);
return str; return str;
} }

View File

@ -195,8 +195,9 @@ void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) {
va_list argList; va_list argList;
va_start(argList, fmt); va_start(argList, fmt);
char szBuf[4096]=""; const int max_debug_buffer_size = 8096;
vsprintf(szBuf,fmt, argList); char szBuf[max_debug_buffer_size]="";
vsnprintf(szBuf,max_debug_buffer_size-1,fmt, argList);
// Either output to a logfile or // Either output to a logfile or
if(currentDebugLog.debugLogFileName != "") { if(currentDebugLog.debugLogFileName != "") {