game/sdlpp/sdlpp_renderobject.cpp
2021-03-07 14:06:55 +01:00

238 lines
7.1 KiB
C++

#include "sdlpp_renderobject.hpp"
#include <cmath>
namespace SDLPP {
bool RenderObject::entireTexture() {
return src_rect.x == -1 || src_rect.y == -1 || src_rect.w == -1 ||
src_rect.h == -1;
}
void RenderObject::render() {
if ( !getHidden() ) {
if ( polygon )
polygon->render( *renderer );
if ( texture != NULL ) {
SDL_Rect *src = NULL;
if ( animation.empty() ) {
if ( !entireTexture() )
src = &src_rect;
} else {
src = &animation[animation_index];
}
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), src, &rect );
}
if ( hasCollisions() && renderer->getRenderColiders() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
}
}
}
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 };
}
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;
}
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
const SDL_Rect &source_rect ) {
texture = t;
src_rect = source_rect;
}
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,
const SDL_Rect &source_rect ) {
texture = std::make_shared< Texture >( renderer, img_path );
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 } );
}
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 );
src_rect = { -1, -1, -1, -1 };
}
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 );
}
void RenderObject::animate( int ticks ) {
if ( animating && !animation.empty() ) {
animation_next_frame -= ticks;
if ( animation_next_frame <= 0 ) {
animation_index = ( animation_index + 1 ) % animation.size();
animation_next_frame =
( 1000 / animation_fps ) + animation_next_frame;
}
}
}
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();
}
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;
}
void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
other->collisions.clear();
for ( auto &colider : collisions ) {
other->collisions.push_back( colider->copySelf() );
}
other->texture.reset();
if ( texture ) {
other->texture = std::make_shared< Texture >( *texture );
}
other->polygon.reset();
if ( polygon ) {
other->polygon = polygon->copySelf();
}
other->colidedWith.clear();
}
void RenderObject::setTextureAlpha( uint8_t alpha ) {
texture->setAlpha( alpha );
}
void RenderObject::setTextureSourceRect( const SDL_Rect &source_rect ) {
src_rect = source_rect;
}
void RenderObject::setTextureSourceRect( int x, int y, int w, int h ) {
setTextureSourceRect( { x, y, w, h } );
}
void RenderObject::setAnimationFrames( const std::vector< SDL_Rect > &frames ) {
animation = frames;
}
void RenderObject::addAnimationFrame( const SDL_Rect &frame ) {
animation.push_back( frame );
}
void RenderObject::addAnimationFrame( const int x, const int y, const int w,
const int h ) {
addAnimationFrame( { x, y, w, h } );
}
void RenderObject::pauseAnimation() {
animating = false;
}
void RenderObject::resumeAnimation() {
animating = true;
}
void RenderObject::removeAnimation() {
animation.clear();
}
void RenderObject::setAnimationSpeed( const int fps ) {
animation_fps = fps;
animation_next_frame = 1000 / fps;
}
} // namespace SDLPP