SDLPP: CircleRenderer can be rendered

This commit is contained in:
zv0n 2021-01-31 21:48:48 +01:00
parent 8481560175
commit 2661d6ca76
7 changed files with 206 additions and 77 deletions

View File

@ -1,4 +1,3 @@
// TODO render function like in rectangle
#include "sdlpp_circlecolider.hpp"
namespace SDLPP {

View File

@ -1,48 +1,159 @@
// TODO textures
#include "sdlpp_circlerenderer.hpp"
#include "sdlpp_circlecolider.hpp"
#include <cmath>
namespace SDLPP {
CircleRender::CircleRender( int x, int y, int rad,
CircleRender::CircleRender( double x, double y, double rad,
std::shared_ptr< Renderer > &r )
: RenderObject( r ) {
x_ = x;
y_ = y;
rad_ = rad;
og_x = x_ = x;
og_y = y_ = y;
og_r = r_ = rad;
}
CircleRender::CircleRender( int x, int y, int rad,
CircleRender::CircleRender( double x, double y, double rad,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t )
: CircleRender( x, y, rad, r ) {
setTexture( t );
throw "I don't support textures yet!!!";
}
CircleRender::CircleRender( int x, int y, int rad,
CircleRender::CircleRender( double x, double y, double rad,
std::shared_ptr< Renderer > &r,
const std::string &img_path )
const std::string &img_or_color,
bool is_polygon )
: CircleRender( x, y, rad, r ) {
auto texture = std::make_shared< Texture >( r, img_path );
setTexture( texture );
if(!is_polygon) {
throw "I don't support textures yet!!!";
} else {
setColor(img_or_color);
color = img_or_color;
}
}
void CircleRender::setColor( const std::string &color ) {
if ( !polygon ) {
polygon = std::make_shared< CircleColider >( 0, 0, 1 );
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
polygon->setColor( color );
}
void CircleRender::setOutlineColor( const std::string &color ) {
if ( !polygon ) {
polygon = std::make_shared< CircleColider >( 0, 0, 1 );
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
polygon->setOutlineColor( color );
}
void CircleRender::render() {
std::cout << "I'm a circle, look at me go!" << std::endl
<< "My dimensions are: [" << x_ << ", " << y_
<< "], radius: " << rad_ << std::endl;
if ( !getHidden() ) {
if ( polygon )
polygon->render( *renderer );
else
std::cerr << "I don't support textures on circles yet!" << std::endl;
if ( hasCollisions() && renderer->getRenderColiders() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
}
}
}
std::pair< std::pair< double, double >, std::pair< double, double > >
CircleRender::getDoubleRect() const {
return { { og_x - og_r, og_y - og_r }, { 2 * og_r, 2 * og_r } };
}
int CircleRender::leftmost() {
return x_ - rad_;
return rect.x;
}
int CircleRender::topmost() {
return y_ - rad_;
return rect.y;
}
int CircleRender::rightmost() {
return rect.x + rect.w;
}
int CircleRender::bottommost() {
return rect.y + rect.h;
}
int CircleRender::collisionPushX() {
return x_;
return rect.x;
}
int CircleRender::collisionPushY() {
return y_;
return rect.y;
}
int CircleRender::collisionWidth() {
return rect.w;
}
int CircleRender::collisionHeight() {
return rect.h;
}
void CircleRender::updateSizeAndPosition() {
updateXY();
auto dimension = renderer->getSmallerSide();
rect.x = std::round( (x_ - r_) * dimension );
rect.y = std::round( (y_ - r_) * dimension );
rect.w = std::round( ( x_ + r_ ) * dimension ) - rect.x;
rect.h = std::round( ( y_ + r_ ) * dimension ) - rect.y;
if ( polygon )
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
for ( auto &x : collisions ) {
x->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
}
SDL_Rect CircleRender::getRect() {
return rect;
}
void CircleRender::centerX() {
centerx = true;
updateSizeAndPosition();
}
std::shared_ptr< RenderObject > CircleRender::copySelf() {
auto ret = std::make_shared< CircleRender >( *this );
copyTo(ret);
return ret;
}
void CircleRender::copyTo(std::shared_ptr<RenderObject> other) {
RenderObject::copyTo(other);
}
std::string CircleRender::getColor() const {
return color;
}
void CircleRender::updateXY() {
if ( !centerx ) {
x_ = og_x;
y_ = og_y;
return;
}
auto width = renderer->getWidth();
auto height = renderer->getHeight();
if ( width > height ) {
auto multiplier =
static_cast< double >( width ) / static_cast< double >( height );
x_ = og_x + static_cast< double >( multiplier - 1 ) / 2;
} else {
x_ = og_x;
}
y_ = og_y;
}
} // namespace SDLPP

View File

@ -11,21 +11,41 @@ class SDLPPSCOPE CircleRender : public RenderObject {
public:
CircleRender() = delete;
virtual ~CircleRender(){};
CircleRender( int x, int y, int rad, std::shared_ptr< Renderer > &r );
CircleRender( int x, int y, int rad, std::shared_ptr< Renderer > &r,
CircleRender( double x, double y, double rad, std::shared_ptr< Renderer > &r );
CircleRender( double x, double y, double rad, std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t );
CircleRender( int x, int y, int rad, std::shared_ptr< Renderer > &r,
const std::string &img_path );
virtual void render();
virtual int leftmost();
virtual int topmost();
virtual int collisionPushX();
virtual int collisionPushY();
CircleRender( double x, double y, double rad, std::shared_ptr< Renderer > &r,
const std::string &img_or_color, bool is_polygon = false );
virtual void setColor( const std::string &color ) override;
virtual void setOutlineColor( const std::string &color ) override;
virtual void specialAction( int /*UNUSED*/ ) override{}
virtual void render() override;
virtual void custom_move( int /*UNUSED*/ ) override{}
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() const override;
virtual int leftmost() override;
virtual int topmost() override;
virtual int rightmost() override;
virtual int bottommost() override;
virtual int collisionPushX() override;
virtual int collisionPushY() override;
virtual int collisionWidth() override;
virtual int collisionHeight() override;
virtual void updateSizeAndPosition() override;
virtual SDL_Rect getRect() override;
virtual void centerX() override;
virtual std::shared_ptr< RenderObject > copySelf() override;
std::string getColor() const;
private:
int x_;
int y_;
int rad_;
virtual void copyTo(std::shared_ptr<RenderObject> other) override;
void updateXY();
double og_r;
double r_;
bool centerx = false;
SDL_Rect rect;
std::string color = "";
};
} // end of namespace SDLPP
#endif

View File

@ -52,46 +52,16 @@ void RectangleRender::render() {
if ( texture != NULL )
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), NULL, &rect );
}
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
if ( hasCollisions() && renderer->getRenderColiders() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
}
}
void RectangleRender::move( int ticks ) {
if ( permanent )
return;
auto addx =
static_cast< double >( movementSpeed * movementDirection.first ) *
( static_cast< double >( ticks ) / 1000 );
auto addy =
static_cast< double >( movementSpeed * movementDirection.second ) *
( static_cast< double >( ticks ) / 1000 );
if ( std::isnan( addx ) || std::isnan( addy ) )
return;
og_x += addx;
og_y += addy;
custom_move( ticks );
updateSizeAndPosition();
}
std::pair< std::pair< double, double >, std::pair< double, double > >
RectangleRender::getDoubleRect() const {
return { { og_x, og_y }, { og_w, og_h } };
}
void RectangleRender::setPos( double x, double y ) {
og_x = x;
og_y = y;
updateSizeAndPosition();
}
void RectangleRender::setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second );
}
std::pair< double, double > RectangleRender::getPos() const {
return { og_x, og_y };
}
int RectangleRender::leftmost() {
return rect.x;
}

View File

@ -21,16 +21,12 @@ public:
const std::string &img_or_color, bool is_polygon = false );
virtual void setColor( const std::string &color ) override;
virtual void setOutlineColor( const std::string &color ) override;
virtual void specialAction( int /*UNUSED*/ ) override{};
virtual void specialAction( int /*UNUSED*/ ) override{}
virtual void render() override;
virtual void move( int ticks ) override;
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() const override;
virtual void setPos( double x, double y ) override;
virtual void setPos( const std::pair< double, double > &pos ) override;
virtual std::pair< double, double > getPos() const override;
virtual int leftmost() override;
virtual int topmost() override;
virtual int rightmost() override;
@ -48,12 +44,8 @@ public:
protected:
virtual void copyTo(std::shared_ptr<RenderObject> other) override;
void updateXY();
double og_x;
double og_y;
double og_w;
double og_h;
double x_;
double y_;
double w_;
double h_;
bool centerx = false;

View File

@ -1,6 +1,18 @@
#include "sdlpp_renderobject.hpp"
#include <cmath>
namespace SDLPP {
void RenderObject::setPos( double x, double y ) {
og_x = x;
og_y = y;
updateSizeAndPosition();
}
void RenderObject::setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second );
}
std::pair< double, double > RenderObject::getPos() const {
return { og_x, og_y };
}
bool RenderObject::colidesWith( const RenderObject &other ) const {
if ( !hasCollisions() || !other.hasCollisions() || getHidden() ||
other.getHidden() ) {
@ -85,6 +97,25 @@ bool RenderObject::getKilled() {
void RenderObject::setColiderColor( const std::string &color ) {
colider_color = getColorsHEX( color );
}
void RenderObject::move( int ticks ) {
if ( permanent )
return;
auto addx =
static_cast< double >( movementSpeed * movementDirection.first ) *
( static_cast< double >( ticks ) / 1000 );
auto addy =
static_cast< double >( movementSpeed * movementDirection.second ) *
( static_cast< double >( ticks ) / 1000 );
if ( std::isnan( addx ) || std::isnan( addy ) )
return;
og_x += addx;
og_y += addy;
custom_move( ticks );
updateSizeAndPosition();
}
void RenderObject::setPermanent( bool perm ) {
permanent = perm;
setStatic( perm );

View File

@ -30,9 +30,9 @@ public:
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() const = 0;
virtual void setPos( double x, double y ) = 0;
virtual void setPos( const std::pair< double, double > &pos ) = 0;
virtual std::pair< double, double > getPos() const = 0;
virtual void setPos( double x, double y );
virtual void setPos( const std::pair< double, double > &pos );
virtual std::pair< double, double > getPos() const;
bool colidesWith( const RenderObject &other ) const;
template < class T > void addCollision( const T &p ) {
collisions.push_back( std::make_shared< T >( p ) );
@ -68,7 +68,7 @@ public:
void destroy();
bool getKilled();
void setColiderColor( const std::string &color );
virtual void move( int ticks ) = 0;
virtual void move( int ticks );
virtual void custom_move( int ticks ) = 0;
virtual void updateSizeAndPosition() = 0;
virtual SDL_Rect getRect() = 0;
@ -101,6 +101,12 @@ protected:
private:
void setSceneID( int id );
friend Scene;
protected:
double og_x;
double og_y;
double x_;
double y_;
};
} // end of namespace SDLPP