2020-07-26 20:37:20 +00:00
|
|
|
#include "sdlpp.hpp"
|
|
|
|
|
|
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
bool SDLPP::init() {
|
|
|
|
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;
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-22 11:17:56 +00:00
|
|
|
if( TTF_Init() == -1 ) {
|
|
|
|
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-26 20:37:20 +00:00
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 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();
|
|
|
|
ret |= iright != -1 && iright >= other.leftmost();
|
|
|
|
ret |= itop != -1 && itop <= other.bottommost();
|
|
|
|
ret |= ibottom != -1 && ibottom >= other.topmost();
|
|
|
|
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);
|
|
|
|
}
|
2020-07-28 17:35:31 +00:00
|
|
|
if(other.isInfinite() ) {
|
2020-07-26 20:37:20 +00:00
|
|
|
return infinityIntersection(other, *this);
|
|
|
|
}
|
2020-07-28 17:35:31 +00:00
|
|
|
if(isInfinite())
|
|
|
|
return infinityIntersection(*this, other);
|
2020-07-26 20:37:20 +00:00
|
|
|
return intersects(*this, other);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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();
|
|
|
|
}
|
|
|
|
int pointx=0, pointy=0;
|
|
|
|
if(centerx > other.rightmost()) {
|
|
|
|
pointx = other.rightmost();
|
|
|
|
} else {
|
|
|
|
pointx = other.leftmost();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2020-08-21 16:32:15 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 11:17:56 +00:00
|
|
|
std::tuple<int, int, int, int> SDLPP::getColorsHEX(const std::string &color) {
|
|
|
|
int red = 0, green = 0, blue = 0, alpha = 255;
|
2020-08-21 16:32:15 +00:00
|
|
|
const char *color_ptr = color.c_str();
|
|
|
|
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]);
|
2020-08-22 11:17:56 +00:00
|
|
|
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 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);
|
|
|
|
return ret_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
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};
|
2020-08-21 16:32:15 +00:00
|
|
|
}
|