4 * Created on: Sep 29, 2012
12 #include "../graphics/Sprite.h"
16 using geometry::Vector;
33 const Area &Map::AreaAt(const Vector<int> &offset) const {
35 Vector<int> coords(TileCoordinates(offset));
36 Vector<int> areaOffset(coords / areas[0].Size());
37 int areaIndex(areaOffset.Index(width));
38 if (areaIndex < numAreas) {
39 return areas[areaIndex];
42 throw std::out_of_range("area offset out of bounds");
45 const Tile &Map::TileAt(const Vector<int> &offset) const {
46 const Area &area(AreaAt(offset));
47 Vector<int> tileOffset(TileCoordinates(offset) % area.Size());
48 return area.TileAt(tileOffset);
51 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
52 // TODO: add support for multiple triggers on a tile?
53 Vector<int> coords(TileCoordinates(offset));
54 for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
55 if (i->TilePosition() == coords) {
62 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
63 return position / tileset->Size();
67 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
68 // TODO: skip invisible areas
69 for (int i(0); i < numAreas; ++i) {
70 const Area &area(areas[i]);
71 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
72 area.Render(dest, tileset, offset);
76 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
77 // TODO: skip invisible areas
78 for (int i(0); i < numAreas; ++i) {
79 const Area &area(areas[i]);
80 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
81 area.RenderDebug(dest, tileset, offset);
83 for (int i(0); i < numTriggers; ++i) {
84 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
86 destRect.x = offset.X() + (tileset->Width() / 4);
87 destRect.y = offset.Y() + (tileset->Height() / 4);
88 destRect.w = tileset->Width() / 2;
89 destRect.h = tileset->Height() / 2;
90 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));