game/sdlpp/sdlpp_circlerenderer.cpp
2021-03-07 12:20:00 +01:00

147 lines
3.8 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 )
: RenderObject( r ) {
og_x = x_ = x;
og_y = y_ = y;
og_r = r_ = rad;
}
CircleRender::CircleRender( double x, double y, double rad,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t )
: CircleRender( x, y, rad, r ) {
throw "I don't support textures yet!!!";
}
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 ) {
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 );
}
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 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( (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