game/sdlpp/sdlpp_renderobject.hpp

157 lines
5.8 KiB
C++
Raw Normal View History

2020-11-21 19:57:40 +00:00
#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 "sdlpp_vector.hpp"
2021-04-25 11:57:33 +00:00
#include "sdlpp_visitor.hpp"
2020-11-21 19:57:40 +00:00
#include <memory>
#include <vector>
namespace SDLPP {
enum ObjectAlignment {
OBJ_START,
OBJ_CENTER,
OBJ_END,
};
class Scene;
2020-11-21 19:57:40 +00:00
2020-11-22 22:37:55 +00:00
class SDLPPSCOPE RenderObject {
2020-11-21 19:57:40 +00:00
public:
RenderObject( const std::shared_ptr< Renderer > &r ) : renderer( r ) {}
virtual ~RenderObject() {}
2021-03-07 11:20:00 +00:00
virtual void render();
2020-11-21 19:57:40 +00:00
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< Vec2D< double >, Vec2D< double > >
2020-11-21 19:57:40 +00:00
getDoubleRect() const = 0;
2021-01-31 20:48:48 +00:00
virtual void setPos( double x, double y );
virtual void setPos( const std::pair< double, double > &pos );
virtual void setPos( const Vec2D< double > &vec );
virtual Vec2D< double > getPos() const;
2021-04-26 19:57:31 +00:00
std::vector< uint64_t > colidesWith( const RenderObject &other ) const;
2020-11-21 19:57:40 +00:00
template < class T > void addCollision( const T &p ) {
collisions.push_back( std::make_shared< T >( p ) );
collisions.back()->updateCollision( collisionPushX(), collisionPushY(),
2021-04-26 19:57:31 +00:00
collisionWidth(), collisionHeight(),
getId() );
2020-11-21 19:57:40 +00:00
}
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,
2021-03-07 13:06:55 +00:00
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,
2021-03-07 13:06:55 +00:00
const SDL_Rect &source_rect = { -1, -1, -1, -1 } );
2020-11-21 19:57:40 +00:00
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 );
2021-04-24 21:26:27 +00:00
void addMovement( double x, double y );
void setMovement( double x, double y );
2021-04-25 11:57:33 +00:00
Vec2D< double > getMovement();
2020-11-21 19:57:40 +00:00
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() const;
2020-11-21 19:57:40 +00:00
void setHidden( bool hid );
bool getHidden() const;
void destroy();
bool getKilled();
void setColiderColor( const std::string &color );
2021-01-31 20:48:48 +00:00
virtual void move( int ticks );
2020-11-21 19:57:40 +00:00
virtual void custom_move( int ticks ) = 0;
virtual void updateSizeAndPosition() = 0;
virtual void updateXY();
2020-11-21 19:57:40 +00:00
virtual SDL_Rect getRect() = 0;
void setPermanent( bool perm = true );
bool getPermanent() const;
virtual std::shared_ptr< RenderObject > copySelf() = 0;
bool isStatic();
void setStatic( bool stat = true );
std::shared_ptr< Renderer > getRenderer() const;
2021-03-07 12:18:58 +00:00
void setTextureAlpha( uint8_t alpha );
2021-03-07 13:06:55 +00:00
void setTextureSourceRect( const SDL_Rect &source_rect );
2021-03-07 12:18:58 +00:00
void setTextureSourceRect( int x, int y, int w, int h );
2021-03-07 13:06:55 +00:00
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 );
2021-04-24 21:26:27 +00:00
void setAnimationSpeed( const double fps );
2021-03-07 13:06:55 +00:00
void pauseAnimation();
void resumeAnimation();
void removeAnimation();
void animate( int ticks );
2021-04-25 11:57:33 +00:00
void visit( Visitor &visitor );
void setAlignment( ObjectAlignment horizontal, ObjectAlignment vertical );
2020-11-21 19:57:40 +00:00
protected:
virtual void copyTo( std::shared_ptr< RenderObject > other );
bool entireTexture();
2020-11-21 19:57:40 +00:00
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
std::shared_ptr< Texture > texture;
std::shared_ptr< Texture > cur_texture;
2020-11-21 19:57:40 +00:00
std::shared_ptr< Renderer > renderer;
std::shared_ptr< CollisionPolygon > polygon;
double movementSpeed = 0;
2021-04-24 21:26:27 +00:00
Vec2D< double > movementDirection = { 0, 0 };
2020-11-21 19:57:40 +00:00
std::vector< std::shared_ptr< RenderObject > > colidedWith;
virtual Vec2D< double > computeAlignmentAdditions();
2020-11-21 19:57:40 +00:00
uint64_t id = -1;
bool hidden = false;
bool kill = false;
2021-03-13 14:05:16 +00:00
SDL_Color colider_color = { 0x00, 0xFF, 0xFF, 0xFF };
2020-11-21 19:57:40 +00:00
uint64_t scene_id = -1;
bool permanent = false;
bool is_static = true;
2021-03-07 11:20:00 +00:00
bool centerx = false;
SDL_Rect rect;
SDL_Rect src_rect = { -1, -1, -1, -1 };
2021-03-07 13:06:55 +00:00
size_t animation_index = 0;
size_t animation_fps = 1;
int animation_next_frame = 1000;
2021-04-24 21:26:27 +00:00
int animation_next_frame_base = 0;
2021-03-07 13:06:55 +00:00
std::vector< SDL_Rect > animation{};
bool animating = true;
2020-11-21 19:57:40 +00:00
private:
friend Scene;
void setSceneID( int id );
2021-01-31 20:48:48 +00:00
protected:
Vec2D< double > original;
Vec2D< double > current;
ObjectAlignment _horizontal = OBJ_START;
ObjectAlignment _vertical = OBJ_START;
2020-11-21 19:57:40 +00:00
};
} // end of namespace SDLPP
#endif