refactor: consistently use time_t t... variables for times in sound system, stats, application and player controls

stable-5.4
Tobias Zwick 2013-11-29 14:16:14 +07:00
parent 30619ae7fd
commit ecf538cd1f
10 changed files with 525 additions and 527 deletions

View File

@ -362,7 +362,7 @@ bool C4PlayerControlAssignment::IsComboMatched(const C4PlayerControlRecentKeyLis
// check if combo is currently fulfilled (assuming TriggerKey is already matched)
if (fComboIsSequence)
{
DWORD tKeyLast = GetTime();
time_t tKeyLast = GetTime();
// combo is a sequence: The last keys of RecentKeys must match the sequence
// the last ComboKey is the TriggerKey, which is omitted because it has already been matched and is not to be found in RecentKeys yet
KeyComboVec::const_reverse_iterator i = KeyCombo.rbegin()+1;
@ -372,7 +372,7 @@ bool C4PlayerControlAssignment::IsComboMatched(const C4PlayerControlRecentKeyLis
if (ri == RecentKeys.rend()) return false;
const C4PlayerControlRecentKey &rk = *ri;
// user waited for too long?
DWORD tKeyRecent = rk.tTime;
time_t tKeyRecent = rk.tTime;
if (tKeyLast - tKeyRecent > C4PlayerControl::MaxSequenceKeyDelay) return false;
// key doesn't match?
const KeyComboItem &k = *i;
@ -1289,7 +1289,7 @@ void C4PlayerControl::Execute()
}
// cleanup old recent keys
C4PlayerControlRecentKeyList::iterator irk;
DWORD tNow = GetTime();
time_t tNow = GetTime();
for (irk = RecentKeys.begin(); irk != RecentKeys.end(); ++irk)
{
C4PlayerControlRecentKey &rk = *irk;

View File

@ -134,8 +134,8 @@ public:
struct C4PlayerControlRecentKey
{
C4KeyCodeEx pressed_key, matched_key;
DWORD tTime;
C4PlayerControlRecentKey(const C4KeyCodeEx &pressed_key, const C4KeyCodeEx &matched_key, DWORD tTime) : pressed_key(pressed_key), matched_key(matched_key), tTime(tTime) {}
time_t tTime;
C4PlayerControlRecentKey(const C4KeyCodeEx &pressed_key, const C4KeyCodeEx &matched_key, time_t tTime) : pressed_key(pressed_key), matched_key(matched_key), tTime(tTime) {}
bool operator ==(const C4KeyCodeEx &cmp) { return pressed_key==cmp; } // comparison op for finding items in lists: Search for the pressed key only
};

View File

@ -837,7 +837,7 @@ bool C4Application::FullScreenMode()
C4ApplicationGameTimer::C4ApplicationGameTimer()
: CStdMultimediaTimerProc(26),
iLastGameTick(0), iGameTickDelay(0)
tLastGameTick(0), iGameTickDelay(0)
{
}
@ -864,18 +864,18 @@ bool C4ApplicationGameTimer::Execute(int iTimeout, pollfd *)
{
// Check timer and reset
if (!CheckAndReset()) return true;
unsigned int Now = GetTime();
time_t tNow = GetTime();
// Execute
if (Now >= iLastGameTick + iGameTickDelay || Game.GameGo)
if (tNow >= tLastGameTick + iGameTickDelay || Game.GameGo)
{
if(iGameTickDelay)
iLastGameTick += iGameTickDelay;
tLastGameTick += iGameTickDelay;
else
iLastGameTick = Now;
tLastGameTick = tNow;
// Compensate if things get too slow
if (Now > iLastGameTick + iGameTickDelay)
iLastGameTick += (Now - iLastGameTick) / 2;
if (tNow > tLastGameTick + iGameTickDelay)
tLastGameTick += (tNow - tLastGameTick) / 2;
Application.GameTick();
}

View File

@ -112,7 +112,8 @@ class C4ApplicationGameTimer : public CStdMultimediaTimerProc
public:
C4ApplicationGameTimer();
private:
unsigned int iLastGameTick, iGameTickDelay;
time_t tLastGameTick;
unsigned int iGameTickDelay;
public:
void SetGameTickDelay(uint32_t iDelay);

View File

@ -350,7 +350,7 @@ bool C4Game::Init()
C4ValueNumbers numbers;
IsRunning = false;
InitProgress=0; LastInitProgress=0; LastInitProgressShowTime=0;
InitProgress=0; LastInitProgress=0;
SetInitProgress(0);
// reinit keyboard to reflect any config changes that might have been done
@ -1445,7 +1445,7 @@ void C4Game::Default()
TimeGo=false;
Time=0;
StartTime=0;
InitProgress=0; LastInitProgress=0; LastInitProgressShowTime=0;
InitProgress=0; LastInitProgress=0;
FPS=cFPS=0;
fScriptCreatedObjects=false;
fLobby=fObserve=false;
@ -3135,7 +3135,6 @@ void C4Game::SetInitProgress(float fToProgress)
if (InitProgress > LastInitProgress)
{
LastInitProgress=InitProgress;
LastInitProgressShowTime=GetTime();
GraphicsSystem.MessageBoard.LogNotify();
}
// Cheap hack to get the Console window updated while loading

View File

@ -1,205 +1,205 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2001, 2007-2008 Peter Wortmann
* Copyright (c) 2006 Günther Brammer
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
// statistics
#include "C4Include.h"
#include <C4Stat.h>
// ** implemetation of C4MainStat
C4MainStat::C4MainStat()
: pFirst(0)
{
}
C4MainStat::~C4MainStat()
{
}
void C4MainStat::RegisterStat(C4Stat* pStat)
{
// add to list
if (!pFirst)
{
pFirst = pStat;
pStat->pNext = 0;
pStat->pPrev = 0;
}
else
{
pStat->pNext = pFirst;
pFirst->pPrev = pStat;
pStat->pPrev = 0;
pFirst = pStat;
}
}
void C4MainStat::UnRegStat(C4Stat* pStat)
{
// first item?
if (!pStat->pPrev)
{
pFirst = pStat->pNext;
pStat->pNext = 0;
}
// last item?
else if (!pStat->pNext)
{
pStat->pPrev->pNext = 0;
pStat->pPrev = 0;
}
else
{
pStat->pNext->pPrev = pStat->pPrev;
pStat->pPrev->pNext = pStat->pNext;
pStat->pNext = 0;
pStat->pPrev = 0;
}
}
void C4MainStat::Reset()
{
for (C4Stat* pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
pAkt->Reset();
}
void C4MainStat::ResetPart()
{
for (C4Stat* pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
pAkt->ResetPart();
}
void C4MainStat::Show()
{
// count stats
unsigned int iCnt = 0;
C4Stat* pAkt;
for (pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
iCnt++;
// create array
C4Stat** StatArray = new C4Stat*[iCnt];
bool* bHS = new bool[iCnt];
// sort it
unsigned int i,ii;
for (ii=0; ii<iCnt; ii++) bHS[ii] = false;
for (i=0; i<iCnt; i++)
{
C4Stat* pBestStat = NULL;
unsigned int iBestNr = ~0;
for (ii=0, pAkt = pFirst; ii<iCnt; ii++, pAkt = pAkt->pNext)
if (!bHS[ii])
{
if (iBestNr == ~0u)
{
iBestNr = ii;
pBestStat = pAkt;
}
else if (stricmp(pBestStat->strName, pAkt->strName) > 0)
{
iBestNr = ii;
pBestStat = pAkt;
}
}
if (iBestNr == (unsigned int) -1)
break;
bHS[iBestNr] = true;
StatArray[i] = pBestStat;
}
delete bHS;
LogSilent("** Stat");
// output in order
for (i=0; i<iCnt; i++)
{
pAkt = StatArray[i];
// output it!
if (pAkt->iCount)
LogSilentF("%s: n = %d, t = %d, td = %.2f",
pAkt->strName, pAkt->iCount, pAkt->iTimeSum,
double(pAkt->iTimeSum) / /*Max<int>(1,*/ pAkt->iCount /*- 100)*/ * 1000);
}
// delete...
delete[] StatArray;
// ok. job done
LogSilent("** Stat end");
}
void C4MainStat::ShowPart(int FrameCounter)
{
C4Stat* pAkt;
// insert tick nr
LogSilentF("** PartStat begin %d", FrameCounter);
// insert all stats
for (pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
LogSilentF("%s: n=%d, t=%d", pAkt->strName, pAkt->iCountPart, pAkt->iTimeSumPart);
// insert part stat end idtf
LogSilentF("** PartStat end\n");
}
// ** implemetation of C4Stat
C4Stat::C4Stat(const char* strnName)
: strName(strnName)
{
Reset();
getMainStat()->RegisterStat(this);
}
C4Stat::~C4Stat()
{
getMainStat()->UnRegStat(this);
}
void C4Stat::Reset()
{
iStartCalled = 0;
iTimeSum = 0;
iCount = 0;
ResetPart();
}
void C4Stat::ResetPart()
{
iTimeSumPart = 0;
iCountPart = 0;
}
C4MainStat *C4Stat::getMainStat()
{
static C4MainStat *pMainStat = new C4MainStat();
return pMainStat;
}
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2001, 2007-2008 Peter Wortmann
* Copyright (c) 2006 Günther Brammer
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
// statistics
#include "C4Include.h"
#include <C4Stat.h>
// ** implemetation of C4MainStat
C4MainStat::C4MainStat()
: pFirst(0)
{
}
C4MainStat::~C4MainStat()
{
}
void C4MainStat::RegisterStat(C4Stat* pStat)
{
// add to list
if (!pFirst)
{
pFirst = pStat;
pStat->pNext = 0;
pStat->pPrev = 0;
}
else
{
pStat->pNext = pFirst;
pFirst->pPrev = pStat;
pStat->pPrev = 0;
pFirst = pStat;
}
}
void C4MainStat::UnRegStat(C4Stat* pStat)
{
// first item?
if (!pStat->pPrev)
{
pFirst = pStat->pNext;
pStat->pNext = 0;
}
// last item?
else if (!pStat->pNext)
{
pStat->pPrev->pNext = 0;
pStat->pPrev = 0;
}
else
{
pStat->pNext->pPrev = pStat->pPrev;
pStat->pPrev->pNext = pStat->pNext;
pStat->pNext = 0;
pStat->pPrev = 0;
}
}
void C4MainStat::Reset()
{
for (C4Stat* pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
pAkt->Reset();
}
void C4MainStat::ResetPart()
{
for (C4Stat* pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
pAkt->ResetPart();
}
void C4MainStat::Show()
{
// count stats
unsigned int iCnt = 0;
C4Stat* pAkt;
for (pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
iCnt++;
// create array
C4Stat** StatArray = new C4Stat*[iCnt];
bool* bHS = new bool[iCnt];
// sort it
unsigned int i,ii;
for (ii=0; ii<iCnt; ii++) bHS[ii] = false;
for (i=0; i<iCnt; i++)
{
C4Stat* pBestStat = NULL;
unsigned int iBestNr = ~0;
for (ii=0, pAkt = pFirst; ii<iCnt; ii++, pAkt = pAkt->pNext)
if (!bHS[ii])
{
if (iBestNr == ~0u)
{
iBestNr = ii;
pBestStat = pAkt;
}
else if (stricmp(pBestStat->strName, pAkt->strName) > 0)
{
iBestNr = ii;
pBestStat = pAkt;
}
}
if (iBestNr == (unsigned int) -1)
break;
bHS[iBestNr] = true;
StatArray[i] = pBestStat;
}
delete bHS;
LogSilent("** Stat");
// output in order
for (i=0; i<iCnt; i++)
{
pAkt = StatArray[i];
// output it!
if (pAkt->iCount)
LogSilentF("%s: n = %d, t = %d, td = %.2f",
pAkt->strName, pAkt->iCount, pAkt->tTimeSum,
double(pAkt->tTimeSum) / /*Max<int>(1,*/ pAkt->iCount /*- 100)*/ * 1000);
}
// delete...
delete[] StatArray;
// ok. job done
LogSilent("** Stat end");
}
void C4MainStat::ShowPart(int FrameCounter)
{
C4Stat* pAkt;
// insert tick nr
LogSilentF("** PartStat begin %d", FrameCounter);
// insert all stats
for (pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
LogSilentF("%s: n=%d, t=%d", pAkt->strName, pAkt->iCountPart, pAkt->tTimeSumPart);
// insert part stat end idtf
LogSilentF("** PartStat end\n");
}
// ** implemetation of C4Stat
C4Stat::C4Stat(const char* strnName)
: strName(strnName)
{
Reset();
getMainStat()->RegisterStat(this);
}
C4Stat::~C4Stat()
{
getMainStat()->UnRegStat(this);
}
void C4Stat::Reset()
{
iStartCalled = 0;
tTimeSum = 0;
iCount = 0;
ResetPart();
}
void C4Stat::ResetPart()
{
tTimeSumPart = 0;
iCountPart = 0;
}
C4MainStat *C4Stat::getMainStat()
{
static C4MainStat *pMainStat = new C4MainStat();
return pMainStat;
}

View File

@ -1,165 +1,163 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2001, 2007 Peter Wortmann
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
// statistics
#ifndef INC_C4Stat
#define INC_C4Stat
class C4Stat;
// *** main statistic class
// should only been constructed once per application
class C4MainStat
{
friend class C4Stat;
public:
C4MainStat();
~C4MainStat();
void Show();
void ShowPart(int FrameCounter);
void Reset();
void ResetPart();
protected:
C4Stat* pFirst;
void RegisterStat(C4Stat* pStat);
void UnRegStat(C4Stat* pStat);
};
// *** one statistic.
// Holds the data about one "checkpoint" in code
// registers himself to C4MainStat when Start() is called the first time
class C4Stat
{
friend class C4MainStat;
public:
C4Stat(const char* strName);
~C4Stat();
inline void Start()
{
if (!iStartCalled)
iStartTick = GetTime();
iCount ++;
iCountPart ++;
iStartCalled ++;
}
inline void Stop()
{
assert(iStartCalled);
iStartCalled --;
if (!iStartCalled && iCount >= 100)
{
unsigned int iTime = GetTime() - iStartTick;
iTimeSum += iTime;
iTimeSumPart += iTime;
}
}
void Reset();
void ResetPart();
static C4MainStat *getMainStat();
protected:
// used by C4MainStat
C4Stat* pNext;
C4Stat* pPrev;
// start tick
unsigned int iStartTick;
// start-call depth
unsigned int iStartCalled;
// ** statistic data
// sum of times
unsigned int iTimeSum;
// number of starts called
unsigned int iCount;
// ** statistic data (partial stat)
// sum of times
unsigned int iTimeSumPart;
// number of starts called
unsigned int iCountPart;
// name of statistic
const char* strName;
// registred?
bool bRegistred;
};
// *** some directives
#ifdef STAT
// used to create and start a new C4Stat object
#define C4ST_STARTNEW(StatName, strName) static C4Stat StatName(strName); StatName.Start();
// used to create a new C4Stat object
#define C4ST_NEW(StatName, strName) C4Stat StatName(strName);
// used to start an existing C4Stat object
#define C4ST_START(StatName) StatName.Start();
// used to stop an existing C4Stat object
#define C4ST_STOP(StatName) StatName.Stop();
// shows the statistic (to log)
#define C4ST_SHOWSTAT C4Stat::getMainStat()->Show();
// shows the statistic (to log)
#define C4ST_SHOWPARTSTAT(FrameCounter) C4Stat::getMainStat()->ShowPart(FrameCounter);
// resets the whole statistic
#define C4ST_RESET C4Stat::getMainStat()->Reset();
// resets the partial statistic
#define C4ST_RESETPART C4Stat::getMainStat()->ResetPart();
#else
#define C4ST_STARTNEW(StatName, strName)
#define C4ST_NEW(StatName, strName)
#define C4ST_START(StatName)
#define C4ST_STOP(StatName)
#define C4ST_SHOWSTAT
#define C4ST_SHOWPARTSTAT(FrameCounter)
#define C4ST_RESET
#define C4ST_RESETPART
#endif
#endif // INC_C4Stat
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2001, 2007 Peter Wortmann
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
// statistics
#ifndef INC_C4Stat
#define INC_C4Stat
class C4Stat;
// *** main statistic class
// should only been constructed once per application
class C4MainStat
{
friend class C4Stat;
public:
C4MainStat();
~C4MainStat();
void Show();
void ShowPart(int FrameCounter);
void Reset();
void ResetPart();
protected:
C4Stat* pFirst;
void RegisterStat(C4Stat* pStat);
void UnRegStat(C4Stat* pStat);
};
// *** one statistic.
// Holds the data about one "checkpoint" in code
// registers himself to C4MainStat when Start() is called the first time
class C4Stat
{
friend class C4MainStat;
public:
C4Stat(const char* strName);
~C4Stat();
inline void Start()
{
if (!iStartCalled)
tStartTime = GetTime();
iCount ++;
iCountPart ++;
iStartCalled ++;
}
inline void Stop()
{
assert(iStartCalled);
iStartCalled --;
if (!iStartCalled && iCount >= 100)
{
time_t tTime = GetTime() - tStartTime;
tTimeSum += tTime;
tTimeSumPart += tTime;
}
}
void Reset();
void ResetPart();
static C4MainStat *getMainStat();
protected:
// used by C4MainStat
C4Stat* pNext;
C4Stat* pPrev;
time_t tStartTime;
// start-call depth
unsigned int iStartCalled;
// ** statistic data
// sum of times
time_t tTimeSum;
// number of starts called
unsigned int iCount;
// ** statistic data (partial stat)
// sum of times
time_t tTimeSumPart;
// number of starts called
unsigned int iCountPart;
// name of statistic
const char* strName;
// registred?
bool bRegistred;
};
// *** some directives
#ifdef STAT
// used to create and start a new C4Stat object
#define C4ST_STARTNEW(StatName, strName) static C4Stat StatName(strName); StatName.Start();
// used to create a new C4Stat object
#define C4ST_NEW(StatName, strName) C4Stat StatName(strName);
// used to start an existing C4Stat object
#define C4ST_START(StatName) StatName.Start();
// used to stop an existing C4Stat object
#define C4ST_STOP(StatName) StatName.Stop();
// shows the statistic (to log)
#define C4ST_SHOWSTAT C4Stat::getMainStat()->Show();
// shows the statistic (to log)
#define C4ST_SHOWPARTSTAT(FrameCounter) C4Stat::getMainStat()->ShowPart(FrameCounter);
// resets the whole statistic
#define C4ST_RESET C4Stat::getMainStat()->Reset();
// resets the partial statistic
#define C4ST_RESETPART C4Stat::getMainStat()->ResetPart();
#else
#define C4ST_STARTNEW(StatName, strName)
#define C4ST_NEW(StatName, strName)
#define C4ST_START(StatName)
#define C4ST_STOP(StatName)
#define C4ST_SHOWSTAT
#define C4ST_SHOWPARTSTAT(FrameCounter)
#define C4ST_RESET
#define C4ST_RESETPART
#endif
#endif // INC_C4Stat

View File

@ -1,134 +1,134 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2007, 2009 Peter Wortmann
* Copyright (c) 2010 Benjamin Herr
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#ifndef C4INTERACTIVETHREAD_H
#define C4INTERACTIVETHREAD_H
#include "StdScheduler.h"
#include "StdSync.h"
#include <boost/function.hpp>
// Event types
enum C4InteractiveEventType
{
Ev_None = 0,
Ev_Function,
Ev_Log,
Ev_LogSilent,
Ev_LogFatal,
Ev_LogDebug,
Ev_FileChange,
Ev_HTTP_Response,
Ev_UPNP_Response,
Ev_IRC_Message,
Ev_Net_Conn,
Ev_Net_Disconn,
Ev_Net_Packet,
Ev_Last = Ev_Net_Packet
};
class C4InteractiveThreadNotifyProc : public CStdNotifyProc
{
private:
class C4InteractiveThread *pNotify;
public:
void SetNotify(class C4InteractiveThread *pnNotify) { pNotify = pnNotify; }
virtual bool Execute(int iTimeout, pollfd * readyfds);
};
// Collects StdSchedulerProc objects and executes them in a separate thread
// Provides an event queue for the procs to communicate with the main thread
class C4InteractiveThread
{
public:
C4InteractiveThread();
~C4InteractiveThread();
// Event callback interface
class Callback
{
public:
virtual void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData) = 0;
virtual ~Callback() { }
};
private:
// the thread itself
StdSchedulerThread Scheduler;
// event queue (signals to main thread)
struct Event
{
C4InteractiveEventType Type;
void *Data;
#ifdef _DEBUG
int Time;
#endif
Event *Next;
};
Event *pFirstEvent, *pLastEvent;
CStdCSec EventPushCSec, EventPopCSec;
// callback objects for events of special types
Callback *pCallbacks[Ev_Last + 1];
// proc that is added to the main thread to receive messages from our thread
C4InteractiveThreadNotifyProc NotifyProc;
public:
// process management
bool AddProc(StdSchedulerProc *pProc);
void RemoveProc(StdSchedulerProc *pProc);
// event queue
bool PushEvent(C4InteractiveEventType eEventType, void *pData = NULL);
void ProcessEvents(); // by main thread
// special events
bool ThreadLog(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
bool ThreadLogFatal(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
bool ThreadLogS(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
bool ThreadLogDebug(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
template<typename Functor>
bool ThreadPostAsync(Functor function)
{
return PushEvent(Ev_Function, new boost::function<void ()>(function));
}
// event handlers
void SetCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
{ pCallbacks[eEvent] = pnNetworkCallback; }
void ClearCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
{ if (pCallbacks[eEvent] == pnNetworkCallback) pCallbacks[eEvent] = NULL; }
private:
bool PopEvent(C4InteractiveEventType *pEventType, void **ppData); // by main thread
};
#endif // C4INTERACTIVETHREAD_H
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2007, 2009 Peter Wortmann
* Copyright (c) 2010 Benjamin Herr
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#ifndef C4INTERACTIVETHREAD_H
#define C4INTERACTIVETHREAD_H
#include "StdScheduler.h"
#include "StdSync.h"
#include <boost/function.hpp>
// Event types
enum C4InteractiveEventType
{
Ev_None = 0,
Ev_Function,
Ev_Log,
Ev_LogSilent,
Ev_LogFatal,
Ev_LogDebug,
Ev_FileChange,
Ev_HTTP_Response,
Ev_UPNP_Response,
Ev_IRC_Message,
Ev_Net_Conn,
Ev_Net_Disconn,
Ev_Net_Packet,
Ev_Last = Ev_Net_Packet
};
class C4InteractiveThreadNotifyProc : public CStdNotifyProc
{
private:
class C4InteractiveThread *pNotify;
public:
void SetNotify(class C4InteractiveThread *pnNotify) { pNotify = pnNotify; }
virtual bool Execute(int iTimeout, pollfd * readyfds);
};
// Collects StdSchedulerProc objects and executes them in a separate thread
// Provides an event queue for the procs to communicate with the main thread
class C4InteractiveThread
{
public:
C4InteractiveThread();
~C4InteractiveThread();
// Event callback interface
class Callback
{
public:
virtual void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData) = 0;
virtual ~Callback() { }
};
private:
// the thread itself
StdSchedulerThread Scheduler;
// event queue (signals to main thread)
struct Event
{
C4InteractiveEventType Type;
void *Data;
#ifdef _DEBUG
time_t Time;
#endif
Event *Next;
};
Event *pFirstEvent, *pLastEvent;
CStdCSec EventPushCSec, EventPopCSec;
// callback objects for events of special types
Callback *pCallbacks[Ev_Last + 1];
// proc that is added to the main thread to receive messages from our thread
C4InteractiveThreadNotifyProc NotifyProc;
public:
// process management
bool AddProc(StdSchedulerProc *pProc);
void RemoveProc(StdSchedulerProc *pProc);
// event queue
bool PushEvent(C4InteractiveEventType eEventType, void *pData = NULL);
void ProcessEvents(); // by main thread
// special events
bool ThreadLog(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
bool ThreadLogFatal(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
bool ThreadLogS(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
bool ThreadLogDebug(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
template<typename Functor>
bool ThreadPostAsync(Functor function)
{
return PushEvent(Ev_Function, new boost::function<void ()>(function));
}
// event handlers
void SetCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
{ pCallbacks[eEvent] = pnNetworkCallback; }
void ClearCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
{ if (pCallbacks[eEvent] == pnNetworkCallback) pCallbacks[eEvent] = NULL; }
private:
bool PopEvent(C4InteractiveEventType *pEventType, void **ppData); // by main thread
};
#endif // C4INTERACTIVETHREAD_H

View File

@ -232,7 +232,7 @@ bool C4SoundInstance::Create(C4SoundEffect *pnEffect, bool fLoop, int32_t inVolu
// Set effect
pEffect = pnEffect;
// Set
iStarted = GetTime();
tStarted = GetTime();
iVolume = inVolume; iPan = 0; iChannel = -1;
iNearInstanceMax = inNearInstanceMax;
this->iFalloffDistance = iFalloffDistance;
@ -248,7 +248,7 @@ bool C4SoundInstance::CheckStart()
// already started?
if (isStarted()) return true;
// don't bother if half the time is up and the sound is not looping
if (GetTime() > iStarted + pEffect->Length / 2 && !fLooping)
if (GetTime() > tStarted + pEffect->Length / 2 && !fLooping)
return false;
// do near-instances check
int32_t iNearInstances = pObj ? pEffect->GetStartedInstanceCount(pObj->GetX(), pObj->GetY(), C4NearSoundRadius)
@ -268,10 +268,10 @@ bool C4SoundInstance::Start()
if (!FSOUND_SetLoopMode(iChannel, fLooping ? FSOUND_LOOP_NORMAL : FSOUND_LOOP_OFF))
{ Stop(); return false; }
// set position
if (GetTime() > iStarted + 20)
if (GetTime() > tStarted + 20)
{
assert(pEffect->Length > 0);
int32_t iTime = (GetTime() - iStarted) % pEffect->Length;
int32_t iTime = (GetTime() - tStarted) % pEffect->Length;
FSOUND_SetCurrentPosition(iChannel, iTime / 10 * pEffect->SampleRate / 100);
}
#elif defined HAVE_LIBSDL_MIXER
@ -318,7 +318,7 @@ bool C4SoundInstance::Stop()
}
#endif
iChannel = -1;
iStarted = 0;
tStarted = 0;
fLooping = false;
return fRet;
}
@ -329,7 +329,7 @@ bool C4SoundInstance::Playing()
#ifdef HAVE_FMOD
if (fLooping) return true;
return isStarted() ? FSOUND_GetCurrentSample(iChannel) == pEffect->pSample
: GetTime() < iStarted + pEffect->Length;
: GetTime() < tStarted + pEffect->Length;
#endif
#ifdef HAVE_LIBSDL_MIXER
return Application.MusicSystem.MODInitialized && (iChannel != -1) && Mix_Playing(iChannel);

View File

@ -90,7 +90,7 @@ public:
protected:
C4SoundEffect *pEffect;
int32_t iVolume, iPan, iChannel;
unsigned long iStarted;
time_t tStarted;
int32_t iNearInstanceMax;
bool fLooping;
C4Object *pObj;