game/sdlpp/sdlpp_circlerenderer.cpp

141 lines
4.2 KiB
C++
Raw Normal View History

2021-01-31 20:48:48 +00:00
// TODO textures
2020-11-21 19:57:40 +00:00
#include "sdlpp_circlerenderer.hpp"
2021-01-31 20:48:48 +00:00
#include "sdlpp_circlecolider.hpp"
#include <cmath>
2020-11-21 19:57:40 +00:00
namespace SDLPP {
2021-01-31 20:48:48 +00:00
CircleRender::CircleRender( double x, double y, double rad,
2020-11-21 19:57:40 +00:00
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 )
2020-11-21 19:57:40 +00:00
: RenderObject( r ) {
original = center;
current = center;
2021-01-31 20:48:48 +00:00
og_r = r_ = rad;
2020-11-21 19:57:40 +00:00
}
CircleRender::CircleRender( Vec2D< double > center, double rad,
2020-11-21 19:57:40 +00:00
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > & /*UNUSED*/ )
: CircleRender( center, rad, r ) {
2021-01-31 20:48:48 +00:00
throw "I don't support textures yet!!!";
2020-11-21 19:57:40 +00:00
}
CircleRender::CircleRender( Vec2D< double > center, double rad,
2020-11-21 19:57:40 +00:00
std::shared_ptr< Renderer > &r,
const std::string &img_or_color, bool is_polygon )
: CircleRender( center, rad, r ) {
if ( !is_polygon ) {
2021-01-31 20:48:48 +00:00
throw "I don't support textures yet!!!";
} else {
setColor( img_or_color );
2021-01-31 20:48:48 +00:00
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(),
2021-04-26 19:57:31 +00:00
collisionWidth(), collisionHeight(),
getId() );
2021-01-31 20:48:48 +00:00
}
polygon->setColor( color );
}
void CircleRender::setOutlineColor( const std::string &color ) {
if ( !polygon ) {
polygon = std::make_shared< CircleColider >( 0, 0, 1 );
polygon->updateCollision( collisionPushX(), collisionPushY(),
2021-04-26 19:57:31 +00:00
collisionWidth(), collisionHeight(),
getId() );
2021-01-31 20:48:48 +00:00
}
polygon->setOutlineColor( color );
2020-11-21 19:57:40 +00:00
}
std::pair< Vec2D< double >, Vec2D< double > >
2021-01-31 20:48:48 +00:00
CircleRender::getDoubleRect() const {
return { { original.getX() - og_r, original.getY() - og_r },
{ 2 * og_r, 2 * og_r } };
2020-11-21 19:57:40 +00:00
}
int CircleRender::leftmost() {
2021-01-31 20:48:48 +00:00
return rect.x;
2020-11-21 19:57:40 +00:00
}
int CircleRender::topmost() {
2021-01-31 20:48:48 +00:00
return rect.y;
}
int CircleRender::rightmost() {
return rect.x + rect.w;
}
int CircleRender::bottommost() {
return rect.y + rect.h;
2020-11-21 19:57:40 +00:00
}
int CircleRender::collisionPushX() {
2021-01-31 20:48:48 +00:00
return rect.x;
2020-11-21 19:57:40 +00:00
}
int CircleRender::collisionPushY() {
2021-01-31 20:48:48 +00:00
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;
2021-01-31 20:48:48 +00:00
if ( polygon )
polygon->updateCollision( collisionPushX(), collisionPushY(),
2021-04-26 19:57:31 +00:00
collisionWidth(), collisionHeight(),
getId() );
2021-01-31 20:48:48 +00:00
for ( auto &x : collisions ) {
x->updateCollision( collisionPushX(), collisionPushY(),
2021-04-26 19:57:31 +00:00
collisionWidth(), collisionHeight(), getId() );
2021-01-31 20:48:48 +00:00
}
}
SDL_Rect CircleRender::getRect() {
return rect;
}
std::shared_ptr< RenderObject > CircleRender::copySelf() {
auto ret = std::make_shared< CircleRender >( *this );
copyTo( ret );
2021-01-31 20:48:48 +00:00
return ret;
}
void CircleRender::copyTo( std::shared_ptr< RenderObject > other ) {
RenderObject::copyTo( other );
2021-01-31 20:48:48 +00:00
}
std::string CircleRender::getColor() const {
return color;
}
2020-11-21 19:57:40 +00:00
} // namespace SDLPP