game/sdlpp/sdlpp_renderobject.hpp

178 lines
6.6 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 "sdlpp_vector.hpp"
#include "sdlpp_visitor.hpp"
#include <memory>
#include <vector>
#ifndef _WIN32
#include <SDL2/SDL_render.h>
#else
#include "SDL2/SDL_render.h"
#endif
namespace SDLPP {
enum ObjectAlignment {
OBJ_START,
OBJ_CENTER,
OBJ_END,
};
class 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< Vec2D< double >, Vec2D< double > >
getDoubleRect() const = 0;
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;
std::vector< uint64_t > 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(),
getId() );
}
template < class T > void addCollision( const std::shared_ptr< T > &p ) {
collisions.push_back( p );
collisions.back()->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight(),
getId() );
}
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( double x, double y );
void setMovement( double x, double y );
Vec2D< double > getMovement();
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;
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 void updateXY();
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;
void setTextureAlpha( uint8_t alpha );
void setTextureSourceRect( const SDL_Rect &source_rect );
void setTextureSourceRect( int x, int y, int w, int h );
SDL_Rect getTextureSourceRect() const;
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 double fps );
void pauseAnimation();
void resumeAnimation();
void removeAnimation();
void animate( int ticks );
virtual void visit( Visitor &visitor );
void setAlignment( ObjectAlignment horizontal, ObjectAlignment vertical );
void flipHorizontally();
void flipVertically();
void setRotationCenter( const Vec2D< double > &center );
void rotateClockwise( int angle );
void rotateCounterClockwise( int angle );
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;
Vec2D< double > movementDirection = { 0, 0 };
std::vector< std::shared_ptr< RenderObject > > colidedWith;
virtual Vec2D< double > computeAlignmentAdditions();
uint64_t id = -1;
bool hidden = false;
bool kill = false;
SDL_Color 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;
int animation_next_frame_base = 0;
std::vector< SDL_Rect > animation{};
bool animating = true;
SDL_RendererFlip flip = SDL_FLIP_NONE;
Vec2D< double > rotation_center = { -1, -1 };
SDL_Point rotation_center_point;
int rotation_angle = 0;
private:
friend Scene;
void setSceneID( int id );
protected:
Vec2D< double > original;
Vec2D< double > current;
ObjectAlignment _horizontal = OBJ_START;
ObjectAlignment _vertical = OBJ_START;
};
} // end of namespace SDLPP
#endif