| 1 | // SuperTux |
| 2 | // Copyright (C) 2006 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 3 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, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | #include "video/surface.hpp" |
| 18 | |
| 19 | #include <sstream> |
| 20 | |
| 21 | #include "util/reader_document.hpp" |
| 22 | #include "util/reader_mapping.hpp" |
| 23 | #include "util/string_util.hpp" |
| 24 | #include "video/texture.hpp" |
| 25 | #include "video/texture_manager.hpp" |
| 26 | #include "video/video_system.hpp" |
| 27 | |
| 28 | SurfacePtr |
| 29 | Surface::from_reader(const ReaderMapping& mapping, const boost::optional<Rect>& rect) |
| 30 | { |
| 31 | TexturePtr diffuse_texture; |
| 32 | boost::optional<ReaderMapping> diffuse_texture_mapping; |
| 33 | if (mapping.get("diffuse-texture" , diffuse_texture_mapping)) |
| 34 | { |
| 35 | diffuse_texture = TextureManager::current()->get(*diffuse_texture_mapping, rect); |
| 36 | } |
| 37 | |
| 38 | TexturePtr displacement_texture; |
| 39 | boost::optional<ReaderMapping> displacement_texture_mapping; |
| 40 | if (mapping.get("displacement-texture" , displacement_texture_mapping)) |
| 41 | { |
| 42 | displacement_texture = TextureManager::current()->get(*displacement_texture_mapping, rect); |
| 43 | } |
| 44 | |
| 45 | Flip flip = NO_FLIP; |
| 46 | std::vector<bool> flip_v; |
| 47 | if (mapping.get("flip" , flip_v)) |
| 48 | { |
| 49 | flip ^= flip_v[0] ? HORIZONTAL_FLIP : NO_FLIP; |
| 50 | flip ^= flip_v[1] ? VERTICAL_FLIP : NO_FLIP; |
| 51 | } |
| 52 | |
| 53 | return SurfacePtr(new Surface(diffuse_texture, displacement_texture, flip)); |
| 54 | } |
| 55 | |
| 56 | SurfacePtr |
| 57 | Surface::from_file(const std::string& filename, const boost::optional<Rect>& rect) |
| 58 | { |
| 59 | if (StringUtil::has_suffix(filename, ".surface" )) |
| 60 | { |
| 61 | ReaderDocument doc = ReaderDocument::from_file(filename); |
| 62 | ReaderObject object = doc.get_root(); |
| 63 | if (object.get_name() != "supertux-surface" ) |
| 64 | { |
| 65 | std::ostringstream msg; |
| 66 | msg << filename << ": error: not a 'supertux-surface' file" ; |
| 67 | throw std::runtime_error(msg.str()); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | return Surface::from_reader(object.get_mapping(), rect); |
| 72 | } |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | if (rect) |
| 77 | { |
| 78 | TexturePtr texture = TextureManager::current()->get(filename, *rect); |
| 79 | return SurfacePtr(new Surface(texture, TexturePtr(), NO_FLIP)); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | TexturePtr texture = TextureManager::current()->get(filename); |
| 84 | return SurfacePtr(new Surface(texture, TexturePtr(), NO_FLIP)); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | Surface::Surface(const TexturePtr& diffuse_texture, |
| 90 | const TexturePtr& displacement_texture, |
| 91 | Flip flip) : |
| 92 | m_diffuse_texture(diffuse_texture), |
| 93 | m_displacement_texture(displacement_texture), |
| 94 | m_region(0, 0, m_diffuse_texture->get_image_width(), m_diffuse_texture->get_image_height()), |
| 95 | m_flip(flip) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | Surface::Surface(const TexturePtr& diffuse_texture, |
| 100 | const TexturePtr& displacement_texture, |
| 101 | const Rect& region, |
| 102 | Flip flip) : |
| 103 | m_diffuse_texture(diffuse_texture), |
| 104 | m_displacement_texture(displacement_texture), |
| 105 | m_region(region), |
| 106 | m_flip(flip) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | SurfacePtr |
| 111 | Surface::from_texture(const TexturePtr& texture) |
| 112 | { |
| 113 | return SurfacePtr(new Surface(texture, TexturePtr(), NO_FLIP)); |
| 114 | } |
| 115 | |
| 116 | Surface::~Surface() |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | SurfacePtr |
| 121 | Surface::clone(Flip flip) const |
| 122 | { |
| 123 | SurfacePtr surface(new Surface(m_diffuse_texture, |
| 124 | m_displacement_texture, |
| 125 | m_region, |
| 126 | m_flip ^ flip)); |
| 127 | return surface; |
| 128 | } |
| 129 | |
| 130 | SurfacePtr |
| 131 | Surface::region(const Rect& rect) const |
| 132 | { |
| 133 | SurfacePtr surface(new Surface(m_diffuse_texture, |
| 134 | m_displacement_texture, |
| 135 | rect, |
| 136 | m_flip)); |
| 137 | return surface; |
| 138 | } |
| 139 | |
| 140 | TexturePtr |
| 141 | Surface::get_texture() const |
| 142 | { |
| 143 | return m_diffuse_texture; |
| 144 | } |
| 145 | |
| 146 | TexturePtr |
| 147 | Surface::get_displacement_texture() const |
| 148 | { |
| 149 | return m_displacement_texture; |
| 150 | } |
| 151 | |
| 152 | int |
| 153 | Surface::get_width() const |
| 154 | { |
| 155 | return m_region.get_width(); |
| 156 | } |
| 157 | |
| 158 | int |
| 159 | Surface::get_height() const |
| 160 | { |
| 161 | return m_region.get_height(); |
| 162 | } |
| 163 | |
| 164 | /* EOF */ |
| 165 | |