4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
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 General Public License for more details.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <SDL_image.h>
32 #include "gameconfig.hpp"
33 #include "physfs/physfs_sdl.hpp"
34 #include "video/surface.hpp"
35 #include "image_texture.hpp"
36 #include "texture_manager.hpp"
38 Surface::Surface(const std::string& file)
40 texture = texture_manager->get(file);
44 uv_right = texture->get_uv_right();
45 uv_bottom = texture->get_uv_bottom();
47 width = texture->get_image_width();
48 height = texture->get_image_height();
51 Surface::Surface(const std::string& file, int x, int y, int w, int h)
53 texture = texture_manager->get(file);
56 float tex_w = static_cast<float> (texture->get_width());
57 float tex_h = static_cast<float> (texture->get_height());
58 uv_left = static_cast<float>(x) / tex_w;
59 uv_top = static_cast<float>(y) / tex_h;
60 uv_right = static_cast<float>(x+w) / tex_w;
61 uv_bottom = static_cast<float>(y+h) / tex_h;
67 Surface::Surface(const Surface& other)
69 texture = other.texture;
72 uv_left = other.uv_left;
73 uv_top = other.uv_top;
74 uv_right = other.uv_right;
75 uv_bottom = other.uv_bottom;
77 height = other.height;
81 Surface::operator= (const Surface& other)
85 texture = other.texture;
87 uv_left = other.uv_left;
88 uv_top = other.uv_top;
89 uv_right = other.uv_right;
90 uv_bottom = other.uv_bottom;
92 height = other.height;
105 std::swap(uv_left, uv_right);
108 static inline void intern_draw(float left, float top, float right, float bottom, float uv_left, float uv_top,
109 float uv_right, float uv_bottom,
110 DrawingEffect effect)
112 if(effect & HORIZONTAL_FLIP)
113 std::swap(uv_left, uv_right);
114 if(effect & VERTICAL_FLIP) {
115 std::swap(uv_top, uv_bottom);
119 glTexCoord2f(uv_left, uv_top);
120 glVertex2f(left, top);
122 glTexCoord2f(uv_right, uv_top);
123 glVertex2f(right, top);
125 glTexCoord2f(uv_right, uv_bottom);
126 glVertex2f(right, bottom);
128 glTexCoord2f(uv_left, uv_bottom);
129 glVertex2f(left, bottom);
134 Surface::draw(float x, float y, float alpha, DrawingEffect effect) const
136 glColor4f(1.0f, 1.0f, 1.0f, alpha);
137 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
140 x + width, y + height,
141 uv_left, uv_top, uv_right, uv_bottom, effect);
145 Surface::draw_part(float src_x, float src_y, float dst_x, float dst_y,
146 float width, float height, float alpha,
147 DrawingEffect effect) const
149 float uv_width = uv_right - uv_left;
150 float uv_height = uv_bottom - uv_top;
152 float uv_left = this->uv_left + (uv_width * src_x) / this->width;
153 float uv_top = this->uv_top + (uv_height * src_y) / this->height;
154 float uv_right = this->uv_left + (uv_width * (src_x + width)) / this->width;
155 float uv_bottom = this->uv_top + (uv_height * (src_y + height)) / this->height;
157 glColor4f(1.0f, 1.0f, 1.0f, alpha);
158 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
160 intern_draw(dst_x, dst_y,
161 dst_x + width, dst_y + height,
162 uv_left, uv_top, uv_right, uv_bottom, effect);