SDLPP: formatting, make compileable on Linux

This commit is contained in:
zvon 2021-03-13 18:36:29 +01:00
parent d2cf54556e
commit 2f20661b5b
16 changed files with 118 additions and 100 deletions

View File

@ -3,7 +3,7 @@
namespace SDLPP { namespace SDLPP {
CircleColider::CircleColider( double x, double y, double rad ) CircleColider::CircleColider( double x, double y, double rad )
: CircleColider( { x, y }, rad ){}; : CircleColider( { x, y }, rad ) {}
CircleColider::CircleColider( const Vec2D< double > &center, double rad ) CircleColider::CircleColider( const Vec2D< double > &center, double rad )
: CollisionPolygon( center ), original_rad( rad ) {} : CollisionPolygon( center ), original_rad( rad ) {}

View File

@ -10,7 +10,7 @@ namespace SDLPP {
class SDLPPSCOPE CircleColider : public CollisionPolygon { class SDLPPSCOPE CircleColider : public CollisionPolygon {
public: public:
CircleColider( double x, double y, double rad ); CircleColider( double x, double y, double rad );
CircleColider( const Vec2D<double> &center, double rad ); CircleColider( const Vec2D< double > &center, double rad );
virtual ~CircleColider() {} virtual ~CircleColider() {}
virtual bool colidesWith( const CollisionPolygon &other ) const override; virtual bool colidesWith( const CollisionPolygon &other ) const override;
@ -25,8 +25,8 @@ public:
const SDL_Color &outline_color ) override; const SDL_Color &outline_color ) override;
virtual void render( Renderer &renderer, const SDL_Color &color ) override; virtual void render( Renderer &renderer, const SDL_Color &color ) override;
virtual void render( Renderer &renderer ) override; virtual void render( Renderer &renderer ) override;
virtual std::shared_ptr<CollisionPolygon> copySelf() override; virtual std::shared_ptr< CollisionPolygon > copySelf() override;
virtual std::vector<Line<int>> getLines() const override; virtual std::vector< Line< int > > getLines() const override;
private: private:
int getRadius() const; int getRadius() const;

View File

@ -28,7 +28,7 @@ CircleRender::CircleRender( Vec2D< double > center, double rad,
CircleRender::CircleRender( Vec2D< double > center, double rad, CircleRender::CircleRender( Vec2D< double > center, double rad,
std::shared_ptr< Renderer > &r, std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &/*UNUSED*/ ) std::shared_ptr< Texture > & /*UNUSED*/ )
: CircleRender( center, rad, r ) { : CircleRender( center, rad, r ) {
throw "I don't support textures yet!!!"; throw "I don't support textures yet!!!";
} }
@ -63,9 +63,10 @@ void CircleRender::setOutlineColor( const std::string &color ) {
polygon->setOutlineColor( color ); polygon->setOutlineColor( color );
} }
std::pair< Vec2D<double>, Vec2D<double> > std::pair< Vec2D< double >, Vec2D< double > >
CircleRender::getDoubleRect() const { CircleRender::getDoubleRect() const {
return { { original.getX() - og_r, original.getY() - og_r }, { 2 * og_r, 2 * og_r } }; return { { original.getX() - og_r, original.getY() - og_r },
{ 2 * og_r, 2 * og_r } };
} }
int CircleRender::leftmost() { int CircleRender::leftmost() {

View File

@ -11,22 +11,28 @@ class SDLPPSCOPE CircleRender : public RenderObject {
public: public:
CircleRender() = delete; CircleRender() = delete;
virtual ~CircleRender(){}; virtual ~CircleRender(){};
CircleRender( double x, double y, double rad, std::shared_ptr< Renderer > &r ); CircleRender( double x, double y, double rad,
CircleRender( double x, double y, double rad, std::shared_ptr< Renderer > &r, std::shared_ptr< Renderer > &r );
CircleRender( double x, double y, double rad,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t ); std::shared_ptr< Texture > &t );
CircleRender( double x, double y, double rad, std::shared_ptr< Renderer > &r, CircleRender( double x, double y, double rad,
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 );
CircleRender( Vec2D<double> center, double rad, std::shared_ptr< Renderer > &r ); CircleRender( Vec2D< double > center, double rad,
CircleRender( Vec2D<double> center, double rad, std::shared_ptr< Renderer > &r, std::shared_ptr< Renderer > &r );
CircleRender( Vec2D< double > center, double rad,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t ); std::shared_ptr< Texture > &t );
CircleRender( Vec2D<double> center, double rad, std::shared_ptr< Renderer > &r, CircleRender( Vec2D< double > center, double rad,
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 );
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 {}
virtual void custom_move( int /*UNUSED*/ ) override{} virtual void custom_move( int /*UNUSED*/ ) override {}
virtual std::pair< Vec2D<double>, Vec2D<double> > virtual std::pair< Vec2D< double >, Vec2D< double > >
getDoubleRect() const override; getDoubleRect() const override;
virtual int leftmost() override; virtual int leftmost() override;
virtual int topmost() override; virtual int topmost() override;
@ -43,7 +49,7 @@ public:
std::string getColor() const; std::string getColor() const;
private: private:
virtual void copyTo(std::shared_ptr<RenderObject> other) override; virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY(); void updateXY();
double og_r; double og_r;
double r_; double r_;

View File

@ -3,7 +3,7 @@
namespace SDLPP { namespace SDLPP {
CollisionPolygon::CollisionPolygon( double x, double y ) CollisionPolygon::CollisionPolygon( double x, double y )
: CollisionPolygon( Vec2D<double>( x, y ) ) {} : CollisionPolygon( Vec2D< double >( x, y ) ) {}
CollisionPolygon::CollisionPolygon( const Vec2D< double > &input ) { CollisionPolygon::CollisionPolygon( const Vec2D< double > &input ) {
original = input; original = input;
@ -54,11 +54,12 @@ bool infinityIntersection( const SDLPP::CollisionPolygon &infinite,
bool intersects( const SDLPP::CollisionPolygon &p1, bool intersects( const SDLPP::CollisionPolygon &p1,
const SDLPP::CollisionPolygon &p2 ) { const SDLPP::CollisionPolygon &p2 ) {
if(p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() || p1.bottommost() < p2.topmost() || p2.bottommost() < p1.topmost()) if ( p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.bottommost() < p2.topmost() || p2.bottommost() < p1.topmost() )
return false; return false;
for(auto &line : p1.getLines()) { for ( auto &line : p1.getLines() ) {
for(auto &line2 : p2.getLines()) { for ( auto &line2 : p2.getLines() ) {
if(linesCross(line, line2)) if ( linesCross( line, line2 ) )
return true; return true;
} }
} }

View File

@ -1,8 +1,8 @@
#include "sdlpp_font.hpp" #include "sdlpp_font.hpp"
namespace SDLPP { namespace SDLPP {
Font::Font( const std::string &font ) : font_path(font) {} Font::Font( const std::string &font ) : font_path( font ) {}
Font::Font( const std::string &font, int size ) : Font(font) { Font::Font( const std::string &font, int size ) : Font( font ) {
original_size = size; original_size = size;
font_ptr = TTF_OpenFont( font_path.c_str(), size ); font_ptr = TTF_OpenFont( font_path.c_str(), size );
checkFont(); checkFont();

View File

@ -3,6 +3,7 @@
#include "sdlpp_common.hpp" #include "sdlpp_common.hpp"
#include "sdlpp_font.hpp" #include "sdlpp_font.hpp"
#include <memory>
namespace SDLPP { namespace SDLPP {
class SDLPPSCOPE FontConfiguration { class SDLPPSCOPE FontConfiguration {

View File

@ -6,24 +6,22 @@
#include <cmath> #include <cmath>
namespace SDLPP { namespace SDLPP {
template<typename T> template < typename T >
double vecDotProduct( const Vec2D<T> &a, const Vec2D<T> &b ) { double vecDotProduct( const Vec2D< T > &a, const Vec2D< T > &b ) {
return a * b; return a * b;
} }
template<typename T> template < typename T > double vecLengthSquared( const Vec2D< T > &vec ) {
double vecLengthSquared( const Vec2D<T> &vec ) {
return vecDotProduct( vec, vec ); return vecDotProduct( vec, vec );
} }
template<typename T> template < typename T > double vecLength( const Vec2D< T > &vec ) {
double vecLength( const Vec2D<T> &vec ) {
return std::sqrt( vecLengthSquared( vec ) ); return std::sqrt( vecLengthSquared( vec ) );
} }
template<typename T> template < typename T >
double vecDistanceSquared( const Vec2D<T> &a, const Vec2D<T> &b ) { double vecDistanceSquared( const Vec2D< T > &a, const Vec2D< T > &b ) {
return vecLengthSquared( a - b ); return vecLengthSquared( a - b );
} }
template<typename T> template < typename T >
double vecDistance( const Vec2D<T> &a, const Vec2D<T> &b ) { double vecDistance( const Vec2D< T > &a, const Vec2D< T > &b ) {
return vecLength( a - b ); return vecLength( a - b );
} }
} // namespace SDLPP } // namespace SDLPP

View File

@ -6,37 +6,37 @@
#include <iostream> #include <iostream>
namespace SDLPP { namespace SDLPP {
template<typename T> template < typename T > class SDLPPSCOPE Line {
class SDLPPSCOPE Line {
public: public:
Line() = delete; Line() = delete;
~Line() = default; ~Line() = default;
Line( const Vec2D<T> &start, const Vec2D<T> &end ) Line( const Vec2D< T > &start, const Vec2D< T > &end )
: _start( start ), _end( end ) { : _start( start ), _end( end ) {
updateMost(); updateMost();
} }
Line( T x_1, T y_1, T x_2, T y_2 ) Line( T x_1, T y_1, T x_2, T y_2 ) : Line( { x_1, y_1 }, { x_2, y_2 } ) {}
: Line( { x_1, y_1 }, { x_2, y_2 } ) {} Line( const Vec2D< T > &start, const Vec2D< T > &end, bool infinite )
Line( const Vec2D<T> &start, const Vec2D<T> &end, bool infinite ) : Line( start, end ) {
: Line( start, end ), _infinite( infinite ) {} _infinite = infinite;
}
Line( T x_1, T y_1, T x_2, T y_2, bool infinite ) Line( T x_1, T y_1, T x_2, T y_2, bool infinite )
: Line( { x_1, y_1 }, { x_2, y_2 }, infinite ) {} : Line( { x_1, y_1 }, { x_2, y_2 }, infinite ) {}
Line(const Line &input) : Line(input.getStart(), input.getEnd()) {} Line( const Line &input ) : Line( input.getStart(), input.getEnd() ) {}
Line &operator=(const Line &input) { Line &operator=( const Line &input ) {
_start = input.getStart(); _start = input.getStart();
_end = input.getEnd(); _end = input.getEnd();
updateMost(); updateMost();
return *this; return *this;
} }
void updateMost() { void updateMost() {
if(_start.getY() < _end.getY()) { if ( _start.getY() < _end.getY() ) {
top = &_start; top = &_start;
bottom = &_end; bottom = &_end;
} else { } else {
top = &_end; top = &_end;
bottom = &_start; bottom = &_start;
} }
if(_start.getX() < _end.getX()) { if ( _start.getX() < _end.getX() ) {
left = &_start; left = &_start;
right = &_end; right = &_end;
} else { } else {
@ -44,10 +44,10 @@ public:
right = &_start; right = &_start;
} }
} }
const Vec2D<T> &getStart() const { const Vec2D< T > &getStart() const {
return _start; return _start;
} }
const Vec2D<T> &getEnd() const { const Vec2D< T > &getEnd() const {
return _end; return _end;
} }
double length() const { double length() const {
@ -62,30 +62,30 @@ public:
bool isInfinite() { bool isInfinite() {
return _infinite; return _infinite;
} }
void add(const Vec2D<T> &vec) { void add( const Vec2D< T > &vec ) {
_start += vec; _start += vec;
_end += vec; _end += vec;
} }
const Vec2D<T> &topmost() const { const Vec2D< T > &topmost() const {
return *top; return *top;
} }
const Vec2D<T> &bottomost() const { const Vec2D< T > &bottomost() const {
return *bottom; return *bottom;
} }
const Vec2D<T> &leftmost() const { const Vec2D< T > &leftmost() const {
return *left; return *left;
} }
const Vec2D<T> &rightmost() const { const Vec2D< T > &rightmost() const {
return *right; return *right;
} }
private: private:
Vec2D<T> _start; Vec2D< T > _start;
Vec2D<T> _end; Vec2D< T > _end;
Vec2D<T> *top = nullptr; Vec2D< T > *top = nullptr;
Vec2D<T> *bottom = nullptr; Vec2D< T > *bottom = nullptr;
Vec2D<T> *left = nullptr; Vec2D< T > *left = nullptr;
Vec2D<T> *right = nullptr; Vec2D< T > *right = nullptr;
bool _infinite = false; bool _infinite = false;
}; };
} // namespace SDLPP } // namespace SDLPP

View File

@ -67,7 +67,7 @@ void LineRenderer::setPos( double x, double y ) {
void LineRenderer::setPos( const std::pair< double, double > &pos ) { void LineRenderer::setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second ); setPos( pos.first, pos.second );
} }
void LineRenderer::setPos( const Vec2D<double> &vec ) { void LineRenderer::setPos( const Vec2D< double > &vec ) {
setPos( vec.getX(), vec.getY() ); setPos( vec.getX(), vec.getY() );
} }
Vec2D< double > LineRenderer::getPos() const { Vec2D< double > LineRenderer::getPos() const {
@ -135,7 +135,7 @@ SDL_Rect LineRenderer::getRect() {
return { leftmost(), topmost(), rightmost() - leftmost(), return { leftmost(), topmost(), rightmost() - leftmost(),
bottommost() - topmost() }; bottommost() - topmost() };
} }
std::pair< Vec2D<double>, Vec2D<double> > std::pair< Vec2D< double >, Vec2D< double > >
LineRenderer::getDoubleRect() const { LineRenderer::getDoubleRect() const {
return { original.getStart(), original.getEnd() - original.getStart() }; return { original.getStart(), original.getEnd() - original.getStart() };
} }
@ -151,14 +151,16 @@ void LineRenderer::updateXY() {
if ( width > height ) { if ( width > height ) {
auto multiplier = auto multiplier =
static_cast< double >( width ) / static_cast< double >( height ); static_cast< double >( width ) / static_cast< double >( height );
x1_ = original.getStart().getX() + static_cast< double >( multiplier - 1 ) / 2; x1_ = original.getStart().getX() +
x2_ = original.getEnd().getX() + static_cast< double >( multiplier - 1 ) / 2; static_cast< double >( multiplier - 1 ) / 2;
x2_ = original.getEnd().getX() +
static_cast< double >( multiplier - 1 ) / 2;
} else { } else {
x1_ = original.getStart().getX(); x1_ = original.getStart().getX();
x2_ = original.getEnd().getX(); x2_ = original.getEnd().getX();
} }
y1_ = original.getStart().getY(); y1_ = original.getStart().getY();
y2_ = original.getEnd().getY(); y2_ = original.getEnd().getY();
current = {{x1_, y1_}, {x2_, y2_}}; current = { { x1_, y1_ }, { x2_, y2_ } };
} }
} // namespace SDLPP } // namespace SDLPP

View File

@ -16,9 +16,9 @@ public:
LineRenderer( double x1, double y1, double x2, double y2, LineRenderer( double x1, double y1, double x2, double y2,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::string &color ); const std::string &color );
LineRenderer( const Line<double> &line, LineRenderer( const Line< double > &line,
const std::shared_ptr< Renderer > &r ); const std::shared_ptr< Renderer > &r );
LineRenderer( const Line<double> &line, LineRenderer( const Line< double > &line,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::string &color ); const std::string &color );
virtual void setColor( const std::string &color ) override; virtual void setColor( const std::string &color ) override;
@ -28,8 +28,8 @@ public:
virtual void custom_move( int /*UNUSED*/ ) override {} virtual void custom_move( int /*UNUSED*/ ) override {}
virtual void setPos( double x, double y ) override; virtual void setPos( double x, double y ) override;
virtual void setPos( const std::pair< double, double > &pos ) override; virtual void setPos( const std::pair< double, double > &pos ) override;
virtual void setPos( const Vec2D<double> &vec ) override; virtual void setPos( const Vec2D< double > &vec ) override;
virtual Vec2D<double> getPos() const override; virtual Vec2D< double > getPos() const override;
virtual int leftmost() override; virtual int leftmost() override;
virtual int topmost() override; virtual int topmost() override;
virtual int rightmost() override; virtual int rightmost() override;
@ -43,16 +43,16 @@ public:
virtual std::shared_ptr< RenderObject > copySelf() override; virtual std::shared_ptr< RenderObject > copySelf() override;
virtual SDL_Rect getRect() override; virtual SDL_Rect getRect() override;
virtual std::pair< Vec2D<double>, Vec2D<double>> virtual std::pair< Vec2D< double >, Vec2D< double > >
getDoubleRect() const override; getDoubleRect() const override;
void setOutlineColor( const std::string & /*UNUSED*/ ) override {} void setOutlineColor( const std::string & /*UNUSED*/ ) override {}
protected: protected:
virtual void copyTo(std::shared_ptr<RenderObject> other) override; virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY(); void updateXY();
Line<double> original = {{0, 0}, {0, 0}}; Line< double > original = { { 0, 0 }, { 0, 0 } };
Line<double> current = {{0, 0}, {0, 0}}; Line< double > current = { { 0, 0 }, { 0, 0 } };
Line<int> pixel_line = {{0, 0}, {0, 0}}; Line< int > pixel_line = { { 0, 0 }, { 0, 0 } };
bool centerx = false; bool centerx = false;
std::tuple< int, int, int, int > _color; std::tuple< int, int, int, int > _color;
}; };

View File

@ -139,9 +139,11 @@ void RectangleRender::updateSizeAndPosition() {
rect.x = std::round( current.getX() * dimension ); rect.x = std::round( current.getX() * dimension );
rect.y = std::round( current.getY() * dimension ); rect.y = std::round( current.getY() * dimension );
rect.w = rect.w =
std::round( ( current.getX() + original_size.getX() ) * dimension ) - rect.x; std::round( ( current.getX() + original_size.getX() ) * dimension ) -
rect.x;
rect.h = rect.h =
std::round( ( current.getY() + original_size.getY() ) * dimension ) - rect.y; std::round( ( current.getY() + original_size.getY() ) * dimension ) -
rect.y;
if ( polygon ) if ( polygon )
polygon->updateCollision( collisionPushX(), collisionPushY(), polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() ); collisionWidth(), collisionHeight() );

View File

@ -32,31 +32,38 @@ public:
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::string &img, const SDL_Rect &source_rect ); const std::string &img, const SDL_Rect &source_rect );
RectangleRender( const Vec2D<double> &top_left, const Vec2D<double> &size, RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r ); const std::shared_ptr< Renderer > &r );
RectangleRender( const Vec2D<double> &top_left, const Vec2D<double> &size, RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t, int source_x, const std::shared_ptr< Texture > &t, int source_x,
int source_y, int source_width, int source_height ); int source_y, int source_width, int source_height );
RectangleRender( const Vec2D<double> &top_left, const Vec2D<double> &size, RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t, const std::shared_ptr< Texture > &t,
const SDL_Rect &source_rect = { -1, -1, -1, -1 } ); const SDL_Rect &source_rect = { -1, -1, -1, -1 } );
RectangleRender( const Vec2D<double> &top_left, const Vec2D<double> &size, RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
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( const Vec2D<double> &top_left, const Vec2D<double> &size, RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::string &img, int source_x, int source_y, const std::string &img, int source_x, int source_y,
int source_width, int source_height ); int source_width, int source_height );
RectangleRender( const Vec2D<double> &top_left, const Vec2D<double> &size, RectangleRender( const Vec2D< double > &top_left,
const Vec2D< double > &size,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::string &img, const 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 {}
virtual void custom_move( int /*UNUSED*/ ) override {} virtual void custom_move( int /*UNUSED*/ ) override {}
virtual std::pair< Vec2D<double>, Vec2D<double> > getDoubleRect() const override; virtual std::pair< Vec2D< double >, Vec2D< double > >
getDoubleRect() const override;
virtual int leftmost() override; virtual int leftmost() override;
virtual int topmost() override; virtual int topmost() override;
virtual int rightmost() override; virtual int rightmost() override;
@ -74,8 +81,8 @@ public:
protected: protected:
virtual void copyTo( std::shared_ptr< RenderObject > other ) override; virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY(); void updateXY();
Vec2D<double> original_size; Vec2D< double > original_size;
Vec2D<double> size; Vec2D< double > size;
std::string color = ""; std::string color = "";
}; };
} // end of namespace SDLPP } // end of namespace SDLPP

View File

@ -34,7 +34,7 @@ void RenderObject::setPos( double x, double y ) {
void RenderObject::setPos( const std::pair< double, double > &pos ) { void RenderObject::setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second ); setPos( pos.first, pos.second );
} }
void RenderObject::setPos( const Vec2D<double> &vec ) { void RenderObject::setPos( const Vec2D< double > &vec ) {
setPos( vec.getX(), vec.getY() ); setPos( vec.getX(), vec.getY() );
} }
Vec2D< double > RenderObject::getPos() const { Vec2D< double > RenderObject::getPos() const {

View File

@ -31,7 +31,7 @@ public:
getDoubleRect() const = 0; getDoubleRect() const = 0;
virtual void setPos( double x, double y ); virtual void setPos( double x, double y );
virtual void setPos( const std::pair< double, double > &pos ); virtual void setPos( const std::pair< double, double > &pos );
virtual void setPos( const Vec2D<double> &vec ); virtual void setPos( const Vec2D< double > &vec );
virtual Vec2D< double > getPos() const; virtual Vec2D< double > getPos() const;
bool colidesWith( const RenderObject &other ) const; bool colidesWith( const RenderObject &other ) const;
template < class T > void addCollision( const T &p ) { template < class T > void addCollision( const T &p ) {

View File

@ -4,53 +4,53 @@
#include "sdlpp_common.hpp" #include "sdlpp_common.hpp"
namespace SDLPP { namespace SDLPP {
template<typename T> template < typename T > class SDLPPSCOPE Vec2D {
class SDLPPSCOPE Vec2D {
public: public:
Vec2D() = default; Vec2D() = default;
~Vec2D() = default; ~Vec2D() = default;
Vec2D(T x, T y) : _x(x), _y(y) {} Vec2D( T x, T y ) : _x( x ), _y( y ) {}
T getX() const { T getX() const {
return _x; return _x;
} }
T getY() const { T getY() const {
return _y; return _y;
} }
Vec2D operator-(const Vec2D &other) const { Vec2D operator-( const Vec2D &other ) const {
return Vec2D( getX() - other.getX(), getY() - other.getY() ); return Vec2D( getX() - other.getX(), getY() - other.getY() );
} }
Vec2D operator+(const Vec2D &other) const { Vec2D operator+( const Vec2D &other ) const {
return Vec2D( getX() + other.getX(), getY() + other.getY() ); return Vec2D( getX() + other.getX(), getY() + other.getY() );
} }
Vec2D operator*(double multiplier) const { Vec2D operator*( double multiplier ) const {
return Vec2D( getX() * multiplier, getY() * multiplier ); return Vec2D( getX() * multiplier, getY() * multiplier );
} }
Vec2D operator/(double divisor) const { Vec2D operator/( double divisor ) const {
return *this * ( 1.0 / divisor ); return *this * ( 1.0 / divisor );
} }
T operator*(const Vec2D &other) const { T operator*( const Vec2D &other ) const {
return getX() * other.getX() + getY() * other.getY(); return getX() * other.getX() + getY() * other.getY();
} }
Vec2D &operator+=(const Vec2D &other) { Vec2D &operator+=( const Vec2D &other ) {
*this = *this + other; *this = *this + other;
return *this; return *this;
} }
Vec2D &operator-=(const Vec2D &other) { Vec2D &operator-=( const Vec2D &other ) {
*this = *this - other; *this = *this - other;
return *this; return *this;
} }
private: private:
T _x = 0.0; T _x = 0.0;
T _y = 0.0; T _y = 0.0;
}; };
template<typename T> template < typename T >
Vec2D<T> operator*(double multiplier, const Vec2D<T> &vec) { Vec2D< T > operator*( double multiplier, const Vec2D< T > &vec ) {
return vec * multiplier; return vec * multiplier;
} }
template<typename T> template < typename T >
Vec2D<T> operator/(double divisor, const Vec2D<T> &vec) { Vec2D< T > operator/( double divisor, const Vec2D< T > &vec ) {
return vec / divisor; return vec / divisor;
} }
} } // namespace SDLPP
#endif #endif