-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulated FPS checking in FPSWatcher Moved ThreadWrapper to Utils
- Loading branch information
1 parent
b743499
commit 5ef6fe4
Showing
10 changed files
with
113 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2013 Vittorio Romeo | ||
// License: Academic Free License ("AFL") v. 3.0 | ||
// AFL License page: http://opensource.org/licenses/AFL-3.0 | ||
|
||
#include <SFML/System.hpp> | ||
#include "FPSWatcher.h" | ||
|
||
using namespace std; | ||
using namespace sf; | ||
using namespace ssvs; | ||
using namespace ssvs::Utils; | ||
|
||
namespace hg | ||
{ | ||
void FPSWatcher::watch() | ||
{ | ||
while(true) | ||
{ | ||
float lastFps{gameWindow.getFPS()}; | ||
sleep(milliseconds(20)); | ||
if(gameWindow.getFPS() < minFPS || (gameWindow.getFPS() == lastFps && lostFrames <= maxLostFrames)) loseFrame(); | ||
sleep(milliseconds(20)); | ||
} | ||
} | ||
void FPSWatcher::loseFrame() { ++lostFrames; log("Slowdown " + toStr(lostFrames) + "/" + toStr(maxLostFrames), "Performance"); } | ||
|
||
FPSWatcher::FPSWatcher(GameWindow& mGameWindow) : gameWindow(mGameWindow), thread([&]{ watch(); }) { thread.launch(); } | ||
bool FPSWatcher::isLimitReached() { return lostFrames >= maxLostFrames; } | ||
void FPSWatcher::reset() { lostFrames = 0; } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2013 Vittorio Romeo | ||
// License: Academic Free License ("AFL") v. 3.0 | ||
// AFL License page: http://opensource.org/licenses/AFL-3.0 | ||
|
||
#ifndef HG_UTILS_FPSWATCHER | ||
#define HG_UTILS_FPSWATCHER | ||
|
||
#include <SSVStart.h> | ||
#include "Utils/ThreadWrapper.h" | ||
|
||
namespace hg | ||
{ | ||
class FPSWatcher | ||
{ | ||
private: | ||
ssvs::GameWindow& gameWindow; | ||
ThreadWrapper thread; | ||
float lostFrames{0}; | ||
const float maxLostFrames{20}, minFPS{20}; | ||
|
||
void watch(); | ||
void loseFrame(); | ||
|
||
public: | ||
FPSWatcher(ssvs::GameWindow& mGameWindow); | ||
bool isLimitReached(); | ||
void reset(); | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2013 Vittorio Romeo | ||
// License: Academic Free License ("AFL") v. 3.0 | ||
// AFL License page: http://opensource.org/licenses/AFL-3.0 | ||
|
||
#ifndef HG_UTILS_THREADWRAPPER | ||
#define HG_UTILS_THERADWRAPPER | ||
|
||
#include <SFML/System.hpp> | ||
#include <functional> | ||
|
||
namespace hg | ||
{ | ||
class ThreadWrapper | ||
{ | ||
private: | ||
bool finished{false}; | ||
std::function<void()> func; | ||
sf::Thread thread; | ||
|
||
public: | ||
ThreadWrapper(std::function<void()> mFunction) : func{[&, mFunction]{ mFunction(); finished = true; }}, thread{func} { } | ||
void launch() { thread.launch(); } | ||
void terminate() { thread.terminate(); } | ||
bool getFinished() { return finished; } | ||
}; | ||
} | ||
|
||
#endif |