-Changed drawing model. Everything is handled through DrawingContext now and
[supertux.git] / src / tilemap.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include "tilemap.h"
20
21 #include <assert.h>
22 #include <algorithm>
23 #include "screen/drawing_context.h"
24 #include "level.h"
25 #include "tile.h"
26 #include "globals.h"
27
28 TileMap::TileMap(Level* newlevel)
29   : level(newlevel)
30 {
31   tilemanager = TileManager::instance();
32 }
33
34 TileMap::~TileMap()
35 {
36 }
37
38 void
39 TileMap::action(float )
40 {
41 }
42
43 void
44 TileMap::draw(const std::vector<unsigned int>& tiles, DrawingContext& context,
45     int layer)
46 {
47   float start_x = context.get_translation().x;
48   float start_y = context.get_translation().y;
49   float end_x = std::min(start_x + screen->w, float(level->width * 32));
50   float end_y = std::min(start_y + screen->h, float(level->height * 32));
51   start_x -= int(start_x) % 32;
52   start_y -= int(start_y) % 32;  
53   int tsx = int(start_x / 32); // tilestartindex x
54   int tsy = int(start_y / 32); // tilestartindex y
55   
56   Vector pos;
57   int tx, ty;
58   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
59     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
60       tilemanager->draw_tile(context, tiles[ty*level->width + tx], pos, layer);
61     }
62   }
63 }
64
65 void
66 TileMap::draw(DrawingContext& context)
67 {
68   draw(level->bg_tiles, context, LAYER_BACKGROUNDTILES);
69   draw(level->ia_tiles, context, LAYER_TILES);
70   draw(level->fg_tiles, context, LAYER_FOREGROUNDTILES);
71 }