Skip to content

Commit

Permalink
Allow any surface to create its own SDL software (surface) renderer
Browse files Browse the repository at this point in the history
The surface object is in charge of the life of the renderer.
  • Loading branch information
Vultraz committed Sep 13, 2016
1 parent 3f3e098 commit dfba435
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
6 changes: 1 addition & 5 deletions src/gui/core/canvas.cpp
Expand Up @@ -1407,8 +1407,6 @@ tcanvas::tcanvas()

tcanvas::~tcanvas()
{
SDL_DestroyRenderer(renderer_);

}

void tcanvas::draw(const bool force)
Expand All @@ -1429,9 +1427,7 @@ void tcanvas::draw(const bool force)
DBG_GUI_D << "Canvas: create new empty canvas.\n";
canvas_.assign(create_neutral_surface(w_, h_));

SDL_DestroyRenderer(renderer_);

renderer_ = SDL_CreateSoftwareRenderer(canvas_);
renderer_ = canvas_.create_renderer();
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);

// draw items
Expand Down
27 changes: 24 additions & 3 deletions src/sdl/utils.hpp
Expand Up @@ -52,20 +52,21 @@ SDLKey sdl_keysym_from_name(std::string const &keyname);
class surface
{
public:
surface() : surface_(nullptr)
surface() : surface_(nullptr), renderer_(nullptr)
{}

surface(SDL_Surface* surf) : surface_(surf)
surface(SDL_Surface* surf) : surface_(surf), renderer_(nullptr)
{}

surface(const surface& s) : surface_(s.get())
surface(const surface& s) : surface_(s.get()), renderer_(nullptr)
{
add_surface_ref(surface_);
}

~surface()
{
free_surface();
destroy_renderer();
}

void assign(SDL_Surface* surf)
Expand All @@ -84,6 +85,12 @@ class surface
return *this;
}

SDL_Renderer* create_renderer()
{
renderer_ = SDL_CreateSoftwareRenderer(surface_);
return renderer_;
}

operator SDL_Surface*() const { return surface_; }

SDL_Surface* get() const { return surface_; }
Expand All @@ -105,6 +112,12 @@ class surface
add_surface_ref(surf); // Needs to be done before assignment to avoid corruption on "a = a;"
free_surface();
surface_ = surf;

// Reassign renderer if it exists
if(renderer_) {
destroy_renderer();
create_renderer();
}
}

void free_surface()
Expand All @@ -114,7 +127,15 @@ class surface
}
}

void destroy_renderer()
{
if(renderer_) {
SDL_DestroyRenderer(renderer_);
}
}

SDL_Surface* surface_;
SDL_Renderer* renderer_;
};

bool operator<(const surface& a, const surface& b);
Expand Down

0 comments on commit dfba435

Please sign in to comment.