summaryrefslogtreecommitdiff
path: root/src/states.cpp
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-11-29 18:19:25 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-11-29 18:19:25 +0100
commitbf34d835e4ea49bd4ef2e5de2b1fea2fd3c95adc (patch)
treeb1970daefeb7a6a8f406c83126de49c3b928484e /src/states.cpp
parentcbf859a37aa6b16db7e53cf75405cda720617392 (diff)
Start to use a normal game structure:
- App class that has a StateStack - StateStack - Some State definiton and a minimal TitleState to use as a template - Makefile updated accordingly - Delete old code that was there for testing
Diffstat (limited to 'src/states.cpp')
-rw-r--r--src/states.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/states.cpp b/src/states.cpp
new file mode 100644
index 0000000..d7799b3
--- /dev/null
+++ b/src/states.cpp
@@ -0,0 +1,89 @@
+#include "states.h"
+
+// StateStack
+
+StateStack::StateStack(State::Context context)
+ : pendingOps_()
+ , context_(context)
+{
+}
+
+void StateStack::update(sf::Time dt){
+ for(State::Ptr &s: states_) {
+ s->update(dt);
+ }
+}
+
+void StateStack::render(){
+ for(State::Ptr &s: states_) {
+ s->render();
+ }
+}
+
+void StateStack::handleEvent(const sf::Event &event){
+ for(State::Ptr &s: states_) {
+ s->handleEvent(event);
+ }
+}
+
+void StateStack::requestOp(StateStack::Op op){
+ pendingOps_.push_back(op);
+}
+
+void StateStack::push(States::Id state){
+ states_.push_back(std::move(createState(state)));
+}
+
+void StateStack::pop(){
+ states_.pop_back();
+}
+
+void StateStack::clear(){
+ states_.clear();
+}
+
+void StateStack::applyPending(){
+ for(StateStack::Op &op: pendingOps_) {
+ switch(op.id){
+ case StateStack::OpId::Push:
+ push(op.state);
+ break;
+ case StateStack::OpId::Pop:
+ pop();
+ break;
+ case StateStack::OpId::Clear:
+ clear();
+ break;
+ }
+ }
+ pendingOps_.clear();
+}
+
+bool StateStack::isEmpty() const{
+ return states_.empty();
+}
+
+State::Ptr StateStack::createState(States::Id id){
+ return stateFactory_[id]();
+}
+
+
+// State
+
+State::State(StateStack &stack, State::Context context)
+ : context_(context)
+ , stack_(&stack)
+{}
+
+void State::requestPush(States::Id state_id){
+ StateStack::Op op {StateStack::OpId::Push, state_id};
+ stack_->requestOp(op);
+}
+void State::requestPop(){
+ StateStack::Op op {StateStack::OpId::Push};
+ stack_->requestOp(op);
+}
+void State::requestClear(){
+ StateStack::Op op {StateStack::OpId::Clear};
+ stack_->requestOp(op);
+}