2020-07-26 20:37:20 +00:00
|
|
|
#include "sdlpp.hpp"
|
|
|
|
|
|
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
bool SDLPP::init() {
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
|
|
|
|
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
|
|
|
|
<< std::endl;
|
2020-07-26 20:37:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
|
|
|
|
std::cerr << "SDL_image could not initialize! SDL_image Error: "
|
|
|
|
<< IMG_GetError() << std::endl;
|
2020-07-26 20:37:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( TTF_Init() == -1 ) {
|
|
|
|
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: "
|
|
|
|
<< TTF_GetError() << std::endl;
|
2020-08-22 11:17:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-26 20:37:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-07-26 20:37:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
|
|
|
|
std::cerr << "SDL_image could not initialize! SDL_image Error: "
|
|
|
|
<< IMG_GetError() << std::endl;
|
2020-07-26 20:37:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-07-26 20:37:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) {
|
|
|
|
std::cerr << "SDL_image could not initialize! SDL_image Error: "
|
|
|
|
<< IMG_GetError() << std::endl;
|
2020-07-26 20:37:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLPP::CircleRender::render() {
|
2020-08-23 07:50:05 +00:00
|
|
|
std::cout << "I'm a circle, look at me go!" << std::endl
|
|
|
|
<< "My dimensions are: [" << x_ << ", " << y_
|
|
|
|
<< "], radius: " << rad_ << std::endl;
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only rectangles for now
|
2020-08-23 07:50:05 +00:00
|
|
|
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() );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
bool infinityIntersection( const SDLPP::CollisionPolygon &infinite,
|
|
|
|
const SDLPP::CollisionPolygon &other ) {
|
2020-07-26 20:37:20 +00:00
|
|
|
int ileft = infinite.leftmost();
|
|
|
|
int iright = infinite.rightmost();
|
|
|
|
int itop = infinite.topmost();
|
|
|
|
int ibottom = infinite.bottommost();
|
2020-08-23 07:50:05 +00:00
|
|
|
bool ret =
|
|
|
|
ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
|
|
|
|
ret |= iright != -1 && iright >= other.leftmost() &&
|
|
|
|
iright <= other.rightmost();
|
2020-08-22 23:08:10 +00:00
|
|
|
ret |= itop != -1 && itop <= other.bottommost() && itop >= other.topmost();
|
2020-08-23 07:50:05 +00:00
|
|
|
ret |= ibottom != -1 && ibottom >= other.topmost() &&
|
|
|
|
ibottom <= other.bottommost();
|
2020-07-26 20:37:20 +00:00
|
|
|
ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
bool SDLPP::Rect::colidesWith( const SDLPP::CollisionPolygon &other ) const {
|
|
|
|
if ( other.isCircle() ) {
|
|
|
|
return other.colidesWith( *this );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( other.isInfinite() ) {
|
|
|
|
return infinityIntersection( other, *this );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( isInfinite() )
|
|
|
|
return infinityIntersection( *this, other );
|
|
|
|
return intersects( *this, other );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
bool SDLPP::Circle::colidesWith( const SDLPP::CollisionPolygon &other ) const {
|
|
|
|
if ( other.isCircle() ) {
|
|
|
|
int otherRad = ( other.rightmost() - other.leftmost() ) / 2;
|
2020-07-26 20:37:20 +00:00
|
|
|
int thisRad = getRadius();
|
|
|
|
int totalDist = otherRad + thisRad;
|
2020-08-23 07:50:05 +00:00
|
|
|
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 );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
int rad = rad_;
|
|
|
|
int centerx = getX();
|
|
|
|
int centery = getY();
|
2020-08-23 07:50:05 +00:00
|
|
|
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();
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
int pointx = 0, pointy = 0;
|
|
|
|
if ( centerx > other.rightmost() ) {
|
2020-07-26 20:37:20 +00:00
|
|
|
pointx = other.rightmost();
|
|
|
|
} else {
|
|
|
|
pointx = other.leftmost();
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( centery < other.topmost() ) {
|
2020-07-26 20:37:20 +00:00
|
|
|
pointy = other.topmost();
|
|
|
|
} else {
|
|
|
|
pointy = other.bottommost();
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
int distancesquared = ( pointx - centerx ) * ( pointx - centerx ) +
|
|
|
|
( pointy - centery ) * ( pointy - centery );
|
|
|
|
return distancesquared <= rad * rad;
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
2020-08-21 16:32:15 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
int SDLPP::hex2num( char c ) {
|
|
|
|
if ( c <= '9' )
|
2020-08-21 16:32:15 +00:00
|
|
|
return c - '0';
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-08-21 16:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
std::tuple< int, int, int, int >
|
|
|
|
SDLPP::getColorsHEX( const std::string &color ) {
|
2020-08-22 11:17:56 +00:00
|
|
|
int red = 0, green = 0, blue = 0, alpha = 255;
|
2020-08-21 16:32:15 +00:00
|
|
|
const char *color_ptr = color.c_str();
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( color_ptr[0] == '#' )
|
2020-08-21 16:32:15 +00:00
|
|
|
color_ptr++;
|
2020-08-23 07:50:05 +00:00
|
|
|
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 };
|
2020-08-22 11:17:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_Color
|
|
|
|
SDLPP::getSDLColorTuple( const std::tuple< int, int, int, int > &tuple ) {
|
2020-08-22 11:17:56 +00:00
|
|
|
SDL_Color ret_color{};
|
2020-08-23 07:50:05 +00:00
|
|
|
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 );
|
2020-08-22 11:17:56 +00:00
|
|
|
return ret_color;
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_Color SDLPP::getSDLColorHEX( const std::string &color ) {
|
|
|
|
auto color_tuple = SDLPP::getColorsHEX( color );
|
|
|
|
return getSDLColorTuple( color_tuple );
|
2020-08-22 11:17:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
std::tuple< int, int, int, int >
|
|
|
|
SDLPP::getColorsSDLColor( const SDL_Color &color ) {
|
|
|
|
return { color.r, color.g, color.b, color.a };
|
2020-08-21 16:32:15 +00:00
|
|
|
}
|