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, 2010, 2012, 2014, 2015 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 "MapRenderer.h"
22 #include "army.h"
23 #include "GameMap.h"
24 #include "player.h"
25 #include "FogMap.h"
26 #include "ImageCache.h"
27 #include "playerlist.h"
28 #include "File.h"
29
MapRenderer(Cairo::RefPtr<Cairo::Surface> surface)30 MapRenderer::MapRenderer(Cairo::RefPtr<Cairo::Surface> surface)
31 {
32 d_surface = surface;
33 gc = Cairo::Context::create(surface);
34 }
35
saveAsBitmap(Glib::ustring filename)36 bool MapRenderer::saveAsBitmap(Glib::ustring filename)
37 {
38 int tilesize = GameMap::getInstance()->getTileSize();
39 int width = GameMap::getWidth() * tilesize;
40 int height = GameMap::getHeight() * tilesize;
41 Cairo::RefPtr<Cairo::Surface> empty = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, width, height);
42 Cairo::RefPtr<Cairo::Surface> surf = Cairo::Surface::create(empty, Cairo::CONTENT_COLOR_ALPHA, width, height);
43 render(0, 0, 0, 0, GameMap::getWidth(), GameMap::getHeight(), surf, Cairo::Context::create(surf));
44 Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create(surf, 0, 0, width, height);
45 pixbuf->save (filename, "png");
46 return true;
47 }
48
render(int x,int y,int tileStartX,int tileStartY,int columns,int rows)49 void MapRenderer::render(int x, int y, int tileStartX, int tileStartY,
50 int columns, int rows)
51 {
52 return render(x, y, tileStartX, tileStartY, columns, rows, d_surface, gc);
53 }
54
render_tile(Vector<int> draw,Vector<int> tile,Cairo::RefPtr<Cairo::Surface> surface)55 void MapRenderer::render_tile(Vector<int> draw, Vector<int> tile,
56 Cairo::RefPtr<Cairo::Surface> surface)
57 {
58 Player *p = Playerlist::getActiveplayer();
59 if (p->getFogMap()->isCompletelyObscuredFogTile(tile) == true)
60 return;
61
62 // get correct tile
63 Maptile *mtile = GameMap::getInstance()->getTile(tile);
64
65 TileStyle *style = mtile->getTileStyle();
66 bool use_default_pic = false;
67 if (style == NULL)
68 {
69 printf ("style for tile %d at col=%d,row=%d is null\n",
70 mtile->getType(), tile.x, tile.y);
71 use_default_pic = true;
72 }
73 else
74 {
75 if (style->getImage() == NULL)
76 {
77 printf ("pic for style %d for tile %d at %d,%d is null\n",
78 style->getType(), mtile->getType(), tile.x, tile.y);
79 use_default_pic = true;
80 }
81 }
82
83 if (use_default_pic == true)
84 {
85 guint32 type = TileStyle::OTHER;
86 if (style)
87 type = style->getType();
88 int tilesize = GameMap::getInstance()->getTileSize();
89 PixMask *img =
90 ImageCache::getInstance()->getDefaultTileStylePic(type,
91 tilesize);
92 if (img)
93 img->blit(surface, draw.x, draw.y);
94 }
95 else
96 style->getImage()->blit(surface, draw.x, draw.y);
97 }
98
render(int x,int y,int tileStartX,int tileStartY,int columns,int rows,Cairo::RefPtr<Cairo::Surface> surface,Cairo::RefPtr<Cairo::Context> context)99 void MapRenderer::render(int x, int y, int tileStartX, int tileStartY,
100 int columns, int rows, Cairo::RefPtr<Cairo::Surface> surface, Cairo::RefPtr<Cairo::Context> context)
101 {
102 int width = GameMap::getWidth();
103 int height = GameMap::getHeight();
104 int tilesize = GameMap::getInstance()->getTileSize();
105 int drawY = y;
106
107 for (int tileY = tileStartY; tileY < (tileStartY + rows); tileY++)
108 {
109 int drawX = x;
110 for (int tileX = tileStartX; tileX < (tileStartX + columns); tileX++)
111 {
112 // first check if we're out of the map bounds
113 if (tileX >= width || tileY >= height)
114 {
115 context->set_source_rgba(FOG_COLOUR.get_red(), FOG_COLOUR.get_blue(), FOG_COLOUR.get_green(), FOG_COLOUR.get_alpha());
116 context->rectangle(drawX, drawY, tilesize, tilesize);
117 context->fill();
118 }
119 else
120 render_tile(Vector<int>(drawX,drawY), Vector<int>(tileX,tileY),
121 surface);
122
123 drawX += tilesize;
124 }
125 drawY += tilesize;
126 }
127
128 }
129 // End of file
130