]> git.localhorst.tv Git - l2e.git/blob - src/map/Area.cpp
added Sort function to Inventory
[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 "../loader/Interpreter.h"
14 #include "../sdl/utility.h"
15
16 #include <stdexcept>
17
18 using geometry::Vector;
19 using loader::FieldDescription;
20 using loader::Interpreter;
21 using loader::TypeDescription;
22
23 namespace map {
24
25 Area::Area()
26 : battlebg(0)
27 , tiles(0)
28 , numTiles(0)
29 , width(0) {
30
31 }
32
33
34 Tile *Area::TileAt(const geometry::Vector<int> &offset) {
35         int tileIndex(offset.Y() * width + offset.X());
36         if (tileIndex < numTiles) {
37                 return tiles +tileIndex;
38         } else {
39                 return 0;
40         }
41 }
42
43 const Tile *Area::TileAt(const geometry::Vector<int> &offset) const {
44         int tileIndex(offset.Y() * width + offset.X());
45         if (tileIndex < numTiles) {
46                 return tiles +tileIndex;
47         } else {
48                 return 0;
49         }
50 }
51
52
53 void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
54         for (int i(0); i < numTiles; ++i) {
55                 Vector<int> offset(
56                                 inOffset.X() + (i % width) * tileset->Width(),
57                                 inOffset.Y() + (i / width) * tileset->Height());
58                 const Tile &tile(tiles[i]);
59                 tileset->Draw(dest, offset, tile.Offset().X(), tile.Offset().Y());
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         }
92 }
93
94
95 void Area::CreateTypeDescription() {
96         Area a;
97
98         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Area"));
99         td.SetConstructor(&Construct);
100         td.SetSize(sizeof(Area));
101
102         td.AddField("battlebg", FieldDescription(((char *)&a.battlebg) - ((char *)&a), Interpreter::IMAGE_ID).SetReferenced());
103         td.AddField("tiles", FieldDescription(((char *)&a.tiles) - ((char *)&a), Tile::TYPE_ID).SetReferenced().SetAggregate());
104         td.AddField("width", FieldDescription(((char *)&a.width) - ((char *)&a), Interpreter::NUMBER_ID));
105 }
106
107 void Area::Construct(void *data) {
108         new (data) Area;
109 }
110
111 }