4 * Created on: Sep 29, 2012
13 #include "../graphics/Sprite.h"
14 #include "../loader/Interpreter.h"
15 #include "../loader/TypeDescription.h"
16 #include "../sdl/utility.h"
20 using geometry::Vector;
21 using graphics::Sprite;
22 using loader::FieldDescription;
23 using loader::Interpreter;
24 using loader::TypeDescription;
42 Area *Map::AreaAt(const Vector<int> &offset) {
44 Vector<int> coords(TileCoordinates(offset));
45 Vector<int> areaOffset(coords / areas[0].Size());
46 int areaIndex(areaOffset.Index(width));
47 if (areaIndex < numAreas) {
48 return areas + areaIndex;
54 const Area *Map::AreaAt(const Vector<int> &offset) const {
56 Vector<int> coords(TileCoordinates(offset));
57 Vector<int> areaOffset(coords / areas[0].Size());
58 int areaIndex(areaOffset.Index(width));
59 if (areaIndex < numAreas) {
60 return areas + areaIndex;
66 Tile *Map::TileAt(const Vector<int> &offset) {
67 Area *area(AreaAt(offset));
69 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
70 return area->TileAt(tileOffset);
76 const Tile *Map::TileAt(const Vector<int> &offset) const {
77 const Area *area(AreaAt(offset));
79 Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
80 return area->TileAt(tileOffset);
86 Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
87 // TODO: add support for multiple triggers on a tile?
88 Vector<int> coords(TileCoordinates(offset));
89 for (Trigger *i(triggers); i != triggers + numTriggers; ++i) {
90 if (i->TilePosition() == coords) {
97 SDL_Surface *Map::BattleBackgroundAt(const geometry::Vector<int> &position) {
98 Tile *tile(TileAt(position));
99 if (tile && tile->BattleBackground()) {
100 return tile->BattleBackground();
102 Area *area(AreaAt(position));
103 if (area && area->BattleBackground()) {
104 return area->BattleBackground();
109 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
110 return position / tileset->Size();
114 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
115 // TODO: skip invisible areas
116 for (int i(0); i < numAreas; ++i) {
117 const Area &area(areas[i]);
118 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
119 area.Render(dest, tileset, offset);
123 void Map::RenderDebug(SDL_Surface *dest, const Vector<int> &inOffset) const {
124 // TODO: skip invisible areas
125 for (int i(0); i < numAreas; ++i) {
126 const Area &area(areas[i]);
127 Vector<int> offset(inOffset + Vector<int>::FromIndex(i, width) * area.Size() * tileset->Size());
128 area.RenderDebug(dest, tileset, offset);
130 for (int i(0); i < numTriggers; ++i) {
131 Vector<int> offset((triggers[i].TilePosition() * tileset->Size()) + inOffset);
132 switch (triggers[i].GetType()) {
133 case Trigger::TYPE_NORTH:
134 sdl::HorizontalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
136 case Trigger::TYPE_EAST:
137 sdl::VerticalLine(dest, offset + Vector<int>(tileset->Width() * 3 / 4, tileset->Height() / 4), tileset->Height() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
139 case Trigger::TYPE_SOUTH:
140 sdl::HorizontalLine(dest, offset + Vector<int>(tileset->Width() / 4, tileset->Height() * 3 / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
142 case Trigger::TYPE_WEST:
143 sdl::VerticalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
145 case Trigger::TYPE_CONTACT: {
147 destRect.x = offset.X() + (tileset->Width() / 4);
148 destRect.y = offset.Y() + (tileset->Height() / 4);
149 destRect.w = tileset->Width() / 2;
150 destRect.h = tileset->Height() / 2;
151 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF));
159 void Map::CreateTypeDescription() {
162 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Map"));
163 td.SetConstructor(&Construct);
164 td.SetSize(sizeof(Map));
166 td.AddField("tileset", FieldDescription(((char *)&m.tileset) - ((char *)&m), Sprite::TYPE_ID).SetReferenced());
167 td.AddField("battlebg", FieldDescription(((char *)&m.battlebg) - ((char *)&m), Interpreter::IMAGE_ID).SetReferenced());
168 td.AddField("areas", FieldDescription(((char *)&m.areas) - ((char *)&m), Area::TYPE_ID).SetReferenced().SetAggregate());
169 td.AddField("triggers", FieldDescription(((char *)&m.triggers) - ((char *)&m), Trigger::TYPE_ID).SetReferenced().SetAggregate());
170 td.AddField("entities", FieldDescription(((char *)&m.entities) - ((char *)&m), Entity::TYPE_ID).SetReferenced().SetAggregate());
171 td.AddField("width", FieldDescription(((char *)&m.width) - ((char *)&m), Interpreter::NUMBER_ID));
174 void Map::Construct(void *data) {