]> git.localhorst.tv Git - l2e.git/blob - src/map/Area.cpp
added area bounds to debug overlay
[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         SDL_Rect destRect;
49         destRect.x = inOffset.X();
50         destRect.y = inOffset.Y();
51         destRect.w = Width() * tileset->Width();
52         destRect.h = 1;
53         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
54         destRect.y += Height() * tileset->Height() - 1;
55         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
56         destRect.y = inOffset.Y();
57         destRect.w = 1;
58         destRect.h = Height() * tileset->Height();
59         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
60         destRect.x += Width() * tileset->Width() - 1;
61         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
62
63         for (int i(0); i < numTiles; ++i) {
64                 Vector<int> offset(
65                                 inOffset.X() + (i % width) * tileset->Width(),
66                                 inOffset.Y() + (i / width) * tileset->Height());
67                 const Tile &tile(tiles[i]);
68
69                 if (tile.BlocksNorth()) {
70                         destRect.x = offset.X();
71                         destRect.y = offset.Y();
72                         destRect.w = tileset->Width();
73                         destRect.h = 1;
74                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
75                 }
76
77                 if (tile.BlocksEast()) {
78                         destRect.x = offset.X() + tileset->Width() - 1;
79                         destRect.y = offset.Y();
80                         destRect.w = 1;
81                         destRect.h = tileset->Height();
82                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
83                 }
84
85                 if (tile.BlocksSouth()) {
86                         destRect.x = offset.X();
87                         destRect.y = offset.Y() + tileset->Height() - 1;
88                         destRect.w = tileset->Width();
89                         destRect.h = 1;
90                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
91                 }
92
93                 if (tile.BlocksWest()) {
94                         destRect.x = offset.X();
95                         destRect.y = offset.Y();
96                         destRect.w = 1;
97                         destRect.h = tileset->Height();
98                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
99                 }
100         }
101 }
102
103 }