Some basic formatting

This commit is contained in:
2020-08-23 09:50:05 +02:00
parent 097fab4329
commit a995ba3b85
4 changed files with 1277 additions and 999 deletions
+108 -87
View File
@@ -4,165 +4,186 @@
#include <iostream>
bool SDLPP::init() {
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
if( TTF_Init() == -1 ) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << std::endl;
if ( TTF_Init() == -1 ) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: "
<< TTF_GetError() << std::endl;
return false;
}
return true;
}
bool SDLPP::init(uint32_t SDL_OPTIONS) {
if( SDL_Init(SDL_OPTIONS) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
bool SDLPP::init( uint32_t SDL_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
return true;
}
bool SDLPP::init(uint32_t SDL_OPTIONS, int IMAGE_OPTIONS) {
if( SDL_Init(SDL_OPTIONS) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
bool SDLPP::init( uint32_t SDL_OPTIONS, int IMAGE_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
if ( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
return true;
}
void SDLPP::CircleRender::render() {
std::cout << "I'm a circle, look at me go!" << std::endl << "My dimensions are: [" << x_ << ", " << y_ << "], radius: " << rad_ << std::endl;
std::cout << "I'm a circle, look at me go!" << std::endl
<< "My dimensions are: [" << x_ << ", " << y_
<< "], radius: " << rad_ << std::endl;
}
// only rectangles for now
bool intersects(const SDLPP::CollisionPolygon &p1, const SDLPP::CollisionPolygon &p2) {
return !(p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost());
bool intersects( const SDLPP::CollisionPolygon &p1,
const SDLPP::CollisionPolygon &p2 ) {
return !(
p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost() );
}
bool infinityIntersection(const SDLPP::CollisionPolygon &infinite, const SDLPP::CollisionPolygon &other) {
bool infinityIntersection( const SDLPP::CollisionPolygon &infinite,
const SDLPP::CollisionPolygon &other ) {
int ileft = infinite.leftmost();
int iright = infinite.rightmost();
int itop = infinite.topmost();
int ibottom = infinite.bottommost();
bool ret = ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
ret |= iright != -1 && iright >= other.leftmost() && iright <= other.rightmost();
bool ret =
ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
ret |= iright != -1 && iright >= other.leftmost() &&
iright <= other.rightmost();
ret |= itop != -1 && itop <= other.bottommost() && itop >= other.topmost();
ret |= ibottom != -1 && ibottom >= other.topmost() && ibottom <= other.bottommost();
ret |= ibottom != -1 && ibottom >= other.topmost() &&
ibottom <= other.bottommost();
ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
return ret;
}
bool SDLPP::Rect::colidesWith(const SDLPP::CollisionPolygon &other) const {
if(other.isCircle()) {
return other.colidesWith(*this);
bool SDLPP::Rect::colidesWith( const SDLPP::CollisionPolygon &other ) const {
if ( other.isCircle() ) {
return other.colidesWith( *this );
}
if(other.isInfinite() ) {
return infinityIntersection(other, *this);
if ( other.isInfinite() ) {
return infinityIntersection( other, *this );
}
if(isInfinite())
return infinityIntersection(*this, other);
return intersects(*this, other);
if ( isInfinite() )
return infinityIntersection( *this, other );
return intersects( *this, other );
}
bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const {
if(other.isCircle()) {
int otherRad = (other.rightmost() - other.leftmost())/2;
bool SDLPP::Circle::colidesWith( const SDLPP::CollisionPolygon &other ) const {
if ( other.isCircle() ) {
int otherRad = ( other.rightmost() - other.leftmost() ) / 2;
int thisRad = getRadius();
int totalDist = otherRad + thisRad;
int xdiff = other.leftmost() + otherRad - (leftmost() + thisRad);
int ydiff = other.topmost() + otherRad - (topmost() + thisRad);
return (xdiff*xdiff + ydiff*ydiff) <= totalDist * totalDist;
} else if (other.isInfinite()) {
return infinityIntersection(other, *this);
int xdiff = other.leftmost() + otherRad - ( leftmost() + thisRad );
int ydiff = other.topmost() + otherRad - ( topmost() + thisRad );
return ( xdiff * xdiff + ydiff * ydiff ) <= totalDist * totalDist;
} else if ( other.isInfinite() ) {
return infinityIntersection( other, *this );
}
int rad = rad_;
int centerx = getX();
int centery = getY();
if(other.topmost() <= centery && other.bottommost() >= centery) {
return other.leftmost() <= rightmost() && other.rightmost() >= leftmost();
} else if (other.leftmost() <= centerx && other.rightmost() >= centerx) {
return other.topmost() <= bottommost() && other.bottommost() >= topmost();
if ( other.topmost() <= centery && other.bottommost() >= centery ) {
return other.leftmost() <= rightmost() &&
other.rightmost() >= leftmost();
} else if ( other.leftmost() <= centerx && other.rightmost() >= centerx ) {
return other.topmost() <= bottommost() &&
other.bottommost() >= topmost();
}
int pointx=0, pointy=0;
if(centerx > other.rightmost()) {
int pointx = 0, pointy = 0;
if ( centerx > other.rightmost() ) {
pointx = other.rightmost();
} else {
pointx = other.leftmost();
}
if(centery < other.topmost()) {
if ( centery < other.topmost() ) {
pointy = other.topmost();
} else {
pointy = other.bottommost();
}
int distancesquared = (pointx - centerx)*(pointx - centerx) + (pointy - centery)*(pointy-centery);
return distancesquared <= rad*rad;
int distancesquared = ( pointx - centerx ) * ( pointx - centerx ) +
( pointy - centery ) * ( pointy - centery );
return distancesquared <= rad * rad;
}
int SDLPP::hex2num(char c) {
if(c <= '9')
int SDLPP::hex2num( char c ) {
if ( c <= '9' )
return c - '0';
switch(c) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
default:
return 15;
switch ( c ) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
default:
return 15;
}
}
std::tuple<int, int, int, int> SDLPP::getColorsHEX(const std::string &color) {
std::tuple< int, int, int, int >
SDLPP::getColorsHEX( const std::string &color ) {
int red = 0, green = 0, blue = 0, alpha = 255;
const char *color_ptr = color.c_str();
if(color_ptr[0] == '#')
if ( color_ptr[0] == '#' )
color_ptr++;
red = hex2num(color_ptr[0])*16 + hex2num(color_ptr[1]);
green = hex2num(color_ptr[2])*16 + hex2num(color_ptr[3]);
blue = hex2num(color_ptr[4])*16 + hex2num(color_ptr[5]);
if( color_ptr[6] != '\0' )
alpha = hex2num(color_ptr[6])*16 + hex2num(color_ptr[7]);
return {red, green, blue, alpha};
red = hex2num( color_ptr[0] ) * 16 + hex2num( color_ptr[1] );
green = hex2num( color_ptr[2] ) * 16 + hex2num( color_ptr[3] );
blue = hex2num( color_ptr[4] ) * 16 + hex2num( color_ptr[5] );
if ( color_ptr[6] != '\0' )
alpha = hex2num( color_ptr[6] ) * 16 + hex2num( color_ptr[7] );
return { red, green, blue, alpha };
}
SDL_Color SDLPP::getSDLColorTuple(const std::tuple<int, int, int, int> &tuple) {
SDL_Color
SDLPP::getSDLColorTuple( const std::tuple< int, int, int, int > &tuple ) {
SDL_Color ret_color{};
ret_color.r = std::get<0>(tuple);
ret_color.g = std::get<1>(tuple);
ret_color.b = std::get<2>(tuple);
ret_color.a = std::get<3>(tuple);
ret_color.r = std::get< 0 >( tuple );
ret_color.g = std::get< 1 >( tuple );
ret_color.b = std::get< 2 >( tuple );
ret_color.a = std::get< 3 >( tuple );
return ret_color;
}
SDL_Color SDLPP::getSDLColorHEX(const std::string &color) {
auto color_tuple = SDLPP::getColorsHEX(color);
return getSDLColorTuple(color_tuple);
SDL_Color SDLPP::getSDLColorHEX( const std::string &color ) {
auto color_tuple = SDLPP::getColorsHEX( color );
return getSDLColorTuple( color_tuple );
}
std::tuple<int, int, int, int> SDLPP::getColorsSDLColor(const SDL_Color &color) {
return {color.r, color.g, color.b, color.a};
std::tuple< int, int, int, int >
SDLPP::getColorsSDLColor( const SDL_Color &color ) {
return { color.r, color.g, color.b, color.a };
}