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