4 * Created on: Sep 26, 2012
11 #include "../graphics/Sprite.h"
12 #include "../loader/TypeDescription.h"
13 #include "../sdl/utility.h"
17 using geometry::Vector;
18 using loader::FieldDescription;
19 using loader::TypeDescription;
32 Tile *Area::TileAt(const geometry::Vector<int> &offset) {
33 int tileIndex(offset.Y() * width + offset.X());
34 if (tileIndex < numTiles) {
35 return tiles +tileIndex;
41 const Tile *Area::TileAt(const geometry::Vector<int> &offset) const {
42 int tileIndex(offset.Y() * width + offset.X());
43 if (tileIndex < numTiles) {
44 return tiles +tileIndex;
51 void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
52 for (int i(0); i < numTiles; ++i) {
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, tile.Offset().X(), tile.Offset().Y());
61 void Area::RenderDebug(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
63 destRect.x = inOffset.X();
64 destRect.y = inOffset.Y();
65 destRect.w = Width() * tileset->Width();
66 destRect.h = Height() * tileset->Height();
67 sdl::OutlineRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
69 Uint32 color(SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
71 for (int i(0); i < numTiles; ++i) {
73 inOffset.X() + (i % width) * tileset->Width(),
74 inOffset.Y() + (i / width) * tileset->Height());
75 const Tile &tile(tiles[i]);
77 if (tile.BlocksNorth()) {
78 sdl::HorizontalLine(dest, offset, tileset->Width(), color);
80 if (tile.BlocksEast()) {
81 sdl::VerticalLine(dest, Vector<int>(offset.X() + tileset->Width() - 1, offset.Y()), tileset->Height(), color);
83 if (tile.BlocksSouth()) {
84 sdl::HorizontalLine(dest, Vector<int>(offset.X(), offset.Y() + tileset->Height() - 1), tileset->Width(), color);
86 if (tile.BlocksWest()) {
87 sdl::VerticalLine(dest, offset, tileset->Height(), color);
93 void Area::CreateTypeDescription() {
96 int imageId(TypeDescription::GetTypeId("Image"));
97 int numberId(TypeDescription::GetTypeId("Number"));
98 int tileId(TypeDescription::GetTypeId("Tile"));
100 TypeDescription &td(TypeDescription::CreateOrGet("Area"));
101 td.SetConstructor(&Construct);
102 td.SetSize(sizeof(Area));
104 td.AddField("battlebg", FieldDescription(((char *)&a.battlebg) - ((char *)&a), imageId).SetReferenced());
105 td.AddField("tiles", FieldDescription(((char *)&a.tiles) - ((char *)&a), tileId).SetReferenced().SetAggregate());
106 td.AddField("width", FieldDescription(((char *)&a.width) - ((char *)&a), numberId));
109 void Area::Construct(void *data) {