From ff5326d6e66b1960bfa4f7e2ee3e7a10084b2585 Mon Sep 17 00:00:00 2001 From: grumbel Date: Mon, 14 Dec 2009 05:01:53 +0000 Subject: [PATCH] Added Surface::clone() method git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6214 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/sprite/sprite_data.cpp | 2 +- src/video/surface.cpp | 15 ++++++--------- src/video/surface.hpp | 11 ++++++++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/sprite/sprite_data.cpp b/src/sprite/sprite_data.cpp index 38e58d5fc..4225de00c 100644 --- a/src/sprite/sprite_data.cpp +++ b/src/sprite/sprite_data.cpp @@ -110,7 +110,7 @@ SpriteData::parse_action(const Reader& lisp, const std::string& basedir) float max_w = 0; float max_h = 0; for(int i = 0; static_cast(i) < act_tmp->surfaces.size(); i++) { - SurfacePtr surface(new Surface(*act_tmp->surfaces[i])); + SurfacePtr surface = act_tmp->surfaces[i]->clone(); surface->hflip(); max_w = std::max(max_w, (float) surface->get_width()); max_h = std::max(max_h, (float) surface->get_height()); diff --git a/src/video/surface.cpp b/src/video/surface.cpp index 6df0cf1c3..98bd86809 100644 --- a/src/video/surface.cpp +++ b/src/video/surface.cpp @@ -67,20 +67,17 @@ Surface::Surface(const Surface& rhs) : surface_data = VideoSystem::new_surface_data(*this); } -const Surface& -Surface::operator=(const Surface& rhs) +Surface::~Surface() { - rhs.texture->ref(); + VideoSystem::free_surface_data(surface_data); texture->unref(); - texture = rhs.texture; - rect = rhs.rect; - return *this; } -Surface::~Surface() +SurfacePtr +Surface::clone() const { - VideoSystem::free_surface_data(surface_data); - texture->unref(); + SurfacePtr surface(new Surface(*this)); + return surface; } /** flip the surface horizontally */ diff --git a/src/video/surface.hpp b/src/video/surface.hpp index ad1d9fef6..9523efd7d 100644 --- a/src/video/surface.hpp +++ b/src/video/surface.hpp @@ -42,13 +42,15 @@ private: Rect rect; bool flipx; -public: +private: Surface(const std::string& file); Surface(const std::string& file, const Rect& rect); - Surface(const Surface& other); + Surface(const Surface&); + +public: ~Surface(); - const Surface& operator= (const Surface& other); + SurfacePtr clone() const; /** flip the surface horizontally */ void hflip(); @@ -64,6 +66,9 @@ public: /** returns a vector containing width and height */ Vector get_size() const; + +private: + Surface& operator=(const Surface&); }; #endif -- 2.11.0