1 /*
2 Copyright (C) 2005 Matthias Braun <matze@braunis.de>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /**
20 * @author Matthias Braun
21 * @file Image.cpp
22 */
23
24 #include <config.h>
25
26 #include "Image.hpp"
27 #include "XmlReader.hpp"
28 #include "TextureManager.hpp"
29 #include "ComponentFactory.hpp"
30 #include "Painter.hpp"
31
Image()32 Image::Image()
33 : texture(0)
34 {
35 }
36
~Image()37 Image::~Image()
38 {
39 }
40
41 void
parse(XmlReader & reader)42 Image::parse(XmlReader& reader)
43 {
44 bool resizable = false;
45
46 bool grey = false;
47
48 XmlReader::AttributeIterator iter(reader);
49 while(iter.next()) {
50 const char* attribute = (const char*) iter.getName();
51 const char* value = (const char*) iter.getValue();
52
53 if(parseAttribute(attribute, value)) {
54 continue;
55 } else if(strcmp(attribute, "width") == 0) {
56 if(sscanf(value, "%f", &width) != 1) {
57 std::stringstream msg;
58 msg << "Couldn't parse width '" << value << "'.";
59 throw std::runtime_error(msg.str());
60 }
61 } else if(strcmp(attribute, "height") == 0) {
62 if(sscanf(value, "%f", &height) != 1) {
63 std::stringstream msg;
64 msg << "Couldn't parse height '" << value << "'.";
65 throw std::runtime_error(msg.str());
66 }
67 } else if(strcmp(attribute, "src") == 0) {
68 filename=value;
69 } else if(strcmp(attribute, "filter") == 0) {
70 if(strcmp(value, "grey") == 0) {
71 grey = true;
72 } else if(strcmp(value, "no") == 0) {
73 grey = false;
74 } else {
75 std::cerr << "Unknown filter value '" << value << "'.\n";
76 std::cerr << "Should be 'grey' or 'no'.\n";
77 }
78 } else if(strcmp(attribute, "resizable") == 0) {
79 if(strcmp(value, "yes") == 0)
80 resizable = true;
81 else if(strcmp(value, "no") == 0)
82 resizable = false;
83 else
84 std::cerr
85 << "You should specify 'yes' or 'no' for the resizable"
86 << "attribute\n";
87 } else {
88 std::cerr << "Skipping unknown attribute '"
89 << attribute << "'.\n";
90 }
91 }
92
93 if(filename == "")
94 throw std::runtime_error("No filename specified for image");
95
96 texture = 0;
97 texture = texture_manager->load(filename,
98 grey ? TextureManager::FILTER_GREY : TextureManager::NO_FILTER);
99
100 if(width <= 0 || height <= 0) {
101 width = texture->getWidth();
102 height = texture->getHeight();
103 }
104
105 if(resizable)
106 flags |= FLAG_RESIZABLE;
107 }
108
109 void
resize(float width,float height)110 Image::resize(float width, float height)
111 {
112 this->width = width;
113 this->height = height;
114 }
115
116 void
draw(Painter & painter)117 Image::draw(Painter& painter)
118 {
119 if(width != texture->getWidth() || height != texture->getHeight())
120 painter.drawStretchTexture(texture, Rect2D(0, 0, width, height));
121 else
122 painter.drawTexture(texture, Vector2(0, 0));
123 }
124
getFilename() const125 std::string Image::getFilename() const
126 {
127 return filename;
128 }
129
setFile(const std::string & pfilename)130 void Image::setFile(const std::string &pfilename)
131 {
132 filename=pfilename;
133 texture = 0;
134 texture = texture_manager->load(pfilename);
135 }
136
137 IMPLEMENT_COMPONENT_FACTORY(Image);
138
139