Animation

This commit is contained in:
2021-03-07 14:06:55 +01:00
parent c7ec12584c
commit bb502b37f4
6 changed files with 74 additions and 16 deletions
+44 -5
View File
@@ -12,8 +12,12 @@ void RenderObject::render() {
polygon->render( *renderer );
if ( texture != NULL ) {
SDL_Rect *src = NULL;
if ( !entireTexture() )
src = &src_rect;
if ( animation.empty() ) {
if ( !entireTexture() )
src = &src_rect;
} else {
src = &animation[animation_index];
}
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), src, &rect );
}
@@ -55,7 +59,7 @@ RenderObject::getCollisions() const {
return collisions;
}
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
SDL_Rect source_rect ) {
const SDL_Rect &source_rect ) {
texture = t;
src_rect = source_rect;
}
@@ -65,7 +69,7 @@ void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
setTexture( t, { source_x, source_y, source_width, source_height } );
}
void RenderObject::setTexture( const std::string &img_path,
SDL_Rect source_rect ) {
const SDL_Rect &source_rect ) {
texture = std::make_shared< Texture >( renderer, img_path );
src_rect = source_rect;
}
@@ -133,6 +137,18 @@ bool RenderObject::getKilled() {
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;
@@ -189,10 +205,33 @@ void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
void RenderObject::setTextureAlpha( uint8_t alpha ) {
texture->setAlpha( alpha );
}
void RenderObject::setTextureSourceRect( SDL_Rect source_rect ) {
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