]> git.localhorst.tv Git - l2e.git/blob - src/map/Map.cpp
added triggers to map debug render
[l2e.git] / src / map / Map.cpp
1 /*
2  * Map.cpp
3  *
4  *  Created on: Sep 29, 2012
5  *      Author: holy
6  */
7
8 #include "Map.h"
9
10 #include "Area.h"
11 #include "Trigger.h"
12 #include "../graphics/Sprite.h"
13
14 #include <stdexcept>
15
16 using geometry::Vector;
17
18 namespace map {
19
20 Map::Map()
21 : tileset(0)
22 , areas(0)
23 , numAreas(0)
24 , triggers(0)
25 , numTriggers(0)
26 , width(0) {
27
28 }
29
30
31 const Area &Map::AreaAt(const Vector<int> &offset) const {
32         if (numAreas > 0) {
33                 Vector<int> coords(TileCoordinates(offset));
34                 Vector<int> areaOffset(coords / areas[0].Size());
35                 int areaIndex(areaOffset.Index(width));
36                 if (areaIndex < numAreas) {
37                         return areas[areaIndex];
38                 }
39         }
40         throw std::out_of_range("area offset out of bounds");
41 }
42
43 const Tile &Map::TileAt(const Vector<int> &offset) const {
44         const Area &area(AreaAt(offset));
45         Vector<int> tileOffset(TileCoordinates(offset) % area.Size());
46         return area.TileAt(tileOffset);
47 }
48
49 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
50         // TODO: add support for multiple triggers on a tile?
51         Vector<int> coords(TileCoordinates(offset));
52         for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
53                 if (i->TilePosition() == coords) {
54                         return i;
55                 }
56         }
57         return 0;
58 }
59
60 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
61         return position / tileset->Size();
62 }
63
64
65 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
66         // TODO: skip invisible areas
67         for (int i(0); i < numAreas; ++i) {
68                 const Area &area(areas[i]);
69                 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
70                 area.Render(dest, tileset, offset);
71         }
72 }
73
74 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
75         // TODO: skip invisible areas
76         for (int i(0); i < numAreas; ++i) {
77                 const Area &area(areas[i]);
78                 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
79                 area.RenderDebug(dest, tileset, offset);
80         }
81         for (int i(0); i < numTriggers; ++i) {
82                 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
83                 SDL_Rect destRect;
84                 destRect.x = offset.X() + (tileset->Width() / 4);
85                 destRect.y = offset.Y() + (tileset->Height() / 4);
86                 destRect.w = tileset->Width() / 2;
87                 destRect.h = tileset->Height() / 2;
88                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
89         }
90 }
91
92 }