From: Daniel Karbach Date: Tue, 29 Jan 2013 23:05:09 +0000 (+0100) Subject: cached some of the battle coordinates X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=0b11a24a8b08c49d6e4301573602fb6d01e7a8c8;p=l2e.git cached some of the battle coordinates --- diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 66097fb..57ff327 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -73,7 +73,9 @@ void BattleState::SwapHeroes(int lhs, int rhs) { void BattleState::OnResize(int w, int h) { - + offset = Vector( + (w - background->w) / 2, + (h - background->h) / 2); } @@ -111,6 +113,8 @@ void BattleState::OnEnterState(SDL_Surface *screen) { smallHeroTagPositions[2] = Vector(xOffset + tagWidth, yOffset); smallHeroTagPositions[3] = Vector(xOffset + 3 * tagWidth, yOffset); + OnResize(screen->w, screen->h); + itemMenu = *res->itemMenuProperties; LoadInventory(); ClearAllAttacks(); @@ -396,12 +400,11 @@ void BattleState::UpdateWorld(Uint32 deltaT) { void BattleState::Render(SDL_Surface *screen) { assert(screen); - Vector offset(CalculateScreenOffset(screen)); - RenderBackground(screen, offset); - RenderMonsters(screen, offset); + RenderBackground(screen); + RenderMonsters(screen); } -void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderBackground(SDL_Surface *screen) { assert(screen); // black for now SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); @@ -413,7 +416,7 @@ void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offse SDL_BlitSurface(background, 0, screen, &destRect); } -void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderMonsters(SDL_Surface *screen) { assert(screen); for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { if (MonsterPositionOccupied(i)) { @@ -426,7 +429,7 @@ void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) } } -void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderHeroes(SDL_Surface *screen) { assert(screen); for (int i(0); i < numHeroes; ++i) { if (heroes[i].GetAnimation().Running()) { @@ -438,7 +441,7 @@ void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { } } -void BattleState::RenderCapsule(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderCapsule(SDL_Surface *screen) { if (!capsule.Active() || capsule.Health() <= 0) return; if (capsule.GetAnimation().Running()) { capsule.GetAnimation().DrawCenter(screen, capsule.Position() + offset); @@ -447,7 +450,7 @@ void BattleState::RenderCapsule(SDL_Surface *screen, const Vector &offset) } } -void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderHeroTags(SDL_Surface *screen) { assert(screen); int tagHeight(attackTypeMenu.Height()); int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2); @@ -457,7 +460,7 @@ void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) } } -void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderSmallHeroTags(SDL_Surface *screen) { assert(screen); int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2); int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2); diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 8eb8f8d..cff0e43 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -129,21 +129,17 @@ public: bool Defeat() const; public: - math::Vector CalculateScreenOffset(SDL_Surface *screen) const { - return math::Vector( - (screen->w - background->w) / 2, - (screen->h - background->h) / 2); - } + const math::Vector &ScreenOffset() const { return offset; } int Width() const { return background->w; } int Height() const { return background->h; } math::Vector Size() const { return math::Vector(Width(), Height()); } - void RenderBackground(SDL_Surface *screen, const math::Vector &offset); - void RenderMonsters(SDL_Surface *screen, const math::Vector &offset); - void RenderHeroes(SDL_Surface *screen, const math::Vector &offset); - void RenderCapsule(SDL_Surface *screen, const math::Vector &offset); - void RenderHeroTags(SDL_Surface *screen, const math::Vector &offset); - void RenderSmallHeroTags(SDL_Surface *screen, const math::Vector &offset); + void RenderBackground(SDL_Surface *screen); + void RenderMonsters(SDL_Surface *screen); + void RenderHeroes(SDL_Surface *screen); + void RenderCapsule(SDL_Surface *screen); + void RenderHeroTags(SDL_Surface *screen); + void RenderSmallHeroTags(SDL_Surface *screen); private: virtual void OnEnterState(SDL_Surface *screen); @@ -177,6 +173,9 @@ private: SmallHeroTag smallHeroTags[4]; math::Vector heroTagPositions[4]; math::Vector smallHeroTagPositions[4]; + + math::Vector offset; + Capsule capsule; int numHeroes; int activeHero; diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index bc6a118..92e5185 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -27,6 +27,7 @@ void PerformAttacks::OnEnterState(SDL_Surface *screen) { battle->CalculateAttackOrder(); numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1); numberPosition.reserve(numberAnimation.size()); + OnResize(screen->w, screen->h); } void PerformAttacks::OnExitState(SDL_Surface *screen) { @@ -43,7 +44,11 @@ void PerformAttacks::OnPauseState(SDL_Surface *screen) { void PerformAttacks::OnResize(int width, int height) { - + const Resources &res = battle->Res(); + framePosition = battle->ScreenOffset(); + frameSize = Vector( + battle->Width(), + res.titleFrame->BorderHeight() * 2 + res.titleFont->CharHeight()); } @@ -119,7 +124,12 @@ void PerformAttacks::HandleEvents(const Input &input) { } } - if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850); + if (titleBarText) { + titleBarTimer = GraphicsTimers().StartCountdown(850); + textPosition = battle->ScreenOffset() + Vector( + (battle->Width() - std::strlen(titleBarText) * battle->Res().titleFont->CharWidth()) / 2, + battle->Res().titleFrame->BorderHeight()); + } if (moveAnimation.Valid()) { moveAnimation.Start(*this); if (battle->CurrentAttack().IsMonster()) { @@ -143,11 +153,11 @@ void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) { if (ts.IsBad(i)) { numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite)); numberPosition.push_back( - battle->MonsterAt(i).Position()); + battle->MonsterAt(i).Position() + battle->ScreenOffset()); } else if (ts.IsGood(i)) { numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); numberPosition.push_back( - battle->MonsterAt(i).Position()); + battle->MonsterAt(i).Position() + battle->ScreenOffset()); } } } else { @@ -155,11 +165,11 @@ void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) { if (ts.IsBad(i)) { numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite)); numberPosition.push_back( - battle->HeroAt(i).Position()); + battle->HeroAt(i).Position() + battle->ScreenOffset()); } else if (ts.IsGood(i)) { numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); numberPosition.push_back( - battle->HeroAt(i).Position()); + battle->HeroAt(i).Position() + battle->ScreenOffset()); } } } @@ -215,51 +225,46 @@ void PerformAttacks::UpdateWorld(Uint32 deltaT) { } void PerformAttacks::Render(SDL_Surface *screen) { - Vector offset(battle->CalculateScreenOffset(screen)); - battle->RenderBackground(screen, offset); - battle->RenderMonsters(screen, offset); - battle->RenderHeroes(screen, offset); - battle->RenderCapsule(screen, offset); - battle->RenderSmallHeroTags(screen, offset); - RenderTitleBar(screen, offset); - RenderNumbers(screen, offset); - RenderTargetAnimation(screen, offset); + battle->RenderBackground(screen); + battle->RenderMonsters(screen); + battle->RenderHeroes(screen); + battle->RenderCapsule(screen); + battle->RenderSmallHeroTags(screen); + RenderTitleBar(screen); + RenderNumbers(screen); + RenderTargetAnimation(screen); } -void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) const { +void PerformAttacks::RenderTitleBar(SDL_Surface *screen) const { if (!titleBarText || !titleBarTimer.Running()) return; - int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight()); - battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height); + battle->Res().titleFrame->Draw(screen, framePosition, frameSize.X(), frameSize.Y()); - Vector textPosition( - (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2, - battle->Res().titleFrame->BorderHeight()); - battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset); + battle->Res().titleFont->DrawString(titleBarText, screen, textPosition); } -void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector &offset) const { +void PerformAttacks::RenderNumbers(SDL_Surface *screen) const { for (vector::size_type i(0), end(numberAnimation.size()); i < end; ++i) { if (numberAnimation[i].Running()) { Vector align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4); - numberAnimation[i].Draw(screen, numberPosition[i] + align + offset); + numberAnimation[i].Draw(screen, numberPosition[i] + align); } } } -void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const math::Vector &offset) const { +void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen) const { if (!targetAnimation.Valid() || !targetAnimation.Running()) return; const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection()); if (ts.TargetsHeroes()) { for (vector >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) { if (ts.IsSelected(i)) { - targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset); + targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + battle->ScreenOffset()); } } } else { for (vector >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) { if (ts.IsSelected(i)) { - targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset); + targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + battle->ScreenOffset()); } } } diff --git a/src/battle/states/PerformAttacks.h b/src/battle/states/PerformAttacks.h index ab15525..3d8b2ff 100644 --- a/src/battle/states/PerformAttacks.h +++ b/src/battle/states/PerformAttacks.h @@ -42,9 +42,9 @@ private: private: void AddNumberAnimations(const TargetSelection &); - void RenderTitleBar(SDL_Surface *screen, const math::Vector &offset) const; - void RenderNumbers(SDL_Surface *screen, const math::Vector &offset) const; - void RenderTargetAnimation(SDL_Surface *screen, const math::Vector &offset) const; + void RenderTitleBar(SDL_Surface *screen) const; + void RenderNumbers(SDL_Surface *screen) const; + void RenderTargetAnimation(SDL_Surface *screen) const; private: BattleState *battle; @@ -55,6 +55,9 @@ private: app::Timer targetAnimationTimer; std::vector numberAnimation; std::vector > numberPosition; + math::Vector framePosition; + math::Vector frameSize; + math::Vector textPosition; int cursor; }; diff --git a/src/battle/states/RunState.cpp b/src/battle/states/RunState.cpp index 7e7dfc4..7fbb783 100644 --- a/src/battle/states/RunState.cpp +++ b/src/battle/states/RunState.cpp @@ -16,7 +16,7 @@ using math::Vector; namespace battle { void RunState::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void RunState::OnExitState(SDL_Surface *screen) { @@ -33,7 +33,14 @@ void RunState::OnPauseState(SDL_Surface *screen) { void RunState::OnResize(int width, int height) { + framePosition = battle->ScreenOffset(); + frameSize = Vector ( + battle->Width(), + battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight()); + textPosition = Vector( + (battle->Width() - std::strlen(battle->Res().escapeText) * battle->Res().titleFont->CharWidth()) / 2, + battle->Res().titleFrame->BorderHeight()); } @@ -50,22 +57,17 @@ void RunState::UpdateWorld(Uint32 deltaT) { } void RunState::Render(SDL_Surface *screen) { - Vector offset(battle->CalculateScreenOffset(screen)); - battle->RenderBackground(screen, offset); - battle->RenderMonsters(screen, offset); - battle->RenderHeroes(screen, offset); - battle->RenderSmallHeroTags(screen, offset); - RenderTitleBar(screen, offset); + battle->RenderBackground(screen); + battle->RenderMonsters(screen); + battle->RenderHeroes(screen); + battle->RenderSmallHeroTags(screen); + RenderTitleBar(screen); } -void RunState::RenderTitleBar(SDL_Surface *screen, const Vector &offset) { - int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight()); - battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height); - - Vector textPosition( - (battle->Width() - (std::strlen(battle->Res().escapeText) * battle->Res().titleFont->CharWidth())) / 2, - battle->Res().titleFrame->BorderHeight()); - battle->Res().titleFont->DrawString(battle->Res().escapeText, screen, textPosition + offset); +void RunState::RenderTitleBar(SDL_Surface *screen) { +const Resources &res = battle->Res(); + res.titleFrame->Draw(screen, framePosition, frameSize.X(), frameSize.Y()); + res.titleFont->DrawString(res.escapeText, screen, textPosition); } } diff --git a/src/battle/states/RunState.h b/src/battle/states/RunState.h index d341197..f94bd3e 100644 --- a/src/battle/states/RunState.h +++ b/src/battle/states/RunState.h @@ -4,12 +4,9 @@ namespace battle { class BattleState; } -namespace math { - template - class Vector; -} #include "../../app/State.h" +#include "../../math/Vector.h" namespace battle { @@ -35,11 +32,14 @@ private: virtual void OnResize(int width, int height); private: - void RenderTitleBar(SDL_Surface *screen, const math::Vector &offset); + void RenderTitleBar(SDL_Surface *screen); private: BattleState *battle; app::Timer timer; + math::Vector framePosition; + math::Vector frameSize; + math::Vector textPosition; }; diff --git a/src/battle/states/SelectAttackType.cpp b/src/battle/states/SelectAttackType.cpp index 20ab7a4..b4f5dcc 100644 --- a/src/battle/states/SelectAttackType.cpp +++ b/src/battle/states/SelectAttackType.cpp @@ -22,7 +22,7 @@ using math::Vector; namespace battle { void SelectAttackType::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void SelectAttackType::OnExitState(SDL_Surface *screen) { @@ -46,7 +46,11 @@ void SelectAttackType::OnPauseState(SDL_Surface *screen) { void SelectAttackType::OnResize(int width, int height) { - + Vector offset(battle->ScreenOffset()); + Vector position( + (battle->Width() - battle->GetAttackTypeMenu().Width()) / 2, + battle->Height() - battle->GetAttackTypeMenu().Height() - battle->GetAttackTypeMenu().Height() / 2); + menuOffset = offset + position; } @@ -120,18 +124,14 @@ void SelectAttackType::UpdateWorld(Uint32 deltaT) { } void SelectAttackType::Render(SDL_Surface *screen) { - Vector offset(battle->CalculateScreenOffset(screen)); - battle->RenderBackground(screen, offset); - battle->RenderMonsters(screen, offset); - battle->RenderHeroTags(screen, offset); - RenderMenu(screen, offset); + battle->RenderBackground(screen); + battle->RenderMonsters(screen); + battle->RenderHeroTags(screen); + RenderMenu(screen); } -void SelectAttackType::RenderMenu(SDL_Surface *screen, const Vector &offset) { - Vector position( - (battle->Width() - battle->GetAttackTypeMenu().Width()) / 2, - battle->Height() - battle->GetAttackTypeMenu().Height() - battle->GetAttackTypeMenu().Height() / 2); - battle->GetAttackTypeMenu().Render(screen, position + offset); +void SelectAttackType::RenderMenu(SDL_Surface *screen) { + battle->GetAttackTypeMenu().Render(screen, menuOffset); } } diff --git a/src/battle/states/SelectAttackType.h b/src/battle/states/SelectAttackType.h index b7903a7..8794a48 100644 --- a/src/battle/states/SelectAttackType.h +++ b/src/battle/states/SelectAttackType.h @@ -4,12 +4,9 @@ namespace battle { class BattleState; } -namespace math { - template - class Vector; -} #include "../../app/State.h" +#include "../../math/Vector.h" namespace battle { @@ -34,10 +31,11 @@ private: virtual void OnResize(int width, int height); private: - void RenderMenu(SDL_Surface *screen, const math::Vector &offset); + void RenderMenu(SDL_Surface *screen); private: BattleState *battle; + math::Vector menuOffset; }; diff --git a/src/battle/states/SelectIkari.cpp b/src/battle/states/SelectIkari.cpp index 8d2006a..2ab66a9 100644 --- a/src/battle/states/SelectIkari.cpp +++ b/src/battle/states/SelectIkari.cpp @@ -19,7 +19,7 @@ using graphics::Frame; namespace battle { void SelectIkari::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void SelectIkari::OnExitState(SDL_Surface *screen) { @@ -40,7 +40,23 @@ void SelectIkari::OnPauseState(SDL_Surface *screen) { void SelectIkari::OnResize(int width, int height) { + const Vector offset = battle->ScreenOffset(); + + const Resources &res = battle->Res(); + const Frame &frame = *res.selectFrame; + + framePosition = offset + frame.BorderSize(); + frameSize = Vector( + battle->Width() - 2 * frame.BorderWidth(), + res.normalFont->CharHeight() * 13); + + headlinePosition = offset + Vector( + 2 * frame.BorderWidth() + res.normalFont->CharWidth(), + 2 * frame.BorderHeight()); + menuPosition = offset + Vector( + 2 * frame.BorderWidth() + res.normalFont->CharWidth(), + 2 * frame.BorderHeight() + 2 * res.normalFont->CharHeight()); } @@ -93,34 +109,23 @@ void SelectIkari::UpdateWorld(Uint32 deltaT) { void SelectIkari::Render(SDL_Surface *screen) { parent->Render(screen); - Vector offset(battle->CalculateScreenOffset(screen)); - RenderFrame(screen, offset); - RenderHeadline(screen, offset); - RenderMenu(screen, offset); + RenderFrame(screen); + RenderHeadline(screen); + RenderMenu(screen); } -void SelectIkari::RenderFrame(SDL_Surface *screen, const Vector &offset) { - const Frame *frame(battle->Res().selectFrame); - Vector position(frame->BorderSize()); - int width(battle->Width() - 2 * frame->BorderWidth()); - int height(battle->Res().normalFont->CharHeight() * 13); - frame->Draw(screen, position + offset, width, height); +void SelectIkari::RenderFrame(SDL_Surface *screen) { + const Frame &frame = *battle->Res().selectFrame; + frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y()); } -void SelectIkari::RenderHeadline(SDL_Surface *screen, const Vector &offset) { - const Resources &res(battle->Res()); - Vector position( - 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(), - 2 * res.selectFrame->BorderHeight()); - res.normalFont->DrawString(res.ikariMenuHeadline, screen, position + offset); +void SelectIkari::RenderHeadline(SDL_Surface *screen) { + const Resources &res = battle->Res(); + res.normalFont->DrawString(res.ikariMenuHeadline, screen, headlinePosition); } -void SelectIkari::RenderMenu(SDL_Surface *screen, const Vector &offset) { - const Resources &res(battle->Res()); - Vector position( - 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(), - 2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight()); - battle->ActiveHero().IkariMenu().Draw(screen, position + offset); +void SelectIkari::RenderMenu(SDL_Surface *screen) { + battle->ActiveHero().IkariMenu().Draw(screen, menuPosition); } } diff --git a/src/battle/states/SelectIkari.h b/src/battle/states/SelectIkari.h index 43fa0cf..254da72 100644 --- a/src/battle/states/SelectIkari.h +++ b/src/battle/states/SelectIkari.h @@ -5,12 +5,9 @@ namespace battle { class BattleState; class SelectAttackType; } -namespace math { - template - class Vector; -} #include "../../app/State.h" +#include "../../math/Vector.h" namespace battle { @@ -36,13 +33,17 @@ private: virtual void OnResize(int width, int height); private: - void RenderFrame(SDL_Surface *, const math::Vector &offset); - void RenderHeadline(SDL_Surface *, const math::Vector &offset); - void RenderMenu(SDL_Surface *, const math::Vector &offset); + void RenderFrame(SDL_Surface *); + void RenderHeadline(SDL_Surface *); + void RenderMenu(SDL_Surface *); private: BattleState *battle; SelectAttackType *parent; + math::Vector framePosition; + math::Vector frameSize; + math::Vector headlinePosition; + math::Vector menuPosition; }; diff --git a/src/battle/states/SelectItem.cpp b/src/battle/states/SelectItem.cpp index 2a90b03..0aaf7a3 100644 --- a/src/battle/states/SelectItem.cpp +++ b/src/battle/states/SelectItem.cpp @@ -18,7 +18,7 @@ using graphics::Frame; namespace battle { void SelectItem::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void SelectItem::OnExitState(SDL_Surface *screen) { @@ -39,7 +39,23 @@ void SelectItem::OnPauseState(SDL_Surface *screen) { void SelectItem::OnResize(int width, int height) { + const Vector offset = battle->ScreenOffset(); + + const Resources &res = battle->Res(); + const Frame &frame = *res.selectFrame; + + framePosition = offset + frame.BorderSize(); + frameSize = Vector( + battle->Width() - 2 * frame.BorderWidth(), + res.normalFont->CharHeight() * 13); + + headlinePosition = offset + Vector( + 2 * frame.BorderWidth() + res.normalFont->CharWidth(), + 2 * frame.BorderHeight()); + menuPosition = offset + Vector( + 2 * frame.BorderWidth() + res.normalFont->CharWidth(), + 2 * frame.BorderHeight() + 2 * res.normalFont->CharHeight()); } @@ -92,34 +108,23 @@ void SelectItem::UpdateWorld(Uint32 deltaT) { void SelectItem::Render(SDL_Surface *screen) { parent->Render(screen); - Vector offset(battle->CalculateScreenOffset(screen)); - RenderFrame(screen, offset); - RenderHeadline(screen, offset); - RenderMenu(screen, offset); + RenderFrame(screen); + RenderHeadline(screen); + RenderMenu(screen); } -void SelectItem::RenderFrame(SDL_Surface *screen, const Vector &offset) { - const Frame *frame(battle->Res().selectFrame); - Vector position(frame->BorderSize()); - int width(battle->Width() - 2 * frame->BorderWidth()); - int height(battle->Res().normalFont->CharHeight() * 13); - frame->Draw(screen, position + offset, width, height); +void SelectItem::RenderFrame(SDL_Surface *screen) { + const Frame &frame = *battle->Res().selectFrame; + frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y()); } -void SelectItem::RenderHeadline(SDL_Surface *screen, const Vector &offset) { - const Resources &res(battle->Res()); - Vector position( - 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(), - 2 * res.selectFrame->BorderHeight()); - res.normalFont->DrawString(res.itemMenuHeadline, screen, position + offset); +void SelectItem::RenderHeadline(SDL_Surface *screen) { + const Resources &res = battle->Res(); + res.normalFont->DrawString(res.itemMenuHeadline, screen, headlinePosition); } -void SelectItem::RenderMenu(SDL_Surface *screen, const Vector &offset) { - const Resources &res(battle->Res()); - Vector position( - 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(), - 2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight()); - battle->ItemMenu().Draw(screen, position + offset); +void SelectItem::RenderMenu(SDL_Surface *screen) { + battle->ItemMenu().Draw(screen, menuPosition); } } diff --git a/src/battle/states/SelectItem.h b/src/battle/states/SelectItem.h index e9a30d8..9e9bcd0 100644 --- a/src/battle/states/SelectItem.h +++ b/src/battle/states/SelectItem.h @@ -5,12 +5,9 @@ namespace battle { class BattleState; class SelectAttackType; } -namespace math { - template - class Vector; -} #include "../../app/State.h" +#include "../../math/Vector.h" namespace battle { @@ -35,13 +32,17 @@ private: virtual void OnResize(int width, int height); private: - void RenderFrame(SDL_Surface *, const math::Vector &offset); - void RenderHeadline(SDL_Surface *, const math::Vector &offset); - void RenderMenu(SDL_Surface *, const math::Vector &offset); + void RenderFrame(SDL_Surface *); + void RenderHeadline(SDL_Surface *); + void RenderMenu(SDL_Surface *); private: BattleState *battle; SelectAttackType *parent; + math::Vector framePosition; + math::Vector frameSize; + math::Vector headlinePosition; + math::Vector menuPosition; }; diff --git a/src/battle/states/SelectMoveAction.cpp b/src/battle/states/SelectMoveAction.cpp index d789874..c8405da 100644 --- a/src/battle/states/SelectMoveAction.cpp +++ b/src/battle/states/SelectMoveAction.cpp @@ -16,7 +16,7 @@ using math::Vector; namespace battle { void SelectMoveAction::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void SelectMoveAction::OnExitState(SDL_Surface *screen) { @@ -33,7 +33,9 @@ void SelectMoveAction::OnPauseState(SDL_Surface *screen) { void SelectMoveAction::OnResize(int width, int height) { - + position = battle->ScreenOffset() + Vector( + (battle->Width() - battle->GetMoveMenu().Width()) / 2, + battle->Height() - battle->GetMoveMenu().Height() - battle->GetMoveMenu().Height() / 2); } @@ -67,18 +69,14 @@ void SelectMoveAction::UpdateWorld(Uint32 deltaT) { } void SelectMoveAction::Render(SDL_Surface *screen) { - Vector offset(battle->CalculateScreenOffset(screen)); - battle->RenderBackground(screen, offset); - battle->RenderMonsters(screen, offset); - battle->RenderHeroTags(screen, offset); - RenderMenu(screen, offset); + battle->RenderBackground(screen); + battle->RenderMonsters(screen); + battle->RenderHeroTags(screen); + RenderMenu(screen); } -void SelectMoveAction::RenderMenu(SDL_Surface *screen, const Vector &offset) { - Vector position( - (battle->Width() - battle->GetMoveMenu().Width()) / 2, - battle->Height() - battle->GetMoveMenu().Height() - battle->GetMoveMenu().Height() / 2); - battle->GetMoveMenu().Render(screen, position + offset); +void SelectMoveAction::RenderMenu(SDL_Surface *screen) { + battle->GetMoveMenu().Render(screen, position); } } diff --git a/src/battle/states/SelectMoveAction.h b/src/battle/states/SelectMoveAction.h index 1eb7a48..eb299ad 100644 --- a/src/battle/states/SelectMoveAction.h +++ b/src/battle/states/SelectMoveAction.h @@ -4,12 +4,9 @@ namespace battle { class BattleState; } -namespace math { - template - class Vector; -} #include "../../app/State.h" +#include "../../math/Vector.h" namespace battle { @@ -34,10 +31,11 @@ private: virtual void OnResize(int width, int height); private: - void RenderMenu(SDL_Surface *screen, const math::Vector &offset); + void RenderMenu(SDL_Surface *screen); private: BattleState *battle; + math::Vector position; }; diff --git a/src/battle/states/SelectSpell.cpp b/src/battle/states/SelectSpell.cpp index b1053d0..7b918b4 100644 --- a/src/battle/states/SelectSpell.cpp +++ b/src/battle/states/SelectSpell.cpp @@ -19,7 +19,7 @@ using graphics::Frame; namespace battle { void SelectSpell::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void SelectSpell::OnExitState(SDL_Surface *screen) { @@ -40,7 +40,23 @@ void SelectSpell::OnPauseState(SDL_Surface *screen) { void SelectSpell::OnResize(int width, int height) { + const Vector offset = battle->ScreenOffset(); + + const Resources &res = battle->Res(); + const Frame &frame = *res.selectFrame; + + framePosition = offset + frame.BorderSize(); + frameSize = Vector( + battle->Width() - 2 * frame.BorderWidth(), + res.normalFont->CharHeight() * 13); + + headlinePosition = offset + Vector( + 2 * frame.BorderWidth() + res.normalFont->CharWidth(), + 2 * frame.BorderHeight()); + menuPosition = offset + Vector( + 2 * frame.BorderWidth() + res.normalFont->CharWidth(), + 2 * frame.BorderHeight() + 2 * res.normalFont->CharHeight()); } @@ -93,34 +109,23 @@ void SelectSpell::UpdateWorld(Uint32 deltaT) { void SelectSpell::Render(SDL_Surface *screen) { parent->Render(screen); - Vector offset(battle->CalculateScreenOffset(screen)); - RenderFrame(screen, offset); - RenderHeadline(screen, offset); - RenderMenu(screen, offset); + RenderFrame(screen); + RenderHeadline(screen); + RenderMenu(screen); } -void SelectSpell::RenderFrame(SDL_Surface *screen, const Vector &offset) { - const Frame *frame(battle->Res().selectFrame); - Vector position(frame->BorderSize()); - int width(battle->Width() - 2 * frame->BorderWidth()); - int height(battle->Res().normalFont->CharHeight() * 13); - frame->Draw(screen, position + offset, width, height); +void SelectSpell::RenderFrame(SDL_Surface *screen) { + const Frame &frame = *battle->Res().selectFrame; + frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y()); } -void SelectSpell::RenderHeadline(SDL_Surface *screen, const Vector &offset) { - const Resources &res(battle->Res()); - Vector position( - 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(), - 2 * res.selectFrame->BorderHeight()); - res.normalFont->DrawString(res.spellMenuHeadline, screen, position + offset); +void SelectSpell::RenderHeadline(SDL_Surface *screen) { + const Resources &res = battle->Res(); + res.normalFont->DrawString(res.spellMenuHeadline, screen, headlinePosition); } -void SelectSpell::RenderMenu(SDL_Surface *screen, const Vector &offset) { - const Resources &res(battle->Res()); - Vector position( - 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(), - 2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight()); - battle->ActiveHero().SpellMenu().Draw(screen, position + offset); +void SelectSpell::RenderMenu(SDL_Surface *screen) { + battle->ActiveHero().SpellMenu().Draw(screen, menuPosition); } } diff --git a/src/battle/states/SelectSpell.h b/src/battle/states/SelectSpell.h index 1087b3f..64cc852 100644 --- a/src/battle/states/SelectSpell.h +++ b/src/battle/states/SelectSpell.h @@ -5,12 +5,9 @@ namespace battle { class BattleState; class SelectAttackType; } -namespace math { - template - class Vector; -} #include "../../app/State.h" +#include "../../math/Vector.h" namespace battle { @@ -35,13 +32,17 @@ private: virtual void OnResize(int width, int height); private: - void RenderFrame(SDL_Surface *, const math::Vector &offset); - void RenderHeadline(SDL_Surface *, const math::Vector &offset); - void RenderMenu(SDL_Surface *, const math::Vector &offset); + void RenderFrame(SDL_Surface *); + void RenderHeadline(SDL_Surface *); + void RenderMenu(SDL_Surface *); private: BattleState *battle; SelectAttackType *parent; + math::Vector framePosition; + math::Vector frameSize; + math::Vector headlinePosition; + math::Vector menuPosition; }; diff --git a/src/battle/states/SelectTarget.cpp b/src/battle/states/SelectTarget.cpp index 800c233..dc8c123 100644 --- a/src/battle/states/SelectTarget.cpp +++ b/src/battle/states/SelectTarget.cpp @@ -14,7 +14,7 @@ using std::vector; namespace battle { void SelectTarget::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void SelectTarget::OnExitState(SDL_Surface *screen) { @@ -31,7 +31,22 @@ void SelectTarget::OnPauseState(SDL_Surface *screen) { void SelectTarget::OnResize(int width, int height) { + Vector offset(battle->ScreenOffset()); + cursorOffset = Vector(cursorIcon->Width() / -2, cursorIcon->Height()) + offset; + // offset the indicator by 1/8th to the right and top + indicatorOffset = cursorOffset + Vector(cursorIcon->Width() / 8, cursorIcon->Height() / -8); + monsterPositions.clear(); + monsterPositions.reserve(battle->MaxMonsters()); + for (int i(0), end(battle->MaxMonsters()); i < end; ++i) { + monsterPositions.push_back(battle->MonsterAt(i).Position()); + } + + heroPositions.clear(); + heroPositions.reserve(battle->NumHeroes()); + for (int i(0), end(battle->NumHeroes()); i < end; ++i) { + heroPositions.push_back(battle->HeroTagPositionAt(i) + battle->HeroTagAt(i).HeroOffset()); + } } @@ -74,34 +89,25 @@ void SelectTarget::UpdateWorld(Uint32 deltaT) { } void SelectTarget::Render(SDL_Surface *screen) { - Vector offset(battle->CalculateScreenOffset(screen)); parent->Render(screen); - RenderCursors(screen, offset); + RenderCursors(screen); } -void SelectTarget::RenderCursors(SDL_Surface *screen, const math::Vector &offset) { - Vector cursorOffset(cursorIcon->Width() / -2, cursorIcon->Height()); - // offset the indicator by 1/8th to the right and top - Vector indicatorOffset(cursorOffset + Vector(cursorIcon->Width() / 8, cursorIcon->Height() / -8)); - vector > positions; - if (selection->TargetsMonsters()) { - for (int i(0), end(battle->MaxMonsters()); i < end; ++i) { - positions.push_back(battle->MonsterAt(i).Position()); - } - } else { - for (int i(0), end(battle->NumHeroes()); i < end; ++i) { - positions.push_back(battle->HeroTagPositionAt(i) + battle->HeroTagAt(i).HeroOffset()); - } - } +void SelectTarget::RenderCursors(SDL_Surface *screen) { + vector > &positions = selection->TargetsMonsters() + ? monsterPositions + : heroPositions; + if (flipFlop) { - for (vector >::size_type i(0); i < positions.size(); ++i) { + for (vector >::size_type i(0), end(positions.size()); + i != end; ++i) { if (selection->IsSelected(i)) { - cursorIcon->DrawTopRight(screen, positions[i] + indicatorOffset + offset); + cursorIcon->DrawTopRight(screen, positions[i] + indicatorOffset); } } } flipFlop = !flipFlop; - cursorIcon->DrawTopRight(screen, positions[selection->Current()] + offset + cursorOffset); + cursorIcon->DrawTopRight(screen, positions[selection->Current()] + cursorOffset); } } diff --git a/src/battle/states/SelectTarget.h b/src/battle/states/SelectTarget.h index 4d53c3c..9bdc470 100644 --- a/src/battle/states/SelectTarget.h +++ b/src/battle/states/SelectTarget.h @@ -6,15 +6,14 @@ namespace battle { class SelectAttackType; class TargetSelection; } -namespace math { - template - class Vector; -} namespace graphics { class Sprite; } #include "../../app/State.h" +#include "../../math/Vector.h" + +#include namespace battle { @@ -39,13 +38,17 @@ private: virtual void OnResize(int width, int height); private: - void RenderCursors(SDL_Surface *screen, const math::Vector &offset); + void RenderCursors(SDL_Surface *screen); private: BattleState *battle; SelectAttackType *parent; TargetSelection *selection; const graphics::Sprite *cursorIcon; + std::vector > monsterPositions; + std::vector > heroPositions; + math::Vector cursorOffset; + math::Vector indicatorOffset; bool flipFlop; }; diff --git a/src/battle/states/SwapHeroes.cpp b/src/battle/states/SwapHeroes.cpp index 7477027..af86747 100644 --- a/src/battle/states/SwapHeroes.cpp +++ b/src/battle/states/SwapHeroes.cpp @@ -14,7 +14,7 @@ using std::vector; namespace battle { void SwapHeroes::OnEnterState(SDL_Surface *screen) { - + OnResize(screen->w, screen->h); } void SwapHeroes::OnExitState(SDL_Surface *screen) { @@ -31,7 +31,24 @@ void SwapHeroes::OnPauseState(SDL_Surface *screen) { void SwapHeroes::OnResize(int width, int height) { + Vector offset(battle->ScreenOffset()); + // offset the cursor by 1/8th to the left and bottom + cursorOffset = Vector(battle->Res().swapCursor->Width() / -8, battle->Res().swapCursor->Height() / 8); + indicatorOffset = Vector(0, 0); + positions.clear(); + positions.reserve(battle->NumHeroes()); + for (int i(0), end(battle->NumHeroes()); i < end; ++i) { + Vector positionCorrection( + battle->Res().swapCursor->Width() / 2, + battle->HeroTagAt(i).HeroSprite()->Height() - battle->Res().swapCursor->Height() / 2); + // indicator offsets are inverted for heroes + positionCorrection -= cursorOffset; + positions.push_back(battle->HeroTagPositionAt(i) + + battle->HeroTagAt(i).HeroOffset() + + positionCorrection + + offset); + } } @@ -109,31 +126,21 @@ void SwapHeroes::UpdateWorld(Uint32 deltaT) { } void SwapHeroes::Render(SDL_Surface *screen) { - Vector offset(battle->CalculateScreenOffset(screen)); parent->Render(screen); - RenderCursors(screen, offset); + RenderCursors(screen); } -void SwapHeroes::RenderCursors(SDL_Surface *screen, const math::Vector &offset) { - // offset the cursor by 1/8th to the left and bottom - Vector cursorOffset(battle->Res().swapCursor->Width() / -8, battle->Res().swapCursor->Height() / 8); - Vector indicatorOffset(0, 0); - vector > positions; - for (int i(0), end(battle->NumHeroes()); i < end; ++i) { - Vector positionCorrection(battle->Res().swapCursor->Width() / 2, battle->HeroTagAt(i).HeroSprite()->Height() - battle->Res().swapCursor->Height() / 2); - // indicator offsets are inverted for heroes - positionCorrection -= cursorOffset; - positions.push_back(battle->HeroTagPositionAt(i) + battle->HeroTagAt(i).HeroOffset() + positionCorrection); - } +void SwapHeroes::RenderCursors(SDL_Surface *screen) { if (flipFlop) { - for (vector >::size_type i(0); i < positions.size(); ++i) { + for (vector >::size_type i(0), end(positions.size()); + i != end; ++i) { if (selected == int(i)) { - battle->Res().swapCursor->DrawTopRight(screen, positions[i] + offset); + battle->Res().swapCursor->DrawTopRight(screen, positions[i]); } } } flipFlop = !flipFlop; - battle->Res().swapCursor->DrawTopRight(screen, positions[cursor] + offset + cursorOffset); + battle->Res().swapCursor->DrawTopRight(screen, positions[cursor] + cursorOffset); } } diff --git a/src/battle/states/SwapHeroes.h b/src/battle/states/SwapHeroes.h index eec0391..1f98772 100644 --- a/src/battle/states/SwapHeroes.h +++ b/src/battle/states/SwapHeroes.h @@ -5,12 +5,11 @@ namespace battle { class BattleState; class SelectMoveAction; } -namespace math { - template - class Vector; -} #include "../../app/State.h" +#include "../../math/Vector.h" + +#include namespace battle { @@ -41,11 +40,14 @@ private: void MoveLeft(); private: - void RenderCursors(SDL_Surface *screen, const math::Vector &offset); + void RenderCursors(SDL_Surface *screen); private: BattleState *battle; SelectMoveAction *parent; + std::vector > positions; + math::Vector cursorOffset; + math::Vector indicatorOffset; int cursor; int selected; bool flipFlop;