From 3f8fac16c7ae2cbe7da47b98aba9b558825723e7 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 17 Oct 2012 21:14:31 +0200 Subject: [PATCH] store an application handle in each state --- src/app/State.cpp | 36 +++++++++++++++++++++++--- src/app/State.h | 9 +++++++ src/battle/states/PerformAttacks.cpp | 4 +-- src/battle/states/PerformAttacks.h | 3 +-- src/battle/states/RunState.cpp | 6 ++--- src/battle/states/RunState.h | 3 +-- src/battle/states/SelectAttackType.cpp | 16 ++++++------ src/battle/states/SelectAttackType.h | 3 +-- src/battle/states/SelectIkari.cpp | 10 +++---- src/battle/states/SelectIkari.h | 3 +-- src/battle/states/SelectItem.cpp | 10 +++---- src/battle/states/SelectItem.h | 3 +-- src/battle/states/SelectMoveAction.cpp | 10 +++---- src/battle/states/SelectMoveAction.h | 3 +-- src/battle/states/SelectSpell.cpp | 10 +++---- src/battle/states/SelectSpell.h | 3 +-- src/battle/states/SelectTarget.cpp | 10 +++---- src/battle/states/SelectTarget.h | 3 +-- src/battle/states/SwapHeroes.cpp | 6 ++--- src/battle/states/SwapHeroes.h | 3 +-- src/graphics/ColorFade.cpp | 8 +++--- src/graphics/ColorFade.h | 1 - src/map/MapState.cpp | 14 +++++----- src/map/MapState.h | 1 - src/map/TransitionState.cpp | 7 +++-- src/map/TransitionState.h | 1 - 26 files changed, 102 insertions(+), 84 deletions(-) diff --git a/src/app/State.cpp b/src/app/State.cpp index 50b5bf6..f1c7712 100644 --- a/src/app/State.cpp +++ b/src/app/State.cpp @@ -7,19 +7,30 @@ #include "State.h" +#include + +using std::domain_error; + namespace app { +State::State() +: ctrl(0) { + +} + State::~State() { } -void State::EnterState(Application &ctrl, SDL_Surface *screen) { - OnEnterState(ctrl, screen); +void State::EnterState(Application &c, SDL_Surface *screen) { + ctrl = &c; + OnEnterState(c, screen); } -void State::ExitState(Application &ctrl, SDL_Surface *screen) { - OnExitState(ctrl, screen); +void State::ExitState(Application &c, SDL_Surface *screen) { + OnExitState(c, screen); + ctrl = 0; } void State::ResumeState(Application &ctrl, SDL_Surface *screen) { @@ -34,4 +45,21 @@ void State::Resize(int width, int height) { OnResize(width, height); } + +Application &State::Ctrl() { + if (ctrl) { + return *ctrl; + } else { + throw domain_error("call to app::State::Ctrl() without application context"); + } +} + +const Application &State::Ctrl() const { + if (ctrl) { + return *ctrl; + } else { + throw domain_error("call to app::State::Ctrl() without application context"); + } +} + } diff --git a/src/app/State.h b/src/app/State.h index a84afde..2a4b5e1 100644 --- a/src/app/State.h +++ b/src/app/State.h @@ -19,6 +19,7 @@ namespace app { class State { public: + State(); virtual ~State(); public: @@ -41,6 +42,13 @@ public: /// Draw a picture of the world. virtual void Render(SDL_Surface *) = 0; +protected: + /// Get a handle to the application this state is running on. + /// Do not call this while the state is off the stack (e.g. in c'tor/d'tor) + /// or you'll get a std::domain_error (potentially evil in d'tor)! + Application &Ctrl(); + const Application &Ctrl() const; + private: /// Do some setup that needs an application and/or screen handle and thus /// can not be done by the constructor. @@ -72,6 +80,7 @@ public: Timers &PhysicsTimers() { return physicsTimers; } private: + Application *ctrl; Timers graphicsTimers; Timers physicsTimers; diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 50c7b14..a1a5b8b 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -30,7 +30,6 @@ using std::vector; namespace battle { void PerformAttacks::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; battle->CalculateAttackOrder(); numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1); numberPosition.reserve(numberAnimation.size()); @@ -38,7 +37,6 @@ void PerformAttacks::OnEnterState(Application &c, SDL_Surface *screen) { void PerformAttacks::OnExitState(Application &c, SDL_Surface *screen) { battle->ClearAllAttacks(); - ctrl = 0; } void PerformAttacks::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -62,7 +60,7 @@ void PerformAttacks::HandleEvents(const Input &input) { battle->ApplyDamage(); battle->NextAttack(); if (battle->AttacksFinished()) { - ctrl->PopState(); + Ctrl().PopState(); return; } diff --git a/src/battle/states/PerformAttacks.h b/src/battle/states/PerformAttacks.h index e52160c..2fe9a06 100644 --- a/src/battle/states/PerformAttacks.h +++ b/src/battle/states/PerformAttacks.h @@ -24,7 +24,7 @@ class PerformAttacks public: explicit PerformAttacks(BattleState *battle) - : ctrl(0), battle(battle), moveAnimation(0), targetAnimation(0), titleBarText(0), cursor(-1) { } + : battle(battle), moveAnimation(0), targetAnimation(0), titleBarText(0), cursor(-1) { } public: virtual void HandleEvents(const app::Input &); @@ -52,7 +52,6 @@ private: void RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector &offset) const; private: - app::Application *ctrl; BattleState *battle; graphics::AnimationRunner moveAnimation; graphics::AnimationRunner targetAnimation; diff --git a/src/battle/states/RunState.cpp b/src/battle/states/RunState.cpp index dbf3868..cf113f2 100644 --- a/src/battle/states/RunState.cpp +++ b/src/battle/states/RunState.cpp @@ -23,11 +23,11 @@ using geometry::Vector; namespace battle { void RunState::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void RunState::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void RunState::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -47,7 +47,7 @@ void RunState::OnResize(int width, int height) { void RunState::HandleEvents(const Input &input) { if (timer.Finished()) { battle->SetRunaway(); - ctrl->PopState(); // pop self + Ctrl().PopState(); // pop self } } diff --git a/src/battle/states/RunState.h b/src/battle/states/RunState.h index 0f2818d..9cee764 100644 --- a/src/battle/states/RunState.h +++ b/src/battle/states/RunState.h @@ -19,7 +19,7 @@ class RunState public: explicit RunState(BattleState *battle) - : ctrl(0), battle(battle){ } + : battle(battle){ } public: @@ -39,7 +39,6 @@ private: void RenderTitleBar(SDL_Surface *screen, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; app::Timer timer; diff --git a/src/battle/states/SelectAttackType.cpp b/src/battle/states/SelectAttackType.cpp index c41eb67..31a0be0 100644 --- a/src/battle/states/SelectAttackType.cpp +++ b/src/battle/states/SelectAttackType.cpp @@ -28,11 +28,11 @@ using geometry::Vector; namespace battle { void SelectAttackType::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void SelectAttackType::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void SelectAttackType::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -86,11 +86,11 @@ void SelectAttackType::HandleEvents(const Input &input) { ac.Selection().SetSingle(); } ac.Selection().Reset(); - ctrl->PushState(new SelectTarget(battle, this, &ac.Selection(), battle->Res().weaponTargetCursor)); + Ctrl().PushState(new SelectTarget(battle, this, &ac.Selection(), battle->Res().weaponTargetCursor)); break; case AttackChoice::MAGIC: if (battle->ActiveHero().CanUseMagic()) { - ctrl->PushState(new SelectSpell(battle, this)); + Ctrl().PushState(new SelectSpell(battle, this)); } break; case AttackChoice::DEFEND: @@ -98,10 +98,10 @@ void SelectAttackType::HandleEvents(const Input &input) { battle->NextHero(); break; case AttackChoice::IKARI: - ctrl->PushState(new SelectIkari(battle, this)); + Ctrl().PushState(new SelectIkari(battle, this)); break; case AttackChoice::ITEM: - ctrl->PushState(new SelectItem(battle, this)); + Ctrl().PushState(new SelectItem(battle, this)); break; default: throw std::logic_error("selected invalid attack type"); @@ -110,14 +110,14 @@ void SelectAttackType::HandleEvents(const Input &input) { ac.Reset(); battle->PreviousHero(); if (battle->BeforeFirstHero()) { - ctrl->ChangeState(new SelectMoveAction(battle)); + Ctrl().ChangeState(new SelectMoveAction(battle)); } else { battle->ActiveHero().GetAttackChoice().Reset(); } } if (battle->AttackSelectionDone()) { - ctrl->PopState(); + Ctrl().PopState(); } } diff --git a/src/battle/states/SelectAttackType.h b/src/battle/states/SelectAttackType.h index 557602f..3f98327 100644 --- a/src/battle/states/SelectAttackType.h +++ b/src/battle/states/SelectAttackType.h @@ -19,7 +19,7 @@ class SelectAttackType public: explicit SelectAttackType(BattleState *battle) - : ctrl(0), battle(battle) { } + : battle(battle) { } public: virtual void HandleEvents(const app::Input &); @@ -38,7 +38,6 @@ private: void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; }; diff --git a/src/battle/states/SelectIkari.cpp b/src/battle/states/SelectIkari.cpp index 1a36e41..604689c 100644 --- a/src/battle/states/SelectIkari.cpp +++ b/src/battle/states/SelectIkari.cpp @@ -25,11 +25,11 @@ using graphics::Frame; namespace battle { void SelectIkari::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void SelectIkari::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void SelectIkari::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -65,19 +65,19 @@ void SelectIkari::HandleEvents(const Input &input) { ac.SetType(AttackChoice::IKARI); ac.SetItem(battle->ActiveHero().IkariMenu().Selected()); battle->NextHero(); - ctrl->PopState(); + Ctrl().PopState(); } else { if (ikari->GetTargetingMode().TargetsSingle()) { ac.Selection().SetSingle(); } else { ac.Selection().SetMultiple(); } - ctrl->PushState(new SelectTarget(battle, parent, &ac.Selection(), ikari->IsMagical() ? battle->Res().magicTargetCursor : battle->Res().weaponTargetCursor)); + Ctrl().PushState(new SelectTarget(battle, parent, &ac.Selection(), ikari->IsMagical() ? battle->Res().magicTargetCursor : battle->Res().weaponTargetCursor)); } } } if (input.JustPressed(Input::ACTION_B)) { - ctrl->PopState(); // return control to parent + Ctrl().PopState(); // return control to parent } if (input.JustPressed(Input::PAD_UP)) { battle->ActiveHero().IkariMenu().PreviousRow(); diff --git a/src/battle/states/SelectIkari.h b/src/battle/states/SelectIkari.h index a7383a0..136436f 100644 --- a/src/battle/states/SelectIkari.h +++ b/src/battle/states/SelectIkari.h @@ -20,7 +20,7 @@ class SelectIkari public: SelectIkari(BattleState *battle, SelectAttackType *parent) - : ctrl(0), battle(battle), parent(parent) { } + : battle(battle), parent(parent) { } public: virtual void HandleEvents(const app::Input &); @@ -41,7 +41,6 @@ private: void RenderMenu(SDL_Surface *, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; SelectAttackType *parent; diff --git a/src/battle/states/SelectItem.cpp b/src/battle/states/SelectItem.cpp index 35a0c61..fe6ce7b 100644 --- a/src/battle/states/SelectItem.cpp +++ b/src/battle/states/SelectItem.cpp @@ -24,11 +24,11 @@ using graphics::Frame; namespace battle { void SelectItem::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void SelectItem::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void SelectItem::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -64,19 +64,19 @@ void SelectItem::HandleEvents(const Input &input) { ac.SetType(AttackChoice::ITEM); ac.SetItem(item); battle->NextHero(); - ctrl->PopState(); + Ctrl().PopState(); } else { if (item->GetTargetingMode().TargetsSingle()) { ac.Selection().SetSingle(); } else { ac.Selection().SetMultiple(); } - ctrl->PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().itemTargetCursor)); + Ctrl().PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().itemTargetCursor)); } } } if (input.JustPressed(Input::ACTION_B)) { - ctrl->PopState(); // return control to parent + Ctrl().PopState(); // return control to parent } if (input.JustPressed(Input::PAD_UP)) { battle->ItemMenu().PreviousRow(); diff --git a/src/battle/states/SelectItem.h b/src/battle/states/SelectItem.h index e3d2168..52429aa 100644 --- a/src/battle/states/SelectItem.h +++ b/src/battle/states/SelectItem.h @@ -19,7 +19,7 @@ class SelectItem public: SelectItem(BattleState *battle, SelectAttackType *parent) - : ctrl(0), battle(battle), parent(parent) { } + : battle(battle), parent(parent) { } public: virtual void HandleEvents(const app::Input &); @@ -40,7 +40,6 @@ private: void RenderMenu(SDL_Surface *, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; SelectAttackType *parent; diff --git a/src/battle/states/SelectMoveAction.cpp b/src/battle/states/SelectMoveAction.cpp index 072b03d..bb71487 100644 --- a/src/battle/states/SelectMoveAction.cpp +++ b/src/battle/states/SelectMoveAction.cpp @@ -22,11 +22,11 @@ using geometry::Vector; namespace battle { void SelectMoveAction::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void SelectMoveAction::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void SelectMoveAction::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -55,14 +55,14 @@ void SelectMoveAction::HandleEvents(const Input &input) { if (input.JustPressed(Input::ACTION_A)) { switch (battle->GetMoveMenu().Selected()) { case MoveMenu::ATTACK: - ctrl->ChangeState(new SelectAttackType(battle)); + Ctrl().ChangeState(new SelectAttackType(battle)); battle->NextHero(); break; case MoveMenu::CHANGE: - ctrl->PushState(new SwapHeroes(battle, this)); + Ctrl().PushState(new SwapHeroes(battle, this)); break; case MoveMenu::RUN: - ctrl->ChangeState(new RunState(battle)); + Ctrl().ChangeState(new RunState(battle)); break; } } diff --git a/src/battle/states/SelectMoveAction.h b/src/battle/states/SelectMoveAction.h index bb04fb5..cd54aed 100644 --- a/src/battle/states/SelectMoveAction.h +++ b/src/battle/states/SelectMoveAction.h @@ -19,7 +19,7 @@ class SelectMoveAction public: explicit SelectMoveAction(BattleState *battle) - : ctrl(0), battle(battle) { } + : battle(battle) { } public: virtual void HandleEvents(const app::Input &); @@ -38,7 +38,6 @@ private: void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; }; diff --git a/src/battle/states/SelectSpell.cpp b/src/battle/states/SelectSpell.cpp index 6ea4675..623181e 100644 --- a/src/battle/states/SelectSpell.cpp +++ b/src/battle/states/SelectSpell.cpp @@ -25,11 +25,11 @@ using graphics::Frame; namespace battle { void SelectSpell::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void SelectSpell::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void SelectSpell::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -65,19 +65,19 @@ void SelectSpell::HandleEvents(const Input &input) { ac.SetType(AttackChoice::MAGIC); ac.SetSpell(spell); battle->NextHero(); - ctrl->PopState(); + Ctrl().PopState(); } else { if (spell->GetTargetingMode().TargetsSingle()) { ac.Selection().SetSingle(); } else { ac.Selection().SetMultiple(); } - ctrl->PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().magicTargetCursor)); + Ctrl().PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().magicTargetCursor)); } } } if (input.JustPressed(Input::ACTION_B)) { - ctrl->PopState(); // return control to parent + Ctrl().PopState(); // return control to parent } if (input.JustPressed(Input::PAD_UP)) { battle->ActiveHero().SpellMenu().PreviousRow(); diff --git a/src/battle/states/SelectSpell.h b/src/battle/states/SelectSpell.h index 7a0bad4..a1bbac7 100644 --- a/src/battle/states/SelectSpell.h +++ b/src/battle/states/SelectSpell.h @@ -19,7 +19,7 @@ class SelectSpell public: SelectSpell(BattleState *battle, SelectAttackType *parent) - : ctrl(0), battle(battle), parent(parent) { } + : battle(battle), parent(parent) { } public: virtual void HandleEvents(const app::Input &); @@ -40,7 +40,6 @@ private: void RenderMenu(SDL_Surface *, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; SelectAttackType *parent; diff --git a/src/battle/states/SelectTarget.cpp b/src/battle/states/SelectTarget.cpp index 4645f6d..740e46c 100644 --- a/src/battle/states/SelectTarget.cpp +++ b/src/battle/states/SelectTarget.cpp @@ -20,11 +20,11 @@ using std::vector; namespace battle { void SelectTarget::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void SelectTarget::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void SelectTarget::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -44,11 +44,11 @@ void SelectTarget::OnResize(int width, int height) { void SelectTarget::HandleEvents(const Input &input) { if (input.JustPressed(Input::ACTION_A)) { if (selection->CurrentIsSelected()) { - ctrl->PopState(); // return control to parent + Ctrl().PopState(); // return control to parent } else { selection->Select(); if (selection->SelectSingle()) { - ctrl->PopState(); // return control to parent + Ctrl().PopState(); // return control to parent } } } @@ -57,7 +57,7 @@ void SelectTarget::HandleEvents(const Input &input) { selection->Unselect(); } else { selection->UnselectAll(); - ctrl->PopState(); // return control to parent + Ctrl().PopState(); // return control to parent } } diff --git a/src/battle/states/SelectTarget.h b/src/battle/states/SelectTarget.h index 4549036..01ebba7 100644 --- a/src/battle/states/SelectTarget.h +++ b/src/battle/states/SelectTarget.h @@ -20,7 +20,7 @@ class SelectTarget public: SelectTarget(BattleState *battle, SelectAttackType *parent, TargetSelection *selection, const graphics::Sprite *cursorIcon) - : ctrl(0), battle(battle), parent(parent), selection(selection), cursorIcon(cursorIcon), flipFlop(true) { } + : battle(battle), parent(parent), selection(selection), cursorIcon(cursorIcon), flipFlop(true) { } public: virtual void HandleEvents(const app::Input &); @@ -39,7 +39,6 @@ private: void RenderCursors(SDL_Surface *screen, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; SelectAttackType *parent; TargetSelection *selection; diff --git a/src/battle/states/SwapHeroes.cpp b/src/battle/states/SwapHeroes.cpp index 678b5c8..0dcd0bd 100644 --- a/src/battle/states/SwapHeroes.cpp +++ b/src/battle/states/SwapHeroes.cpp @@ -20,11 +20,11 @@ using std::vector; namespace battle { void SwapHeroes::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void SwapHeroes::OnExitState(Application &c, SDL_Surface *screen) { - ctrl = 0; + } void SwapHeroes::OnResumeState(Application &ctrl, SDL_Surface *screen) { @@ -54,7 +54,7 @@ void SwapHeroes::HandleEvents(const Input &input) { if (cursor == selected) { selected = -1; } else { - ctrl->PopState(); + Ctrl().PopState(); } } diff --git a/src/battle/states/SwapHeroes.h b/src/battle/states/SwapHeroes.h index a5f5c62..1710721 100644 --- a/src/battle/states/SwapHeroes.h +++ b/src/battle/states/SwapHeroes.h @@ -19,7 +19,7 @@ class SwapHeroes public: SwapHeroes(BattleState *battle, SelectMoveAction *parent) - : ctrl(0), battle(battle), parent(parent), cursor(0), selected(-1), flipFlop(true) { } + : battle(battle), parent(parent), cursor(0), selected(-1), flipFlop(true) { } public: virtual void HandleEvents(const app::Input &); @@ -44,7 +44,6 @@ private: void RenderCursors(SDL_Surface *screen, const geometry::Vector &offset); private: - app::Application *ctrl; BattleState *battle; SelectMoveAction *parent; int cursor; diff --git a/src/graphics/ColorFade.cpp b/src/graphics/ColorFade.cpp index 196b0d8..f194665 100644 --- a/src/graphics/ColorFade.cpp +++ b/src/graphics/ColorFade.cpp @@ -16,8 +16,7 @@ using app::Input; namespace graphics { ColorFade::ColorFade(State *slave, Uint32 color, int duration, bool in, bool interactive) -: ctrl(0) -, slave(slave) +: slave(slave) , blinds(0) , color(color) , duration(duration) @@ -31,7 +30,6 @@ ColorFade::ColorFade(State *slave, Uint32 color, int duration, bool in, bool int } void ColorFade::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; if (leadIn > 0) { timer = GraphicsTimers().StartCountdown(leadIn); } else { @@ -94,10 +92,10 @@ void ColorFade::HandleEvents(const Input &input) { if (leadOut > 0) { timer = GraphicsTimers().StartCountdown(leadOut); } else { - ctrl->PopState(); + Ctrl().PopState(); } } else { - ctrl->PopState(); + Ctrl().PopState(); } } } diff --git a/src/graphics/ColorFade.h b/src/graphics/ColorFade.h index 2694272..5e14663 100644 --- a/src/graphics/ColorFade.h +++ b/src/graphics/ColorFade.h @@ -45,7 +45,6 @@ private: private: app::Timer timer; - app::Application *ctrl; app::State *slave; SDL_Surface *blinds; Uint32 color; diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index 7e42e13..8e70acd 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -31,7 +31,6 @@ namespace map { MapState::MapState(GameConfig *g, Map *map) : game(g) -, ctrl(0) , map(map) , controlled(0) , pushed(0) @@ -48,7 +47,6 @@ MapState::MapState(GameConfig *g, Map *map) void MapState::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; camera.Resize(screen->w, screen->h); LoadMap(map); } @@ -307,9 +305,9 @@ bool MapState::CheckMonster() { ColorFade *fadeOut(new ColorFade(this, 0, 500)); fadeOut->SetLeadOutTime(500); - ctrl->PushState(fadeIn); - ctrl->PushState(battleState); - ctrl->PushState(fadeOut); + Ctrl().PushState(fadeIn); + Ctrl().PushState(battleState); + Ctrl().PushState(fadeOut); // TODO: move entity erase to happen after the transition or battle entities.erase(e); return true; @@ -445,11 +443,11 @@ bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) { void MapState::HandleSyscall(common::ScriptRunner &r) { switch (r.Integer0()) { case TRANSITION: { - ctrl->PushState(new ColorFade(this, 0, 500, true)); - ctrl->PushState(new TransitionState(this, reinterpret_cast(r.Address0()), r.Vector0())); + Ctrl().PushState(new ColorFade(this, 0, 500, true)); + Ctrl().PushState(new TransitionState(this, reinterpret_cast(r.Address0()), r.Vector0())); ColorFade *fadeOut(new ColorFade(this, 0, 500, false)); fadeOut->SetLeadOutTime(500); - ctrl->PushState(fadeOut); + Ctrl().PushState(fadeOut); break; } } diff --git a/src/map/MapState.h b/src/map/MapState.h index fcea893..9cff10c 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -82,7 +82,6 @@ private: private: common::GameConfig *game; - app::Application *ctrl; Map *map; Entity *controlled; Entity *pushed; diff --git a/src/map/TransitionState.cpp b/src/map/TransitionState.cpp index 1a2afb5..b339381 100644 --- a/src/map/TransitionState.cpp +++ b/src/map/TransitionState.cpp @@ -18,15 +18,14 @@ using geometry::Vector; namespace map { TransitionState::TransitionState(MapState *ms, Map *map, const Vector &coordinates) -: ctrl(0) -, ms(ms) +: ms(ms) , map(map) , coordinates(coordinates) { } void TransitionState::OnEnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; + } void TransitionState::OnExitState(Application &, SDL_Surface *screen) { @@ -49,7 +48,7 @@ void TransitionState::OnResize(int width, int height) { void TransitionState::HandleEvents(const Input &input) { ms->Transition(map, coordinates); - ctrl->PopState(); + Ctrl().PopState(); } diff --git a/src/map/TransitionState.h b/src/map/TransitionState.h index 0dfa7cf..134e54a 100644 --- a/src/map/TransitionState.h +++ b/src/map/TransitionState.h @@ -35,7 +35,6 @@ private: virtual void OnResize(int width, int height); private: - app::Application *ctrl; MapState *ms; Map *map; const geometry::Vector &coordinates; -- 2.39.2