108 lines
3.9 KiB
C++
108 lines
3.9 KiB
C++
#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;
|
|
}
|
|
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);
|
|
}
|
|
if(other.isInfinite()) {
|
|
return infinityIntersection(other, *this);
|
|
}
|
|
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;
|
|
}
|