-// $Id: surface.cpp 2175 2004-11-24 19:02:49Z sik0fewl $
-//
// SuperTux
-// Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-// 02111-1307, USA.
-#include <config.h>
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
-#include <cassert>
-#include <iostream>
-#include <algorithm>
-#include <stdexcept>
-#include <sstream>
-#include <math.h>
+#include "video/surface.hpp"
+
+#include <config.h>
#include <SDL.h>
-#include <SDL_image.h>
-#include "gameconfig.hpp"
-#include "physfs/physfs_sdl.hpp"
-#include "video/surface.hpp"
-#include "video/screen.hpp"
-#include "image_texture.hpp"
-#include "texture_manager.hpp"
+#include "video/texture.hpp"
+#include "video/video_system.hpp"
-Surface::Surface(const std::string& file)
+SurfacePtr
+Surface::create(const std::string& file)
{
- texture = texture_manager->get(file);
- texture->ref();
- uv_left = 0;
- uv_top = 0;
- uv_right = texture->get_uv_right();
- uv_bottom = texture->get_uv_bottom();
-
- width = texture->get_image_width();
- height = texture->get_image_height();
+ return SurfacePtr(new Surface(file));
}
-Surface::Surface(const std::string& file, int x, int y, int w, int h)
+SurfacePtr
+Surface::create(const std::string& file, const Rect& rect)
{
- texture = texture_manager->get(file);
- texture->ref();
+ return SurfacePtr(new Surface(file, rect));
+}
- float tex_w = static_cast<float> (texture->get_width());
- float tex_h = static_cast<float> (texture->get_height());
- uv_left = static_cast<float>(x) / tex_w;
- uv_top = static_cast<float>(y) / tex_h;
- uv_right = static_cast<float>(x+w) / tex_w;
- uv_bottom = static_cast<float>(y+h) / tex_h;
+Surface::Surface(const std::string& file) :
+ texture(TextureManager::current()->get(file)),
+ surface_data(),
+ rect(0, 0,
+ Size(texture->get_image_width(),
+ texture->get_image_height())),
+ flipx(false)
+{
+ surface_data = VideoSystem::current()->new_surface_data(*this);
+}
- width = w;
- height = h;
+Surface::Surface(const std::string& file, const Rect& rect_) :
+ texture(TextureManager::current()->get(file, rect_)),
+ surface_data(),
+ rect(0, 0, Size(rect_.get_width(), rect_.get_height())),
+ flipx(false)
+{
+ surface_data = VideoSystem::current()->new_surface_data(*this);
}
-Surface::Surface(const Surface& other)
+Surface::Surface(const Surface& rhs) :
+ texture(rhs.texture),
+ surface_data(),
+ rect(rhs.rect),
+ flipx(false)
{
- texture = other.texture;
- texture->ref();
+ surface_data = VideoSystem::current()->new_surface_data(*this);
+}
- uv_left = other.uv_left;
- uv_top = other.uv_top;
- uv_right = other.uv_right;
- uv_bottom = other.uv_bottom;
- width = other.width;
- height = other.height;
+Surface::~Surface()
+{
+ VideoSystem::current()->free_surface_data(surface_data);
}
-const Surface&
-Surface::operator= (const Surface& other)
+SurfacePtr
+Surface::clone() const
{
- other.texture->ref();
- texture->unref();
- texture = other.texture;
+ SurfacePtr surface(new Surface(*this));
+ return surface;
+}
- uv_left = other.uv_left;
- uv_top = other.uv_top;
- uv_right = other.uv_right;
- uv_bottom = other.uv_bottom;
- width = other.width;
- height = other.height;
+/** flip the surface horizontally */
+void Surface::hflip()
+{
+ flipx = !flipx;
+}
- return *this;
+bool Surface::get_flipx() const
+{
+ return flipx;
}
-Surface::~Surface()
+TexturePtr
+Surface::get_texture() const
{
- texture->unref();
+ return texture;
}
-void
-Surface::hflip()
+SurfaceData*
+Surface::get_surface_data() const
{
- std::swap(uv_left, uv_right);
+ return surface_data;
}
-static inline void intern_draw(float left, float top, float right, float bottom, float uv_left, float uv_top,
- float uv_right, float uv_bottom,
- DrawingEffect effect)
+int
+Surface::get_x() const
{
- if(effect & HORIZONTAL_FLIP)
- std::swap(uv_left, uv_right);
- if(effect & VERTICAL_FLIP) {
- std::swap(uv_top, uv_bottom);
- }
-
- glBegin(GL_QUADS);
- glTexCoord2f(uv_left, uv_top);
- glVertex2f(left, top);
-
- glTexCoord2f(uv_right, uv_top);
- glVertex2f(right, top);
+ return rect.left;
+}
- glTexCoord2f(uv_right, uv_bottom);
- glVertex2f(right, bottom);
+int
+Surface::get_y() const
+{
+ return rect.top;
+}
- glTexCoord2f(uv_left, uv_bottom);
- glVertex2f(left, bottom);
- glEnd();
+int
+Surface::get_width() const
+{
+ return rect.get_width();
}
-void
-Surface::draw(float x, float y, float alpha, DrawingEffect effect) const
+int
+Surface::get_height() const
{
- glColor4f(1.0f, 1.0f, 1.0f, alpha);
- glBindTexture(GL_TEXTURE_2D, texture->get_handle());
+ return rect.get_height();
+}
- intern_draw(x, y,
- x + width, y + height,
- uv_left, uv_top, uv_right, uv_bottom, effect);
+Vector
+Surface::get_position() const
+{
+ return Vector(get_x(), get_y());
}
-void
-Surface::draw_part(float src_x, float src_y, float dst_x, float dst_y,
- float width, float height, float alpha,
- DrawingEffect effect) const
+Vector
+Surface::get_size() const
{
- float uv_width = uv_right - uv_left;
- float uv_height = uv_bottom - uv_top;
-
- float uv_left = this->uv_left + (uv_width * src_x) / this->width;
- float uv_top = this->uv_top + (uv_height * src_y) / this->height;
- float uv_right = this->uv_left + (uv_width * (src_x + width)) / this->width;
- float uv_bottom = this->uv_top + (uv_height * (src_y + height)) / this->height;
-
- glColor4f(1.0f, 1.0f, 1.0f, alpha);
- glBindTexture(GL_TEXTURE_2D, texture->get_handle());
-
- intern_draw(dst_x, dst_y,
- dst_x + width, dst_y + height,
- uv_left, uv_top, uv_right, uv_bottom, effect);
+ return Vector(get_width(), get_height());
}
+/* EOF */