From 7252571fb926a187c4c40e8f4eec718f16d63ffa Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 17 Oct 2012 20:44:51 +0200 Subject: [PATCH] wrapped some virtual State methods in non-virtual calls --- Debug/src/app/subdir.mk | 9 ++++--- Release/src/app/subdir.mk | 9 ++++--- src/app/Application.cpp | 26 +++++++++---------- src/app/State.cpp | 37 ++++++++++++++++++++++++++++ src/app/State.h | 29 ++++++++++++++++------ src/battle/BattleState.h | 15 +++++------ src/battle/states/PerformAttacks.h | 9 ++++--- src/battle/states/RunState.h | 10 +++++--- src/battle/states/SelectAttackType.h | 9 ++++--- src/battle/states/SelectIkari.h | 9 ++++--- src/battle/states/SelectItem.h | 9 ++++--- src/battle/states/SelectMoveAction.h | 9 ++++--- src/battle/states/SelectSpell.h | 9 ++++--- src/battle/states/SelectTarget.h | 9 ++++--- src/battle/states/SwapHeroes.h | 9 ++++--- src/graphics/ColorFade.cpp | 2 +- src/graphics/ColorFade.h | 9 ++++--- src/map/MapState.h | 14 ++++++----- src/map/TransitionState.h | 9 ++++--- 19 files changed, 156 insertions(+), 85 deletions(-) create mode 100644 src/app/State.cpp diff --git a/Debug/src/app/subdir.mk b/Debug/src/app/subdir.mk index 8fad388..caae915 100644 --- a/Debug/src/app/subdir.mk +++ b/Debug/src/app/subdir.mk @@ -6,17 +6,20 @@ CPP_SRCS += \ ../src/app/Application.cpp \ ../src/app/Arguments.cpp \ -../src/app/Input.cpp +../src/app/Input.cpp \ +../src/app/State.cpp OBJS += \ ./src/app/Application.o \ ./src/app/Arguments.o \ -./src/app/Input.o +./src/app/Input.o \ +./src/app/State.o CPP_DEPS += \ ./src/app/Application.d \ ./src/app/Arguments.d \ -./src/app/Input.d +./src/app/Input.d \ +./src/app/State.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Release/src/app/subdir.mk b/Release/src/app/subdir.mk index cf54190..57cbd25 100644 --- a/Release/src/app/subdir.mk +++ b/Release/src/app/subdir.mk @@ -6,17 +6,20 @@ CPP_SRCS += \ ../src/app/Application.cpp \ ../src/app/Arguments.cpp \ -../src/app/Input.cpp +../src/app/Input.cpp \ +../src/app/State.cpp OBJS += \ ./src/app/Application.o \ ./src/app/Arguments.o \ -./src/app/Input.o +./src/app/Input.o \ +./src/app/State.o CPP_DEPS += \ ./src/app/Application.d \ ./src/app/Arguments.d \ -./src/app/Input.d +./src/app/Input.d \ +./src/app/State.d # Each subdirectory must supply rules for building sources it contributes diff --git a/src/app/Application.cpp b/src/app/Application.cpp index b84d724..f773cfc 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -85,32 +85,32 @@ void Application::PopState() { void Application::RealChangeState(State *s) { if (!states.empty()) { - states.top()->OnPauseState(*this, screen.Screen()); - states.top()->OnExitState(*this, screen.Screen()); + states.top()->PauseState(*this, screen.Screen()); + states.top()->ExitState(*this, screen.Screen()); states.pop(); } states.push(s); - s->OnEnterState(*this, screen.Screen()); - s->OnResumeState(*this, screen.Screen()); + s->EnterState(*this, screen.Screen()); + s->ResumeState(*this, screen.Screen()); } void Application::RealPushState(State *s) { if (!states.empty()) { - states.top()->OnPauseState(*this, screen.Screen()); + states.top()->PauseState(*this, screen.Screen()); } states.push(s); - s->OnEnterState(*this, screen.Screen()); - s->OnResumeState(*this, screen.Screen()); + s->EnterState(*this, screen.Screen()); + s->ResumeState(*this, screen.Screen()); } void Application::RealPopState() { if (states.empty()) return; - states.top()->OnPauseState(*this, screen.Screen()); - states.top()->OnExitState(*this, screen.Screen()); + states.top()->PauseState(*this, screen.Screen()); + states.top()->ExitState(*this, screen.Screen()); delete states.top(); states.pop(); if (!states.empty()) { - states.top()->OnResumeState(*this, screen.Screen()); + states.top()->ResumeState(*this, screen.Screen()); } } @@ -120,8 +120,8 @@ void Application::Quit() { void Application::PopAllStates() { while (!states.empty()) { - states.top()->OnPauseState(*this, screen.Screen()); - states.top()->OnExitState(*this, screen.Screen()); + states.top()->PauseState(*this, screen.Screen()); + states.top()->ExitState(*this, screen.Screen()); delete states.top(); states.pop(); } @@ -165,7 +165,7 @@ void Application::HandleEvents() { break; case SDL_VIDEORESIZE: screen.Resize(event.resize.w, event.resize.h); - CurrentState()->OnResize(event.resize.w, event.resize.h); + CurrentState()->Resize(event.resize.w, event.resize.h); break; case SDL_KEYDOWN: case SDL_KEYUP: diff --git a/src/app/State.cpp b/src/app/State.cpp new file mode 100644 index 0000000..50b5bf6 --- /dev/null +++ b/src/app/State.cpp @@ -0,0 +1,37 @@ +/* + * State.cpp + * + * Created on: Oct 17, 2012 + * Author: holy + */ + +#include "State.h" + +namespace app { + +State::~State() { + +} + + +void State::EnterState(Application &ctrl, SDL_Surface *screen) { + OnEnterState(ctrl, screen); +} + +void State::ExitState(Application &ctrl, SDL_Surface *screen) { + OnExitState(ctrl, screen); +} + +void State::ResumeState(Application &ctrl, SDL_Surface *screen) { + OnResumeState(ctrl, screen); +} + +void State::PauseState(Application &ctrl, SDL_Surface *screen) { + OnPauseState(ctrl, screen); +} + +void State::Resize(int width, int height) { + OnResize(width, height); +} + +} diff --git a/src/app/State.h b/src/app/State.h index 6783d76..a84afde 100644 --- a/src/app/State.h +++ b/src/app/State.h @@ -19,9 +19,29 @@ namespace app { class State { public: - virtual ~State() { }; + virtual ~State(); public: + /// Called by Application when pushing this state. + void EnterState(Application &ctrl, SDL_Surface *screen); + /// Called by Application when popping this state. + void ExitState(Application &ctrl, SDL_Surface *screen); + /// Called by Application when this state becomes the top state. + void ResumeState(Application &ctrl, SDL_Surface *screen); + /// Called by Application when this state no longer is the top state. + void PauseState(Application &ctrl, SDL_Surface *screen); + + /// Called by Application on SDL window resize events. + void Resize(int width, int height); + + /// Handle interactive events such as input and timers. + virtual void HandleEvents(const Input &) = 0; + /// Update the time-dependant world representation. + virtual void UpdateWorld(float deltaT) = 0; + /// Draw a picture of the world. + virtual void Render(SDL_Surface *) = 0; + +private: /// Do some setup that needs an application and/or screen handle and thus /// can not be done by the constructor. /// Called when the state first enters the stack. @@ -41,13 +61,6 @@ public: /// Will be fixed soom ;). virtual void OnResize(int width, int height) = 0; - /// Handle interactive events such as input and timers. - virtual void HandleEvents(const Input &) = 0; - /// Update the time-dependant world representation. - virtual void UpdateWorld(float deltaT) = 0; - /// Draw a picture of the world. - virtual void Render(SDL_Surface *) = 0; - public: /// Timers handle intended for graphics, sync'ed with render time. /// These timers are only updated for the stack top and thus appear paused diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 00cea78..cc9eaf1 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -56,13 +56,6 @@ public: void AddHero(const Hero &); public: - virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); - virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); - virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); - virtual void OnPauseState(app::Application &ctrl, SDL_Surface *screen); - - virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); virtual void UpdateWorld(float deltaT); virtual void Render(SDL_Surface *); @@ -137,6 +130,14 @@ public: void RenderHeroTags(SDL_Surface *screen, const geometry::Vector &offset); void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector &offset); +private: + virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); + virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); + virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); + virtual void OnPauseState(app::Application &ctrl, SDL_Surface *screen); + + virtual void OnResize(int width, int height); + private: void LoadInventory(); diff --git a/src/battle/states/PerformAttacks.h b/src/battle/states/PerformAttacks.h index fa16b4c..e52160c 100644 --- a/src/battle/states/PerformAttacks.h +++ b/src/battle/states/PerformAttacks.h @@ -27,6 +27,11 @@ public: : ctrl(0), battle(battle), moveAnimation(0), targetAnimation(0), titleBarText(0), cursor(-1) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -34,10 +39,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void CheckAnimations(); bool HasAnimationsRunning() const; diff --git a/src/battle/states/RunState.h b/src/battle/states/RunState.h index e2d1b1b..0f2818d 100644 --- a/src/battle/states/RunState.h +++ b/src/battle/states/RunState.h @@ -22,6 +22,12 @@ public: : ctrl(0), battle(battle){ } public: + + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -29,10 +35,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void RenderTitleBar(SDL_Surface *screen, const geometry::Vector &offset); diff --git a/src/battle/states/SelectAttackType.h b/src/battle/states/SelectAttackType.h index 690427f..557602f 100644 --- a/src/battle/states/SelectAttackType.h +++ b/src/battle/states/SelectAttackType.h @@ -22,6 +22,11 @@ public: : ctrl(0), battle(battle) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -29,10 +34,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset); diff --git a/src/battle/states/SelectIkari.h b/src/battle/states/SelectIkari.h index c213b7f..a7383a0 100644 --- a/src/battle/states/SelectIkari.h +++ b/src/battle/states/SelectIkari.h @@ -23,6 +23,11 @@ public: : ctrl(0), battle(battle), parent(parent) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -30,10 +35,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void RenderFrame(SDL_Surface *, const geometry::Vector &offset); void RenderHeadline(SDL_Surface *, const geometry::Vector &offset); diff --git a/src/battle/states/SelectItem.h b/src/battle/states/SelectItem.h index 5fe98d0..e3d2168 100644 --- a/src/battle/states/SelectItem.h +++ b/src/battle/states/SelectItem.h @@ -22,6 +22,11 @@ public: : ctrl(0), battle(battle), parent(parent) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -29,10 +34,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void RenderFrame(SDL_Surface *, const geometry::Vector &offset); void RenderHeadline(SDL_Surface *, const geometry::Vector &offset); diff --git a/src/battle/states/SelectMoveAction.h b/src/battle/states/SelectMoveAction.h index 2a55a92..bb04fb5 100644 --- a/src/battle/states/SelectMoveAction.h +++ b/src/battle/states/SelectMoveAction.h @@ -22,6 +22,11 @@ public: : ctrl(0), battle(battle) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -29,10 +34,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset); diff --git a/src/battle/states/SelectSpell.h b/src/battle/states/SelectSpell.h index 823bef2..7a0bad4 100644 --- a/src/battle/states/SelectSpell.h +++ b/src/battle/states/SelectSpell.h @@ -22,6 +22,11 @@ public: : ctrl(0), battle(battle), parent(parent) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -29,10 +34,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void RenderFrame(SDL_Surface *, const geometry::Vector &offset); void RenderHeadline(SDL_Surface *, const geometry::Vector &offset); diff --git a/src/battle/states/SelectTarget.h b/src/battle/states/SelectTarget.h index 2f1f384..4549036 100644 --- a/src/battle/states/SelectTarget.h +++ b/src/battle/states/SelectTarget.h @@ -23,6 +23,11 @@ public: : ctrl(0), battle(battle), parent(parent), selection(selection), cursorIcon(cursorIcon), flipFlop(true) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -30,10 +35,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void RenderCursors(SDL_Surface *screen, const geometry::Vector &offset); diff --git a/src/battle/states/SwapHeroes.h b/src/battle/states/SwapHeroes.h index 68e7360..a5f5c62 100644 --- a/src/battle/states/SwapHeroes.h +++ b/src/battle/states/SwapHeroes.h @@ -22,6 +22,11 @@ public: : ctrl(0), battle(battle), parent(parent), cursor(0), selected(-1), flipFlop(true) { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -29,10 +34,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void MoveUp(); void MoveRight(); diff --git a/src/graphics/ColorFade.cpp b/src/graphics/ColorFade.cpp index b33ee08..196b0d8 100644 --- a/src/graphics/ColorFade.cpp +++ b/src/graphics/ColorFade.cpp @@ -57,7 +57,7 @@ void ColorFade::OnPauseState(Application &ctrl, SDL_Surface *screen) { void ColorFade::OnResize(int width, int height) { - slave->OnResize(width, height); + slave->Resize(width, height); UpdateBlinds(width, height); } diff --git a/src/graphics/ColorFade.h b/src/graphics/ColorFade.h index 947675e..2694272 100644 --- a/src/graphics/ColorFade.h +++ b/src/graphics/ColorFade.h @@ -27,6 +27,11 @@ public: void SetLeadOutTime(int ms) { leadOut = ms; } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -34,10 +39,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: void UpdateBlinds(int width, int height); Uint8 GetAlpha() const; diff --git a/src/map/MapState.h b/src/map/MapState.h index c5f26e1..fcea893 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -30,12 +30,6 @@ public: virtual ~MapState() { } public: - virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); - virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); - virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); - virtual void OnPauseState(app::Application &ctrl, SDL_Surface *screen); - virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); virtual void UpdateWorld(float deltaT); virtual void Render(SDL_Surface *); @@ -50,6 +44,14 @@ public: virtual void HandleSyscall(common::ScriptRunner &); +private: + virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); + virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); + virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); + virtual void OnPauseState(app::Application &ctrl, SDL_Surface *screen); + + virtual void OnResize(int width, int height); + private: static bool ZCompare(const Entity *lhs, const Entity *rhs); diff --git a/src/map/TransitionState.h b/src/map/TransitionState.h index 2a99f1a..0dfa7cf 100644 --- a/src/map/TransitionState.h +++ b/src/map/TransitionState.h @@ -22,6 +22,11 @@ public: virtual ~TransitionState() { } public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: virtual void OnEnterState(app::Application &ctrl, SDL_Surface *screen); virtual void OnExitState(app::Application &ctrl, SDL_Surface *screen); virtual void OnResumeState(app::Application &ctrl, SDL_Surface *screen); @@ -29,10 +34,6 @@ public: virtual void OnResize(int width, int height); - virtual void HandleEvents(const app::Input &); - virtual void UpdateWorld(float deltaT); - virtual void Render(SDL_Surface *); - private: app::Application *ctrl; MapState *ms; -- 2.39.2