From 17afcc24569b38a9816f616bc025ba871ad3e48e Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 4 Nov 2012 15:13:59 +0100 Subject: [PATCH] added 'change' menu state --- Debug/src/menu/subdir.mk | 3 + Release/src/menu/subdir.mk | 3 + src/menu/ChangeHero.cpp | 120 +++++++++++++++++++++++++++++++++++++ src/menu/ChangeHero.h | 57 ++++++++++++++++++ src/menu/HeroStatus.cpp | 23 +++---- src/menu/HeroStatus.h | 5 +- src/menu/PartyMenu.cpp | 12 ++-- src/menu/PartyMenu.h | 2 +- src/menu/SelectHero.cpp | 24 ++++---- src/menu/SelectHero.h | 8 ++- src/menu/fwd.h | 1 + 11 files changed, 226 insertions(+), 32 deletions(-) create mode 100644 src/menu/ChangeHero.cpp create mode 100644 src/menu/ChangeHero.h diff --git a/Debug/src/menu/subdir.mk b/Debug/src/menu/subdir.mk index a815457..22cbe4b 100644 --- a/Debug/src/menu/subdir.mk +++ b/Debug/src/menu/subdir.mk @@ -4,6 +4,7 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../src/menu/ChangeHero.cpp \ ../src/menu/HeroStatus.cpp \ ../src/menu/PartyMenu.cpp \ ../src/menu/Resources.cpp \ @@ -11,6 +12,7 @@ CPP_SRCS += \ ../src/menu/StatusMenu.cpp OBJS += \ +./src/menu/ChangeHero.o \ ./src/menu/HeroStatus.o \ ./src/menu/PartyMenu.o \ ./src/menu/Resources.o \ @@ -18,6 +20,7 @@ OBJS += \ ./src/menu/StatusMenu.o CPP_DEPS += \ +./src/menu/ChangeHero.d \ ./src/menu/HeroStatus.d \ ./src/menu/PartyMenu.d \ ./src/menu/Resources.d \ diff --git a/Release/src/menu/subdir.mk b/Release/src/menu/subdir.mk index a17f156..b4b3d7a 100644 --- a/Release/src/menu/subdir.mk +++ b/Release/src/menu/subdir.mk @@ -4,6 +4,7 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../src/menu/ChangeHero.cpp \ ../src/menu/HeroStatus.cpp \ ../src/menu/PartyMenu.cpp \ ../src/menu/Resources.cpp \ @@ -11,6 +12,7 @@ CPP_SRCS += \ ../src/menu/StatusMenu.cpp OBJS += \ +./src/menu/ChangeHero.o \ ./src/menu/HeroStatus.o \ ./src/menu/PartyMenu.o \ ./src/menu/Resources.o \ @@ -18,6 +20,7 @@ OBJS += \ ./src/menu/StatusMenu.o CPP_DEPS += \ +./src/menu/ChangeHero.d \ ./src/menu/HeroStatus.d \ ./src/menu/PartyMenu.d \ ./src/menu/Resources.d \ diff --git a/src/menu/ChangeHero.cpp b/src/menu/ChangeHero.cpp new file mode 100644 index 0000000..f1d871d --- /dev/null +++ b/src/menu/ChangeHero.cpp @@ -0,0 +1,120 @@ +/* + * ChangeHero.cpp + * + * Created on: Nov 4, 2012 + * Author: holy + */ + +#include "ChangeHero.h" + +#include "HeroStatus.h" +#include "PartyMenu.h" +#include "Resources.h" +#include "SelectHero.h" +#include "../app/Application.h" +#include "../app/Input.h" +#include "../common/GameConfig.h" +#include "../common/GameState.h" + +#include + +using app::Input; +using geometry::Vector; +using std::swap; + +namespace menu { + +ChangeHero::ChangeHero(PartyMenu *parent) +: parent(parent) +, highlight(0) +, selection(0) { + +} + + +void ChangeHero::OnEnterState(SDL_Surface *) { + const HeroStatus &status(parent->GetHeroStatus(0)); + highlight = SDL_CreateRGBSurface(0, status.Width(), status.Height(), 32, 0xFF000000, 0xFF0000, 0xFF00, 0); + SDL_FillRect(highlight, 0, SDL_MapRGB(highlight->format, 0xFF, 0xFF, 0xFF)); + SDL_SetAlpha(highlight, SDL_SRCALPHA|SDL_RLEACCEL, 0x20); +} + +void ChangeHero::OnExitState(SDL_Surface *) { + SDL_FreeSurface(highlight); +} + +void ChangeHero::OnResumeState(SDL_Surface *) { + if (selection < 0) { + Ctrl().PopState(); + } else { + selection = -1; + Ctrl().PushState(new SelectHero(this, parent, this, OnHeroSelected)); + } +} + +void ChangeHero::OnPauseState(SDL_Surface *) { + +} + + +void ChangeHero::OnResize(int width, int height) { + +} + + +int ChangeHero::Width() const { + return parent->Width(); +} + +int ChangeHero::Height() const { + return parent->Height(); +} + + +void ChangeHero::HandleEvents(const Input &input) { + +} + +void ChangeHero::UpdateWorld(float deltaT) { + +} + +void ChangeHero::Render(SDL_Surface *screen) { + Vector offset((screen->w - Width()) / 2, (screen->h - Height()) / 2); + + parent->RenderBackground(screen); + RenderHighlight(screen, offset); + parent->RenderHeros(screen, offset); + parent->RenderMenu(screen, offset + Vector(8 * parent->Res().normalFont->CharWidth(), 13 * parent->Res().normalFont->CharHeight() + parent->Res().normalFont->CharHeight() / 8)); + parent->RenderInfo(screen, offset + Vector(14 * parent->Res().normalFont->CharWidth(), 21 * parent->Res().normalFont->CharHeight() + parent->Res().normalFont->CharHeight() / 8)); +} + +void ChangeHero::RenderHighlight(SDL_Surface *screen, const Vector &offset) const { + if (selection < 0) return; + Vector statusOffset(parent->StatusOffset(selection)); + statusOffset -= Vector(0, parent->Res().normalFont->CharHeight() / 8); + SDL_Rect rect; + rect.x = statusOffset.X(); + rect.y = statusOffset.Y(); + SDL_BlitSurface(highlight, 0, screen, &rect); +} + + +void ChangeHero::OnHeroSelected(void *ref, int index) { + ChangeHero *self(reinterpret_cast(ref)); + self->SelectedHero(index); +} + +void ChangeHero::SelectedHero(int index) { + if (selection < 0) { + selection = index; + } else { + if (index != selection) { + swap(parent->Game().state->party[selection], + parent->Game().state->party[index]); + } + selection = -1; + } +} + +} diff --git a/src/menu/ChangeHero.h b/src/menu/ChangeHero.h new file mode 100644 index 0000000..6bb5f31 --- /dev/null +++ b/src/menu/ChangeHero.h @@ -0,0 +1,57 @@ +/* + * ChangeHero.h + * + * Created on: Nov 4, 2012 + * Author: holy + */ + +#ifndef MENU_CHANGEHERO_H_ +#define MENU_CHANGEHERO_H_ + +#include "fwd.h" +#include "../app/State.h" +#include "../geometry/Vector.h" + +#include + +namespace menu { + +class ChangeHero +: public app::State { + +public: + explicit ChangeHero(PartyMenu *parent); + +public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +public: + int Width() const; + int Height() const; + +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); + + void SelectedHero(int index); + + void RenderHighlight(SDL_Surface *screen, const geometry::Vector &offset) const; + + static void OnHeroSelected(void *, int); + +private: + PartyMenu *parent; + SDL_Surface *highlight; + int selection; + +}; + +} + +#endif /* MENU_CHANGEHERO_H_ */ diff --git a/src/menu/HeroStatus.cpp b/src/menu/HeroStatus.cpp index 2d038b1..5bc0c3b 100644 --- a/src/menu/HeroStatus.cpp +++ b/src/menu/HeroStatus.cpp @@ -18,6 +18,7 @@ namespace menu { HeroStatus::HeroStatus() : res(0) +, party(0) , hero(0) { } @@ -28,55 +29,55 @@ HeroStatus::~HeroStatus() { int HeroStatus::Width() const { - return hero->BattleSprite()->Width() + res->statusFont->CharWidth() * 11; + return party[hero]->BattleSprite()->Width() + res->statusFont->CharWidth() * 11; } int HeroStatus::Height() const { - return hero->BattleSprite()->Height() + res->statusFont->CharWidth(); + return party[hero]->BattleSprite()->Height() + res->statusFont->CharWidth(); } void HeroStatus::Render(SDL_Surface *screen, const Vector &offset) const { - if (!hero) return; + if (!party) return; - hero->BattleSprite()->Draw(screen, offset, 0, 0); + party[hero]->BattleSprite()->Draw(screen, offset, 0, 0); // for some reason, fonts are shifted by one pixel in the original Vector nameOffset( - hero->BattleSprite()->Width(), + party[hero]->BattleSprite()->Width(), res->statusFont->CharHeight() * 7 / 8); nameOffset += offset; - res->statusFont->DrawString(hero->Name(), screen, nameOffset, 5); + res->statusFont->DrawString(party[hero]->Name(), screen, nameOffset, 5); Vector levelLabelOffset(nameOffset.X() + 6 * res->statusFont->CharWidth(), nameOffset.Y()); res->statusLabels->Draw(screen, levelLabelOffset, 0, 0); Vector levelOffset(levelLabelOffset.X() + 2 * res->statusFont->CharWidth(), levelLabelOffset.Y()); - res->statusFont->DrawNumber(hero->Level(), screen, levelOffset, 2); + res->statusFont->DrawNumber(party[hero]->Level(), screen, levelOffset, 2); Vector healthLabelOffset(nameOffset.X(), nameOffset.Y() + res->statusFont->CharHeight()); res->statusLabels->Draw(screen, healthLabelOffset, 0, 1); Vector healthOffset(nameOffset.X() + 3 * res->statusFont->CharWidth(), nameOffset.Y() + res->statusFont->CharHeight()); - res->statusFont->DrawNumber(hero->Health(), screen, healthOffset, 3); + res->statusFont->DrawNumber(party[hero]->Health(), screen, healthOffset, 3); Vector healthSeparatorOffset(healthOffset.X() + 3 * res->statusFont->CharWidth(), healthOffset.Y()); res->statusFont->DrawChar('/', screen, healthSeparatorOffset); Vector maxHealthOffset(healthSeparatorOffset.X() + res->statusFont->CharWidth(), healthOffset.Y()); - res->statusFont->DrawNumber(hero->MaxHealth(), screen, maxHealthOffset, 3); + res->statusFont->DrawNumber(party[hero]->MaxHealth(), screen, maxHealthOffset, 3); Vector manaLabelOffset(healthLabelOffset.X(), healthLabelOffset.Y() + res->statusFont->CharHeight()); res->statusLabels->Draw(screen, manaLabelOffset, 0, 2); Vector manaOffset(healthOffset.X(), healthOffset.Y() + res->statusFont->CharHeight()); - res->statusFont->DrawNumber(hero->Mana(), screen, manaOffset, 3); + res->statusFont->DrawNumber(party[hero]->Mana(), screen, manaOffset, 3); Vector manaSeparatorOffset(healthSeparatorOffset.X(), manaOffset.Y()); res->statusFont->DrawChar('/', screen, manaSeparatorOffset); Vector maxManaOffset(maxHealthOffset.X(), manaOffset.Y()); - res->statusFont->DrawNumber(hero->MaxMana(), screen, maxManaOffset, 3); + res->statusFont->DrawNumber(party[hero]->MaxMana(), screen, maxManaOffset, 3); } } diff --git a/src/menu/HeroStatus.h b/src/menu/HeroStatus.h index 2888b15..27c048e 100644 --- a/src/menu/HeroStatus.h +++ b/src/menu/HeroStatus.h @@ -24,7 +24,7 @@ public: public: void SetResources(const Resources *r) { res = r; } - void SetHero(const common::Hero *h) { hero = h; } + void SetHero(common::Hero **p, int h) { party = p; hero = h; } int Width() const; int Height() const; @@ -34,7 +34,8 @@ public: private: const Resources *res; - const common::Hero *hero; + common::Hero **party; + int hero; }; diff --git a/src/menu/PartyMenu.cpp b/src/menu/PartyMenu.cpp index 79b2be0..e605c19 100644 --- a/src/menu/PartyMenu.cpp +++ b/src/menu/PartyMenu.cpp @@ -7,6 +7,7 @@ #include "PartyMenu.h" +#include "ChangeHero.h" #include "Resources.h" #include "SelectHero.h" #include "StatusMenu.h" @@ -29,7 +30,7 @@ PartyMenu::PartyMenu(GameConfig *game) : game(game) , mainMenu(*game->menuResources->mainMenuProperties) { for (int i(0); i < 4; ++i) { - status[i].SetHero(game->state->party[i]); + status[i].SetHero(game->state->party, i); status[i].SetResources(game->menuResources); } statusPositions[0] = Vector(0, 0); @@ -101,9 +102,10 @@ void PartyMenu::HandleEvents(const Input &input) { case MENU_ITEM_EQUIP: break; case MENU_ITEM_STATUS: - Ctrl().PushState(new SelectHero(this, OnStatusSelect)); + Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect)); break; case MENU_ITEM_CHANGE: + Ctrl().PushState(new ChangeHero(this)); break; case MENU_ITEM_CONFIG: break; @@ -196,8 +198,10 @@ const Resources &PartyMenu::Res() const { return *game->menuResources; } -void PartyMenu::OnStatusSelect(PartyMenu *self, int index) { - self->Ctrl().ChangeState(new StatusMenu(self, index)); +void PartyMenu::OnStatusSelect(void *ref, int index) { + PartyMenu *self(reinterpret_cast(ref)); + self->Ctrl().ChangeState( + new StatusMenu(self, index)); } } diff --git a/src/menu/PartyMenu.h b/src/menu/PartyMenu.h index d7edf31..fa1b92f 100644 --- a/src/menu/PartyMenu.h +++ b/src/menu/PartyMenu.h @@ -47,7 +47,7 @@ public: geometry::Vector StatusOffset(int index) const; const HeroStatus &GetHeroStatus(int index) const { return status[index]; } - static void OnStatusSelect(PartyMenu *, int); + static void OnStatusSelect(void *, int); private: virtual void OnEnterState(SDL_Surface *screen); diff --git a/src/menu/SelectHero.cpp b/src/menu/SelectHero.cpp index 74e6f88..12690db 100644 --- a/src/menu/SelectHero.cpp +++ b/src/menu/SelectHero.cpp @@ -23,8 +23,10 @@ using geometry::Vector; namespace menu { -SelectHero::SelectHero(PartyMenu *parent, Callback cb, int cursor) +SelectHero::SelectHero(app::State *parent, PartyMenu *pm, void *ref, Callback cb, int cursor) : parent(parent) +, partyMenu(pm) +, ref(ref) , callback(cb) , cursor(cursor) { @@ -55,7 +57,7 @@ void SelectHero::OnResize(int width, int height) { void SelectHero::HandleEvents(const Input &input) { if (input.JustPressed(Input::ACTION_A)) { - callback(parent, cursor); + callback(ref, cursor); } if (input.JustPressed(Input::ACTION_B)) { Ctrl().PopState(); @@ -73,40 +75,40 @@ void SelectHero::HandleEvents(const Input &input) { } void SelectHero::SelectUp() { - cursor = (cursor + 2) % parent->Game().state->partySize; + cursor = (cursor + 2) % partyMenu->Game().state->partySize; cursorBlink.Restart(); } void SelectHero::SelectRight() { - cursor = (cursor + 1) % parent->Game().state->partySize; + cursor = (cursor + 1) % partyMenu->Game().state->partySize; cursorBlink.Restart(); } void SelectHero::SelectDown() { - cursor = (cursor + 2) % parent->Game().state->partySize; + cursor = (cursor + 2) % partyMenu->Game().state->partySize; cursorBlink.Restart(); } void SelectHero::SelectLeft() { - cursor = (cursor + 3) % parent->Game().state->partySize; + cursor = (cursor + 3) % partyMenu->Game().state->partySize; cursorBlink.Restart(); } common::GameConfig &SelectHero::Game() { - return parent->Game(); + return partyMenu->Game(); } const common::GameConfig &SelectHero::Game() const { - return parent->Game(); + return partyMenu->Game(); } Resources &SelectHero::Res() { - return parent->Res(); + return partyMenu->Res(); } const Resources &SelectHero::Res() const { - return parent->Res(); + return partyMenu->Res(); } @@ -125,7 +127,7 @@ void SelectHero::Render(SDL_Surface *screen) { void SelectHero::RenderCursor(SDL_Surface *screen) const { Vector position( 0, Game().state->party[cursor]->BattleSprite()->Height()); - position += parent->StatusOffset(cursor); + position += partyMenu->StatusOffset(cursor); Res().heroCursor->Draw(screen, position); } diff --git a/src/menu/SelectHero.h b/src/menu/SelectHero.h index 8228387..5e67152 100644 --- a/src/menu/SelectHero.h +++ b/src/menu/SelectHero.h @@ -21,10 +21,10 @@ class SelectHero : public app::State { public: - typedef void (*Callback)(PartyMenu *, int selection); + typedef void (*Callback)(void *, int selection); public: - explicit SelectHero(PartyMenu *parent, Callback, int initialHero = 0); + SelectHero(app::State *parent, PartyMenu *partyMenu, void *ref, Callback, int initialHero = 0); public: virtual void HandleEvents(const app::Input &); @@ -53,7 +53,9 @@ private: void RenderCursor(SDL_Surface *screen) const; private: - PartyMenu *parent; + app::State *parent; + PartyMenu *partyMenu; + void *ref; Callback callback; app::Timer cursorBlink; int cursor; diff --git a/src/menu/fwd.h b/src/menu/fwd.h index ae7932c..3017e93 100644 --- a/src/menu/fwd.h +++ b/src/menu/fwd.h @@ -10,6 +10,7 @@ namespace menu { +class ChangeHero; class HeroStatus; class PartyMenu; struct Resources; -- 2.39.2