SDLPP: make copySelf actually copy everything

This commit is contained in:
zv0n 2020-12-18 16:02:02 +01:00
parent 67a56d31de
commit 20c30712b9
9 changed files with 36 additions and 3 deletions

View File

@ -130,4 +130,8 @@ void CircleColider::render( Renderer &renderer ) {
int CircleColider::getRadius() const {
return rad_;
}
std::shared_ptr< CollisionPolygon > CircleColider::copySelf() {
return std::make_shared< CircleColider >( *this );
}
} // namespace SDLPP

View File

@ -24,6 +24,7 @@ public:
render( Renderer &renderer,
const std::tuple< int, int, int, int > &color ) override;
virtual void render( Renderer &renderer ) override;
virtual std::shared_ptr<CollisionPolygon> copySelf() override;
private:
int getRadius() const;

View File

@ -25,6 +25,7 @@ public:
int getY() const;
void setColor( const std::string &color );
void setOutlineColor( const std::string &color );
virtual std::shared_ptr<CollisionPolygon> copySelf() = 0;
protected:
double original_x;

View File

@ -88,4 +88,9 @@ SDL_Rect RectColider::getRect() {
r.h = pixel_h;
return r;
}
std::shared_ptr< CollisionPolygon > RectColider::copySelf() {
return std::make_shared< RectColider >( *this );
}
} // namespace SDLPP

View File

@ -22,6 +22,7 @@ public:
render( Renderer &renderer,
const std::tuple< int, int, int, int > &color ) override;
virtual void render( Renderer &renderer ) override;
virtual std::shared_ptr<CollisionPolygon> copySelf() override;
private:
SDL_Rect getRect();

View File

@ -139,9 +139,12 @@ void RectangleRender::centerX() {
updateSizeAndPosition();
}
std::shared_ptr< RenderObject > RectangleRender::copySelf() {
// TODO ACTUALLY copy, don't just copy pointers to textures and whatnot,
// create new textures!!!
return std::make_shared< RectangleRender >( *this );
auto ret = std::make_shared< RectangleRender >( *this );
copyTo(ret);
return ret;
}
void RectangleRender::copyTo(std::shared_ptr<RenderObject> other) {
RenderObject::copyTo(other);
}
std::string RectangleRender::getColor() const {
return color;

View File

@ -4,6 +4,7 @@
#include "sdlpp_common.hpp"
#include "sdlpp_renderobject.hpp"
#include "sdlpp_rectcolider.hpp"
#include "sdlpp_circlecolider.hpp"
namespace SDLPP {
class SDLPPSCOPE RectangleRender : public RenderObject {
@ -45,6 +46,7 @@ public:
std::string getColor() const;
protected:
virtual void copyTo(std::shared_ptr<RenderObject> other) override;
void updateXY();
double og_x;
double og_y;

View File

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

View File

@ -80,6 +80,7 @@ public:
std::shared_ptr< Renderer > getRenderer() const;
protected:
virtual void copyTo(std::shared_ptr<RenderObject> other);
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
std::shared_ptr< Texture > texture;
std::shared_ptr< Renderer > renderer;