]> git.localhorst.tv Git - l2e.git/commitdiff
added Frame class for drawing bordered windows
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 7 Aug 2012 18:00:08 +0000 (20:00 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 7 Aug 2012 18:00:08 +0000 (20:00 +0200)
12 files changed:
Debug/src/graphics/subdir.mk
Release/src/graphics/subdir.mk
src/battle/BattleState.cpp
src/battle/BattleState.h
src/battle/HeroTag.cpp
src/battle/HeroTag.h
src/graphics/Frame.cpp [new file with mode: 0644]
src/graphics/Frame.h [new file with mode: 0644]
src/graphics/Sprite.cpp
src/graphics/Sprite.h
src/main.cpp
test-data/tag-frames.png [new file with mode: 0644]

index 63083ec2666d6b447a625bf891e23c29f0bd128e..b7c5c2d3d80a76c2d9ef98c3d0875f578c9ed870 100644 (file)
@@ -4,12 +4,15 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/graphics/Frame.cpp \
 ../src/graphics/Sprite.cpp 
 
 OBJS += \
+./src/graphics/Frame.o \
 ./src/graphics/Sprite.o 
 
 CPP_DEPS += \
+./src/graphics/Frame.d \
 ./src/graphics/Sprite.d 
 
 
index 6325d124c2c7059c5a5b4cdea5470fd5fdf22f79..1471b9ca42591d48d7d8bf411c35086fcd7d5df3 100644 (file)
@@ -4,12 +4,15 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/graphics/Frame.cpp \
 ../src/graphics/Sprite.cpp 
 
 OBJS += \
+./src/graphics/Frame.o \
 ./src/graphics/Sprite.o 
 
 CPP_DEPS += \
+./src/graphics/Frame.d \
 ./src/graphics/Sprite.d 
 
 
index 30a33b691b04fc27382b9f1b29c02e593531d89d..36c19979224c281bf83227c60d6792dc03a4a7ab 100644 (file)
@@ -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<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
-               heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], HeroTag::Alignment((i + 1) % 2)));
+               heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], heroTagFrame, activeHeroTagFrame, HeroTag::Alignment((i + 1) % 2)));
        }
 }
 
index 42094d66bec33c70422b967dc30c535f7910acc8..f4f8bc1c3093aed7ce4cc8fffb907975f839ee3e 100644 (file)
 #include <SDL.h>
 
 namespace app { class Input; }
-namespace graphics { class Sprite; }
+namespace graphics {
+       class Frame;
+       class Sprite;
+}
 
 namespace battle {
 
@@ -32,10 +35,12 @@ class BattleState
 : public app::State {
 
 public:
-       BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const graphics::Sprite *attackIcons, const graphics::Sprite *moveIcons)
+       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)
        : background(background)
        , monstersLayout(&monstersLayout)
        , heroesLayout(&heroesLayout)
+       , heroTagFrame(heroTagFrame)
+       , activeHeroTagFrame(activeHeroTagFrame)
        , attackTypeMenu(attackIcons)
        , moveMenu(moveIcons)
        , activeHero(-1) { }
@@ -84,6 +89,8 @@ private:
        SDL_Surface *background;
        const PartyLayout *monstersLayout;
        const PartyLayout *heroesLayout;
+       const graphics::Frame *heroTagFrame;
+       const graphics::Frame *activeHeroTagFrame;
        AttackTypeMenu attackTypeMenu;
        MoveMenu moveMenu;
        std::vector<geometry::Point<int> > monsterPositions;
index 713fb8fe35774fbf9766ef72f8791236d8d6b277..0b7aa2700b83f33308bc05644100b0f101c5323c 100644 (file)
@@ -10,6 +10,7 @@
 #include "Hero.h"
 #include "../geometry/operators.h"
 #include "../geometry/Vector.h"
+#include "../graphics/Frame.h"
 #include "../graphics/Sprite.h"
 
 using geometry::Point;
@@ -18,23 +19,11 @@ using geometry::Vector;
 namespace battle {
 
 void HeroTag::Render(SDL_Surface *screen, int width, int height, Point<int> position, bool active) const {
-       SDL_Rect destRect;
-       destRect.x = position.X();
-       destRect.y = position.Y();
-       destRect.w = width;
-       destRect.h = height;
-
-       destRect.x += 1;
-       destRect.y += 1;
-       destRect.w -= 2;
-       destRect.h -= 2;
-       SDL_FillRect(screen, &destRect, SDL_MapRGB(screen->format, 0xFF, active ? 0 : 0xFF, active ? 0 : 0xFF));
-
-       destRect.x += 1;
-       destRect.y += 1;
-       destRect.w -= 2;
-       destRect.h -= 2;
-       SDL_FillRect(screen, &destRect, SDL_MapRGB(screen->format, 0, 0, 0));
+       if (active) {
+               activeFrame->Draw(screen, position, width, height);
+       } else {
+               frame->Draw(screen, position, width, height);
+       }
 
        Vector<int> heroOffset(
                        (align == LEFT) ? 3 : width - hero->Sprite()->Width() - 3,
index 3e42edd44e9dca2accf3d1c1f1bc50a9346e50d3..6799e9470fd75a1c6445e3573ff3ce7032886f52 100644 (file)
@@ -12,6 +12,8 @@
 
 #include <SDL.h>
 
+namespace graphics { class Frame; }
+
 namespace battle {
 
 class AttackChoice;
@@ -26,7 +28,8 @@ public:
        };
 
 public:
-       HeroTag(const Hero *hero, const AttackChoice *choice, Alignment align) : hero(hero), choice(choice), align(align) { }
+       HeroTag(const Hero *hero, const AttackChoice *choice, const graphics::Frame *frame, const graphics::Frame *activeFrame, Alignment align)
+       : hero(hero), choice(choice), frame(frame), activeFrame(activeFrame), align(align) { }
        ~HeroTag() { }
 
 public:
@@ -35,6 +38,8 @@ public:
 private:
        const Hero *hero;
        const AttackChoice *choice;
+       const graphics::Frame *frame;
+       const graphics::Frame *activeFrame;
        Alignment align;
 
 };
diff --git a/src/graphics/Frame.cpp b/src/graphics/Frame.cpp
new file mode 100644 (file)
index 0000000..ac394d1
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Frame.cpp
+ *
+ *  Created on: Aug 7, 2012
+ *      Author: holy
+ */
+
+#include "Frame.h"
+
+using geometry::Point;
+
+namespace graphics {
+
+// TODO: maybe create a cache for frames?
+void Frame::Draw(SDL_Surface *dest, Point<int> position, int width, int height) const {
+       // top-left corner
+       SDL_Rect srcRect;
+       srcRect.x = xOffset;
+       srcRect.y = yOffset;
+       srcRect.w = borderWidth;
+       srcRect.h = borderHeight;
+       SDL_Rect destRect;
+       destRect.x = position.X();
+       destRect.y = position.Y();
+       SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+
+       // top border
+       srcRect.x += borderWidth;
+       srcRect.w = repeatWidth;
+       destRect.x += borderWidth;
+       int fullRepeatWidth(width - (2 * borderWidth));
+       int repeatCursor(0);
+       while (repeatCursor < fullRepeatWidth) {
+               SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+               destRect.x += repeatWidth;
+               repeatCursor += repeatWidth;
+       }
+
+       // top-right corner
+       srcRect.x += repeatWidth;
+       srcRect.w = borderWidth;
+       SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+
+       // middle
+       destRect.y += borderHeight;
+       int fullRepeatHeight(height - (2 * borderHeight));
+       int hRepeatCursor(0);
+       while (hRepeatCursor < fullRepeatHeight) {
+
+               // left border
+               srcRect.x = xOffset;
+               srcRect.y = yOffset + borderHeight;
+               srcRect.w = borderWidth;
+               srcRect.h = repeatHeight;
+               destRect.x = position.X();
+               SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+
+               // fill
+               repeatCursor = 0;
+               srcRect.x += borderWidth;
+               srcRect.w = repeatWidth;
+               destRect.x += borderWidth;
+               while (repeatCursor < fullRepeatWidth) {
+                       SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+                       destRect.x += repeatWidth;
+                       repeatCursor += repeatWidth;
+               }
+
+               // right border
+               srcRect.x += repeatWidth;
+               srcRect.w = borderWidth;
+               SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+
+               destRect.y += repeatHeight;
+               hRepeatCursor += repeatHeight;
+       }
+
+       // bottom-left corner
+       srcRect.x = xOffset;
+       srcRect.y = yOffset + borderHeight + repeatHeight;
+       srcRect.w = borderWidth;
+       srcRect.h = borderHeight;
+       destRect.x = position.X();
+       SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+
+       // bottom border
+       srcRect.x += borderWidth;
+       srcRect.w = repeatWidth;
+       destRect.x += borderWidth;
+       repeatCursor = 0;
+       while (repeatCursor < fullRepeatWidth) {
+               SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+               destRect.x += repeatWidth;
+               repeatCursor += repeatWidth;
+       }
+       if (fullRepeatWidth < fullRepeatWidth) {
+               srcRect.w = fullRepeatWidth - fullRepeatWidth;
+               SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+               destRect.x += fullRepeatWidth - fullRepeatWidth;
+       }
+
+       // bottom-right corner
+       srcRect.x += repeatWidth;
+       srcRect.w = borderWidth;
+       SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+}
+
+}
diff --git a/src/graphics/Frame.h b/src/graphics/Frame.h
new file mode 100644 (file)
index 0000000..0f1287c
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Frame.h
+ *
+ *  Created on: Aug 7, 2012
+ *      Author: holy
+ */
+
+#ifndef GRAPHICS_FRAME_H_
+#define GRAPHICS_FRAME_H_
+
+#include "../geometry/Point.h"
+
+#include <SDL.h>
+
+namespace graphics {
+
+class Frame {
+
+public:
+       Frame(SDL_Surface *s, int borderWidth, int borderHeight, int repeatWidth = 1, int repeatHeight = 1, int xOffset = 0, int yOffset = 0)
+       : surface(s), borderWidth(borderWidth), borderHeight(borderHeight), repeatWidth(repeatWidth), repeatHeight(repeatHeight), xOffset(xOffset), yOffset(yOffset) { }
+
+public:
+       int MinWidth() const { return 2 * borderWidth; }
+       int MinHeight() const { return 2 * borderHeight; }
+       void Draw(SDL_Surface *dest, geometry::Point<int> position, int width, int height) const;
+
+private:
+       SDL_Surface *surface;
+       int borderWidth;
+       int borderHeight;
+       int repeatWidth;
+       int repeatHeight;
+       int xOffset;
+       int yOffset;
+
+};
+
+}
+
+#endif /* GRAPHICS_FRAME_H_ */
index 5b974bd3817ed3ed97b89c1879fc0595ec80b78f..363be11230caa6ea462383f636ccc8270d7dac45 100644 (file)
@@ -13,17 +13,17 @@ namespace graphics {
 
 void Sprite::Draw(SDL_Surface *dest, Point<int> position, int col, int row) const {
        SDL_Rect srcRect, destRect;
-       srcRect.x = col * Width();
-       srcRect.y = row * Height();
+       srcRect.x = xOffset + col * Width();
+       srcRect.y = yOffset + row * Height();
        srcRect.w = Width();
        srcRect.h = Height();
        destRect.x = position.X();
        destRect.y = position.Y();
-       destRect.w = Width();
-       destRect.h = Height();
        if (surface) {
                SDL_BlitSurface(surface, &srcRect, dest, &destRect);
        } else {
+               destRect.w = Width();
+               destRect.h = Height();
                bool red(true);
                while (destRect.w > 1 && destRect.h > 1) {
                        SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, red ? 0xFF : 0, 0, 0));
index 475d863638d060e3a2bad0d1ff4a1575d7fb2fb4..202b96f3f5a05e2a2eb95ffc2f03b1f8d8c9a08d 100644 (file)
@@ -17,8 +17,8 @@ namespace graphics {
 class Sprite {
 
 public:
-       Sprite(SDL_Surface *s, int width, int height)
-       : surface(s), width(width), height(height) { }
+       Sprite(SDL_Surface *s, int width, int height, int xOffset = 0, int yOffset = 0)
+       : surface(s), width(width), height(height), xOffset(xOffset), yOffset(yOffset) { }
 
 public:
        int Width() const { return width; }
@@ -35,6 +35,8 @@ private:
        SDL_Surface *surface;
        int width;
        int height;
+       int xOffset;
+       int yOffset;
 
 };
 
index 807784300ae1c5fe416ea0c44f058e3c7596ca66..767464bfe5f7be7b33799aa87cf4b38cc26c1f88 100644 (file)
@@ -12,6 +12,7 @@
 #include "battle/Monster.h"
 #include "battle/PartyLayout.h"
 #include "geometry/Point.h"
+#include "graphics/Frame.h"
 #include "graphics/Sprite.h"
 #include "sdl/InitImage.h"
 #include "sdl/InitScreen.h"
@@ -29,6 +30,7 @@ using battle::Hero;
 using battle::Monster;
 using battle::PartyLayout;
 using geometry::Point;
+using graphics::Frame;
 using graphics::Sprite;
 using sdl::InitImage;
 using sdl::InitScreen;
@@ -82,8 +84,11 @@ int main(int argc, char **argv) {
                Sprite attackIconsSprite(attackIcons, 32, 32);
                SDL_Surface *moveIcons(IMG_Load("test-data/move-icons.png"));
                Sprite moveIconsSprite(moveIcons, 32, 32);
+               SDL_Surface *tagFrames(IMG_Load("test-data/tag-frames.png"));
+               Frame heroTagFrame(tagFrames, 8, 8, 1, 1, 17);
+               Frame activeHeroTagFrame(tagFrames, 8, 8);
 
-               BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite, &moveIconsSprite));
+               BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite, &moveIconsSprite, &heroTagFrame, &activeHeroTagFrame));
                battleState->AddMonster(monster);
                battleState->AddMonster(monster);
                battleState->AddMonster(monster);
diff --git a/test-data/tag-frames.png b/test-data/tag-frames.png
new file mode 100644 (file)
index 0000000..6387c4d
Binary files /dev/null and b/test-data/tag-frames.png differ