From ca31ddeab37eebaa2de5e5b1c94974fac06d418b Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 8 Aug 2012 16:32:23 +0200 Subject: [PATCH] added simple font implementation numbers only for now --- Debug/src/graphics/subdir.mk | 3 ++ Release/src/graphics/subdir.mk | 3 ++ src/battle/BattleState.cpp | 2 +- src/battle/BattleState.h | 5 ++- src/battle/HeroTag.cpp | 32 +++++++++++++------ src/battle/HeroTag.h | 6 ++-- src/graphics/Font.cpp | 54 +++++++++++++++++++++++++++++++++ src/graphics/Font.h | 38 +++++++++++++++++++++++ src/main.cpp | 7 ++++- test-data/numbers.png | Bin 0 -> 411 bytes 10 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 src/graphics/Font.cpp create mode 100644 src/graphics/Font.h create mode 100644 test-data/numbers.png diff --git a/Debug/src/graphics/subdir.mk b/Debug/src/graphics/subdir.mk index df30151..fbd8230 100644 --- a/Debug/src/graphics/subdir.mk +++ b/Debug/src/graphics/subdir.mk @@ -4,16 +4,19 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../src/graphics/Font.cpp \ ../src/graphics/Frame.cpp \ ../src/graphics/Gauge.cpp \ ../src/graphics/Sprite.cpp OBJS += \ +./src/graphics/Font.o \ ./src/graphics/Frame.o \ ./src/graphics/Gauge.o \ ./src/graphics/Sprite.o CPP_DEPS += \ +./src/graphics/Font.d \ ./src/graphics/Frame.d \ ./src/graphics/Gauge.d \ ./src/graphics/Sprite.d diff --git a/Release/src/graphics/subdir.mk b/Release/src/graphics/subdir.mk index 44c1049..2611758 100644 --- a/Release/src/graphics/subdir.mk +++ b/Release/src/graphics/subdir.mk @@ -4,16 +4,19 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../src/graphics/Font.cpp \ ../src/graphics/Frame.cpp \ ../src/graphics/Gauge.cpp \ ../src/graphics/Sprite.cpp OBJS += \ +./src/graphics/Font.o \ ./src/graphics/Frame.o \ ./src/graphics/Gauge.o \ ./src/graphics/Sprite.o CPP_DEPS += \ +./src/graphics/Font.d \ ./src/graphics/Frame.d \ ./src/graphics/Gauge.d \ ./src/graphics/Sprite.d diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 4053220..a4efc84 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, HeroTag::Alignment((i + 1) % 2))); + heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], heroTagFrame, activeHeroTagFrame, healthGauge, manaGauge, ikariGauge, heroTagSprites, heroTagFont, HeroTag::Alignment((i + 1) % 2))); } } diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 0e3436b..72342f0 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -23,6 +23,7 @@ namespace app { class Input; } namespace graphics { + class Font; class Frame; class Gauge; class Sprite; @@ -36,7 +37,7 @@ 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) + 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) : background(background) , monstersLayout(&monstersLayout) , heroesLayout(&heroesLayout) @@ -46,6 +47,7 @@ public: , manaGauge(manaGauge) , ikariGauge(ikariGauge) , heroTagSprites(heroTagSprites) + , heroTagFont(heroTagFont) , attackTypeMenu(attackIcons) , moveMenu(moveIcons) , activeHero(-1) { } @@ -100,6 +102,7 @@ private: const graphics::Gauge *manaGauge; const graphics::Gauge *ikariGauge; const graphics::Sprite *heroTagSprites; + const graphics::Font *heroTagFont; AttackTypeMenu attackTypeMenu; MoveMenu moveMenu; std::vector > monsterPositions; diff --git a/src/battle/HeroTag.cpp b/src/battle/HeroTag.cpp index a823749..33c42db 100644 --- a/src/battle/HeroTag.cpp +++ b/src/battle/HeroTag.cpp @@ -10,6 +10,7 @@ #include "Hero.h" #include "../geometry/operators.h" #include "../geometry/Vector.h" +#include "../graphics/Font.h" #include "../graphics/Frame.h" #include "../graphics/Gauge.h" #include "../graphics/Sprite.h" @@ -34,37 +35,48 @@ void HeroTag::Render(SDL_Surface *screen, int width, int height, Point posi // gauges // NOTE: assuming frame border is unit size until charsets are impemented - int gaugeX((align == LEFT ? 10 : 6) * frameOffset.X()); + int gaugeX((align == LEFT ? 10 : 6) * font->CharWidth()); // 4 units reserved for hero, gaugeX already includes frame offset - int gaugeWidth(width - gaugeX - (align == LEFT ? 1 : 5) * frameOffset.X()); + int gaugeWidth(width - gaugeX - (align == LEFT ? 1 : 5) * font->CharWidth()); // health gauge, second line - Vector healthGaugeOffset(gaugeX, 2 * frameOffset.Y()); + Vector healthGaugeOffset(gaugeX, frameOffset.Y() + font->CharHeight()); healthGauge->Draw(screen, position + healthGaugeOffset, gaugeWidth, hero->RelativeHealth(gaugeWidth)); // mana gauge, third line - Vector manaGaugeOffset(gaugeX, 3 * frameOffset.Y()); + Vector manaGaugeOffset(gaugeX, frameOffset.Y() + 2 * font->CharHeight()); manaGauge->Draw(screen, position + manaGaugeOffset, gaugeWidth, hero->RelativeMana(gaugeWidth)); // ikari gauge, fourth line - Vector ikariGaugeOffset(gaugeX, 4 * frameOffset.Y()); + Vector ikariGaugeOffset(gaugeX, frameOffset.Y() + 3 * font->CharHeight()); ikariGauge->Draw(screen, position + ikariGaugeOffset, gaugeWidth, hero->RelativeIP(gaugeWidth)); // labels - int labelX((align == LEFT ? 5 : 1) * frameOffset.X()); + int labelX((align == LEFT ? 5 : 1) * font->CharWidth()); // level Vector levelLabelOffset(gaugeX, frameOffset.Y()); sprites->Draw(screen, position + levelLabelOffset, 0, 0); // hp - Vector healthLabelOffset(labelX, 2 * frameOffset.Y()); + Vector healthLabelOffset(labelX, frameOffset.Y() + font->CharHeight()); sprites->Draw(screen, position + healthLabelOffset, 0, 1); // mp - Vector manaLabelOffset(labelX, 3 * frameOffset.Y()); + Vector manaLabelOffset(labelX, frameOffset.Y() + 2 * font->CharHeight()); sprites->Draw(screen, position + manaLabelOffset, 0, 2); // cm - Vector moveLabelOffset(labelX, 4 * frameOffset.Y()); + Vector moveLabelOffset(labelX, frameOffset.Y() + 3 * font->CharHeight()); sprites->Draw(screen, position + moveLabelOffset, 0, 3); // ip - Vector ikariLabelOffset(labelX + 3 * frameOffset.X(), 4 * frameOffset.Y()); + Vector ikariLabelOffset(labelX + 3 * font->CharWidth(), frameOffset.Y() + 3 * font->CharHeight()); sprites->Draw(screen, position + ikariLabelOffset, 0, 4); + // numbers + // level + Vector levelNumberOffset(gaugeX + sprites->Width(), levelLabelOffset.Y()); + font->DrawNumber(hero->Level(), screen, position + levelNumberOffset, 2); + // health + Vector healthNumberOffset(labelX + sprites->Width(), healthLabelOffset.Y()); + font->DrawNumber(hero->Health(), screen, position + healthNumberOffset, 3); + //mana + Vector manaNumberOffset(labelX + sprites->Width(), manaLabelOffset.Y()); + font->DrawNumber(hero->Mana(), screen, position + manaNumberOffset, 3); + // hero Vector heroOffset( (align == LEFT) ? yOffset : width - hero->Sprite()->Width() - yOffset, diff --git a/src/battle/HeroTag.h b/src/battle/HeroTag.h index 004d3ec..542b8c1 100644 --- a/src/battle/HeroTag.h +++ b/src/battle/HeroTag.h @@ -13,6 +13,7 @@ #include namespace graphics { + class Font; class Frame; class Gauge; class Sprite; @@ -32,8 +33,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, Alignment align) - : hero(hero), choice(choice), frame(frame), activeFrame(activeFrame), healthGauge(healthGauge), manaGauge(manaGauge), ikariGauge(ikariGauge), sprites(sprites), align(align) { } + 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() { } public: @@ -48,6 +49,7 @@ private: const graphics::Gauge *manaGauge; const graphics::Gauge *ikariGauge; const graphics::Sprite *sprites; + const graphics::Font *font; Alignment align; }; diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp new file mode 100644 index 0000000..0ff3d9d --- /dev/null +++ b/src/graphics/Font.cpp @@ -0,0 +1,54 @@ +/* + * Font.cpp + * + * Created on: Aug 8, 2012 + * Author: holy + */ + +#include "Font.h" + +#include "../geometry/operators.h" +#include "../geometry/Vector.h" + +#include + +using geometry::Point; +using geometry::Vector; +using std::pow; + +namespace graphics { + +void Font::DrawDigit(int digit, SDL_Surface *dest, Point position) const { + sprite->Draw(dest, position, digitsCol + digit, digitsRow); +} + +void Font::DrawNumber(int numberIn, SDL_Surface *dest, Point positionIn, int digits) const { + int number(numberIn); + if (digits > 0 && numberIn >= pow(10.0, digits)) { + numberIn = pow(10.0, digits) - 1; + } + + Point position(positionIn); + Vector step(sprite->Width(), 0); + + if (digits > 0) { + int i(digits - 1); + while (number < pow(10.0, i)) { + position += step; + --i; + } + } + + int m(10); + while (m <= number) { + m *= 10; + } + + while (m > 9) { + m /= 10; + DrawDigit((number / m) % 10, dest, position); + position += step; + } +} + +} diff --git a/src/graphics/Font.h b/src/graphics/Font.h new file mode 100644 index 0000000..0294d25 --- /dev/null +++ b/src/graphics/Font.h @@ -0,0 +1,38 @@ +/* + * Font.h + * + * Created on: Aug 8, 2012 + * Author: holy + */ + +#ifndef GRAPHICS_FONT_H_ +#define GRAPHICS_FONT_H_ + +#include "Sprite.h" +#include "../geometry/Point.h" + +#include + +namespace graphics { + +class Font { + +public: + explicit Font(const Sprite *sprite, int digitsCol = 0, int digitsRow = 0) : sprite(sprite), digitsCol(digitsCol), digitsRow(digitsRow) { } + +public: + int CharWidth() const { return sprite->Width(); } + int CharHeight() const { return sprite->Height(); } + void DrawDigit(int digit, SDL_Surface *dest, geometry::Point position) const; + void DrawNumber(int number, SDL_Surface *dest, geometry::Point position, int digits = 0) const; + +private: + const Sprite *sprite; + int digitsCol; + int digitsRow; + +}; + +} + +#endif /* GRAPHICS_FONT_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 71a4afb..850907f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include "battle/Monster.h" #include "battle/PartyLayout.h" #include "geometry/Point.h" +#include "graphics/Font.h" #include "graphics/Frame.h" #include "graphics/Gauge.h" #include "graphics/Sprite.h" @@ -31,6 +32,7 @@ using battle::Hero; using battle::Monster; using battle::PartyLayout; using geometry::Point; +using graphics::Font; using graphics::Frame; using graphics::Gauge; using graphics::Sprite; @@ -88,6 +90,9 @@ int main(int argc, char **argv) { 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); + 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); @@ -97,7 +102,7 @@ int main(int argc, char **argv) { Gauge manaGauge(gauges, 0, 32, 0, 0, 16, 6, 1, 6); Gauge ikariGauge(gauges, 0, 48, 0, 0, 16, 6, 1, 6); - BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite, &moveIconsSprite, &heroTagFrame, &activeHeroTagFrame, &healthGauge, &manaGauge, &ikariGauge, &heroTagSprite)); + BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite, &moveIconsSprite, &heroTagFrame, &activeHeroTagFrame, &healthGauge, &manaGauge, &ikariGauge, &heroTagSprite, &heroTagFont)); battleState->AddMonster(monster); battleState->AddMonster(monster); battleState->AddMonster(monster); diff --git a/test-data/numbers.png b/test-data/numbers.png new file mode 100644 index 0000000000000000000000000000000000000000..206628ba7316c8b5a176de03a81d53d2017be8f0 GIT binary patch literal 411 zcmV;M0c8G(P)000mO1^@s6kp*;E0004ENkla#@#J`@B9ZW4>@isAaN-C`X$#CtMGoKfLaA*+Pe}Dh;M?*S1Nd| zR9Vrl3KkwrZD-97<%97H<8WQ`A8h$+@2P;q;T~`(*2=JYGHb;Zl+UEVRpl1SXPCK! zC|XTHVZPv5yZ<;?c^D&`G+y5Q>%j3loS3Bh@Qq{e&^|4ef*^?h!4r#1{Anv-P!9kA002ovPDHLk FV1m+9xi|m- literal 0 HcmV?d00001 -- 2.39.2