1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
struct rectangle{
rectangle() :X1(0), Y1(0), X2(0), Y2(0) {};
rectangle(int X1, int Y1, int X2, int Y2)
:X1(X1), Y1(Y1), X2(X2), Y2(Y2) {};
int X1;
int Y1;
int X2;
int Y2;
int GetWidth() { return (X2-X1+1); }
int GetHeight() { return (Y2-Y1+1); }
bool CollidesWith(rectangle rec);
rectangle OverlapRegion(rectangle rec);
void Draw(ALLEGRO_BITMAP *Surface, ALLEGRO_COLOR Color, int Thickness = 1, bool Filled = false)
{
if (currentSurface != Surface) { currentSurface = Surface; al_set_target_bitmap(currentSurface); }
if (Filled == false) al_draw_rectangle(X1, Y1, X2, Y2, Color, Thickness);
if (Filled == true) al_draw_filled_rectangle(X1, Y1, X2, Y2, Color);
}
};
bool rectangle::CollidesWith(rectangle rec)
{
if (Y1 > rec.Y2) return false;
if (Y2 < rec.Y1) return false;
if (X1 > rec.X2) return false;
if (X2 < rec.X1) return false;
return true;
}
rectangle rectangle::OverlapRegion(rectangle rec)
{
rectangle regionRec(-1,-1,-1,-1);
if (CollidesWith(rec) == false) return regionRec;
regionRec.X1 = GetMin(X1, rec.X1) + abs(X1 - rec.X1);
regionRec.Y1 = GetMin(Y1, rec.Y1) + abs(Y1 - rec.Y1);
regionRec.X2 = GetMax(X2, rec.X2) - abs(X2 - rec.X2);
regionRec.Y2 = GetMax(Y2, rec.Y2) - abs(Y2 - rec.Y2);
return regionRec;
}
|