From: Daniel Karbach Date: Mon, 6 Aug 2012 14:54:35 +0000 (+0200) Subject: added attack type selection menu in battle state X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c0860451b5fd681c3b3b8d985e8831276bbd917f;p=l2e.git added attack type selection menu in battle state 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 --- diff --git a/Debug/objects.mk b/Debug/objects.mk index b99d1e5..718d8a1 100644 --- a/Debug/objects.mk +++ b/Debug/objects.mk @@ -4,5 +4,5 @@ USER_OBJS := -LIBS := -lSDL +LIBS := -lSDL -lSDL_image diff --git a/Debug/src/battle/subdir.mk b/Debug/src/battle/subdir.mk index 52e0e02..d0073bc 100644 --- a/Debug/src/battle/subdir.mk +++ b/Debug/src/battle/subdir.mk @@ -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 \ diff --git a/Debug/src/sdl/subdir.mk b/Debug/src/sdl/subdir.mk index 67239ce..b800be3 100644 --- a/Debug/src/sdl/subdir.mk +++ b/Debug/src/sdl/subdir.mk @@ -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/Release/objects.mk b/Release/objects.mk index b99d1e5..718d8a1 100644 --- a/Release/objects.mk +++ b/Release/objects.mk @@ -4,5 +4,5 @@ USER_OBJS := -LIBS := -lSDL +LIBS := -lSDL -lSDL_image diff --git a/Release/src/battle/subdir.mk b/Release/src/battle/subdir.mk index 6cff662..d1c5b2f 100644 --- a/Release/src/battle/subdir.mk +++ b/Release/src/battle/subdir.mk @@ -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 \ diff --git a/Release/src/sdl/subdir.mk b/Release/src/sdl/subdir.mk index ad789ab..3bef090 100644 --- a/Release/src/sdl/subdir.mk +++ b/Release/src/sdl/subdir.mk @@ -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 index 0000000..2a23a09 --- /dev/null +++ b/src/battle/AttackTypeMenu.cpp @@ -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 ¢er) { + Vector swordOffset(icons->Width() / -2, icons->Height() / -2); + Vector magicOffset(swordOffset.X(), swordOffset.Y() - icons->Height()); + Vector defendOffset(swordOffset.X() + icons->Width(), swordOffset.Y()); + Vector ikariOffset(swordOffset.X(), swordOffset.Y() + icons->Height()); + Vector 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 index 0000000..b63ec97 --- /dev/null +++ b/src/battle/AttackTypeMenu.h @@ -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 + +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 ¢er); + +private: + const graphics::Sprite *icons; + Icon selected; + +}; + +} + +#endif /* BATTLE_ATTACKTYPEMENU_H_ */ diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 9f1acac..b11f52c 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -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(background->w / 2, background->h * 3 / 4)); } void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offset) { diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 397e5e3..9e504ed 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -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" @@ -18,19 +19,22 @@ #include 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 > monsterPositions; std::vector > heroesPositions; std::vector monsters; diff --git a/src/main.cpp b/src/main.cpp index 6bce28f..4ea0e0e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,11 +13,14 @@ #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 #include +#include +#include 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(50, 100)); - monstersLayout.AddPosition(Point(100, 100)); - monstersLayout.AddPosition(Point(150, 100)); - monstersLayout.AddPosition(Point(200, 100)); - PartyLayout heroesLayout; - heroesLayout.AddPosition(Point(27, 219)); - heroesLayout.AddPosition(Point(104, 227)); - heroesLayout.AddPosition(Point(66, 238)); - heroesLayout.AddPosition(Point(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(50, 100)); + monstersLayout.AddPosition(Point(100, 100)); + monstersLayout.AddPosition(Point(150, 100)); + monstersLayout.AddPosition(Point(200, 100)); + PartyLayout heroesLayout; + heroesLayout.AddPosition(Point(27, 219)); + heroesLayout.AddPosition(Point(104, 227)); + heroesLayout.AddPosition(Point(66, 238)); + heroesLayout.AddPosition(Point(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 index 0000000..2c7dfcd --- /dev/null +++ b/src/sdl/InitImage.cpp @@ -0,0 +1,27 @@ +/* + * InitImage.cpp + * + * Created on: Aug 6, 2012 + * Author: holy + */ + +#include "InitImage.h" + +#include +#include + +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 index 0000000..425b7fe --- /dev/null +++ b/src/sdl/InitImage.h @@ -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 index 0000000..65791f5 Binary files /dev/null and b/test-data/attack-type-icons.png differ