initial commit, lordsawar source, slightly modified
[lordsawar] / src / shieldstyle.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 <iostream>
19 #include <sstream>
20 #include "shieldstyle.h"
21 #include "GraphicsCache.h"
22 #include "xmlhelper.h"
23 #include "File.h"
24 #include "shieldset.h"
25 #include "gui/image-helpers.h"
26
27 std::string ShieldStyle::d_tag = "shieldstyle";
28
29 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
30 #define debug(x)
31
32 ShieldStyle::ShieldStyle(ShieldStyle::Type type)
33 {
34   d_type = type;
35   d_image_name = "";
36   d_image = NULL;
37   d_mask = NULL;
38 }
39
40 ShieldStyle::ShieldStyle(XML_Helper* helper)
41   :d_image(0), d_mask(0)
42 {
43   std::string type_str;
44   helper->getData(type_str, "type");
45   d_type = shieldStyleTypeFromString(type_str);
46   helper->getData(d_image_name, "image");
47 }
48
49 ShieldStyle::~ShieldStyle()
50 {
51 }
52
53         
54 std::string ShieldStyle::shieldStyleTypeToString(const ShieldStyle::Type type)
55 {
56   switch (type)
57     {
58       case ShieldStyle::SMALL:
59         return "ShieldStyle::SMALL";
60         break;
61       case ShieldStyle::MEDIUM:
62         return "ShieldStyle::MEDIUM";
63         break;
64       case ShieldStyle::LARGE:
65         return "ShieldStyle::LARGE";
66         break;
67     }
68   return "ShieldStyle::SMALL";
69 }
70
71 ShieldStyle::Type ShieldStyle::shieldStyleTypeFromString(const std::string str)
72 {
73   if (str.size() > 0 && isdigit(str.c_str()[0]))
74     return ShieldStyle::Type(atoi(str.c_str()));
75   if (str == "ShieldStyle::SMALL")
76     return ShieldStyle::SMALL;
77   else if (str == "ShieldStyle::MEDIUM")
78     return ShieldStyle::MEDIUM;
79   else if (str == "ShieldStyle::LARGE")
80     return ShieldStyle::LARGE;
81   return ShieldStyle::SMALL;
82 }
83
84 bool ShieldStyle::save(XML_Helper *helper) const
85 {
86   bool retval = true;
87
88   retval &= helper->openTag(d_tag);
89   std::string s = shieldStyleTypeToString(ShieldStyle::Type(d_type));
90   retval &= helper->saveData("type", s);
91   retval &= helper->saveData("image", d_image_name);
92   retval &= helper->closeTag();
93   return retval;
94 }
95 void ShieldStyle::instantiateImages(std::string filename, Shieldset *s)
96 {
97   if (filename.empty() == true)
98     return;
99   // The shield image consists of two halves. On the left is the shield 
100   // image, on the right the mask.
101   debug("loading shield file: " << filename);
102   std::vector<PixMask* > half = disassemble_row(filename, 2);
103   if (half[0] == NULL)
104     debug("failed to load image file");
105
106   int xsize = 0;
107   int ysize = 0;
108   switch (getType())
109     {
110     case ShieldStyle::SMALL:
111       xsize = s->getSmallWidth(); ysize = s->getSmallHeight(); break;
112     case ShieldStyle::MEDIUM:
113       xsize = s->getMediumWidth(); ysize = s->getMediumHeight(); break;
114     case ShieldStyle::LARGE:
115       xsize = s->getLargeWidth(); ysize = s->getLargeHeight(); break;
116     }
117   PixMask::scale(half[0], xsize, ysize);
118   PixMask::scale(half[1], xsize, ysize);
119   setImage(half[0]);
120   setMask(half[1]);
121 }
122
123 void ShieldStyle::uninstantiateImages()
124 {
125   if (getImage() != NULL)
126     {
127       delete getImage();
128       setImage(NULL);
129     }
130   if (getMask() != NULL)
131     {
132       delete getMask();
133       setMask(NULL);
134     }
135 }
136