demo memory leak fix
This commit is contained in:
@@ -19,21 +19,57 @@ namespace Demo {
|
||||
template <typename StateTypeT>
|
||||
class StateManager {
|
||||
public:
|
||||
StateManager(const StateTypeT& initialState) {
|
||||
StateManager(const StateTypeT& t_initialState,
|
||||
const StateTypeT& t_exitState) {
|
||||
stateInitialized = false;
|
||||
currentState = initialState;
|
||||
currentState = t_initialState;
|
||||
exitState = t_exitState;
|
||||
}
|
||||
|
||||
~StateManager() {
|
||||
for (auto& state : states) {
|
||||
delete state;
|
||||
}
|
||||
}
|
||||
~StateManager() { freeAll(); }
|
||||
|
||||
const StateTypeT& getCurrentState() const { return currentState; }
|
||||
|
||||
const StateTypeT& getExitState() const { return exitState; }
|
||||
|
||||
bool finished() const { return currentState == exitState; }
|
||||
|
||||
std::vector<State<StateTypeT>*>* getAll() { return &states; }
|
||||
|
||||
State<StateTypeT>* get(const StateTypeT& stateType) {
|
||||
for (auto& state : states) {
|
||||
if (state->getState() == stateType) {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void add(State<StateTypeT>* state) { states.push_back(state); }
|
||||
|
||||
/** remove (without free) */
|
||||
void remove(const State<StateTypeT>* state) {
|
||||
states.erase(std::remove(states.begin(), states.end(), state),
|
||||
states.end());
|
||||
}
|
||||
|
||||
/** free and remove */
|
||||
void free(const State<StateTypeT>* state) {
|
||||
auto* found = get(state);
|
||||
if (!found) return;
|
||||
|
||||
remove(found);
|
||||
delete found;
|
||||
}
|
||||
|
||||
/** free and remove all */
|
||||
void freeAll() {
|
||||
for (auto& state : states) {
|
||||
delete state;
|
||||
}
|
||||
states.clear();
|
||||
}
|
||||
|
||||
void update() {
|
||||
for (auto& state : states) {
|
||||
if (state->getState() == currentState) {
|
||||
@@ -57,7 +93,7 @@ class StateManager {
|
||||
private:
|
||||
bool stateInitialized;
|
||||
std::vector<State<StateTypeT>*> states;
|
||||
StateTypeT currentState;
|
||||
StateTypeT currentState, exitState;
|
||||
};
|
||||
|
||||
} // namespace Demo
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
# ______ ____ ___
|
||||
# | \/ ____| |___|
|
||||
# | | | \ | |
|
||||
#-----------------------------------------------------------------------
|
||||
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
||||
# Licenced under Apache License 2.0
|
||||
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "state/state.hpp"
|
||||
#include "state/global_state_type.hpp"
|
||||
|
||||
namespace Demo {
|
||||
|
||||
class GameState : public State<GlobalStateType> {
|
||||
public:
|
||||
GameState(Engine* t_engine);
|
||||
~GameState();
|
||||
|
||||
const GlobalStateType& getState() const { return state; }
|
||||
|
||||
const bool& wantFinish() const { return _wantFinish; };
|
||||
|
||||
void onStart();
|
||||
|
||||
void update();
|
||||
|
||||
/** @return Next game state */
|
||||
GlobalStateType onFinish();
|
||||
|
||||
private:
|
||||
GlobalStateType state;
|
||||
bool _wantFinish;
|
||||
bool initialized;
|
||||
};
|
||||
|
||||
} // namespace Demo
|
||||
@@ -36,6 +36,7 @@ class IntroState : public State<GlobalStateType> {
|
||||
private:
|
||||
GlobalStateType state;
|
||||
bool _wantFinish;
|
||||
bool initialized;
|
||||
|
||||
StateManager<IntroStateType> stateManager;
|
||||
};
|
||||
|
||||
@@ -33,11 +33,11 @@ class IntroPressKeyState : public State<IntroStateType> {
|
||||
|
||||
void onStart();
|
||||
|
||||
void update();
|
||||
|
||||
/** @return Next game state */
|
||||
IntroStateType onFinish();
|
||||
|
||||
void update();
|
||||
|
||||
private:
|
||||
void renderFiller();
|
||||
void updateMap();
|
||||
|
||||
Reference in New Issue
Block a user