4 * Created on: Sep 26, 2012
11 #include "../graphics/Sprite.h"
12 #include "../loader/TypeDescription.h"
13 #include "../loader/Interpreter.h"
14 #include "../sdl/utility.h"
18 using geometry::Vector;
19 using loader::FieldDescription;
20 using loader::Interpreter;
21 using loader::TypeDescription;
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;
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;
53 void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
54 for (int i(0); i < numTiles; ++i) {
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());
63 void Area::RenderDebug(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
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));
71 Uint32 color(SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
73 for (int i(0); i < numTiles; ++i) {
75 inOffset.X() + (i % width) * tileset->Width(),
76 inOffset.Y() + (i / width) * tileset->Height());
77 const Tile &tile(tiles[i]);
79 if (tile.BlocksNorth()) {
80 sdl::HorizontalLine(dest, offset, tileset->Width(), color);
82 if (tile.BlocksEast()) {
83 sdl::VerticalLine(dest, Vector<int>(offset.X() + tileset->Width() - 1, offset.Y()), tileset->Height(), color);
85 if (tile.BlocksSouth()) {
86 sdl::HorizontalLine(dest, Vector<int>(offset.X(), offset.Y() + tileset->Height() - 1), tileset->Width(), color);
88 if (tile.BlocksWest()) {
89 sdl::VerticalLine(dest, offset, tileset->Height(), color);
95 void Area::CreateTypeDescription() {
98 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Area"));
99 td.SetConstructor(&Construct);
100 td.SetSize(sizeof(Area));
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));
107 void Area::Construct(void *data) {