From aa560c34b66a63da81956104cf115c0eafc979da Mon Sep 17 00:00:00 2001 From: Ed94 Date: Mon, 17 Feb 2020 12:39:31 -0500 Subject: [PATCH] updated actions with a pool of glue. --- Actions.hpp | 167 ++++++++++++++++++++++++++++++++++++++++++++------ Cpp_Alias.hpp | 39 ++++++------ Execution.cpp | 21 ++++--- 3 files changed, 183 insertions(+), 44 deletions(-) diff --git a/Actions.hpp b/Actions.hpp index 7d7650f..d7ce6c9 100644 --- a/Actions.hpp +++ b/Actions.hpp @@ -7,20 +7,29 @@ Description: This was a little experiment of mine to mess with action binding... Allows for non-member functions to be binded to an action, implements a functioning queue as well. TODO: Possibly add support for member functions. Have it so that deduction of delegate typef is not required to add to queue properly (right now it does, see input procedure for example); + +Note: Right now due to type dynamics all actions are allocated via a pool and reuse is attempted by not guaranteed... */ #pragma once + + + #include "Cpp_Alias.hpp" namespace Actions { + using IndexType = DataSize; + + + struct IAction { - virtual void DoAction() = NULL; + virtual sfn DoAction() -> void = NULL; }; template @@ -31,28 +40,55 @@ namespace Actions public: AAction(ActionType _actionToAssign, ActionParams... _params) : action(_actionToAssign), - params(_params... ) + params(_params... ), + done (false ) {}; - private: - using IndexType = DataSize; + sfn Used() -> bool + { + return done; + } - void DoAction_Implementation(ActionParams... _params) { action(_params...); } + sfn IsSame(ActionParams... _paramsForAction) -> bool + { + Tuple paramsToCheck(_paramsForAction...); + + if (params == paramsToCheck) + { + return true; + } + else + { + return false; + } + } + + sfn ReInitalize(ActionParams... _params) + { + params = Tuple (_params...); + + done = false; + } + + private: + sfn DoAction_Implementation(ActionParams... _params) { action(_params...); } template // TuplePackSequence - void ExpandTuple_CallDoActionImplementaiton(const Ref(Tuple) _paramsToExpand, std::index_sequence ) + sfn ExpandTuple_CallDoActionImplementaiton(const Ref(Tuple) _paramsToExpand, std::index_sequence ) { - // ExpandTuplePack - DoAction_Implementation(std::get (_paramsToExpand)...); + // ExpandTuplePack + DoAction_Implementation(std::get(_paramsToExpand)...); } Tuple params; ActionType action; + bool done; + public: // IAction - virtual void DoAction() override + virtual sfn DoAction() -> void override { ExpandTuple_CallDoActionImplementaiton ( @@ -60,22 +96,111 @@ namespace Actions // MakeTuplePackSequence () std::index_sequence_for() ); + + done = true; }; }; + struct ActionPool_Dynamic + { + template + using AllocationsOf = std::forward_list; + + using TypeIndex = std::type_index ; + using Managed_AAction = SPtr < IAction >; + using Managed_AActions = AllocationsOf < Managed_AAction >; + using AActions_Registry = std::map ; + + public: + template + sfn Available(Ref(Entry) _entry) -> bool + { + return _entry != aActions_Available.end() ? true : false; + } + + template + sfn Contains(Ref(Entry) _entry) -> bool + { + return _entry != aActions_Available.end() ? true : false; + } + + sfn Make_Managed_Actions() -> Ref(Managed_AActions) + { + mAAaction_Allocations.push_front(MakeSPtr()); + + return Dref(mAAaction_Allocations.front().get()); + } + + template + sfn Request_AAction(Delegate< FunctionType> _actionToQueue, ActionParams... _paramsForAction) -> ptr + { + using ActionType = AAction < FunctionType, ActionParams...>; + + TypeIndex AActionID = typeid(ActionType); + + deduce possibleEntry = aActions_Available.find(AActionID); + + if (Contains(possibleEntry)) + { + using Element = decltype(possibleEntry->second.begin()); + + for (Element possibleAction = possibleEntry->second.begin(); possibleAction != possibleEntry->second.end(); possibleAction++) + { + ptr< ActionType> castedEntry = static_cast< ptr< ActionType>>(possibleAction->get()); + + if (castedEntry->IsSame(_paramsForAction...)) + { + return castedEntry; + } + else if (castedEntry->Used()) + { + castedEntry->ReInitalize(_paramsForAction...); + + return castedEntry; + } + } + + SPtr< IAction> newAction = MakeSPtr< AAction>(_actionToQueue, _paramsForAction...); + ptr < IAction> returnRef = newAction.get (); + + aActions_Available.at(AActionID).push_front(newAction); + + return returnRef; + } + + SPtr< IAction> newAction = MakeSPtr< AAction>(_actionToQueue, _paramsForAction...); + ptr < IAction> returnRef = newAction.get (); + + aActions_Available.insert(std::make_pair(AActionID, Make_Managed_Actions())); + + aActions_Available.at(AActionID).push_front(newAction); + + return returnRef; + } + + + private: + AllocationsOf< SPtr > mAAaction_Allocations; + + AActions_Registry aActions_Available; + }; + + ActionPool_Dynamic DefaultActionPool_Dynamic; + + struct ActionQueue { - sfn HasAction() - { - return actionQueue.size() > 0; - } + using QueueType = std::deque< ptr>; + + template sfn AddToQueue(Delegate< FunctionType> _actionToQueue, ActionParams... _paramsForAction) { - // This is extremely inefficient, but in order to fix requires an object pool or something else... - SPtr< AAction > ptrToAction = MakeSPtr< AAction >(_actionToQueue, _paramsForAction...); + using GeneratedActionType = AAction; + + ptr< IAction > actionRequested = DefaultActionPool_Dynamic.Request_AAction(_actionToQueue, _paramsForAction...); if (HasAction()) { @@ -85,7 +210,7 @@ namespace Actions for (Element element = actionQueue.begin(); element != actionQueue.end(); element++) { - if ((*element).get() == ptrToAction.get()) + if ( (*element) == actionRequested ) { found = true; } @@ -93,12 +218,12 @@ namespace Actions if (not found) { - actionQueue.push_front(std::move(ptrToAction)); + actionQueue.push_front(actionRequested); } } else { - actionQueue.push_front(std::move(ptrToAction)); + actionQueue.push_front(actionRequested); } } @@ -112,7 +237,11 @@ namespace Actions } } - using QueueType = std::deque< SPtr>; + sfn HasAction() + { + return actionQueue.size() > 0; + } + QueueType actionQueue; }; diff --git a/Cpp_Alias.hpp b/Cpp_Alias.hpp index b34905a..421c2ea 100644 --- a/Cpp_Alias.hpp +++ b/Cpp_Alias.hpp @@ -12,24 +12,27 @@ This merely removes the need to use operators I don't like and wraps them in eas #pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/Execution.cpp b/Execution.cpp index 9296b4b..cb78a90 100644 --- a/Execution.cpp +++ b/Execution.cpp @@ -80,8 +80,8 @@ namespace Execution TimeValDec CycleStart , // Snapshot of cycle loop start time. CycleEnd , // Snapshot of cycle loop end time. DeltaTime , // Delta between last cycle start and end. - InputInterval = 1.0f / 576.0f, // Interval per second to complete the input process of the cycle. - PhysicsInterval = 1.0f / 288.0f, // Interval per second to complete the physics process of the cycle. + InputInterval = 1.0f / 400.0f, // Interval per second to complete the input process of the cycle. + PhysicsInterval = 1.0f / 240.0f, // Interval per second to complete the physics process of the cycle. RenderInterval = 1.0f / 144.0f ; // Interval per second to complete the render process of the cycle. ptr DefaultWindow; // Default window to use for execution. @@ -97,6 +97,13 @@ namespace Execution ActionQueue ActionsToComplete; // Actions queue to run during the physics process of the cycle. + template + sfn RoundOff(Type _value, gInt _numDigitsToKeep) -> Type + { + uInt64 Rounder = pow(10, _numDigitsToKeep); + + return round(_value * Rounder) / Rounder; + } // Functionality @@ -234,15 +241,15 @@ namespace Execution return; } - sfn ModifyCamSpeed(bool _isPositive) + sfn ModifyCamSpeed(bool _isPositive, gFloat _delta) { if (_isPositive) { - CamMoveSpeed++; + CamMoveSpeed += CamMoveSpeed * _delta; } else { - CamMoveSpeed--; + CamMoveSpeed -= CamMoveSpeed * _delta; } } @@ -272,12 +279,12 @@ namespace Execution if (KeyPressed(_currentWindowContext, EKeyCodes::UpArrow)) { - ActionsToComplete.AddToQueue(ModifyCamSpeedDelegate, true); + ActionsToComplete.AddToQueue(ModifyCamSpeedDelegate, true, PhysicsDelta); } if (KeysPressed(_currentWindowContext, EKeyCodes::DnArrow)) { - ActionsToComplete.AddToQueue(ModifyCamSpeedDelegate, false); + ActionsToComplete.AddToQueue(ModifyCamSpeedDelegate, false, PhysicsDelta); } if (KeyPressed(_currentWindowContext, EKeyCodes::F2))