From b6502719f0f02bdb4fc39aa66fa4a369ea583036 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 8 Aug 2012 19:45:46 +0200 Subject: [PATCH] added struct for battle resources --- src/battle/BattleState.cpp | 2 +- src/battle/BattleState.h | 27 ++++---------- src/battle/HeroTag.cpp | 61 +++++++++++++++---------------- src/battle/HeroTag.h | 13 ++----- src/battle/Resources.h | 59 ++++++++++++++++++++++++++++++ src/battle/states/SelectSpell.cpp | 10 ++--- src/main.cpp | 38 ++++++++++++------- 7 files changed, 130 insertions(+), 80 deletions(-) create mode 100644 src/battle/Resources.h diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index f1e3d07..ae882a6 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -50,7 +50,7 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { heroesLayout->CalculatePositions(background->w, background->h, heroesPositions); attackChoices.resize(heroes.size()); for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { - heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], heroTagFrame, activeHeroTagFrame, healthGauge, manaGauge, ikariGauge, heroTagSprites, heroTagFont, HeroTag::Alignment((i + 1) % 2))); + heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2))); } } diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 858b111..484e452 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -14,6 +14,7 @@ #include "HeroTag.h" #include "Monster.h" #include "MoveMenu.h" +#include "Resources.h" #include "../app/State.h" #include "../geometry/Point.h" #include "../geometry/Vector.h" @@ -37,20 +38,13 @@ class BattleState : public app::State { public: - BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const graphics::Sprite *attackIcons, const graphics::Sprite *moveIcons, const graphics::Frame *heroTagFrame, const graphics::Frame *activeHeroTagFrame, const graphics::Gauge *healthGauge, const graphics::Gauge *manaGauge, const graphics::Gauge *ikariGauge, const graphics::Sprite *heroTagSprites, const graphics::Font *heroTagFont, const graphics::Frame *selectFrame) + BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res) : background(background) , monstersLayout(&monstersLayout) , heroesLayout(&heroesLayout) - , heroTagFrame(heroTagFrame) - , activeHeroTagFrame(activeHeroTagFrame) - , healthGauge(healthGauge) - , manaGauge(manaGauge) - , ikariGauge(ikariGauge) - , heroTagSprites(heroTagSprites) - , heroTagFont(heroTagFont) - , selectFrame(selectFrame) - , attackTypeMenu(attackIcons) - , moveMenu(moveIcons) + , res(res) + , attackTypeMenu(res->attackIcons) + , moveMenu(res->moveIcons) , activeHero(-1) { } public: @@ -70,9 +64,9 @@ public: virtual void Render(SDL_Surface *); public: + const Resources &Res() const { return *res; } AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; } MoveMenu &GetMoveMenu() { return moveMenu; } - const graphics::Frame &GetSelectFrame() const { return *selectFrame; } bool HasMoreHeroes() const { return activeHero < (int) heroes.size(); } void NextHero() { ++activeHero; } @@ -102,14 +96,7 @@ private: SDL_Surface *background; const PartyLayout *monstersLayout; const PartyLayout *heroesLayout; - const graphics::Frame *heroTagFrame; - const graphics::Frame *activeHeroTagFrame; - const graphics::Gauge *healthGauge; - const graphics::Gauge *manaGauge; - const graphics::Gauge *ikariGauge; - const graphics::Sprite *heroTagSprites; - const graphics::Font *heroTagFont; - const graphics::Frame *selectFrame; + const Resources *res; AttackTypeMenu attackTypeMenu; MoveMenu moveMenu; std::vector > monsterPositions; diff --git a/src/battle/HeroTag.cpp b/src/battle/HeroTag.cpp index 33c42db..01cee44 100644 --- a/src/battle/HeroTag.cpp +++ b/src/battle/HeroTag.cpp @@ -8,6 +8,7 @@ #include "HeroTag.h" #include "Hero.h" +#include "Resources.h" #include "../geometry/operators.h" #include "../geometry/Vector.h" #include "../graphics/Font.h" @@ -17,65 +18,61 @@ using geometry::Point; using geometry::Vector; +using graphics::Frame; namespace battle { void HeroTag::Render(SDL_Surface *screen, int width, int height, Point position, bool active) const { // frame - Vector frameOffset; - if (active) { - activeFrame->Draw(screen, position, width, height); - frameOffset = Vector(activeFrame->BorderWidth(), activeFrame->BorderHeight()); - } else { - frame->Draw(screen, position, width, height); - frameOffset = Vector(frame->BorderWidth(), frame->BorderHeight()); - } + const Frame *frame(active ? res->activeHeroTagFrame : res->heroTagFrame); + Vector frameOffset(frame->BorderWidth(), frame->BorderHeight()); + frame->Draw(screen, position, width, height); int yOffset((height - hero->Sprite()->Height()) / 2); // gauges // NOTE: assuming frame border is unit size until charsets are impemented - int gaugeX((align == LEFT ? 10 : 6) * font->CharWidth()); + int gaugeX((align == LEFT ? 10 : 6) * res->heroTagFont->CharWidth()); // 4 units reserved for hero, gaugeX already includes frame offset - int gaugeWidth(width - gaugeX - (align == LEFT ? 1 : 5) * font->CharWidth()); + int gaugeWidth(width - gaugeX - (align == LEFT ? 1 : 5) * res->heroTagFont->CharWidth()); // health gauge, second line - Vector healthGaugeOffset(gaugeX, frameOffset.Y() + font->CharHeight()); - healthGauge->Draw(screen, position + healthGaugeOffset, gaugeWidth, hero->RelativeHealth(gaugeWidth)); + Vector healthGaugeOffset(gaugeX, frameOffset.Y() + res->heroTagFont->CharHeight()); + res->healthGauge->Draw(screen, position + healthGaugeOffset, gaugeWidth, hero->RelativeHealth(gaugeWidth)); // mana gauge, third line - Vector manaGaugeOffset(gaugeX, frameOffset.Y() + 2 * font->CharHeight()); - manaGauge->Draw(screen, position + manaGaugeOffset, gaugeWidth, hero->RelativeMana(gaugeWidth)); + Vector manaGaugeOffset(gaugeX, frameOffset.Y() + 2 * res->heroTagFont->CharHeight()); + res->manaGauge->Draw(screen, position + manaGaugeOffset, gaugeWidth, hero->RelativeMana(gaugeWidth)); // ikari gauge, fourth line - Vector ikariGaugeOffset(gaugeX, frameOffset.Y() + 3 * font->CharHeight()); - ikariGauge->Draw(screen, position + ikariGaugeOffset, gaugeWidth, hero->RelativeIP(gaugeWidth)); + Vector ikariGaugeOffset(gaugeX, frameOffset.Y() + 3 * res->heroTagFont->CharHeight()); + res->ikariGauge->Draw(screen, position + ikariGaugeOffset, gaugeWidth, hero->RelativeIP(gaugeWidth)); // labels - int labelX((align == LEFT ? 5 : 1) * font->CharWidth()); + int labelX((align == LEFT ? 5 : 1) * res->heroTagFont->CharWidth()); // level Vector levelLabelOffset(gaugeX, frameOffset.Y()); - sprites->Draw(screen, position + levelLabelOffset, 0, 0); + res->heroTagLabels->Draw(screen, position + levelLabelOffset, 0, 0); // hp - Vector healthLabelOffset(labelX, frameOffset.Y() + font->CharHeight()); - sprites->Draw(screen, position + healthLabelOffset, 0, 1); + Vector healthLabelOffset(labelX, frameOffset.Y() + res->heroTagFont->CharHeight()); + res->heroTagLabels->Draw(screen, position + healthLabelOffset, 0, 1); // mp - Vector manaLabelOffset(labelX, frameOffset.Y() + 2 * font->CharHeight()); - sprites->Draw(screen, position + manaLabelOffset, 0, 2); + Vector manaLabelOffset(labelX, frameOffset.Y() + 2 * res->heroTagFont->CharHeight()); + res->heroTagLabels->Draw(screen, position + manaLabelOffset, 0, 2); // cm - Vector moveLabelOffset(labelX, frameOffset.Y() + 3 * font->CharHeight()); - sprites->Draw(screen, position + moveLabelOffset, 0, 3); + Vector moveLabelOffset(labelX, frameOffset.Y() + 3 * res->heroTagFont->CharHeight()); + res->heroTagLabels->Draw(screen, position + moveLabelOffset, 0, 3); // ip - Vector ikariLabelOffset(labelX + 3 * font->CharWidth(), frameOffset.Y() + 3 * font->CharHeight()); - sprites->Draw(screen, position + ikariLabelOffset, 0, 4); + Vector ikariLabelOffset(labelX + 3 * res->heroTagFont->CharWidth(), frameOffset.Y() + 3 * res->heroTagFont->CharHeight()); + res->heroTagLabels->Draw(screen, position + ikariLabelOffset, 0, 4); // numbers // level - Vector levelNumberOffset(gaugeX + sprites->Width(), levelLabelOffset.Y()); - font->DrawNumber(hero->Level(), screen, position + levelNumberOffset, 2); + Vector levelNumberOffset(gaugeX + res->heroTagLabels->Width(), levelLabelOffset.Y()); + res->heroTagFont->DrawNumber(hero->Level(), screen, position + levelNumberOffset, 2); // health - Vector healthNumberOffset(labelX + sprites->Width(), healthLabelOffset.Y()); - font->DrawNumber(hero->Health(), screen, position + healthNumberOffset, 3); + Vector healthNumberOffset(labelX + res->heroTagLabels->Width(), healthLabelOffset.Y()); + res->heroTagFont->DrawNumber(hero->Health(), screen, position + healthNumberOffset, 3); //mana - Vector manaNumberOffset(labelX + sprites->Width(), manaLabelOffset.Y()); - font->DrawNumber(hero->Mana(), screen, position + manaNumberOffset, 3); + Vector manaNumberOffset(labelX + res->heroTagLabels->Width(), manaLabelOffset.Y()); + res->heroTagFont->DrawNumber(hero->Mana(), screen, position + manaNumberOffset, 3); // hero Vector heroOffset( diff --git a/src/battle/HeroTag.h b/src/battle/HeroTag.h index 542b8c1..d361252 100644 --- a/src/battle/HeroTag.h +++ b/src/battle/HeroTag.h @@ -23,6 +23,7 @@ namespace battle { class AttackChoice; class Hero; +struct Resources; class HeroTag { @@ -33,8 +34,8 @@ public: }; public: - HeroTag(const Hero *hero, const AttackChoice *choice, const graphics::Frame *frame, const graphics::Frame *activeFrame, const graphics::Gauge *healthGauge, const graphics::Gauge *manaGauge, const graphics::Gauge *ikariGauge, const graphics::Sprite *sprites, const graphics::Font *font, Alignment align) - : hero(hero), choice(choice), frame(frame), activeFrame(activeFrame), healthGauge(healthGauge), manaGauge(manaGauge), ikariGauge(ikariGauge), sprites(sprites), font(font), align(align) { } + HeroTag(const Hero *hero, const AttackChoice *choice, const Resources *res, Alignment align) + : hero(hero), choice(choice), res(res), align(align) { } ~HeroTag() { } public: @@ -43,13 +44,7 @@ public: private: const Hero *hero; const AttackChoice *choice; - const graphics::Frame *frame; - const graphics::Frame *activeFrame; - const graphics::Gauge *healthGauge; - const graphics::Gauge *manaGauge; - const graphics::Gauge *ikariGauge; - const graphics::Sprite *sprites; - const graphics::Font *font; + const Resources *res; Alignment align; }; diff --git a/src/battle/Resources.h b/src/battle/Resources.h new file mode 100644 index 0000000..7c37bca --- /dev/null +++ b/src/battle/Resources.h @@ -0,0 +1,59 @@ +/* + * Resources.h + * + * Created on: Aug 8, 2012 + * Author: holy + */ + +#ifndef BATTLE_RESOURCES_H_ +#define BATTLE_RESOURCES_H_ + +namespace graphics { + class Font; + class Frame; + class Gauge; + class Sprite; +} + +namespace battle { + +struct Resources { + + graphics::Sprite *moveIcons; + graphics::Sprite *attackIcons; + + graphics::Frame *heroTagFrame; + graphics::Frame *activeHeroTagFrame; + + graphics::Font *heroTagFont; + graphics::Sprite *heroTagLabels; + + graphics::Gauge *healthGauge; + graphics::Gauge *manaGauge; + graphics::Gauge *ikariGauge; + + graphics::Frame *selectFrame; + + + Resources() + : moveIcons(0) + , attackIcons(0) + + , heroTagFrame(0) + , activeHeroTagFrame(0) + + , heroTagFont(0) + , heroTagLabels(0) + + , healthGauge(0) + , manaGauge(0) + , ikariGauge(0) + + , selectFrame(0) + { } + +}; + +} + +#endif /* BATTLE_RESOURCES_H_ */ diff --git a/src/battle/states/SelectSpell.cpp b/src/battle/states/SelectSpell.cpp index 66b55f1..1b01250 100644 --- a/src/battle/states/SelectSpell.cpp +++ b/src/battle/states/SelectSpell.cpp @@ -63,13 +63,13 @@ void SelectSpell::UpdateWorld(float deltaT) { void SelectSpell::Render(SDL_Surface *screen) { parent->Render(screen); - const Frame &frame(battle->GetSelectFrame()); - Point position(frame.BorderWidth(), frame.BorderHeight()); + const Frame *frame(battle->Res().selectFrame); + Point position(frame->BorderWidth(), frame->BorderHeight()); Vector offset(battle->CalculateScreenOffset(screen)); - int width(battle->BackgroundWidth() - 2 * frame.BorderWidth()); + int width(battle->BackgroundWidth() - 2 * frame->BorderWidth()); // TODO: replace with font height - int height(frame.BorderHeight() * 13); - frame.Draw(screen, position + offset, width, height); + int height(frame->BorderHeight() * 13); + frame->Draw(screen, position + offset, width, height); } } diff --git a/src/main.cpp b/src/main.cpp index 97a9399..29c08a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include "battle/Hero.h" #include "battle/Monster.h" #include "battle/PartyLayout.h" +#include "battle/Resources.h" #include "geometry/Point.h" #include "graphics/Font.h" #include "graphics/Frame.h" @@ -120,29 +121,40 @@ int main(int argc, char **argv) { dekar.SetMana(0); dekar.SetIP(0); - SDL_Surface *attackIcons(IMG_Load("test-data/attack-type-icons.png")); - Sprite attackIconsSprite(attackIcons, 32, 32); - SDL_Surface *moveIcons(IMG_Load("test-data/move-icons.png")); - Sprite moveIconsSprite(moveIcons, 32, 32); - SDL_Surface *heroTagSprites(IMG_Load("test-data/hero-tag-sprites.png")); - Sprite heroTagSprite(heroTagSprites, 32, 16); - SDL_Surface *numbers(IMG_Load("test-data/numbers.png")); - Sprite numbersSprite(numbers, 16, 16); + battle::Resources battleRes; + + SDL_Surface *attackIconsImg(IMG_Load("test-data/attack-type-icons.png")); + Sprite attackIconsSprite(attackIconsImg, 32, 32); + battleRes.attackIcons = &attackIconsSprite; + SDL_Surface *moveIconsImg(IMG_Load("test-data/move-icons.png")); + Sprite moveIconsSprite(moveIconsImg, 32, 32); + battleRes.moveIcons = &moveIconsSprite; + SDL_Surface *heroTagImg(IMG_Load("test-data/hero-tag-sprites.png")); + Sprite heroTagSprite(heroTagImg, 32, 16); + battleRes.heroTagLabels = &heroTagSprite; + SDL_Surface *numbersImg(IMG_Load("test-data/numbers.png")); + Sprite numbersSprite(numbersImg, 16, 16); Font heroTagFont(&numbersSprite); - SDL_Surface *tagFrames(IMG_Load("test-data/tag-frames.png")); - Frame heroTagFrame(tagFrames, 16, 16, 1, 1, 0, 33); - Frame activeHeroTagFrame(tagFrames, 16, 16); + battleRes.heroTagFont = &heroTagFont; + SDL_Surface *tagFramesImg(IMG_Load("test-data/tag-frames.png")); + Frame heroTagFrame(tagFramesImg, 16, 16, 1, 1, 0, 33); + battleRes.heroTagFrame = &heroTagFrame; + Frame activeHeroTagFrame(tagFramesImg, 16, 16); + battleRes.activeHeroTagFrame = &activeHeroTagFrame; SDL_Surface *gauges(IMG_Load("test-data/gauges.png")); Gauge healthGauge(gauges, 0, 16, 0, 0, 16, 6, 1, 6); + battleRes.healthGauge = &healthGauge; Gauge manaGauge(gauges, 0, 32, 0, 0, 16, 6, 1, 6); + battleRes.manaGauge = &manaGauge; Gauge ikariGauge(gauges, 0, 48, 0, 0, 16, 6, 1, 6); + battleRes.ikariGauge = &ikariGauge; SDL_Surface *selectFrameImg(IMG_Load("test-data/select-frame.png")); Frame selectFrame(selectFrameImg, 16, 16); + battleRes.selectFrame = &selectFrame; - // TODO: create a container for all the battle resources - BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite, &moveIconsSprite, &heroTagFrame, &activeHeroTagFrame, &healthGauge, &manaGauge, &ikariGauge, &heroTagSprite, &heroTagFont, &selectFrame)); + BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &battleRes)); battleState->AddMonster(monster); battleState->AddMonster(monster); battleState->AddMonster(monster); -- 2.39.2