Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Decouple game and ui frame rates #100

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/BlobbyApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ const char* BlobbyApp::getCurrenStateName() const
return mCurrentState->getStateName();
}

void BlobbyApp::step()
bool BlobbyApp::step()
{
mInputMgr->updateInput();
bool running = mInputMgr->running();

mIMGUI->begin();

// perform a state step
mCurrentState->step_impl();

Expand All @@ -66,6 +71,8 @@ void BlobbyApp::step()
mIMGUI->resetSelection();
mCurrentState->init();
}

return running;
}

BlobbyApp::BlobbyApp(std::unique_ptr<State> initState, const IUserConfigReader& config) :
Expand Down
2 changes: 1 addition & 1 deletion src/BlobbyApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BlobbyApp {
public:
explicit BlobbyApp(std::unique_ptr<State> initState, const IUserConfigReader& config);

void step();
bool step();

void switchToState(std::unique_ptr<State> newState);

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set(common_SRC
Color.cpp Color.h
NetworkMessage.cpp NetworkMessage.h
PhysicWorld.cpp PhysicWorld.h
SpeedController.cpp SpeedController.h
RateController.cpp RateController.h
UserConfig.cpp UserConfig.h
PhysicState.cpp PhysicState.h
DuelMatchState.cpp DuelMatchState.h
Expand Down
18 changes: 9 additions & 9 deletions src/DuelMatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DuelMatch::DuelMatch(bool remote, const std::string& rules, int score_to_win) :
setInputSources(std::make_shared<InputSource>(), std::make_shared<InputSource>());

if(!mRemote)
mPhysicWorld->setEventCallback( [this]( const MatchEvent& event ) { mEvents.push_back(event); } );
mPhysicWorld->setEventCallback( [this]( const MatchEvent& event ) { mNewEvents.push_back(event); } );
}

void DuelMatch::setPlayers(PlayerIdentity left_player, PlayerIdentity right_player)
Expand Down Expand Up @@ -108,7 +108,7 @@ void DuelMatch::step()

// process events
// process all physics events and relay them to logic
for( const auto& event : mEvents )
for( const auto& event : mNewEvents )
{
switch( event.event )
{
Expand Down Expand Up @@ -138,7 +138,7 @@ void DuelMatch::step()
auto errorside = mLogic->getLastErrorSide();
if(errorside != NO_PLAYER)
{
mEvents.emplace_back( MatchEvent::PLAYER_ERROR, errorside, 0 );
mNewEvents.emplace_back( MatchEvent::PLAYER_ERROR, errorside, 0 );
mPhysicWorld->setBallVelocity( mPhysicWorld->getBallVelocity().scale(0.6) );
}

Expand All @@ -150,12 +150,12 @@ void DuelMatch::step()
{
resetBall( mLogic->getServingPlayer() );
mLogic->onServe();
mEvents.emplace_back( MatchEvent::RESET_BALL, NO_PLAYER, 0 );
mNewEvents.emplace_back( MatchEvent::RESET_BALL, NO_PLAYER, 0 );
}

// reset events
mLastEvents = mEvents;
mEvents.clear();
std::move(begin(mNewEvents), end(mNewEvents), std::back_inserter(mLastEvents));
mNewEvents.clear();
}

void DuelMatch::setScore(int left, int right)
Expand Down Expand Up @@ -280,7 +280,7 @@ void DuelMatch::setState(const DuelMatchState& state)

void DuelMatch::trigger( const MatchEvent& event )
{
mEvents.push_back( event );
mNewEvents.push_back( event );
}

DuelMatchState DuelMatch::getState() const
Expand Down Expand Up @@ -350,6 +350,6 @@ PlayerIdentity& DuelMatch::getPlayer(PlayerSide player)
void DuelMatch::updateEvents()
{
/// \todo more economical with a swap?
mLastEvents = mEvents;
mEvents.clear();
mLastEvents = mNewEvents;
mNewEvents.clear();
}
5 changes: 4 additions & 1 deletion src/DuelMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class DuelMatch : public ObjectCounter<DuelMatch>
void setServingPlayer(PlayerSide side);

const std::vector<MatchEvent>& getEvents() const { return mLastEvents; }
std::vector<MatchEvent> fetchEvents() {
return std::move(mLastEvents);
}
// this function will move all events into mLastEvents, so they will be returned by get events.
// use this if no match step is performed, but external events have to be processed.
void updateEvents();
Expand All @@ -146,7 +149,7 @@ class DuelMatch : public ObjectCounter<DuelMatch>
bool mPaused;

// accumulation of physic events since last event processing
std::vector<MatchEvent> mEvents;
std::vector<MatchEvent> mNewEvents;
std::vector<MatchEvent> mLastEvents; // events that were generated in the last processed frame

bool mRemote;
Expand Down
52 changes: 52 additions & 0 deletions src/RateController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*=============================================================================
Blobby Volley 2
Copyright (C) 2023 Daniel Knobe ([email protected])
Copyright (C) 2023 Erik Schultheis ([email protected])

This program is free software; you can redistribute it 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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
=============================================================================*/

/* header include */
#include "RateController.h"

/* includes */
#include <cassert>

/* implementation */
using namespace std::chrono;

RateController::RateController() : mFrameDuration(0) {

}

void RateController::start(int frames_per_second) {
mFrameDuration = duration_cast<nanoseconds>(seconds(1)) / frames_per_second;
mLastTicks = clock_t::now();
}

bool RateController::handle_next_frame() {
assert(mFrameDuration.count() != 0);
if(wants_next_frame()) {
mLastTicks += mFrameDuration;
return true;
}
return false;
}

bool RateController::wants_next_frame() const
{
assert(mFrameDuration.count() != 0);
return clock_t::now() > mLastTicks + mFrameDuration;
}
37 changes: 37 additions & 0 deletions src/RateController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*=============================================================================
Blobby Volley 2
Copyright (C) 2023 Daniel Knobe ([email protected])
Copyright (C) 2023 Erik Schultheis ([email protected])

This program is free software; you can redistribute it 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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
=============================================================================*/

#pragma once

#include <chrono>

class RateController {
public:
RateController();
using clock_t = std::chrono::steady_clock;

void start(int frames_per_second);
bool handle_next_frame();
bool wants_next_frame() const;

private:
std::chrono::nanoseconds mFrameDuration;
clock_t::time_point mLastTicks;
};
124 changes: 0 additions & 124 deletions src/SpeedController.cpp

This file was deleted.

Loading