From b2c7b81400ed7bb2d9cc4c8605fc27f14253de8c Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Fri, 3 Sep 2004 21:42:00 +0000 Subject: [PATCH] Cleanup of surface class: trivial functions should be inline, and a bit of reorganization. --- src/sdl_utils.cpp | 11 ---------- src/sdl_utils.hpp | 53 ++++++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/sdl_utils.cpp b/src/sdl_utils.cpp index d32d67d44b0..3fd1d3eaebe 100644 --- a/src/sdl_utils.cpp +++ b/src/sdl_utils.cpp @@ -57,17 +57,6 @@ namespace { } -void free_sdl_surface::operator()(SDL_Surface* surf) const -{ - if(surf != NULL) - SDL_FreeSurface(surf); -} - -surface::surface(SDL_Surface* surf) : surface_(surf) -{ -} - - surface make_neutral_surface(surface surf) { if(surf == NULL) { diff --git a/src/sdl_utils.hpp b/src/sdl_utils.hpp index 5be73a6fb32..2612906d07e 100644 --- a/src/sdl_utils.hpp +++ b/src/sdl_utils.hpp @@ -36,62 +36,63 @@ bool point_in_rect(int x, int y, const SDL_Rect& rect); bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2); -struct free_sdl_surface { - void operator()(SDL_Surface* surface) const; -}; - - struct surface { private: - inline int sdl_add_ref(SDL_Surface* surf); + static void sdl_add_ref(SDL_Surface *surf) + { + if (surf != NULL) + ++surf->refcount; + } + + struct free_sdl_surface { + void operator()(SDL_Surface *surf) const + { + if (surf != NULL) + SDL_FreeSurface(surf); + } + }; + typedef util::scoped_resource scoped_sdl_surface; public: surface() : surface_(NULL) {} - surface(SDL_Surface* surf); + surface(SDL_Surface *surf) : surface_(surf) + {} surface(const surface& o) : surface_(o.surface_.get()) { - sdl_add_ref(get()); + sdl_add_ref(surface_.get()); + } + + void assign(const surface& o) + { + SDL_Surface *surf = o.surface_.get(); + sdl_add_ref(surf); // need to be done before assign to avoid corruption on "a=a;" + surface_.assign(surf); } surface& operator=(const surface& o) { - surface_.assign(o.surface_.get()); - sdl_add_ref(get()); + assign(o); return *this; } - operator SDL_Surface*() const { return surface_; } + operator SDL_Surface*() const { return surface_.get(); } SDL_Surface* get() const { return surface_.get(); } SDL_Surface* operator->() const { return surface_.get(); } - void assign(const surface& o) - { - operator=(o); - } - void assign(SDL_Surface* surf) { surface_.assign(surf); } - bool null() const { return get() == NULL; } + bool null() const { return surface_.get() == NULL; } private: scoped_sdl_surface surface_; }; -int surface::sdl_add_ref(SDL_Surface* surf) -{ - if(surf != NULL) { - return surf->refcount++; - } else { - return 0; - } -} - bool operator<(const surface& a, const surface& b); surface make_neutral_surface(surface surf);