From: Ingo Ruhnke Date: Mon, 14 Dec 2009 05:01:53 +0000 (+0000) Subject: Added Surface::clone() method X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ac68ad04a541a6089afa1a4a7f0768cab6280e46;p=supertux.git Added Surface::clone() method SVN-Revision: 6214 --- 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