SDLPP: Save texture before rendering

In some cases texture might be changed after render() has been called
but before renderer presented the render, causing errors.
This commit is contained in:
zv0n 2021-01-30 21:24:54 +01:00
parent b65b0bf034
commit 1f7a80d7d2
4 changed files with 11 additions and 4 deletions

View File

@ -104,10 +104,10 @@ std::shared_ptr< Renderer > RenderObject::getRenderer() const {
void RenderObject::setSceneID( int id ) { void RenderObject::setSceneID( int id ) {
scene_id = id; scene_id = id;
} }
void RenderObject::copyTo(std::shared_ptr<RenderObject> other) { void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
other->collisions.clear(); other->collisions.clear();
for ( auto &colider : collisions ) { for ( auto &colider : collisions ) {
other->collisions.push_back(colider->copySelf()); other->collisions.push_back( colider->copySelf() );
} }
other->texture.reset(); other->texture.reset();
if ( texture ) { if ( texture ) {
@ -119,4 +119,7 @@ void RenderObject::copyTo(std::shared_ptr<RenderObject> other) {
} }
other->colidedWith.clear(); other->colidedWith.clear();
} }
void RenderObject::saveCurTexture() {
cur_texture = texture;
}
} // namespace SDLPP } // namespace SDLPP

View File

@ -78,11 +78,13 @@ public:
bool isStatic(); bool isStatic();
void setStatic( bool stat = true ); void setStatic( bool stat = true );
std::shared_ptr< Renderer > getRenderer() const; std::shared_ptr< Renderer > getRenderer() const;
void saveCurTexture();
protected: protected:
virtual void copyTo(std::shared_ptr<RenderObject> other); virtual void copyTo( std::shared_ptr< RenderObject > other );
std::vector< std::shared_ptr< CollisionPolygon > > collisions; std::vector< std::shared_ptr< CollisionPolygon > > collisions;
std::shared_ptr< Texture > texture; std::shared_ptr< Texture > texture;
std::shared_ptr< Texture > cur_texture;
std::shared_ptr< Renderer > renderer; std::shared_ptr< Renderer > renderer;
std::shared_ptr< CollisionPolygon > polygon; std::shared_ptr< CollisionPolygon > polygon;
double movementSpeed = 0; double movementSpeed = 0;

View File

@ -121,6 +121,7 @@ void Scene::renderScene( bool clear_renderer ) {
SDL_RenderCopy( renderer->getRendererPtr(), background->getTexturePtr(), SDL_RenderCopy( renderer->getRendererPtr(), background->getTexturePtr(),
NULL, NULL ); NULL, NULL );
for ( const auto &x : render_objects ) { for ( const auto &x : render_objects ) {
x->saveCurTexture();
x->render(); x->render();
} }
} }

View File

@ -55,6 +55,7 @@ Texture::Texture( std::shared_ptr< Renderer > &renderer, Font &font,
setTextureFromSurface( renderer, surface ); setTextureFromSurface( renderer, surface );
} }
Texture::~Texture() { Texture::~Texture() {
if ( texture != nullptr )
SDL_DestroyTexture( texture ); SDL_DestroyTexture( texture );
} }
SDL_Texture *Texture::getTexturePtr() { SDL_Texture *Texture::getTexturePtr() {