Animation

This commit is contained in:
zv0n 2021-03-07 14:06:55 +01:00
parent c7ec12584c
commit bb502b37f4
6 changed files with 74 additions and 16 deletions

View File

@ -22,7 +22,7 @@ RectangleRender::RectangleRender( double x, double y, double w, double h,
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
SDL_Rect source_rect )
const SDL_Rect &source_rect )
: RectangleRender( x, y, w, h, r ) {
setTexture( t, source_rect );
}
@ -48,7 +48,8 @@ RectangleRender::RectangleRender( double x, double y, double w, double h,
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img, SDL_Rect source_rect )
const std::string &img,
const SDL_Rect &source_rect )
: RectangleRender( x, y, w, h, r ) {
setTexture( img, source_rect );
}

View File

@ -20,17 +20,17 @@ public:
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
SDL_Rect source_rect = { -1, -1, -1, -1 } );
const SDL_Rect &source_rect = { -1, -1, -1, -1 } );
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img_or_color, bool is_polygon = false );
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 );
const std::string &img, int source_x, int source_y,
int source_width, int source_height );
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img, SDL_Rect source_rect );
const std::string &img, const SDL_Rect &source_rect );
virtual void setColor( const std::string &color ) override;
virtual void setOutlineColor( const std::string &color ) override;
virtual void specialAction( int /*UNUSED*/ ) override {}

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

View File

@ -46,12 +46,12 @@ public:
int source_y, int source_width,
int source_height );
virtual void setTexture( const std::shared_ptr< Texture > &t,
SDL_Rect source_rect = { -1, -1, -1, -1 } );
const SDL_Rect &source_rect = { -1, -1, -1, -1 } );
virtual void setTexture( const std::string &img_path, int source_x,
int source_y, int source_width,
int source_height );
virtual void setTexture( const std::string &img_path,
SDL_Rect source_rect = { -1, -1, -1, -1 } );
const SDL_Rect &source_rect = { -1, -1, -1, -1 } );
virtual void setTexture( Font &font, const std::string &text,
const std::string &color = "FFFFFF",
const std::string &outline_color = "000000",
@ -87,8 +87,17 @@ public:
void setStatic( bool stat = true );
std::shared_ptr< Renderer > getRenderer() const;
void setTextureAlpha( uint8_t alpha );
void setTextureSourceRect( SDL_Rect source_rect );
void setTextureSourceRect( const SDL_Rect &source_rect );
void setTextureSourceRect( int x, int y, int w, int h );
void setAnimationFrames( const std::vector< SDL_Rect > &frames );
void addAnimationFrame( const SDL_Rect &frame );
void addAnimationFrame( const int x, const int y, const int w,
const int h );
void setAnimationSpeed( const int fps );
void pauseAnimation();
void resumeAnimation();
void removeAnimation();
void animate( int ticks );
protected:
virtual void copyTo( std::shared_ptr< RenderObject > other );
@ -111,6 +120,11 @@ protected:
bool centerx = false;
SDL_Rect rect;
SDL_Rect src_rect = { -1, -1, -1, -1 };
size_t animation_index = 0;
size_t animation_fps = 1;
int animation_next_frame = 1000;
std::vector< SDL_Rect > animation{};
bool animating = true;
private:
void setSceneID( int id );

View File

@ -76,12 +76,16 @@ Scene::getObjects( const std::unordered_set< int > &objectIDs ) {
}
return ret;
}
void Scene::movement() {
void Scene::updateScene() {
// check for objects that should be removed
checkKilled();
std::lock_guard< std::mutex > lock( render_mutex );
int now_ticks = SDL_GetTicks();
for ( const auto &x : render_objects ) {
// move objects that should be moved
x->move( now_ticks - prev_ticks );
// animate objects that should be animated
x->animate( now_ticks - prev_ticks );
}
prev_ticks = now_ticks;
}

View File

@ -23,7 +23,7 @@ public:
std::vector< std::shared_ptr< RenderObject > > getObjects();
std::vector< std::shared_ptr< RenderObject > >
getObjects( const std::unordered_set< int > &objectIDs );
void movement();
void updateScene();
std::vector< std::shared_ptr< RenderObject > >
getCollisions( RenderObject &r );
std::vector< std::shared_ptr< RenderObject > >