initial commit, lordsawar source, slightly modified
[lordsawar] / src / MapRenderer.cpp
1 // Copyright (C) 2003 Michael Bartl
2 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2005 Andrea Paternesi
4 // Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
5 //
6 //  This program is free software; you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation; either version 3 of the License, or
9 //  (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 Library 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., 51 Franklin Street, Fifth Floor, Boston, MA 
19 //  02110-1301, USA.
20
21 #include <string>
22
23 #include "MapRenderer.h"
24 #include "GameMap.h"
25 #include "player.h"
26 #include "FogMap.h"
27
28 using namespace std;
29
30 MapRenderer::MapRenderer(Glib::RefPtr<Gdk::Pixmap> surface)
31 {
32     d_surface = surface;
33     gc = Gdk::GC::create(surface);
34 }
35  
36 bool MapRenderer::saveAsBitmap(std::string filename)
37 {
38   int tilesize = GameMap::getInstance()->getTileset()->getTileSize();
39   int width = GameMap::getWidth() * tilesize;
40   int height = GameMap::getHeight() * tilesize;
41   Glib::RefPtr<Gdk::Pixmap> surf = Gdk::Pixmap::create(Glib::RefPtr<Gdk::Drawable>(d_surface), width, height, 24);
42   render(0, 0, 0, 0, GameMap::getWidth(), GameMap::getHeight(), surf, Gdk::GC::create(surf));
43   Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(surf), 0, 0, width, height);
44   pixbuf->save (filename, "png");
45   return true;
46 }
47
48 bool MapRenderer::saveViewAsBitmap(std::string filename)
49 {
50   int width;
51   int height;
52   d_surface->get_size(width, height);
53   remove (filename.c_str());
54   Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>(d_surface), 0, 0, width, height);
55   pixbuf->save (filename, "png");
56   return true;
57 }
58
59 MapRenderer::~MapRenderer()
60 {
61 }
62
63 void MapRenderer::render(int x, int y, int tileStartX, int tileStartY,
64                          int columns, int rows)
65 {
66   return render(x, y, tileStartX, tileStartY, columns, rows, d_surface, gc);
67 }
68
69 void MapRenderer::render_tile(Vector<int> draw, Vector<int> tile,
70                               Glib::RefPtr<Gdk::Pixmap> surface, 
71                               Glib::RefPtr<Gdk::GC> context)
72 {
73   Player *p = Playerlist::getActiveplayer();
74   if (p->getFogMap()->isCompletelyObscuredFogTile(tile) == true)
75     return;
76
77   // get correct tile
78   Maptile *mtile = GameMap::getInstance()->getTile(tile);
79
80   TileStyle *style = mtile->getTileStyle();
81   if (style == NULL)
82     printf ("style for tile %d at col=%d,row=%d is null\n",
83             mtile->getMaptileType(), tile.x, tile.y);
84   else
85     {
86       if (style->getImage() == false)
87         {
88           printf ("pic for style %d for tile %d at %d,%d is null\n",
89                   style->getType(),
90                   mtile->getMaptileType(), tile.x, tile.y);
91         }
92     }
93                 
94   style->getImage()->blit(surface, draw.x, draw.y);
95 }
96
97 void MapRenderer::render(int x, int y, int tileStartX, int tileStartY,
98                          int columns, int rows, Glib::RefPtr<Gdk::Pixmap> surface, Glib::RefPtr<Gdk::GC> context)
99 {
100     GameMap* map = GameMap::getInstance();
101     int width = GameMap::getWidth();
102     int height = GameMap::getHeight();
103     int tilesize = map->getTileset()->getTileSize();
104     int drawY = y;
105
106     Gdk::Color background_color = Gdk::Color();
107     background_color.set_rgb_p(0,0,0);
108     
109     for (int tileY = tileStartY; tileY < (tileStartY + rows); tileY++)
110     {
111         int drawX = x;
112         for (int tileX = tileStartX; tileX < (tileStartX + columns); tileX++)
113         {
114             // first check if we're out of the map bounds
115             if (tileX >= width || tileY >= height) {
116                 context->set_rgb_fg_color(background_color);
117                 surface->draw_rectangle(context, true, drawX, drawY, 
118                                         tilesize, tilesize);
119             }
120             else {
121               render_tile(Vector<int>(drawX,drawY), Vector<int>(tileX,tileY),
122                           surface, context);
123             }
124             
125             drawX += tilesize;
126         }
127         drawY += tilesize;
128     }
129
130 }
131
132
133 // End of file