]> git.localhorst.tv Git - l2e.git/blob - src/map/Area.cpp
added debug mode for maps
[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
13 #include <stdexcept>
14
15 using geometry::Vector;
16
17 namespace map {
18
19 Area::Area()
20 : tiles(0)
21 , numTiles(0)
22 , width(0) {
23
24 }
25
26
27 const Tile &Area::TileAt(const geometry::Vector<int> &offset) const {
28         int tileIndex(offset.Y() * width + offset.X());
29         if (tileIndex < numTiles) {
30                 return tiles[tileIndex];
31         } else {
32                 throw std::out_of_range("tile index out of range");
33         }
34 }
35
36
37 void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
38         for (int i(0); i < numTiles; ++i) {
39                 Vector<int> offset(
40                                 inOffset.X() + (i % width) * tileset->Width(),
41                                 inOffset.Y() + (i / width) * tileset->Height());
42                 const Tile &tile(tiles[i]);
43                 tileset->Draw(dest, offset, tile.Offset().X(), tile.Offset().Y());
44         }
45 }
46
47 void Area::RenderDebug(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
54                 if (tile.BlocksNorth()) {
55                         SDL_Rect destRect;
56                         destRect.x = offset.X();
57                         destRect.y = offset.Y();
58                         destRect.w = tileset->Width();
59                         destRect.h = 1;
60                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
61                 }
62
63                 if (tile.BlocksEast()) {
64                         SDL_Rect destRect;
65                         destRect.x = offset.X() + tileset->Width() - 1;
66                         destRect.y = offset.Y();
67                         destRect.w = 1;
68                         destRect.h = tileset->Height();
69                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
70                 }
71
72                 if (tile.BlocksSouth()) {
73                         SDL_Rect destRect;
74                         destRect.x = offset.X();
75                         destRect.y = offset.Y() + tileset->Height() - 1;
76                         destRect.w = tileset->Width();
77                         destRect.h = 1;
78                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
79                 }
80
81                 if (tile.BlocksWest()) {
82                         SDL_Rect destRect;
83                         destRect.x = offset.X();
84                         destRect.y = offset.Y();
85                         destRect.w = 1;
86                         destRect.h = tileset->Height();
87                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
88                 }
89         }
90 }
91
92 }