Just added a missing header.
[supertux.git] / src / tilemap.cpp
1 #include <assert.h>
2
3 #include "tilemap.h"
4 #include "display_manager.h"
5 #include "level.h"
6 #include "tile.h"
7 #include "globals.h"
8
9 TileMap::TileMap(DisplayManager& display_manager, Level* newlevel)
10   : level(newlevel)
11 {
12   display_manager.add_drawable(this, LAYER_BACKGROUNDTILES);
13   display_manager.add_drawable(this, LAYER_TILES);
14   display_manager.add_drawable(this, LAYER_FOREGROUNDTILES);
15 }
16
17 TileMap::~TileMap()
18 {
19 }
20
21 void
22 TileMap::action(float )
23 {
24 }
25
26 void
27 TileMap::draw(ViewPort& viewport, int layer)
28 {
29   std::vector<std::vector<unsigned int> >* tiles;
30   switch(layer) {
31     case LAYER_BACKGROUNDTILES:
32       tiles = &level->bg_tiles; break;
33     case LAYER_TILES:
34       tiles = &level->ia_tiles; break;
35     case LAYER_FOREGROUNDTILES:
36       tiles = &level->fg_tiles; break;
37     default:
38       assert(!"Wrong layer when drawing tilemap.");
39   }
40
41   int tsx = int(viewport.get_translation().x / 32); // tilestartindex x
42   int tsy = int(viewport.get_translation().y / 32); // tilestartindex y
43   int sx = - (int(viewport.get_translation().x) % 32);
44   int sy = - (int(viewport.get_translation().y) % 32);
45   for(int x = sx, tx = tsx; x < screen->w && tx < int((*tiles)[0].size());
46       x += 32, ++tx) {
47     for(int y = sy, ty = tsy; y < screen->h && ty < int(tiles->size());
48           y += 32, ++ty) {
49       Tile::draw(x, y, (*tiles) [ty][tx]);
50     }
51   }
52 }