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