2020-11-21 19:57:40 +00:00
|
|
|
#include "sdlpp_renderobject.hpp"
|
2021-01-31 20:48:48 +00:00
|
|
|
#include <cmath>
|
2020-11-21 19:57:40 +00:00
|
|
|
|
|
|
|
namespace SDLPP {
|
2021-03-07 12:06:08 +00:00
|
|
|
bool RenderObject::entireTexture() {
|
|
|
|
return src_rect.x == -1 || src_rect.y == -1 || src_rect.w == -1 ||
|
|
|
|
src_rect.h == -1;
|
|
|
|
}
|
2021-03-07 11:20:00 +00:00
|
|
|
void RenderObject::render() {
|
|
|
|
if ( !getHidden() ) {
|
|
|
|
if ( polygon )
|
|
|
|
polygon->render( *renderer );
|
2021-03-07 12:06:08 +00:00
|
|
|
if ( texture != NULL ) {
|
|
|
|
SDL_Rect *src = NULL;
|
2021-03-07 12:18:58 +00:00
|
|
|
if ( !entireTexture() )
|
2021-03-07 12:06:08 +00:00
|
|
|
src = &src_rect;
|
2021-03-07 11:20:00 +00:00
|
|
|
SDL_RenderCopy( renderer->getRendererPtr(),
|
2021-03-07 12:06:08 +00:00
|
|
|
texture->getTexturePtr(), src, &rect );
|
|
|
|
}
|
2021-03-07 11:20:00 +00:00
|
|
|
if ( hasCollisions() && renderer->getRenderColiders() ) {
|
|
|
|
for ( const auto &col : getCollisions() )
|
|
|
|
col->render( *renderer, colider_color );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 20:48:48 +00:00
|
|
|
void RenderObject::setPos( double x, double y ) {
|
|
|
|
og_x = x;
|
|
|
|
og_y = y;
|
|
|
|
updateSizeAndPosition();
|
|
|
|
}
|
|
|
|
void RenderObject::setPos( const std::pair< double, double > &pos ) {
|
|
|
|
setPos( pos.first, pos.second );
|
|
|
|
}
|
|
|
|
std::pair< double, double > RenderObject::getPos() const {
|
|
|
|
return { og_x, og_y };
|
|
|
|
}
|
2020-11-21 19:57:40 +00:00
|
|
|
bool RenderObject::colidesWith( const RenderObject &other ) const {
|
|
|
|
if ( !hasCollisions() || !other.hasCollisions() || getHidden() ||
|
|
|
|
other.getHidden() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for ( const auto &x : collisions ) {
|
|
|
|
for ( const auto &y : other.getCollisions() ) {
|
|
|
|
if ( x->colidesWith( *y ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool RenderObject::hasCollisions() const {
|
|
|
|
return !collisions.empty();
|
|
|
|
}
|
|
|
|
const std::vector< std::shared_ptr< CollisionPolygon > > &
|
|
|
|
RenderObject::getCollisions() const {
|
|
|
|
return collisions;
|
|
|
|
}
|
2021-03-07 12:06:08 +00:00
|
|
|
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
|
|
|
|
SDL_Rect source_rect ) {
|
2020-11-21 19:57:40 +00:00
|
|
|
texture = t;
|
2021-03-07 12:06:08 +00:00
|
|
|
src_rect = source_rect;
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
2021-03-07 12:06:08 +00:00
|
|
|
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
|
|
|
|
int source_x, int source_y, int source_width,
|
|
|
|
int source_height ) {
|
|
|
|
setTexture( t, { source_x, source_y, source_width, source_height } );
|
|
|
|
}
|
|
|
|
void RenderObject::setTexture( const std::string &img_path,
|
|
|
|
SDL_Rect source_rect ) {
|
2020-11-21 19:57:40 +00:00
|
|
|
texture = std::make_shared< Texture >( renderer, img_path );
|
2021-03-07 12:06:08 +00:00
|
|
|
src_rect = source_rect;
|
|
|
|
}
|
|
|
|
void RenderObject::setTexture( const std::string &img_path, int source_x,
|
|
|
|
int source_y, int source_width,
|
|
|
|
int source_height ) {
|
|
|
|
setTexture( img_path, { source_x, source_y, source_width, source_height } );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
void RenderObject::setTexture( Font &font, const std::string &text,
|
|
|
|
const std::string &color,
|
|
|
|
const std::string &outline_color,
|
|
|
|
int outline_size ) {
|
|
|
|
texture = std::make_shared< Texture >( renderer, font, text, color,
|
|
|
|
outline_color, outline_size );
|
2021-03-07 12:06:08 +00:00
|
|
|
src_rect = { -1, -1, -1, -1 };
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
void RenderObject::unsetTexture() {
|
|
|
|
texture.reset();
|
|
|
|
}
|
|
|
|
void RenderObject::unsetColor() {
|
|
|
|
polygon.reset();
|
|
|
|
}
|
|
|
|
// per second, relative to window width
|
|
|
|
void RenderObject::setMovementSpeed( double speed ) {
|
|
|
|
movementSpeed = speed;
|
|
|
|
}
|
|
|
|
void RenderObject::addMovement( int x, int y ) {
|
|
|
|
movementDirection.first += x;
|
|
|
|
movementDirection.second += y;
|
|
|
|
}
|
|
|
|
void RenderObject::resetMovementX() {
|
|
|
|
movementDirection.first = 0;
|
|
|
|
}
|
|
|
|
void RenderObject::resetMovementY() {
|
|
|
|
movementDirection.second = 0;
|
|
|
|
}
|
|
|
|
void RenderObject::clearColided() {
|
|
|
|
colidedWith.clear();
|
|
|
|
}
|
|
|
|
void RenderObject::addColided( std::shared_ptr< RenderObject > &obj ) {
|
|
|
|
colidedWith.push_back( obj );
|
|
|
|
}
|
|
|
|
std::vector< std::shared_ptr< RenderObject > > &RenderObject::getColidedWith() {
|
|
|
|
return colidedWith;
|
|
|
|
}
|
|
|
|
void RenderObject::setId( uint64_t input_id ) {
|
|
|
|
id = input_id;
|
|
|
|
}
|
|
|
|
uint64_t RenderObject::getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
void RenderObject::setHidden( bool hid ) {
|
|
|
|
hidden = hid;
|
|
|
|
}
|
|
|
|
bool RenderObject::getHidden() const {
|
|
|
|
return hidden;
|
|
|
|
}
|
|
|
|
void RenderObject::destroy() {
|
|
|
|
setHidden( true );
|
|
|
|
kill = true;
|
|
|
|
}
|
|
|
|
bool RenderObject::getKilled() {
|
|
|
|
return kill;
|
|
|
|
}
|
|
|
|
void RenderObject::setColiderColor( const std::string &color ) {
|
|
|
|
colider_color = getColorsHEX( color );
|
|
|
|
}
|
2021-01-31 20:48:48 +00:00
|
|
|
void RenderObject::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();
|
|
|
|
}
|
2020-11-21 19:57:40 +00:00
|
|
|
void RenderObject::setPermanent( bool perm ) {
|
|
|
|
permanent = perm;
|
|
|
|
setStatic( perm );
|
|
|
|
}
|
|
|
|
bool RenderObject::getPermanent() const {
|
|
|
|
return permanent;
|
|
|
|
}
|
|
|
|
bool RenderObject::isStatic() {
|
|
|
|
return is_static;
|
|
|
|
}
|
|
|
|
void RenderObject::setStatic( bool stat ) {
|
|
|
|
is_static = stat;
|
|
|
|
}
|
|
|
|
std::shared_ptr< Renderer > RenderObject::getRenderer() const {
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
void RenderObject::setSceneID( int id ) {
|
|
|
|
scene_id = id;
|
|
|
|
}
|
2021-01-30 20:24:54 +00:00
|
|
|
void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
|
2020-12-18 15:02:02 +00:00
|
|
|
other->collisions.clear();
|
|
|
|
for ( auto &colider : collisions ) {
|
2021-01-30 20:24:54 +00:00
|
|
|
other->collisions.push_back( colider->copySelf() );
|
2020-12-18 15:02:02 +00:00
|
|
|
}
|
|
|
|
other->texture.reset();
|
|
|
|
if ( texture ) {
|
|
|
|
other->texture = std::make_shared< Texture >( *texture );
|
|
|
|
}
|
|
|
|
other->polygon.reset();
|
|
|
|
if ( polygon ) {
|
|
|
|
other->polygon = polygon->copySelf();
|
|
|
|
}
|
|
|
|
other->colidedWith.clear();
|
|
|
|
}
|
2021-03-07 12:18:58 +00:00
|
|
|
void RenderObject::setTextureAlpha( uint8_t alpha ) {
|
|
|
|
texture->setAlpha( alpha );
|
|
|
|
}
|
|
|
|
void RenderObject::setTextureSourceRect( SDL_Rect source_rect ) {
|
|
|
|
src_rect = source_rect;
|
|
|
|
}
|
|
|
|
void RenderObject::setTextureSourceRect( int x, int y, int w, int h ) {
|
|
|
|
setTextureSourceRect( { x, y, w, h } );
|
|
|
|
}
|
2020-11-21 19:57:40 +00:00
|
|
|
} // namespace SDLPP
|