]> git.localhorst.tv Git - l2e.git/blob - src/map/Area.cpp
commented headers in app/ directory
[l2e.git] / src / map / Area.cpp
1 /*
2  * Area.cpp
3  *
4  *  Created on: Sep 26, 2012
5  *      Author: holy
6  */
7
8 #include "Area.h"
9
10 #include "Tile.h"
11 #include "../graphics/Sprite.h"
12 #include "../loader/TypeDescription.h"
13 #include "../sdl/utility.h"
14
15 #include <stdexcept>
16
17 using geometry::Vector;
18 using loader::FieldDescription;
19 using loader::TypeDescription;
20
21 namespace map {
22
23 Area::Area()
24 : battlebg(0)
25 , tiles(0)
26 , numTiles(0)
27 , width(0) {
28
29 }
30
31
32 Tile *Area::TileAt(const geometry::Vector<int> &offset) {
33         int tileIndex(offset.Y() * width + offset.X());
34         if (tileIndex < numTiles) {
35                 return tiles +tileIndex;
36         } else {
37                 return 0;
38         }
39 }
40
41 const Tile *Area::TileAt(const geometry::Vector<int> &offset) const {
42         int tileIndex(offset.Y() * width + offset.X());
43         if (tileIndex < numTiles) {
44                 return tiles +tileIndex;
45         } else {
46                 return 0;
47         }
48 }
49
50
51 void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
52         for (int i(0); i < numTiles; ++i) {
53                 Vector<int> offset(
54                                 inOffset.X() + (i % width) * tileset->Width(),
55                                 inOffset.Y() + (i / width) * tileset->Height());
56                 const Tile &tile(tiles[i]);
57                 tileset->Draw(dest, offset, tile.Offset().X(), tile.Offset().Y());
58         }
59 }
60
61 void Area::RenderDebug(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
62         SDL_Rect destRect;
63         destRect.x = inOffset.X();
64         destRect.y = inOffset.Y();
65         destRect.w = Width() * tileset->Width();
66         destRect.h = Height() * tileset->Height();
67         sdl::OutlineRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
68
69         Uint32 color(SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
70
71         for (int i(0); i < numTiles; ++i) {
72                 Vector<int> offset(
73                                 inOffset.X() + (i % width) * tileset->Width(),
74                                 inOffset.Y() + (i / width) * tileset->Height());
75                 const Tile &tile(tiles[i]);
76
77                 if (tile.BlocksNorth()) {
78                         sdl::HorizontalLine(dest, offset, tileset->Width(), color);
79                 }
80                 if (tile.BlocksEast()) {
81                         sdl::VerticalLine(dest, Vector<int>(offset.X() + tileset->Width() - 1, offset.Y()), tileset->Height(), color);
82                 }
83                 if (tile.BlocksSouth()) {
84                         sdl::HorizontalLine(dest, Vector<int>(offset.X(), offset.Y() + tileset->Height() - 1), tileset->Width(), color);
85                 }
86                 if (tile.BlocksWest()) {
87                         sdl::VerticalLine(dest, offset, tileset->Height(), color);
88                 }
89         }
90 }
91
92
93 void Area::CreateTypeDescription() {
94         Area a;
95
96         int imageId(TypeDescription::GetTypeId("Image"));
97         int numberId(TypeDescription::GetTypeId("Number"));
98         int tileId(TypeDescription::GetTypeId("Tile"));
99
100         TypeDescription &td(TypeDescription::CreateOrGet("Area"));
101         td.SetConstructor(&Construct);
102         td.SetSize(sizeof(Area));
103
104         td.AddField("battlebg", FieldDescription(((char *)&a.battlebg) - ((char *)&a), imageId).SetReferenced());
105         td.AddField("tiles", FieldDescription(((char *)&a.tiles) - ((char *)&a), tileId).SetReferenced().SetAggregate());
106         td.AddField("width", FieldDescription(((char *)&a.width) - ((char *)&a), numberId));
107 }
108
109 void Area::Construct(void *data) {
110         new (data) Area;
111 }
112
113 }