From 59c4aea0762cbc5f1bf74c5b1b35629408fb92af Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 29 Sep 2012 18:45:01 +0200 Subject: [PATCH] added basic map classes --- Debug/makefile | 1 + Debug/sources.mk | 1 + Debug/src/map/subdir.mk | 30 ++++++++++++++++++++++ Release/makefile | 1 + Release/sources.mk | 1 + Release/src/map/subdir.mk | 30 ++++++++++++++++++++++ src/main.cpp | 52 ++++++++++++++++++++++----------------- src/map/Area.cpp | 34 +++++++++++++++++++++++++ src/map/Area.h | 49 ++++++++++++++++++++++++++++++++++++ src/map/Map.cpp | 37 ++++++++++++++++++++++++++++ src/map/Map.h | 48 ++++++++++++++++++++++++++++++++++++ src/map/Tile.cpp | 17 +++++++++++++ src/map/Tile.h | 51 ++++++++++++++++++++++++++++++++++++++ 13 files changed, 329 insertions(+), 23 deletions(-) create mode 100644 Debug/src/map/subdir.mk create mode 100644 Release/src/map/subdir.mk create mode 100644 src/map/Area.cpp create mode 100644 src/map/Area.h create mode 100644 src/map/Map.cpp create mode 100644 src/map/Map.h create mode 100644 src/map/Tile.cpp create mode 100644 src/map/Tile.h diff --git a/Debug/makefile b/Debug/makefile index 13878f3..fb78d77 100644 --- a/Debug/makefile +++ b/Debug/makefile @@ -9,6 +9,7 @@ RM := rm -rf # All of the sources participating in the build are defined here -include sources.mk -include src/sdl/subdir.mk +-include src/map/subdir.mk -include src/loader/subdir.mk -include src/graphics/subdir.mk -include src/common/subdir.mk diff --git a/Debug/sources.mk b/Debug/sources.mk index 7aaa6cf..ccb0eeb 100644 --- a/Debug/sources.mk +++ b/Debug/sources.mk @@ -24,6 +24,7 @@ C_UPPER_DEPS := # Every subdirectory with source files must be described here SUBDIRS := \ src/sdl \ +src/map \ src \ src/loader \ src/graphics \ diff --git a/Debug/src/map/subdir.mk b/Debug/src/map/subdir.mk new file mode 100644 index 0000000..5f0138c --- /dev/null +++ b/Debug/src/map/subdir.mk @@ -0,0 +1,30 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/map/Area.cpp \ +../src/map/Map.cpp \ +../src/map/Tile.cpp + +OBJS += \ +./src/map/Area.o \ +./src/map/Map.o \ +./src/map/Tile.o + +CPP_DEPS += \ +./src/map/Area.d \ +./src/map/Map.d \ +./src/map/Tile.d + + +# Each subdirectory must supply rules for building sources it contributes +src/map/%.o: ../src/map/%.cpp + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -I/usr/include/SDL -O0 -g3 -Wall -Werror -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/Release/makefile b/Release/makefile index 13878f3..fb78d77 100644 --- a/Release/makefile +++ b/Release/makefile @@ -9,6 +9,7 @@ RM := rm -rf # All of the sources participating in the build are defined here -include sources.mk -include src/sdl/subdir.mk +-include src/map/subdir.mk -include src/loader/subdir.mk -include src/graphics/subdir.mk -include src/common/subdir.mk diff --git a/Release/sources.mk b/Release/sources.mk index 7aaa6cf..ccb0eeb 100644 --- a/Release/sources.mk +++ b/Release/sources.mk @@ -24,6 +24,7 @@ C_UPPER_DEPS := # Every subdirectory with source files must be described here SUBDIRS := \ src/sdl \ +src/map \ src \ src/loader \ src/graphics \ diff --git a/Release/src/map/subdir.mk b/Release/src/map/subdir.mk new file mode 100644 index 0000000..384a876 --- /dev/null +++ b/Release/src/map/subdir.mk @@ -0,0 +1,30 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/map/Area.cpp \ +../src/map/Map.cpp \ +../src/map/Tile.cpp + +OBJS += \ +./src/map/Area.o \ +./src/map/Map.o \ +./src/map/Tile.o + +CPP_DEPS += \ +./src/map/Area.d \ +./src/map/Map.d \ +./src/map/Tile.d + + +# Each subdirectory must supply rules for building sources it contributes +src/map/%.o: ../src/map/%.cpp + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -DNDEBUG -I/usr/include/SDL -O3 -Wall -Werror -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/src/main.cpp b/src/main.cpp index 965da00..a2cdae9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,6 +84,8 @@ int main(int argc, char **argv) { const int width = 800; const int height = 480; + const bool battle(false); + // std::srand(std::time(0)); try { @@ -224,29 +226,33 @@ int main(int argc, char **argv) { InitScreen screen(width, height); - BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes)); - battleState->AddMonster(monster); - battleState->AddMonster(monster); - battleState->AddMonster(monster); - battleState->AddMonster(monster); - battleState->AddHero(maxim); - battleState->AddHero(selan); - battleState->AddHero(guy); - battleState->AddHero(dekar); - Application app(&screen, battleState); - app.Buttons().MapKey(SDLK_w, Input::PAD_UP); - app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT); - app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN); - app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT); - app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A); - app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B); - app.Buttons().MapKey(SDLK_UP, Input::ACTION_X); - app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y); - app.Buttons().MapKey(SDLK_RETURN, Input::START); - app.Buttons().MapKey(SDLK_SPACE, Input::SELECT); - app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT); - app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT); - app.Run(); + if (battle) { + BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes)); + battleState->AddMonster(monster); + battleState->AddMonster(monster); + battleState->AddMonster(monster); + battleState->AddMonster(monster); + battleState->AddHero(maxim); + battleState->AddHero(selan); + battleState->AddHero(guy); + battleState->AddHero(dekar); + Application app(&screen, battleState); + app.Buttons().MapKey(SDLK_w, Input::PAD_UP); + app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT); + app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN); + app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT); + app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A); + app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B); + app.Buttons().MapKey(SDLK_UP, Input::ACTION_X); + app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y); + app.Buttons().MapKey(SDLK_RETURN, Input::START); + app.Buttons().MapKey(SDLK_SPACE, Input::SELECT); + app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT); + app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT); + app.Run(); + } else { + + } return 0; } catch (Parser::Error &e) { diff --git a/src/map/Area.cpp b/src/map/Area.cpp new file mode 100644 index 0000000..a1ae680 --- /dev/null +++ b/src/map/Area.cpp @@ -0,0 +1,34 @@ +/* + * Area.cpp + * + * Created on: Sep 26, 2012 + * Author: holy + */ + +#include "Area.h" + +#include "Tile.h" +#include "../graphics/Sprite.h" + +using geometry::Vector; + +namespace map { + +Area::Area() +: tiles(0) +, numTiles(0) +, width(0) { + +} + +void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector &inOffset) const { + for (int i(0); i < numTiles; ++i) { + Vector offset( + inOffset.X() + (i % width) * tileset->Width(), + inOffset.Y() + (i / width) * tileset->Height()); + const Tile &tile(tiles[i]); + tileset->Draw(dest, offset, tile.Offset().X(), tile.Offset().Y()); + } +} + +} diff --git a/src/map/Area.h b/src/map/Area.h new file mode 100644 index 0000000..9bca341 --- /dev/null +++ b/src/map/Area.h @@ -0,0 +1,49 @@ +/* + * Area.h + * + * Created on: Sep 26, 2012 + * Author: holy + */ + +#ifndef MAP_AREA_H_ +#define MAP_AREA_H_ + +#include "../geometry/Vector.h" + +#include + +namespace graphics { + class Sprite; +} + +namespace map { + +class Tile; + +class Area { + +public: + Area(); + ~Area() { } + +public: + int Width() const { return width; } + int Height() const { return numTiles / width + (numTiles % width ? 1 : 0); } + + void Render(SDL_Surface *dest, const graphics::Sprite *tileset, const geometry::Vector &offset) const; + +// temporary setters +public: + void SetTiles(const Tile *t, int num) { tiles = t; numTiles = num; } + void SetWidth(int w) { width = w; } + +private: + const Tile *tiles; + int numTiles; + int width; + +}; + +} + +#endif /* MAP_AREA_H_ */ diff --git a/src/map/Map.cpp b/src/map/Map.cpp new file mode 100644 index 0000000..450c004 --- /dev/null +++ b/src/map/Map.cpp @@ -0,0 +1,37 @@ +/* + * Map.cpp + * + * Created on: Sep 29, 2012 + * Author: holy + */ + +#include "Map.h" + +#include "Area.h" +#include "../graphics/Sprite.h" + +using geometry::Vector; + +namespace map { + +Map::Map() +: tileset(0) +, areas(0) +, numAreas(0) +, width(0) { + +} + + +void Map::Render(SDL_Surface *dest, const Vector &inOffset) const { + // TODO: skip invisible areas + for (int i(0); i < numAreas; ++i) { + const Area &area(areas[i]); + Vector offset( + inOffset.X() + (i % width) * area.Width() * tileset->Width(), + inOffset.Y() + (i / width) * area.Height() * tileset->Height()); + area.Render(dest, tileset, offset); + } +} + +} diff --git a/src/map/Map.h b/src/map/Map.h new file mode 100644 index 0000000..9fb24af --- /dev/null +++ b/src/map/Map.h @@ -0,0 +1,48 @@ +/* + * Map.h + * + * Created on: Sep 29, 2012 + * Author: holy + */ + +#ifndef MAP_MAP_H_ +#define MAP_MAP_H_ + +#include "../geometry/Vector.h" + +#include + +namespace graphics { + class Sprite; +} + +namespace map { + +class Area; + +class Map { + +public: + Map(); + ~Map() { } + +public: + void Render(SDL_Surface *dest, const geometry::Vector &offset) const; + +// temporary setters +public: + void SetTileset(const graphics::Sprite *t) { tileset = t; } + void SetAreas(const Area *a, int num) { areas = a; numAreas = num; } + void SetWidth(int w) { width = w; } + +private: + const graphics::Sprite *tileset; + const Area *areas; + int numAreas; + int width; + +}; + +} + +#endif /* MAP_MAP_H_ */ diff --git a/src/map/Tile.cpp b/src/map/Tile.cpp new file mode 100644 index 0000000..7b77fb1 --- /dev/null +++ b/src/map/Tile.cpp @@ -0,0 +1,17 @@ +/* + * Tile.cpp + * + * Created on: Sep 29, 2012 + * Author: holy + */ + +#include "Tile.h" + +namespace map { + +Tile::Tile() +: flags(0) { + +} + +} diff --git a/src/map/Tile.h b/src/map/Tile.h new file mode 100644 index 0000000..d21de56 --- /dev/null +++ b/src/map/Tile.h @@ -0,0 +1,51 @@ +/* + * Tile.h + * + * Created on: Sep 29, 2012 + * Author: holy + */ + +#ifndef MAP_TILE_H_ +#define MAP_TILE_H_ + +#include "../geometry/Vector.h" + +#include + +namespace map { + +class Tile { + +public: + Tile(); + ~Tile() { } + +public: + enum Flag { + BLOCK_NORTH = 0x01, + BLOCK_EAST = 0x02, + BLOCK_SOUTH = 0x04, + BLOCK_WEST = 0x08, + }; + + const geometry::Vector &Offset() const { return offset; } + + bool BlocksNorth() const { return flags & BLOCK_NORTH; } + bool BlocksEast() const { return flags & BLOCK_EAST; } + bool BlocksSouth() const { return flags & BLOCK_SOUTH; } + bool BlocksWest() const { return flags & BLOCK_WEST; } + +// temporary setters +public: + void SetOffset(const geometry::Vector &o) { offset = o; } + void SetFlags(Uint32 f) { flags = f; } + +private: + geometry::Vector offset; + Uint32 flags; + +}; + +} + +#endif /* MAP_TILE_H_ */ -- 2.39.2