Skip to content

Commit

Permalink
Major changes
Browse files Browse the repository at this point in the history
Encapsulated FPS checking in FPSWatcher
Moved ThreadWrapper to Utils
  • Loading branch information
vittorioromeo committed Mar 12, 2013
1 parent b743499 commit 5ef6fe4
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 54 deletions.
7 changes: 1 addition & 6 deletions HGUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ namespace hg
inputMovement = 0; inputFocused = false;

if(status.mustRestart) changeLevel(restartId, restartFirstTime);

if(getOfficial())
{
if(status.currentTime > 1 && window.getFPS() < 20) loseFrame();
if(status.lostFrames >= maxLostFrames) invalidateScore();
}
if(!status.scoreInvalid && getOfficial() && fpsWatcher.isLimitReached()) invalidateScore();
}
void HexagonGame::updateEvents(float mFrameTime)
{
Expand Down
20 changes: 4 additions & 16 deletions HexagonGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,8 @@ using namespace sses;

namespace hg
{
HexagonGame::HexagonGame(GameWindow& mGameWindow) : window(mGameWindow),
lostFramesThread([&]
{
while(true)
{
if(status.currentTime >= 1)
{
float lastFps{window.getFPS()};
sleep(milliseconds(20));
if(window.getFPS() == lastFps && status.lostFrames <= maxLostFrames) loseFrame();
}
sleep(milliseconds(20));
}
})
HexagonGame::HexagonGame(GameWindow& mGameWindow) : window(mGameWindow), fpsWatcher(window)
{
lostFramesThread.launch();
initFlashEffect();

game.onUpdate += [&](float mFrameTime) { update(mFrameTime); };
Expand Down Expand Up @@ -92,6 +78,9 @@ namespace hg
messageTimeline.clear();
effectTimelineManager.clear();

// FPSWatcher reset
fpsWatcher.reset();

// LUA context cleanup
if(!mFirstPlay) runLuaFunction<void>("onUnload");
lua = Lua::LuaContext{};
Expand Down Expand Up @@ -212,6 +201,5 @@ namespace hg
void HexagonGame::playLevelMusic() { if(!getNoMusic()) musicData.playRandomSegment(musicPtr); }
void HexagonGame::stopLevelMusic() { if(!getNoMusic()) if(musicPtr != nullptr) musicPtr->stop(); }

void HexagonGame::loseFrame() { ++status.lostFrames; log("Slowdown " + toStr(status.lostFrames) + "/" + toStr(maxLostFrames), "Performance"); }
void HexagonGame::invalidateScore() { status.scoreInvalid = true; log("Too much slowdown, invalidating official game", "Performance"); }
}
6 changes: 2 additions & 4 deletions HexagonGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "Data/StyleData.h"
#include "Global/Config.h"
#include "Global/Factory.h"
#include "Online/ThreadWrapper.h"
#include "Utils/FPSWatcher.h"
#pragma GCC system_header
#include <SSVLuaWrapper.h>

Expand Down Expand Up @@ -57,8 +57,7 @@ namespace hg
float difficultyMult{1};
int inputMovement{0};
bool inputFocused{false}, mustTakeScreenshot{false};
ThreadWrapper lostFramesThread;
float maxLostFrames{20};
FPSWatcher fpsWatcher;

// LUA-related methods
void initLua();
Expand Down Expand Up @@ -112,7 +111,6 @@ namespace hg
void goToMenu(bool mSendScores = true);
void changeLevel(std::string mId, bool mFirstTime);

void loseFrame();
void invalidateScore();

public:
Expand Down
2 changes: 1 addition & 1 deletion Online/Online.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "Global/Config.h"
#include "Utils/Utils.h"
#include "Utils/MD5.h"
#include "Online/ThreadWrapper.h"
#include "Utils/ThreadWrapper.h"
#include "Online/Definitions.h"

using namespace std;
Expand Down
25 changes: 0 additions & 25 deletions Online/ThreadWrapper.h

This file was deleted.

13 changes: 12 additions & 1 deletion SSVOpenHexagon.mk
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ WXWIN:=C:\wxWidgets-2.9.4
WXCFG:=gcc_dll\mswu
Objects0=$(IntermediateDirectory)/main$(ObjectSuffix) $(IntermediateDirectory)/HexagonGame$(ObjectSuffix) $(IntermediateDirectory)/MenuGame$(ObjectSuffix) $(IntermediateDirectory)/HGScripting$(ObjectSuffix) $(IntermediateDirectory)/HGUpdate$(ObjectSuffix) $(IntermediateDirectory)/HGProperties$(ObjectSuffix) $(IntermediateDirectory)/HGGraphics$(ObjectSuffix) $(IntermediateDirectory)/Components_CPlayer$(ObjectSuffix) $(IntermediateDirectory)/Components_CWall$(ObjectSuffix) $(IntermediateDirectory)/Global_Assets$(ObjectSuffix) \
$(IntermediateDirectory)/Global_Config$(ObjectSuffix) $(IntermediateDirectory)/Global_Factory$(ObjectSuffix) $(IntermediateDirectory)/Data_StyleData$(ObjectSuffix) $(IntermediateDirectory)/Data_LevelData$(ObjectSuffix) $(IntermediateDirectory)/Data_ProfileData$(ObjectSuffix) $(IntermediateDirectory)/Data_EventData$(ObjectSuffix) $(IntermediateDirectory)/Data_PackData$(ObjectSuffix) $(IntermediateDirectory)/Data_MusicData$(ObjectSuffix) $(IntermediateDirectory)/Utils_Utils$(ObjectSuffix) $(IntermediateDirectory)/Utils_MD5$(ObjectSuffix) \
$(IntermediateDirectory)/Online_Online$(ObjectSuffix)
$(IntermediateDirectory)/Utils_FPSWatcher$(ObjectSuffix) $(IntermediateDirectory)/Online_Online$(ObjectSuffix)

Objects=$(Objects0)

Expand Down Expand Up @@ -250,6 +250,14 @@ $(IntermediateDirectory)/Utils_MD5$(DependSuffix): Utils/MD5.cpp
$(IntermediateDirectory)/Utils_MD5$(PreprocessSuffix): Utils/MD5.cpp
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/Utils_MD5$(PreprocessSuffix) "Utils/MD5.cpp"

$(IntermediateDirectory)/Utils_FPSWatcher$(ObjectSuffix): Utils/FPSWatcher.cpp $(IntermediateDirectory)/Utils_FPSWatcher$(DependSuffix)
$(CXX) $(IncludePCH) $(SourceSwitch) "D:/Vee/Software/GitHub/OHWorkspace/SSVOpenHexagon/Utils/FPSWatcher.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/Utils_FPSWatcher$(ObjectSuffix) $(IncludePath)
$(IntermediateDirectory)/Utils_FPSWatcher$(DependSuffix): Utils/FPSWatcher.cpp
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/Utils_FPSWatcher$(ObjectSuffix) -MF$(IntermediateDirectory)/Utils_FPSWatcher$(DependSuffix) -MM "Utils/FPSWatcher.cpp"

$(IntermediateDirectory)/Utils_FPSWatcher$(PreprocessSuffix): Utils/FPSWatcher.cpp
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/Utils_FPSWatcher$(PreprocessSuffix) "Utils/FPSWatcher.cpp"

$(IntermediateDirectory)/Online_Online$(ObjectSuffix): Online/Online.cpp $(IntermediateDirectory)/Online_Online$(DependSuffix)
$(CXX) $(IncludePCH) $(SourceSwitch) "D:/Vee/Software/GitHub/OHWorkspace/SSVOpenHexagon/Online/Online.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/Online_Online$(ObjectSuffix) $(IncludePath)
$(IntermediateDirectory)/Online_Online$(DependSuffix): Online/Online.cpp
Expand Down Expand Up @@ -324,6 +332,9 @@ clean:
$(RM) $(IntermediateDirectory)/Utils_MD5$(ObjectSuffix)
$(RM) $(IntermediateDirectory)/Utils_MD5$(DependSuffix)
$(RM) $(IntermediateDirectory)/Utils_MD5$(PreprocessSuffix)
$(RM) $(IntermediateDirectory)/Utils_FPSWatcher$(ObjectSuffix)
$(RM) $(IntermediateDirectory)/Utils_FPSWatcher$(DependSuffix)
$(RM) $(IntermediateDirectory)/Utils_FPSWatcher$(PreprocessSuffix)
$(RM) $(IntermediateDirectory)/Online_Online$(ObjectSuffix)
$(RM) $(IntermediateDirectory)/Online_Online$(DependSuffix)
$(RM) $(IntermediateDirectory)/Online_Online$(PreprocessSuffix)
Expand Down
4 changes: 3 additions & 1 deletion SSVOpenHexagon.project
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<File Name="Utils/Utils.h"/>
<File Name="Utils/MD5.h"/>
<File Name="Utils/MD5.cpp"/>
<File Name="Utils/ThreadWrapper.h"/>
<File Name="Utils/FPSWatcher.h"/>
<File Name="Utils/FPSWatcher.cpp"/>
</VirtualDirectory>
<File Name="HGScripting.cpp"/>
<File Name="HGUpdate.cpp"/>
Expand All @@ -55,7 +58,6 @@
<VirtualDirectory Name="Online">
<File Name="Online/Online.cpp"/>
<File Name="Online/Online.h"/>
<File Name="Online/ThreadWrapper.h"/>
<File Name="Online/Definitions.h"/>
</VirtualDirectory>
</VirtualDirectory>
Expand Down
31 changes: 31 additions & 0 deletions Utils/FPSWatcher.cpp
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; }
}

31 changes: 31 additions & 0 deletions Utils/FPSWatcher.h
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
28 changes: 28 additions & 0 deletions Utils/ThreadWrapper.h
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

0 comments on commit 5ef6fe4

Please sign in to comment.