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 ) {
scene_id = id;
}
void RenderObject::copyTo(std::shared_ptr<RenderObject> other) {
void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
other->collisions.clear();
for ( auto &colider : collisions ) {
other->collisions.push_back(colider->copySelf());
other->collisions.push_back( colider->copySelf() );
}
other->texture.reset();
if ( texture ) {
@ -119,4 +119,7 @@ void RenderObject::copyTo(std::shared_ptr<RenderObject> other) {
}
other->colidedWith.clear();
}
void RenderObject::saveCurTexture() {
cur_texture = texture;
}
} // namespace SDLPP

View File

@ -78,11 +78,13 @@ public:
bool isStatic();
void setStatic( bool stat = true );
std::shared_ptr< Renderer > getRenderer() const;
void saveCurTexture();
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::shared_ptr< Texture > texture;
std::shared_ptr< Texture > cur_texture;
std::shared_ptr< Renderer > renderer;
std::shared_ptr< CollisionPolygon > polygon;
double movementSpeed = 0;

View File

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

View File

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