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 {
CircleColider::CircleColider( double x, double y, double rad )
: CircleColider( { x, y }, rad ){};
: CircleColider( { x, y }, rad ) {}
CircleColider::CircleColider( const Vec2D< double > &center, double rad )
: CollisionPolygon( center ), original_rad( rad ) {}

View File

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

View File

@ -28,7 +28,7 @@ CircleRender::CircleRender( Vec2D< double > center, double rad,
CircleRender::CircleRender( Vec2D< double > center, double rad,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &/*UNUSED*/ )
std::shared_ptr< Texture > & /*UNUSED*/ )
: CircleRender( center, rad, r ) {
throw "I don't support textures yet!!!";
}
@ -63,9 +63,10 @@ void CircleRender::setOutlineColor( const std::string &color ) {
polygon->setOutlineColor( color );
}
std::pair< Vec2D<double>, Vec2D<double> >
std::pair< Vec2D< double >, Vec2D< double > >
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() {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -32,31 +32,38 @@ public:
const std::shared_ptr< Renderer > &r,
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 );
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< Texture > &t, int source_x,
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< Texture > &t,
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::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::string &img, int source_x, 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::string &img, const SDL_Rect &source_rect );
virtual void setColor( const std::string &color ) override;
virtual void setOutlineColor( const std::string &color ) override;
virtual void specialAction( 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 topmost() override;
virtual int rightmost() override;
@ -74,8 +81,8 @@ public:
protected:
virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY();
Vec2D<double> original_size;
Vec2D<double> size;
Vec2D< double > original_size;
Vec2D< double > size;
std::string color = "";
};
} // 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 ) {
setPos( pos.first, pos.second );
}
void RenderObject::setPos( const Vec2D<double> &vec ) {
void RenderObject::setPos( const Vec2D< double > &vec ) {
setPos( vec.getX(), vec.getY() );
}
Vec2D< double > RenderObject::getPos() const {

View File

@ -31,7 +31,7 @@ public:
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 void setPos( const Vec2D< double > &vec );
virtual Vec2D< double > getPos() const;
bool colidesWith( const RenderObject &other ) const;
template < class T > void addCollision( const T &p ) {

View File

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