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