initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / tile-preview-scene.cpp
1 //  Copyright (C) 2008, 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include <config.h>
19
20 #include <sigc++/functors/mem_fun.h>
21
22 #include "tile-preview-scene.h"
23
24 #include "glade-helpers.h"
25 #include "gui/image-helpers.h"
26 #include "ucompose.hpp"
27 #include "defs.h"
28 #include "File.h"
29
30
31 TilePreviewScene::TilePreviewScene (Tile *tile, 
32                                     std::vector<PixMask* > 
33                                       standard_images, 
34                                     guint32 height, guint32 width, 
35                                     std::string scene)
36 {
37   std::list<TileStyle::Type> tilescene;
38   for (const char *letter = scene.c_str(); *letter != '\0'; letter++)
39     if (*letter - 'a' >=0 && *letter - 'a' <= TileStyle::OTHER)
40       tilescene.push_back(TileStyle::Type(*letter - 'a'));
41
42   if (height * width != tilescene.size())
43     return;
44
45   d_tile = tile;
46   d_standard_images = standard_images;
47   d_height = height;
48   d_width = width;
49   d_model = tilescene;
50   regenerate();
51 }
52
53 Glib::RefPtr<Gdk::Pixbuf> TilePreviewScene::getTileStylePixbuf(int x, int y)
54 {
55   return d_view[x * d_width + y];
56 }
57   
58 TileStyle* TilePreviewScene::getTileStyle(int x, int y)
59 {
60   return d_tilestyles[x * d_width + y];
61 }
62   
63 void TilePreviewScene::regenerate()
64 {
65   //populate d_view
66   d_view.clear();
67   for (std::list<TileStyle::Type>::iterator it = d_model.begin(); 
68        it != d_model.end(); it++)
69     {
70       TileStyle::Type type = *it;
71       TileStyle *tilestyle = NULL;
72       if (d_tile)
73         {
74           tilestyle = d_tile->getRandomTileStyle(type);
75           d_tilestyles.push_back(tilestyle);
76         }
77
78       if (tilestyle)
79         d_view.push_back(tilestyle->getImage()->to_pixbuf());
80       else
81         d_view.push_back(d_standard_images[type]->to_pixbuf());
82     }
83 }
84   
85 Glib::RefPtr<Gdk::Pixbuf> TilePreviewScene::renderScene(guint32 tilesize)
86 {
87   Glib::RefPtr<Gdk::Pixbuf> dest;
88   dest = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB,true, 8, (int)(d_height * tilesize), (int)(d_width * tilesize));
89   for (unsigned int i = 0; i < d_width; i++)
90     for (unsigned int j = 0; j < d_height; j++)
91       {
92         getTileStylePixbuf(i,j)->copy_area (0, 0, tilesize, tilesize, dest, j * tilesize, i *tilesize);
93       }
94   return dest;
95 }