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
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.
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.
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
23 #include "MapRenderer.h"
30 MapRenderer::MapRenderer(Glib::RefPtr<Gdk::Pixmap> surface)
33 gc = Gdk::GC::create(surface);
36 bool MapRenderer::saveAsBitmap(std::string filename)
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");
48 bool MapRenderer::saveViewAsBitmap(std::string filename)
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");
59 MapRenderer::~MapRenderer()
63 void MapRenderer::render(int x, int y, int tileStartX, int tileStartY,
64 int columns, int rows)
66 return render(x, y, tileStartX, tileStartY, columns, rows, d_surface, gc);
69 void MapRenderer::render_tile(Vector<int> draw, Vector<int> tile,
70 Glib::RefPtr<Gdk::Pixmap> surface,
71 Glib::RefPtr<Gdk::GC> context)
73 Player *p = Playerlist::getActiveplayer();
74 if (p->getFogMap()->isCompletelyObscuredFogTile(tile) == true)
78 Maptile *mtile = GameMap::getInstance()->getTile(tile);
80 TileStyle *style = mtile->getTileStyle();
82 printf ("style for tile %d at col=%d,row=%d is null\n",
83 mtile->getMaptileType(), tile.x, tile.y);
86 if (style->getImage() == false)
88 printf ("pic for style %d for tile %d at %d,%d is null\n",
90 mtile->getMaptileType(), tile.x, tile.y);
94 style->getImage()->blit(surface, draw.x, draw.y);
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)
100 GameMap* map = GameMap::getInstance();
101 int width = GameMap::getWidth();
102 int height = GameMap::getHeight();
103 int tilesize = map->getTileset()->getTileSize();
106 Gdk::Color background_color = Gdk::Color();
107 background_color.set_rgb_p(0,0,0);
109 for (int tileY = tileStartY; tileY < (tileStartY + rows); tileY++)
112 for (int tileX = tileStartX; tileX < (tileStartX + columns); tileX++)
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,
121 render_tile(Vector<int>(drawX,drawY), Vector<int>(tileX,tileY),