]> git.localhorst.tv Git - l2e.git/blob - Area.cpp
6299290a6a98057dfcbb3257675fcd07bdb37324
[l2e.git] / 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(
48                 SDL_Surface *dest,
49                 const graphics::Sprite *tileset,
50                 const Vector<int> &inOffset,
51                 unsigned int frame) 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,
58                                 tile.Offset().X(),
59                                 tile.Offset().Y() + (frame % tile.NumFrames()));
60         }
61 }
62
63 void Area::RenderDebug(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
64         SDL_Rect destRect;
65         destRect.x = inOffset.X();
66         destRect.y = inOffset.Y();
67         destRect.w = Width() * tileset->Width();
68         destRect.h = Height() * tileset->Height();
69         sdl::OutlineRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
70
71         Uint32 color(SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
72
73         for (int i(0); i < numTiles; ++i) {
74                 Vector<int> offset(
75                                 inOffset.X() + (i % width) * tileset->Width(),
76                                 inOffset.Y() + (i / width) * tileset->Height());
77                 const Tile &tile(tiles[i]);
78
79                 if (tile.BlocksNorth()) {
80                         sdl::HorizontalLine(dest, offset, tileset->Width(), color);
81                 }
82                 if (tile.BlocksEast()) {
83                         sdl::VerticalLine(dest, Vector<int>(offset.X() + tileset->Width() - 1, offset.Y()), tileset->Height(), color);
84                 }
85                 if (tile.BlocksSouth()) {
86                         sdl::HorizontalLine(dest, Vector<int>(offset.X(), offset.Y() + tileset->Height() - 1), tileset->Width(), color);
87                 }
88                 if (tile.BlocksWest()) {
89                         sdl::VerticalLine(dest, offset, tileset->Height(), color);
90                 }
91                 if (tile.IsLadder()) {
92                         SDL_Rect rect;
93                         rect.x = offset.X() + (tileset->Width() / 2) - 2;
94                         rect.y = offset.Y();
95                         rect.w = 4;
96                         rect.h = tileset->Height();
97                         SDL_FillRect(dest, &rect, color);
98                 }
99         }
100 }
101
102
103 void Area::CreateTypeDescription() {
104         Area a;
105
106         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Area"));
107         td.SetConstructor(&Construct);
108         td.SetSize(sizeof(Area));
109
110         td.AddField("battlebg", FieldDescription(((char *)&a.battlebg) - ((char *)&a), Interpreter::IMAGE_ID).SetReferenced());
111         td.AddField("tiles", FieldDescription(((char *)&a.tiles) - ((char *)&a), Tile::TYPE_ID).SetAggregate());
112         td.AddField("width", FieldDescription(((char *)&a.width) - ((char *)&a), Interpreter::NUMBER_ID));
113 }
114
115 void Area::Construct(void *data) {
116         new (data) Area;
117 }
118
119 }