game/sdlpp/sdlpp_rectrenderer.cpp

187 lines
7.8 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 )
: RectangleRender( { x, y }, { w, h }, r ) {}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
int source_x, int source_y, int source_width,
int source_height )
: RectangleRender( { x, y }, { w, h }, r, t, source_x, source_y,
source_width, source_height ) {}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
const SDL_Rect &source_rect )
: RectangleRender( { x, y }, { w, h }, r, t, source_rect ) {}
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, img_or_color, is_polygon ) {}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img, int source_x,
int source_y, int source_width,
int source_height )
: RectangleRender( { x, y }, { w, h }, r, img, source_x, source_y,
source_width, source_height ) {}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img,
const SDL_Rect &source_rect )
: RectangleRender( { x, y }, { w, h }, r, img, source_rect ) {}
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r )
: RenderObject( r ) {
original = top_left;
original_size = size;
updateSizeAndPosition();
}
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
int source_x, int source_y, int source_width,
int source_height )
: RectangleRender( top_left, size, r ) {
setTexture( t, source_x, source_y, source_width, source_height );
}
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
const SDL_Rect &source_rect )
: RectangleRender( top_left, size, r ) {
setTexture( t, source_rect );
}
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r,
const std::string &img_or_color,
bool is_polygon )
: RectangleRender( top_left, size, r ) {
if ( !is_polygon ) {
setTexture( img_or_color );
} else {
setColor( img_or_color );
color = img_or_color;
}
}
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r,
const std::string &img, int source_x,
int source_y, int source_width,
int source_height )
: RectangleRender( top_left, size, r ) {
setTexture( img, source_x, source_y, source_width, source_height );
}
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r,
const std::string &img,
const SDL_Rect &source_rect )
: RectangleRender( top_left, size, r ) {
setTexture( img, source_rect );
}
void RectangleRender::setColor( const std::string &color ) {
if ( !polygon ) {
polygon = std::make_shared< RectColider >( 0, 0, 1, 1 );
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight(),
getId() );
}
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(),
getId() );
}
polygon->setOutlineColor( color );
}
std::pair< Vec2D< double >, Vec2D< double > >
RectangleRender::getDoubleRect() const {
return { original, original_size };
}
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( current.getX() * dimension );
rect.y = std::round( current.getY() * dimension );
rect.w =
std::round( ( current.getX() + original_size.getX() ) * dimension ) -
rect.x;
rect.h =
std::round( ( current.getY() + original_size.getY() ) * dimension ) -
rect.y;
if(rect.w < min_size.getX())
rect.w = min_size.getX();
if(rect.h < min_size.getY())
rect.h = min_size.getY();
if ( polygon )
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight(),
getId() );
for ( auto &x : collisions ) {
x->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight(), getId() );
}
}
SDL_Rect RectangleRender::getRect() {
return rect;
}
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::setMinWidth( uint64_t width ) {
min_size = {width, min_size.getY()};
}
void RectangleRender::setMinHeight( uint64_t height ) {
min_size = {min_size.getX(), height};
}
void RectangleRender::setSize( Vec2D< double > size ) {
original_size = size;
updateSizeAndPosition();
}
} // namespace SDLPP