Animation
This commit is contained in:
parent
c7ec12584c
commit
bb502b37f4
@ -22,7 +22,7 @@ RectangleRender::RectangleRender( double x, double y, double w, double h,
|
|||||||
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< Renderer > &r,
|
||||||
const std::shared_ptr< Texture > &t,
|
const std::shared_ptr< Texture > &t,
|
||||||
SDL_Rect source_rect )
|
const SDL_Rect &source_rect )
|
||||||
: RectangleRender( x, y, w, h, r ) {
|
: RectangleRender( x, y, w, h, r ) {
|
||||||
setTexture( t, source_rect );
|
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,
|
RectangleRender::RectangleRender( double x, double y, double w, double h,
|
||||||
const std::shared_ptr< Renderer > &r,
|
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 ) {
|
: RectangleRender( x, y, w, h, r ) {
|
||||||
setTexture( img, source_rect );
|
setTexture( img, source_rect );
|
||||||
}
|
}
|
||||||
|
@ -20,17 +20,17 @@ public:
|
|||||||
RectangleRender( double x, double y, double w, double h,
|
RectangleRender( double x, double y, double w, double h,
|
||||||
const std::shared_ptr< Renderer > &r,
|
const std::shared_ptr< Renderer > &r,
|
||||||
const std::shared_ptr< Texture > &t,
|
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,
|
RectangleRender( double x, double y, double w, double h,
|
||||||
const std::shared_ptr< Renderer > &r,
|
const std::shared_ptr< Renderer > &r,
|
||||||
const std::string &img_or_color, bool is_polygon = false );
|
const std::string &img_or_color, bool is_polygon = false );
|
||||||
RectangleRender( double x, double y, double w, double h,
|
RectangleRender( double x, double y, double w, double h,
|
||||||
const std::shared_ptr< Renderer > &r,
|
const std::shared_ptr< Renderer > &r,
|
||||||
const std::string &img, int source_x,
|
const std::string &img, int source_x, int source_y,
|
||||||
int source_y, int source_width, int source_height );
|
int source_width, int source_height );
|
||||||
RectangleRender( double x, double y, double w, double h,
|
RectangleRender( double x, double y, double w, double h,
|
||||||
const std::shared_ptr< Renderer > &r,
|
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 setColor( const std::string &color ) override;
|
||||||
virtual void setOutlineColor( const std::string &color ) override;
|
virtual void setOutlineColor( const std::string &color ) override;
|
||||||
virtual void specialAction( int /*UNUSED*/ ) override {}
|
virtual void specialAction( int /*UNUSED*/ ) override {}
|
||||||
|
@ -12,8 +12,12 @@ void RenderObject::render() {
|
|||||||
polygon->render( *renderer );
|
polygon->render( *renderer );
|
||||||
if ( texture != NULL ) {
|
if ( texture != NULL ) {
|
||||||
SDL_Rect *src = NULL;
|
SDL_Rect *src = NULL;
|
||||||
if ( !entireTexture() )
|
if ( animation.empty() ) {
|
||||||
src = &src_rect;
|
if ( !entireTexture() )
|
||||||
|
src = &src_rect;
|
||||||
|
} else {
|
||||||
|
src = &animation[animation_index];
|
||||||
|
}
|
||||||
SDL_RenderCopy( renderer->getRendererPtr(),
|
SDL_RenderCopy( renderer->getRendererPtr(),
|
||||||
texture->getTexturePtr(), src, &rect );
|
texture->getTexturePtr(), src, &rect );
|
||||||
}
|
}
|
||||||
@ -55,7 +59,7 @@ RenderObject::getCollisions() const {
|
|||||||
return collisions;
|
return collisions;
|
||||||
}
|
}
|
||||||
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
|
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
|
||||||
SDL_Rect source_rect ) {
|
const SDL_Rect &source_rect ) {
|
||||||
texture = t;
|
texture = t;
|
||||||
src_rect = source_rect;
|
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 } );
|
setTexture( t, { source_x, source_y, source_width, source_height } );
|
||||||
}
|
}
|
||||||
void RenderObject::setTexture( const std::string &img_path,
|
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 );
|
texture = std::make_shared< Texture >( renderer, img_path );
|
||||||
src_rect = source_rect;
|
src_rect = source_rect;
|
||||||
}
|
}
|
||||||
@ -133,6 +137,18 @@ bool RenderObject::getKilled() {
|
|||||||
void RenderObject::setColiderColor( const std::string &color ) {
|
void RenderObject::setColiderColor( const std::string &color ) {
|
||||||
colider_color = getColorsHEX( 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 ) {
|
void RenderObject::move( int ticks ) {
|
||||||
if ( permanent )
|
if ( permanent )
|
||||||
return;
|
return;
|
||||||
@ -189,10 +205,33 @@ void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) {
|
|||||||
void RenderObject::setTextureAlpha( uint8_t alpha ) {
|
void RenderObject::setTextureAlpha( uint8_t alpha ) {
|
||||||
texture->setAlpha( alpha );
|
texture->setAlpha( alpha );
|
||||||
}
|
}
|
||||||
void RenderObject::setTextureSourceRect( SDL_Rect source_rect ) {
|
void RenderObject::setTextureSourceRect( const SDL_Rect &source_rect ) {
|
||||||
src_rect = source_rect;
|
src_rect = source_rect;
|
||||||
}
|
}
|
||||||
void RenderObject::setTextureSourceRect( int x, int y, int w, int h ) {
|
void RenderObject::setTextureSourceRect( int x, int y, int w, int h ) {
|
||||||
setTextureSourceRect( { x, y, w, 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
|
} // namespace SDLPP
|
||||||
|
@ -46,12 +46,12 @@ public:
|
|||||||
int source_y, int source_width,
|
int source_y, int source_width,
|
||||||
int source_height );
|
int source_height );
|
||||||
virtual void setTexture( const std::shared_ptr< Texture > &t,
|
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,
|
virtual void setTexture( const std::string &img_path, int source_x,
|
||||||
int source_y, int source_width,
|
int source_y, int source_width,
|
||||||
int source_height );
|
int source_height );
|
||||||
virtual void setTexture( const std::string &img_path,
|
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,
|
virtual void setTexture( Font &font, const std::string &text,
|
||||||
const std::string &color = "FFFFFF",
|
const std::string &color = "FFFFFF",
|
||||||
const std::string &outline_color = "000000",
|
const std::string &outline_color = "000000",
|
||||||
@ -87,8 +87,17 @@ public:
|
|||||||
void setStatic( bool stat = true );
|
void setStatic( bool stat = true );
|
||||||
std::shared_ptr< Renderer > getRenderer() const;
|
std::shared_ptr< Renderer > getRenderer() const;
|
||||||
void setTextureAlpha( uint8_t alpha );
|
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 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:
|
protected:
|
||||||
virtual void copyTo( std::shared_ptr< RenderObject > other );
|
virtual void copyTo( std::shared_ptr< RenderObject > other );
|
||||||
@ -111,6 +120,11 @@ protected:
|
|||||||
bool centerx = false;
|
bool centerx = false;
|
||||||
SDL_Rect rect;
|
SDL_Rect rect;
|
||||||
SDL_Rect src_rect = { -1, -1, -1, -1 };
|
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:
|
private:
|
||||||
void setSceneID( int id );
|
void setSceneID( int id );
|
||||||
|
@ -76,12 +76,16 @@ Scene::getObjects( const std::unordered_set< int > &objectIDs ) {
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
void Scene::movement() {
|
void Scene::updateScene() {
|
||||||
|
// check for objects that should be removed
|
||||||
checkKilled();
|
checkKilled();
|
||||||
std::lock_guard< std::mutex > lock( render_mutex );
|
std::lock_guard< std::mutex > lock( render_mutex );
|
||||||
int now_ticks = SDL_GetTicks();
|
int now_ticks = SDL_GetTicks();
|
||||||
for ( const auto &x : render_objects ) {
|
for ( const auto &x : render_objects ) {
|
||||||
|
// move objects that should be moved
|
||||||
x->move( now_ticks - prev_ticks );
|
x->move( now_ticks - prev_ticks );
|
||||||
|
// animate objects that should be animated
|
||||||
|
x->animate( now_ticks - prev_ticks );
|
||||||
}
|
}
|
||||||
prev_ticks = now_ticks;
|
prev_ticks = now_ticks;
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ public:
|
|||||||
std::vector< std::shared_ptr< RenderObject > > getObjects();
|
std::vector< std::shared_ptr< RenderObject > > getObjects();
|
||||||
std::vector< std::shared_ptr< RenderObject > >
|
std::vector< std::shared_ptr< RenderObject > >
|
||||||
getObjects( const std::unordered_set< int > &objectIDs );
|
getObjects( const std::unordered_set< int > &objectIDs );
|
||||||
void movement();
|
void updateScene();
|
||||||
std::vector< std::shared_ptr< RenderObject > >
|
std::vector< std::shared_ptr< RenderObject > >
|
||||||
getCollisions( RenderObject &r );
|
getCollisions( RenderObject &r );
|
||||||
std::vector< std::shared_ptr< RenderObject > >
|
std::vector< std::shared_ptr< RenderObject > >
|
||||||
|
Loading…
Reference in New Issue
Block a user