141 lines
4.2 KiB
C++
141 lines
4.2 KiB
C++
// TODO textures
|
|
#include "sdlpp_circlerenderer.hpp"
|
|
#include "sdlpp_circlecolider.hpp"
|
|
#include <cmath>
|
|
|
|
namespace SDLPP {
|
|
CircleRender::CircleRender( double x, double y, double rad,
|
|
std::shared_ptr< Renderer > &r )
|
|
: CircleRender( { x, y }, rad, r ) {}
|
|
|
|
CircleRender::CircleRender( double x, double y, double rad,
|
|
std::shared_ptr< Renderer > &r,
|
|
std::shared_ptr< Texture > &t )
|
|
: CircleRender( { x, y }, rad, r, t ) {}
|
|
|
|
CircleRender::CircleRender( double x, double y, double rad,
|
|
std::shared_ptr< Renderer > &r,
|
|
const std::string &img_or_color, bool is_polygon )
|
|
: CircleRender( { x, y }, rad, r, img_or_color, is_polygon ) {}
|
|
|
|
CircleRender::CircleRender( Vec2D< double > center, double rad,
|
|
std::shared_ptr< Renderer > &r )
|
|
: RenderObject( r ) {
|
|
original = center;
|
|
current = center;
|
|
og_r = r_ = rad;
|
|
}
|
|
|
|
CircleRender::CircleRender( Vec2D< double > center, double rad,
|
|
std::shared_ptr< Renderer > &r,
|
|
std::shared_ptr< Texture > & /*UNUSED*/ )
|
|
: CircleRender( center, rad, r ) {
|
|
throw "I don't support textures yet!!!";
|
|
}
|
|
|
|
CircleRender::CircleRender( Vec2D< double > center, double rad,
|
|
std::shared_ptr< Renderer > &r,
|
|
const std::string &img_or_color, bool is_polygon )
|
|
: CircleRender( center, rad, r ) {
|
|
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(),
|
|
getId() );
|
|
}
|
|
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(),
|
|
getId() );
|
|
}
|
|
polygon->setOutlineColor( color );
|
|
}
|
|
|
|
std::pair< Vec2D< double >, Vec2D< double > >
|
|
CircleRender::getDoubleRect() const {
|
|
return { { original.getX() - og_r, original.getY() - og_r },
|
|
{ 2 * og_r, 2 * og_r } };
|
|
}
|
|
|
|
int CircleRender::leftmost() {
|
|
return rect.x;
|
|
}
|
|
|
|
int CircleRender::topmost() {
|
|
return rect.y;
|
|
}
|
|
|
|
int CircleRender::rightmost() {
|
|
return rect.x + rect.w;
|
|
}
|
|
|
|
int CircleRender::bottommost() {
|
|
return rect.y + rect.h;
|
|
}
|
|
|
|
int CircleRender::collisionPushX() {
|
|
return rect.x;
|
|
}
|
|
|
|
int CircleRender::collisionPushY() {
|
|
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( ( current.getX() - r_ ) * dimension );
|
|
rect.y = std::round( ( current.getY() - r_ ) * dimension );
|
|
rect.w = std::round( ( current.getX() + r_ ) * dimension ) - rect.x;
|
|
rect.h = std::round( ( current.getY() + r_ ) * dimension ) - rect.y;
|
|
if ( polygon )
|
|
polygon->updateCollision( collisionPushX(), collisionPushY(),
|
|
collisionWidth(), collisionHeight(),
|
|
getId() );
|
|
for ( auto &x : collisions ) {
|
|
x->updateCollision( collisionPushX(), collisionPushY(),
|
|
collisionWidth(), collisionHeight(), getId() );
|
|
}
|
|
}
|
|
|
|
SDL_Rect CircleRender::getRect() {
|
|
return rect;
|
|
}
|
|
|
|
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;
|
|
}
|
|
} // namespace SDLPP
|