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