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

142 lines
5.3 KiB
C++

#ifndef SDLPP_HPP_RENDER_OBJECT
#define SDLPP_HPP_RENDER_OBJECT
#include "sdlpp_common.hpp"
#include "sdlpp_collision.hpp"
#include "sdlpp_renderer.hpp"
#include "sdlpp_texture.hpp"
#include <memory>
#include <vector>
namespace SDLPP {
class SDLPPSCOPE Scene;
class SDLPPSCOPE RenderObject {
public:
RenderObject( const std::shared_ptr< Renderer > &r ) : renderer( r ) {}
virtual ~RenderObject() {}
virtual void render();
virtual int leftmost() = 0;
virtual int topmost() = 0;
virtual int rightmost() = 0;
virtual int bottommost() = 0;
virtual int collisionPushX() = 0;
virtual int collisionPushY() = 0;
virtual int collisionWidth() = 0;
virtual int collisionHeight() = 0;
virtual void specialAction( int code ) = 0;
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() const = 0;
virtual void setPos( double x, double y );
virtual void setPos( const std::pair< double, double > &pos );
virtual std::pair< double, double > getPos() const;
bool colidesWith( const RenderObject &other ) const;
template < class T > void addCollision( const T &p ) {
collisions.push_back( std::make_shared< T >( p ) );
collisions.back()->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(),
collisionHeight() );
}
bool hasCollisions() const;
const std::vector< std::shared_ptr< CollisionPolygon > > &
getCollisions() const;
virtual void setTexture( const std::shared_ptr< Texture > &t, int source_x,
int source_y, int source_width,
int source_height );
virtual void setTexture( const std::shared_ptr< Texture > &t,
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,
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",
int outline_size = -1 );
virtual void setColor( const std::string &color ) = 0;
virtual void setOutlineColor( const std::string &color ) = 0;
virtual void unsetTexture();
virtual void unsetColor();
// per second, relative to window width
void setMovementSpeed( double speed );
void addMovement( int x, int y );
void resetMovementX();
void resetMovementY();
void clearColided();
void addColided( std::shared_ptr< RenderObject > &obj );
std::vector< std::shared_ptr< RenderObject > > &getColidedWith();
void setId( uint64_t input_id );
uint64_t getId();
void setHidden( bool hid );
bool getHidden() const;
void destroy();
bool getKilled();
void setColiderColor( const std::string &color );
virtual void move( int ticks );
virtual void custom_move( int ticks ) = 0;
virtual void updateSizeAndPosition() = 0;
virtual SDL_Rect getRect() = 0;
void setPermanent( bool perm = true );
bool getPermanent() const;
virtual void centerX() = 0;
virtual std::shared_ptr< RenderObject > copySelf() = 0;
bool isStatic();
void setStatic( bool stat = true );
std::shared_ptr< Renderer > getRenderer() const;
void setTextureAlpha( uint8_t alpha );
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 );
bool entireTexture();
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
std::shared_ptr< Texture > texture;
std::shared_ptr< Texture > cur_texture;
std::shared_ptr< Renderer > renderer;
std::shared_ptr< CollisionPolygon > polygon;
double movementSpeed = 0;
std::pair< int, int > movementDirection = { 0, 0 };
std::vector< std::shared_ptr< RenderObject > > colidedWith;
uint64_t id = -1;
bool hidden = false;
bool kill = false;
std::tuple< int, int, int, int > colider_color = { 0x00, 0xFF, 0xFF, 0xFF };
uint64_t scene_id = -1;
bool permanent = false;
bool is_static = true;
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 );
friend Scene;
protected:
double og_x;
double og_y;
double x_;
double y_;
};
} // end of namespace SDLPP
#endif