From bba1ac05e8c1854dc61bd737d228d9bc18bbb6ad Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 22 Oct 2012 22:19:56 +0200 Subject: [PATCH] added hero selection state + status stub --- Debug/src/menu/subdir.mk | 12 +++- Release/src/menu/subdir.mk | 12 +++- src/common/GameState.cpp | 3 +- src/common/GameState.h | 1 + src/main.cpp | 5 ++ src/menu/HeroStatus.h | 1 + src/menu/PartyMenu.cpp | 42 +++++++++++- src/menu/PartyMenu.h | 27 +++++++- src/menu/Resources.cpp | 23 ++++++- src/menu/Resources.h | 3 + src/menu/SelectHero.cpp | 133 +++++++++++++++++++++++++++++++++++++ src/menu/SelectHero.h | 65 ++++++++++++++++++ src/menu/StatusMenu.cpp | 60 +++++++++++++++++ src/menu/StatusMenu.h | 42 ++++++++++++ src/menu/fwd.h | 2 + test-data/hero-cursor.png | Bin 0 -> 199 bytes 16 files changed, 418 insertions(+), 13 deletions(-) create mode 100644 src/menu/SelectHero.cpp create mode 100644 src/menu/SelectHero.h create mode 100644 src/menu/StatusMenu.cpp create mode 100644 src/menu/StatusMenu.h create mode 100644 test-data/hero-cursor.png diff --git a/Debug/src/menu/subdir.mk b/Debug/src/menu/subdir.mk index 15d3746..a815457 100644 --- a/Debug/src/menu/subdir.mk +++ b/Debug/src/menu/subdir.mk @@ -6,17 +6,23 @@ CPP_SRCS += \ ../src/menu/HeroStatus.cpp \ ../src/menu/PartyMenu.cpp \ -../src/menu/Resources.cpp +../src/menu/Resources.cpp \ +../src/menu/SelectHero.cpp \ +../src/menu/StatusMenu.cpp OBJS += \ ./src/menu/HeroStatus.o \ ./src/menu/PartyMenu.o \ -./src/menu/Resources.o +./src/menu/Resources.o \ +./src/menu/SelectHero.o \ +./src/menu/StatusMenu.o CPP_DEPS += \ ./src/menu/HeroStatus.d \ ./src/menu/PartyMenu.d \ -./src/menu/Resources.d +./src/menu/Resources.d \ +./src/menu/SelectHero.d \ +./src/menu/StatusMenu.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Release/src/menu/subdir.mk b/Release/src/menu/subdir.mk index a0ea3ff..a17f156 100644 --- a/Release/src/menu/subdir.mk +++ b/Release/src/menu/subdir.mk @@ -6,17 +6,23 @@ CPP_SRCS += \ ../src/menu/HeroStatus.cpp \ ../src/menu/PartyMenu.cpp \ -../src/menu/Resources.cpp +../src/menu/Resources.cpp \ +../src/menu/SelectHero.cpp \ +../src/menu/StatusMenu.cpp OBJS += \ ./src/menu/HeroStatus.o \ ./src/menu/PartyMenu.o \ -./src/menu/Resources.o +./src/menu/Resources.o \ +./src/menu/SelectHero.o \ +./src/menu/StatusMenu.o CPP_DEPS += \ ./src/menu/HeroStatus.d \ ./src/menu/PartyMenu.d \ -./src/menu/Resources.d +./src/menu/Resources.d \ +./src/menu/SelectHero.d \ +./src/menu/StatusMenu.d # Each subdirectory must supply rules for building sources it contributes diff --git a/src/common/GameState.cpp b/src/common/GameState.cpp index 91e8d4a..ee7b1b2 100644 --- a/src/common/GameState.cpp +++ b/src/common/GameState.cpp @@ -10,7 +10,8 @@ namespace common { GameState::GameState() -: money(0) +: partySize(1) +, money(0) , time(0) { party[0] = heroes; party[1] = 0; diff --git a/src/common/GameState.h b/src/common/GameState.h index 0ef442b..078da1a 100644 --- a/src/common/GameState.h +++ b/src/common/GameState.h @@ -21,6 +21,7 @@ struct GameState { Hero heroes[7]; Hero *party[4]; + int partySize; Inventory inventory; diff --git a/src/main.cpp b/src/main.cpp index 8fcb09e..7cb7bd2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -191,6 +191,7 @@ int main(int argc, char **argv) { gameState.party[1] = &gameState.heroes[1]; gameState.party[2] = &gameState.heroes[2]; gameState.party[3] = &gameState.heroes[3]; + gameState.partySize = 4; GameConfig gameConfig; gameConfig.state = &gameState; @@ -318,6 +319,10 @@ int main(int argc, char **argv) { menuResources.mainMenuTimeText = "TIME"; menuResources.mainMenuGoldText = "GOLD"; + graphics::Sprite heroCursor(IMG_Load("test-data/hero-cursor.png"), 64, 16); + menuResources.heroCursor = &heroCursor; + menuResources.heroCursorBlinkTime = 532; + InitScreen screen(width, height); app::State *state(0); diff --git a/src/menu/HeroStatus.h b/src/menu/HeroStatus.h index 1b88da2..12fe62f 100644 --- a/src/menu/HeroStatus.h +++ b/src/menu/HeroStatus.h @@ -30,6 +30,7 @@ public: int Width() const; int Height() const; geometry::Vector Size() const { return geometry::Vector(Width(), Height()); } + const geometry::Vector &Position() const { return position; } void Render(SDL_Surface *screen, const geometry::Vector &offset) const; diff --git a/src/menu/PartyMenu.cpp b/src/menu/PartyMenu.cpp index 5a68ca4..85d19d2 100644 --- a/src/menu/PartyMenu.cpp +++ b/src/menu/PartyMenu.cpp @@ -8,6 +8,8 @@ #include "PartyMenu.h" #include "Resources.h" +#include "SelectHero.h" +#include "StatusMenu.h" #include "../app/Application.h" #include "../app/Input.h" #include "../common/GameConfig.h" @@ -86,6 +88,30 @@ void PartyMenu::HandleEvents(const Input &input) { } else if (input.JustPressed(Input::PAD_LEFT)) { mainMenu.PreviousItem(); } + + if (input.JustPressed(Input::ACTION_A)) { + switch (mainMenu.Selected()) { + case MENU_ITEM_ITEM: + break; + case MENU_ITEM_SPELL: + break; + case MENU_ITEM_CAPSULE: + break; + case MENU_ITEM_EQUIP: + break; + case MENU_ITEM_STATUS: + Ctrl().PushState(new SelectHero(this, OnStatusSelect)); + break; + case MENU_ITEM_CHANGE: + break; + case MENU_ITEM_CONFIG: + break; + case MENU_ITEM_SCENARIO: + break; + default: + break; + } + } } void PartyMenu::UpdateWorld(float deltaT) { @@ -93,12 +119,20 @@ void PartyMenu::UpdateWorld(float deltaT) { } void PartyMenu::Render(SDL_Surface *screen) { - Res().menubg->Render(screen, Vector(), Vector(screen->w, screen->h)); - RenderHeros(screen, Vector(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight())); + RenderBackground(screen); + RenderHeros(screen, StatusOffset()); RenderMenu(screen, Vector(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8)); RenderInfo(screen, Vector(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8)); } +Vector PartyMenu::StatusOffset() const { + return Vector(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight()); +} + +void PartyMenu::RenderBackground(SDL_Surface *screen) const { + Res().menubg->Render(screen, Vector(), Vector(screen->w, screen->h)); +} + void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector &offset) const { for (int i(0); i < 4; ++i) { status[i].Render(screen, offset); @@ -146,4 +180,8 @@ const Resources &PartyMenu::Res() const { return *game->menuResources; } +void PartyMenu::OnStatusSelect(PartyMenu *self, int index) { + self->Ctrl().ChangeState(new StatusMenu(self)); +} + } diff --git a/src/menu/PartyMenu.h b/src/menu/PartyMenu.h index 5baaf69..a55b2c1 100644 --- a/src/menu/PartyMenu.h +++ b/src/menu/PartyMenu.h @@ -30,9 +30,22 @@ public: virtual void Render(SDL_Surface *); public: + common::GameConfig &Game() { return *game; } + const common::GameConfig &Game() const { return *game; } Resources &Res(); const Resources &Res() const; +public: + void RenderBackground(SDL_Surface *screen) const; + void RenderHeros(SDL_Surface *screen, const geometry::Vector &offset) const; + void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset) const; + void RenderInfo(SDL_Surface *screen, const geometry::Vector &offset) const; + + geometry::Vector StatusOffset() const; + const HeroStatus &GetHeroStatus(int index) const { return status[index]; } + + static void OnStatusSelect(PartyMenu *, int); + private: virtual void OnEnterState(SDL_Surface *screen); virtual void OnExitState(SDL_Surface *screen); @@ -41,9 +54,17 @@ private: virtual void OnResize(int width, int height); - void RenderHeros(SDL_Surface *screen, const geometry::Vector &offset) const; - void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset) const; - void RenderInfo(SDL_Surface *screen, const geometry::Vector &offset) const; +private: + enum MenuItem { + MENU_ITEM_ITEM, + MENU_ITEM_SPELL, + MENU_ITEM_CAPSULE, + MENU_ITEM_EQUIP, + MENU_ITEM_STATUS, + MENU_ITEM_CHANGE, + MENU_ITEM_CONFIG, + MENU_ITEM_SCENARIO, + }; private: HeroStatus status[4]; diff --git a/src/menu/Resources.cpp b/src/menu/Resources.cpp index 98787f6..79342b2 100644 --- a/src/menu/Resources.cpp +++ b/src/menu/Resources.cpp @@ -10,7 +10,28 @@ namespace menu { Resources::Resources() -: menubg(0) { +: menubg(0) +, normalFont(0) +, statusFont(0) + +, statusLabels(0) +, statusFrame(0) + +, mainMenuProperties(0) +, mainMenuItemText(0) +, mainMenuSpellText(0) +, mainMenuCapsuleText(0) +, mainMenuEquipmentText(0) +, mainMenuStatusText(0) +, mainMenuChangeText(0) +, mainMenuConfigText(0) +, mainMenuScenarioText(0) + +, mainMenuTimeText(0) +, mainMenuGoldText(0) + +, heroCursor(0) +, heroCursorBlinkTime(0) { } diff --git a/src/menu/Resources.h b/src/menu/Resources.h index 6472d3c..e5241c3 100644 --- a/src/menu/Resources.h +++ b/src/menu/Resources.h @@ -35,6 +35,9 @@ struct Resources { const char *mainMenuTimeText; const char *mainMenuGoldText; + graphics::Sprite *heroCursor; + int heroCursorBlinkTime; + Resources(); }; diff --git a/src/menu/SelectHero.cpp b/src/menu/SelectHero.cpp new file mode 100644 index 0000000..723414f --- /dev/null +++ b/src/menu/SelectHero.cpp @@ -0,0 +1,133 @@ +/* + * SelectHero.cpp + * + * Created on: Oct 22, 2012 + * Author: holy + */ + +#include "SelectHero.h" + +#include "HeroStatus.h" +#include "PartyMenu.h" +#include "Resources.h" +#include "../app/Application.h" +#include "../app/Input.h" +#include "../common/GameConfig.h" +#include "../common/GameState.h" +#include "../common/Hero.h" +#include "../geometry/Vector.h" +#include "../graphics/Sprite.h" + +using app::Input; +using geometry::Vector; + +namespace menu { + +SelectHero::SelectHero(PartyMenu *parent, Callback cb, int cursor) +: parent(parent) +, callback(cb) +, cursor(cursor) { + +} + + +void SelectHero::OnEnterState(SDL_Surface *) { + cursorBlink = GraphicsTimers().StartInterval(Res().heroCursorBlinkTime); +} + +void SelectHero::OnExitState(SDL_Surface *) { + +} + +void SelectHero::OnResumeState(SDL_Surface *) { + +} + +void SelectHero::OnPauseState(SDL_Surface *) { + +} + + +void SelectHero::OnResize(int width, int height) { + +} + + +void SelectHero::HandleEvents(const Input &input) { + if (input.JustPressed(Input::ACTION_A)) { + callback(parent, cursor); + } + if (input.JustPressed(Input::ACTION_B)) { + Ctrl().PopState(); + } + + if (input.JustPressed(Input::PAD_UP)) { + SelectUp(); + } else if (input.JustPressed(Input::PAD_RIGHT)) { + SelectRight(); + } else if (input.JustPressed(Input::PAD_DOWN)) { + SelectDown(); + } else if (input.JustPressed(Input::PAD_LEFT)) { + SelectLeft(); + } +} + +void SelectHero::SelectUp() { + cursor = (cursor + 2) % parent->Game().state->partySize; + cursorBlink.Restart(); +} + +void SelectHero::SelectRight() { + cursor = (cursor + 1) % parent->Game().state->partySize; + cursorBlink.Restart(); +} + +void SelectHero::SelectDown() { + cursor = (cursor + 2) % parent->Game().state->partySize; + cursorBlink.Restart(); +} + +void SelectHero::SelectLeft() { + cursor = (cursor + 3) % parent->Game().state->partySize; + cursorBlink.Restart(); +} + + +common::GameConfig &SelectHero::Game() { + return parent->Game(); +} + +const common::GameConfig &SelectHero::Game() const { + return parent->Game(); +} + +Resources &SelectHero::Res() { + return parent->Res(); +} + +const Resources &SelectHero::Res() const { + return parent->Res(); +} + + +void SelectHero::UpdateWorld(float deltaT) { + +} + + +void SelectHero::Render(SDL_Surface *screen) { + parent->Render(screen); + if (cursorBlink.Iteration() % 2 == 0) { + RenderCursor(screen); + } +} + +void SelectHero::RenderCursor(SDL_Surface *screen) const { + const HeroStatus &status(parent->GetHeroStatus(cursor)); + Vector position( + 0, Game().state->party[cursor]->BattleSprite()->Height()); + position += status.Position() + parent->StatusOffset(); + Res().heroCursor->Draw(screen, position); +} + +} diff --git a/src/menu/SelectHero.h b/src/menu/SelectHero.h new file mode 100644 index 0000000..8228387 --- /dev/null +++ b/src/menu/SelectHero.h @@ -0,0 +1,65 @@ +/* + * SelectHero.h + * + * Created on: Oct 22, 2012 + * Author: holy + */ + +#ifndef MENU_SELECTHERO_H_ +#define MENU_SELECTHERO_H_ + +#include "fwd.h" +#include "../app/State.h" +#include "../app/Timer.h" +#include "../common/fwd.h" + +#include + +namespace menu { + +class SelectHero +: public app::State { + +public: + typedef void (*Callback)(PartyMenu *, int selection); + +public: + explicit SelectHero(PartyMenu *parent, Callback, int initialHero = 0); + +public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: + virtual void OnEnterState(SDL_Surface *screen); + virtual void OnExitState(SDL_Surface *screen); + virtual void OnResumeState(SDL_Surface *screen); + virtual void OnPauseState(SDL_Surface *screen); + + virtual void OnResize(int width, int height); + +private: + common::GameConfig &Game(); + const common::GameConfig &Game() const; + Resources &Res(); + const Resources &Res() const; + + void SelectUp(); + void SelectRight(); + void SelectDown(); + void SelectLeft(); + + void RenderCursor(SDL_Surface *screen) const; + +private: + PartyMenu *parent; + Callback callback; + app::Timer cursorBlink; + int cursor; + +}; + +} + +#endif /* MENU_SELECTHERO_H_ */ diff --git a/src/menu/StatusMenu.cpp b/src/menu/StatusMenu.cpp new file mode 100644 index 0000000..efc807f --- /dev/null +++ b/src/menu/StatusMenu.cpp @@ -0,0 +1,60 @@ +/* + * StatusMenu.cpp + * + * Created on: Oct 22, 2012 + * Author: holy + */ + +#include "StatusMenu.h" + +#include "PartyMenu.h" +#include "../app/Application.h" +#include "../app/Input.h" + +using app::Input; + +namespace menu { + +StatusMenu::StatusMenu(PartyMenu *parent) +: parent(parent) { + +} + + +void StatusMenu::OnEnterState(SDL_Surface *) { + +} + +void StatusMenu::OnExitState(SDL_Surface *) { + +} + +void StatusMenu::OnResumeState(SDL_Surface *) { + +} + +void StatusMenu::OnPauseState(SDL_Surface *) { + +} + + +void StatusMenu::OnResize(int width, int height) { + +} + + +void StatusMenu::HandleEvents(const Input &input) { + if (input.JustPressed(Input::ACTION_B)) { + Ctrl().PopState(); + } +} + +void StatusMenu::UpdateWorld(float deltaT) { + +} + +void StatusMenu::Render(SDL_Surface *screen) { + parent->RenderBackground(screen); +} + +} diff --git a/src/menu/StatusMenu.h b/src/menu/StatusMenu.h new file mode 100644 index 0000000..465c7c4 --- /dev/null +++ b/src/menu/StatusMenu.h @@ -0,0 +1,42 @@ +/* + * StatusMenu.h + * + * Created on: Oct 22, 2012 + * Author: holy + */ + +#ifndef MENU_STATUSMENU_H_ +#define MENU_STATUSMENU_H_ + +#include "fwd.h" +#include "../app/State.h" + +namespace menu { + +class StatusMenu +: public app::State { + +public: + explicit StatusMenu(PartyMenu *parent); + +public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: + virtual void OnEnterState(SDL_Surface *screen); + virtual void OnExitState(SDL_Surface *screen); + virtual void OnResumeState(SDL_Surface *screen); + virtual void OnPauseState(SDL_Surface *screen); + + virtual void OnResize(int width, int height); + +private: + PartyMenu *parent; + +}; + +} + +#endif /* MENU_STATUSMENU_H_ */ diff --git a/src/menu/fwd.h b/src/menu/fwd.h index 8885525..ae7932c 100644 --- a/src/menu/fwd.h +++ b/src/menu/fwd.h @@ -13,6 +13,8 @@ namespace menu { class HeroStatus; class PartyMenu; struct Resources; +class SelectHero; +class StatusMenu; } diff --git a/test-data/hero-cursor.png b/test-data/hero-cursor.png new file mode 100644 index 0000000000000000000000000000000000000000..0496815f094af7bfa566b5fc1457aec56bad9df7 GIT binary patch literal 199 zcmeAS@N?(olHy`uVBq!ia0vp^4nQox!3HFkJ+IURQhlB-jv*0;lM^IZm8A_HO$j{l z&wfqKuLP~I{YJfCL=H3=KQrAdDFFmJsw!YwMYoP-JM#>H