game/sdlpp/sdlpp_rectrenderer.cpp

171 lines
5.2 KiB
C++

#include <cmath>
#include "sdlpp_rectrenderer.hpp"
namespace SDLPP {
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r )
: RenderObject( r ) {
og_x = x_ = x;
og_y = y_ = y;
og_w = w_ = w;
og_h = h_ = h;
updateSizeAndPosition();
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t )
: RectangleRender( x, y, w, h, r ) {
setTexture( t );
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img_or_color,
bool is_polygon )
: RectangleRender( x, y, w, h, r ) {
if ( !is_polygon ) {
setTexture( img_or_color );
} else {
setColor( img_or_color );
color = img_or_color;
}
}
void RectangleRender::setColor( const std::string &color ) {
if ( !polygon ) {
polygon = std::make_shared< RectColider >( 0, 0, 1, 1 );
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
polygon->setColor( color );
}
void RectangleRender::setOutlineColor( const std::string &color ) {
if ( !polygon ) {
polygon = std::make_shared< RectColider >( 0, 0, 1, 1 );
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
polygon->setOutlineColor( color );
}
void RectangleRender::render() {
if ( !getHidden() ) {
if ( polygon )
polygon->render( *renderer );
if ( texture != NULL )
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), NULL, &rect );
}
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
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;
}
int RectangleRender::topmost() {
return rect.y;
}
int RectangleRender::rightmost() {
return rect.x + rect.w;
}
int RectangleRender::bottommost() {
return rect.y + rect.h;
}
int RectangleRender::collisionPushX() {
return rect.x;
}
int RectangleRender::collisionPushY() {
return rect.y;
}
int RectangleRender::collisionWidth() {
return rect.w;
}
int RectangleRender::collisionHeight() {
return rect.h;
}
void RectangleRender::updateSizeAndPosition() {
updateXY();
auto dimension = renderer->getSmallerSide();
rect.x = std::round( x_ * dimension );
rect.y = std::round( y_ * dimension );
rect.w = std::round( ( x_ + w_ ) * dimension ) - rect.x;
rect.h = std::round( ( y_ + h_ ) * dimension ) - rect.y;
if ( polygon )
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
for ( auto &x : collisions ) {
x->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
}
SDL_Rect RectangleRender::getRect() {
return rect;
}
void RectangleRender::centerX() {
centerx = true;
updateSizeAndPosition();
}
std::shared_ptr< RenderObject > RectangleRender::copySelf() {
auto ret = std::make_shared< RectangleRender >( *this );
copyTo(ret);
return ret;
}
void RectangleRender::copyTo(std::shared_ptr<RenderObject> other) {
RenderObject::copyTo(other);
}
std::string RectangleRender::getColor() const {
return color;
}
void RectangleRender::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