SDLPP: CircleRenderer can be rendered
This commit is contained in:
+128
-17
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user