4 * Created on: Sep 29, 2012
13 #include "../graphics/Sprite.h"
17 using geometry::Vector;
35 Area *Map::AreaAt(const Vector<int> &offset) {
37 Vector<int> coords(TileCoordinates(offset));
38 Vector<int> areaOffset(coords / areas[0].Size());
39 int areaIndex(areaOffset.Index(width));
40 if (areaIndex < numAreas) {
41 return areas + areaIndex;
47 const Area *Map::AreaAt(const Vector<int> &offset) const {
49 Vector<int> coords(TileCoordinates(offset));
50 Vector<int> areaOffset(coords / areas[0].Size());
51 int areaIndex(areaOffset.Index(width));
52 if (areaIndex < numAreas) {
53 return areas + areaIndex;
59 Tile *Map::TileAt(const Vector<int> &offset) {
60 Area *area(AreaAt(offset));
62 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
63 return area->TileAt(tileOffset);
69 const Tile *Map::TileAt(const Vector<int> &offset) const {
70 const Area *area(AreaAt(offset));
72 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
73 return area->TileAt(tileOffset);
79 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
80 // TODO: add support for multiple triggers on a tile?
81 Vector<int> coords(TileCoordinates(offset));
82 for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
83 if (i->TilePosition() == coords) {
90 SDL_Surface *Map::BattleBackgroundAt(const geometry::Vector<int> &position) {
91 Tile *tile(TileAt(position));
92 if (tile && tile->BattleBackground()) {
93 return tile->BattleBackground();
95 Area *area(AreaAt(position));
96 if (area && area->BattleBackground()) {
97 return area->BattleBackground();
102 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
103 return position / tileset->Size();
107 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
108 // TODO: skip invisible areas
109 for (int i(0); i < numAreas; ++i) {
110 const Area &area(areas[i]);
111 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
112 area.Render(dest, tileset, offset);
116 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
117 // TODO: skip invisible areas
118 for (int i(0); i < numAreas; ++i) {
119 const Area &area(areas[i]);
120 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
121 area.RenderDebug(dest, tileset, offset);
123 for (int i(0); i < numTriggers; ++i) {
124 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
126 destRect.x = offset.X() + (tileset->Width() / 4);
127 destRect.y = offset.Y() + (tileset->Height() / 4);
128 destRect.w = tileset->Width() / 2;
129 destRect.h = tileset->Height() / 2;
130 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));