]> git.localhorst.tv Git - l2e.git/commitdiff
added attack type selection menu in battle state
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 6 Aug 2012 14:54:35 +0000 (16:54 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 6 Aug 2012 14:54:35 +0000 (16:54 +0200)
also added a test sprite for the menu icons
executables need to be run from the project root in order for main to find the test data files

14 files changed:
Debug/objects.mk
Debug/src/battle/subdir.mk
Debug/src/sdl/subdir.mk
Release/objects.mk
Release/src/battle/subdir.mk
Release/src/sdl/subdir.mk
src/battle/AttackTypeMenu.cpp [new file with mode: 0644]
src/battle/AttackTypeMenu.h [new file with mode: 0644]
src/battle/BattleState.cpp
src/battle/BattleState.h
src/main.cpp
src/sdl/InitImage.cpp [new file with mode: 0644]
src/sdl/InitImage.h [new file with mode: 0644]
test-data/attack-type-icons.png [new file with mode: 0644]

index b99d1e557524bff7e90047ab535352d6c2d0af69..718d8a15ffca90e53060cf729fc14d99efa670da 100644 (file)
@@ -4,5 +4,5 @@
 
 USER_OBJS :=
 
-LIBS := -lSDL
+LIBS := -lSDL -lSDL_image
 
index 52e0e02ff429823c32b8a22ba7f4d5d2cd4ff84f..d0073bc4a97075a97a3eca051c07dabc04bd2ce4 100644 (file)
@@ -4,18 +4,21 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/battle/AttackTypeMenu.cpp \
 ../src/battle/BattleState.cpp \
 ../src/battle/Hero.cpp \
 ../src/battle/Monster.cpp \
 ../src/battle/PartyLayout.cpp 
 
 OBJS += \
+./src/battle/AttackTypeMenu.o \
 ./src/battle/BattleState.o \
 ./src/battle/Hero.o \
 ./src/battle/Monster.o \
 ./src/battle/PartyLayout.o 
 
 CPP_DEPS += \
+./src/battle/AttackTypeMenu.d \
 ./src/battle/BattleState.d \
 ./src/battle/Hero.d \
 ./src/battle/Monster.d \
index 67239ce416f3d4e95ddbe9a23942753bc2ddd406..b800be3840a024a5b6c15b7bf66d89f0e3e3dd97 100644 (file)
@@ -4,14 +4,17 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/sdl/InitImage.cpp \
 ../src/sdl/InitSDL.cpp \
 ../src/sdl/InitScreen.cpp 
 
 OBJS += \
+./src/sdl/InitImage.o \
 ./src/sdl/InitSDL.o \
 ./src/sdl/InitScreen.o 
 
 CPP_DEPS += \
+./src/sdl/InitImage.d \
 ./src/sdl/InitSDL.d \
 ./src/sdl/InitScreen.d 
 
index b99d1e557524bff7e90047ab535352d6c2d0af69..718d8a15ffca90e53060cf729fc14d99efa670da 100644 (file)
@@ -4,5 +4,5 @@
 
 USER_OBJS :=
 
-LIBS := -lSDL
+LIBS := -lSDL -lSDL_image
 
index 6cff662588de8f10fc4da35013998ded97394a24..d1c5b2fb7e712b2bc02ca25e3584b9d7af66bb0e 100644 (file)
@@ -4,18 +4,21 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/battle/AttackTypeMenu.cpp \
 ../src/battle/BattleState.cpp \
 ../src/battle/Hero.cpp \
 ../src/battle/Monster.cpp \
 ../src/battle/PartyLayout.cpp 
 
 OBJS += \
+./src/battle/AttackTypeMenu.o \
 ./src/battle/BattleState.o \
 ./src/battle/Hero.o \
 ./src/battle/Monster.o \
 ./src/battle/PartyLayout.o 
 
 CPP_DEPS += \
+./src/battle/AttackTypeMenu.d \
 ./src/battle/BattleState.d \
 ./src/battle/Hero.d \
 ./src/battle/Monster.d \
index ad789ab35c19ed5021d1c110cee01c80bc0e89e8..3bef090e820eab2623f2df5c3df37661e80760cc 100644 (file)
@@ -4,14 +4,17 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/sdl/InitImage.cpp \
 ../src/sdl/InitSDL.cpp \
 ../src/sdl/InitScreen.cpp 
 
 OBJS += \
+./src/sdl/InitImage.o \
 ./src/sdl/InitSDL.o \
 ./src/sdl/InitScreen.o 
 
 CPP_DEPS += \
+./src/sdl/InitImage.d \
 ./src/sdl/InitSDL.d \
 ./src/sdl/InitScreen.d 
 
diff --git a/src/battle/AttackTypeMenu.cpp b/src/battle/AttackTypeMenu.cpp
new file mode 100644 (file)
index 0000000..2a23a09
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * AttackTypeMenu.cpp
+ *
+ *  Created on: Aug 6, 2012
+ *      Author: holy
+ */
+
+#include "AttackTypeMenu.h"
+
+#include "../app/Input.h"
+#include "../geometry/operators.h"
+#include "../geometry/Vector.h"
+#include "../graphics/Sprite.h"
+
+using app::Input;
+using geometry::Point;
+using geometry::Vector;
+
+namespace battle {
+
+void AttackTypeMenu::ReadInput(const Input &input) {
+       if (input.IsDown(Input::PAD_UP)) {
+               selected = MAGIC;
+       } else if (input.IsDown(Input::PAD_RIGHT)) {
+               selected = DEFEND;
+       } else if (input.IsDown(Input::PAD_DOWN)) {
+               selected = IKARI;
+       } else if (input.IsDown(Input::PAD_LEFT)) {
+               selected = ITEM;
+       } else {
+               selected = SWORD;
+       }
+}
+
+void AttackTypeMenu::Render(SDL_Surface *screen, const geometry::Point<int> &center) {
+       Vector<int> swordOffset(icons->Width() / -2, icons->Height() / -2);
+       Vector<int> magicOffset(swordOffset.X(), swordOffset.Y() - icons->Height());
+       Vector<int> defendOffset(swordOffset.X() + icons->Width(), swordOffset.Y());
+       Vector<int> ikariOffset(swordOffset.X(), swordOffset.Y() + icons->Height());
+       Vector<int> itemOffset(swordOffset.X() - icons->Width(), swordOffset.Y());
+
+       icons->Draw(screen, center + swordOffset, SWORD, (selected == SWORD) ? 1 : 0);
+       icons->Draw(screen, center + magicOffset, MAGIC, (selected == MAGIC) ? 1 : 0);
+       icons->Draw(screen, center + defendOffset, DEFEND, (selected == DEFEND) ? 1 : 0);
+       icons->Draw(screen, center + ikariOffset, IKARI, (selected == IKARI) ? 1 : 0);
+       icons->Draw(screen, center + itemOffset, ITEM, (selected == ITEM) ? 1 : 0);
+}
+
+}
diff --git a/src/battle/AttackTypeMenu.h b/src/battle/AttackTypeMenu.h
new file mode 100644 (file)
index 0000000..b63ec97
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * AttackTypeMenu.h
+ *
+ *  Created on: Aug 6, 2012
+ *      Author: holy
+ */
+
+#ifndef BATTLE_ATTACKTYPEMENU_H_
+#define BATTLE_ATTACKTYPEMENU_H_
+
+namespace app { class Input; }
+namespace graphics { class Sprite; }
+
+#include "../geometry/Point.h"
+
+#include <SDL.h>
+
+namespace battle {
+
+class AttackTypeMenu {
+
+public:
+       enum Icon {
+               SWORD,
+               MAGIC,
+               DEFEND,
+               IKARI,
+               ITEM
+       };
+
+public:
+       explicit AttackTypeMenu(const graphics::Sprite *icons)
+       : icons(icons), selected(SWORD) { }
+
+public:
+       void ReadInput(const app::Input &);
+       Icon Selected() const { return selected; }
+       void Render(SDL_Surface *screen, const geometry::Point<int> &center);
+
+private:
+       const graphics::Sprite *icons;
+       Icon selected;
+
+};
+
+}
+
+#endif /* BATTLE_ATTACKTYPEMENU_H_ */
index 9f1acac18d859b83d378f494cc3cc380b71d2f58..b11f52cc90b4950f73fd76641cc1699420d17bb3 100644 (file)
@@ -55,7 +55,7 @@ void BattleState::ExitState() {
 
 
 void BattleState::HandleInput(const Input &input) {
-
+       attackTypeMenu.ReadInput(input);
 }
 
 void BattleState::UpdateWorld(float deltaT) {
@@ -69,7 +69,8 @@ void BattleState::Render(SDL_Surface *screen) {
 
        RenderBackground(screen, offset);
        RenderMonsters(screen, offset);
-       RenderHeroes(screen, offset);
+//     RenderHeroes(screen, offset);
+       attackTypeMenu.Render(screen, Point<int>(background->w / 2, background->h * 3 / 4));
 }
 
 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
index 397e5e3b02a59314bd97dc7518c2746d25ab54b7..9e504ed8c13b04db85168aa5bf7acd2cb672a4e4 100644 (file)
@@ -8,6 +8,7 @@
 #ifndef BATTLE_BATTLESTATE_H_
 #define BATTLE_BATTLESTATE_H_
 
+#include "AttackTypeMenu.h"
 #include "Hero.h"
 #include "Monster.h"
 #include "../app/State.h"
 #include <SDL.h>
 
 namespace app { class Input; }
+namespace graphics { class Sprite; }
 
 namespace battle {
 
 class PartyLayout;
 
+// TODO: maybe split battle state into substates for each menu?
 class BattleState
 : public app::State {
 
 public:
-       BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout)
+       BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const graphics::Sprite *attackIcons)
        : background(background)
        , monstersLayout(&monstersLayout)
-       , heroesLayout(&heroesLayout) { }
+       , heroesLayout(&heroesLayout)
+       , attackTypeMenu(attackIcons) { }
 
 public:
        void AddMonster(const Monster &);
@@ -55,6 +59,7 @@ private:
        SDL_Surface *background;
        const PartyLayout *monstersLayout;
        const PartyLayout *heroesLayout;
+       AttackTypeMenu attackTypeMenu;
        std::vector<geometry::Point<int> > monsterPositions;
        std::vector<geometry::Point<int> > heroesPositions;
        std::vector<Monster> monsters;
index 6bce28f0efe5d5e9eee4dbc044f0d6567c0139f9..4ea0e0e3f067c530f33a4ffa17721ddb6d84d980 100644 (file)
 #include "battle/PartyLayout.h"
 #include "geometry/Point.h"
 #include "graphics/Sprite.h"
+#include "sdl/InitImage.h"
 #include "sdl/InitScreen.h"
 #include "sdl/InitSDL.h"
 
 #include <exception>
 #include <iostream>
+#include <SDL.h>
+#include <SDL_image.h>
 
 using app::Application;
 using app::Input;
@@ -27,6 +30,7 @@ using battle::Monster;
 using battle::PartyLayout;
 using geometry::Point;
 using graphics::Sprite;
+using sdl::InitImage;
 using sdl::InitScreen;
 using sdl::InitSDL;
 
@@ -39,38 +43,42 @@ int main(int argc, char **argv) {
        const int width = 800;
        const int height = 480;
 
-       // temporary test data
-       SDL_Surface *bg(SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF));
-       SDL_FillRect(bg, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF));
-       SDL_Rect r;
-       r.x = 1;
-       r.y = 1;
-       r.w = width - 2;
-       r.h = height - 2;
-       SDL_FillRect(bg, &r, SDL_MapRGB(bg->format, 0, 0, 0));
-       PartyLayout monstersLayout;
-       monstersLayout.AddPosition(Point<Uint8>(50, 100));
-       monstersLayout.AddPosition(Point<Uint8>(100, 100));
-       monstersLayout.AddPosition(Point<Uint8>(150, 100));
-       monstersLayout.AddPosition(Point<Uint8>(200, 100));
-       PartyLayout heroesLayout;
-       heroesLayout.AddPosition(Point<Uint8>(27, 219));
-       heroesLayout.AddPosition(Point<Uint8>(104, 227));
-       heroesLayout.AddPosition(Point<Uint8>(66, 238));
-       heroesLayout.AddPosition(Point<Uint8>(143, 246));
-       SDL_Surface *white96(SDL_CreateRGBSurface(0, 96, 96, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF));
-       SDL_FillRect(white96, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF));
-       Sprite dummySprite(white96, 96, 96);
-       Monster monster;
-       monster.SetSprite(&dummySprite);
-       Hero hero;
-       hero.SetSprite(&dummySprite);
-
        try {
                InitSDL sdl;
+               InitImage image(IMG_INIT_PNG);
                InitScreen screen(width, height);
 
-               BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout));
+               // temporary test data
+               SDL_Surface *bg(SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF));
+               SDL_FillRect(bg, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF));
+               SDL_Rect r;
+               r.x = 1;
+               r.y = 1;
+               r.w = width - 2;
+               r.h = height - 2;
+               SDL_FillRect(bg, &r, SDL_MapRGB(bg->format, 0, 0, 0));
+               PartyLayout monstersLayout;
+               monstersLayout.AddPosition(Point<Uint8>(50, 100));
+               monstersLayout.AddPosition(Point<Uint8>(100, 100));
+               monstersLayout.AddPosition(Point<Uint8>(150, 100));
+               monstersLayout.AddPosition(Point<Uint8>(200, 100));
+               PartyLayout heroesLayout;
+               heroesLayout.AddPosition(Point<Uint8>(27, 219));
+               heroesLayout.AddPosition(Point<Uint8>(104, 227));
+               heroesLayout.AddPosition(Point<Uint8>(66, 238));
+               heroesLayout.AddPosition(Point<Uint8>(143, 246));
+               SDL_Surface *white96(SDL_CreateRGBSurface(0, 96, 96, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF));
+               SDL_FillRect(white96, 0, SDL_MapRGB(bg->format, 0xFF, 0xFF, 0xFF));
+               Sprite dummySprite(white96, 96, 96);
+               Monster monster;
+               monster.SetSprite(&dummySprite);
+               Hero hero;
+               hero.SetSprite(&dummySprite);
+
+               SDL_Surface *attackIcons(IMG_Load("test-data/attack-type-icons.png"));
+               Sprite attackIconsSprite(attackIcons, 32, 32);
+
+               BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite));
                battleState->AddMonster(monster);
                battleState->AddMonster(monster);
                battleState->AddMonster(monster);
diff --git a/src/sdl/InitImage.cpp b/src/sdl/InitImage.cpp
new file mode 100644 (file)
index 0000000..2c7dfcd
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * InitImage.cpp
+ *
+ *  Created on: Aug 6, 2012
+ *      Author: holy
+ */
+
+#include "InitImage.h"
+
+#include <SDL_image.h>
+#include <stdexcept>
+
+using std::runtime_error;
+
+namespace sdl {
+
+InitImage::InitImage(int flags) {
+       if (IMG_Init(flags) != flags) {
+               throw runtime_error(IMG_GetError());
+       }
+}
+
+InitImage::~InitImage() {
+       IMG_Quit();
+}
+
+}
diff --git a/src/sdl/InitImage.h b/src/sdl/InitImage.h
new file mode 100644 (file)
index 0000000..425b7fe
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * InitImage.h
+ *
+ *  Created on: Aug 6, 2012
+ *      Author: holy
+ */
+
+#ifndef SDL_INITIMAGE_H_
+#define SDL_INITIMAGE_H_
+
+namespace sdl {
+
+class InitImage {
+
+public:
+       explicit InitImage(int flags);
+       ~InitImage();
+private:
+       InitImage(const InitImage &);
+       InitImage &operator =(const InitImage &);
+
+};
+
+}
+
+#endif /* SDL_INITIMAGE_H_ */
diff --git a/test-data/attack-type-icons.png b/test-data/attack-type-icons.png
new file mode 100644 (file)
index 0000000..65791f5
Binary files /dev/null and b/test-data/attack-type-icons.png differ