zengine/src/ZE_ZRect.cpp

213 lines
4.6 KiB
C++
Raw Normal View History

2002-11-21 05:40:49 +00:00
/*******************************************************************************
This file is Part of the ZEngine Library for SDL Game Development.
Copyright (C) 2002 ConceptOfZero.net
Licensed under the BSD License, see licensing.txt.
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.conceptofzero.net/
*******************************************************************************/
/*!
\par File Header:
File: ZE_ZRect.cpp <br>
Description: Implementation source file for core ZEngine Rectangle Object. <br>
Author(s): James Turk <br>
2002-12-01 08:36:39 +00:00
$Id: ZE_ZRect.cpp,v 1.2 2002/12/01 08:36:39 cozman Exp $<br>
2002-11-21 05:40:49 +00:00
\file ZE_ZRect.cpp
\brief Source file for ZRect.
Implementation of ZRect, the Rectangle class for ZEngine.
**/
#include "ZE_ZRect.h"
namespace ZE
{
ZRect::ZRect() :
2002-12-01 08:36:39 +00:00
rX(0),rY(0),rWidth(0),rHeight(0)
2002-11-21 05:40:49 +00:00
{
}
ZRect::ZRect(int x, int y, int width, int height) :
2002-12-01 08:36:39 +00:00
rX(x),rY(y),rWidth(width),rHeight(height)
2002-11-21 05:40:49 +00:00
{
}
ZRect::ZRect(const ZRect &rhs) :
2002-12-01 08:36:39 +00:00
rX(rhs.X()),rY(rhs.Y()),rWidth(rhs.Width()),rHeight(rhs.Height())
2002-11-21 05:40:49 +00:00
{
}
const ZRect& ZRect::operator=(const ZRect &rhs)
{
if(this != &rhs)
{
2002-12-01 08:36:39 +00:00
rX = rhs.X();
rY = rhs.Y();
rWidth = rhs.Width();
rHeight = rhs.Height();
2002-11-21 05:40:49 +00:00
}
return *this;
}
bool ZRect::operator<(const ZRect &rhs) const
{
//< is the one that is closer to top corner (as a whole)//
2002-12-01 08:36:39 +00:00
if(rY < rhs.Y()) //check Ys
2002-11-21 05:40:49 +00:00
return true;
2002-12-01 08:36:39 +00:00
else if(rY > rhs.Y())
2002-11-21 05:40:49 +00:00
return false;
else
{
2002-12-01 08:36:39 +00:00
if(rX < rhs.X()) //check Xs
2002-11-21 05:40:49 +00:00
return true;
2002-12-01 08:36:39 +00:00
else if(rX > rhs.X())
2002-11-21 05:40:49 +00:00
return false;
else
{
2002-12-01 08:36:39 +00:00
if(rHeight < rhs.Height()) //check heights
2002-11-21 05:40:49 +00:00
return true;
2002-12-01 08:36:39 +00:00
else if(rHeight > rhs.Height())
2002-11-21 05:40:49 +00:00
return false;
else
{
2002-12-01 08:36:39 +00:00
if(rWidth < rhs.Width()) //check widths
2002-11-21 05:40:49 +00:00
return true;
2002-12-01 08:36:39 +00:00
else if(rWidth > rhs.Width())
2002-11-21 05:40:49 +00:00
return false;
else
return false; //nothing left to check they are ==
}
}
}
}
2002-12-01 08:36:39 +00:00
void ZRect::Draw(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha)
{
glColor4ub(red,green,blue,alpha);
glBegin(GL_QUADS);
glVertex2i(rX, rY);
glVertex2i(rX+rWidth, rY);
glVertex2i(rX+rWidth, rY+rHeight);
glVertex2i(rX, rY+rHeight);
glEnd();
}
2002-11-21 05:40:49 +00:00
void ZRect::Move(int x, int y)
{
2002-12-01 08:36:39 +00:00
rX = x;
rY = y;
2002-11-21 05:40:49 +00:00
}
void ZRect::MoveRel(int xMove, int yMove)
{
2002-12-01 08:36:39 +00:00
rX += xMove;
rY += yMove;
2002-11-21 05:40:49 +00:00
}
void ZRect::Resize(int width, int height)
{
2002-12-01 08:36:39 +00:00
rWidth = width;
rHeight = height;
2002-11-21 05:40:49 +00:00
}
void ZRect::ResizeRel(int widthChange, int heightChange)
{
2002-12-01 08:36:39 +00:00
rWidth += widthChange;
rHeight += heightChange;
2002-11-21 05:40:49 +00:00
}
bool ZRect::Intersects(const ZRect &rect) const
{
2002-12-01 08:36:39 +00:00
return !(rX > rect.Right() || rect.Left() > rX+rWidth ||
rY > rect.Bottom() || rect.Top() > rY+rHeight);
2002-11-21 05:40:49 +00:00
}
bool ZRect::Contains(int x, int y) const
{
2002-12-01 08:36:39 +00:00
return x > rX && x < rX+rWidth && y > rY && y < rY+rHeight;
2002-11-21 05:40:49 +00:00
}
bool ZRect::Contains(const ZRect &rect) const
{
//contains all 4 points
return Contains(rect.Left(),rect.Top()) && Contains(rect.Right(),rect.Top()) &&
Contains(rect.Left(),rect.Bottom()) && Contains(rect.Right(),rect.Bottom());
}
ZRect ZRect::Intersection(const ZRect &rect) const
{
int tempX=0,tempY=0,tempW=0,tempH=0;
if(Intersects(rect))
{
2002-12-01 08:36:39 +00:00
tempX = rX > rect.X() ? rX : rect.X();
tempY = rY > rect.Y() ? rY : rect.Y();
tempW = rX+rWidth < rect.Right() ? rX+rWidth : rect.Right();
tempH = rY+rHeight < rect.Bottom() ? rY+rHeight : rect.Bottom();
2002-11-21 05:40:49 +00:00
tempW -= tempX; //adjust width and height
tempH -= tempY;
}
return ZRect(tempX,tempY,tempW,tempH);
}
SDL_Rect ZRect::SDLrect() const
{
SDL_Rect ret;
2002-12-01 08:36:39 +00:00
ret.x = static_cast<Sint16>(rX);
ret.y = static_cast<Sint16>(rY);
ret.w = static_cast<Sint16>(rWidth);
ret.h = static_cast<Sint16>(rHeight);
2002-11-21 05:40:49 +00:00
return ret;
}
int ZRect::X() const
{
2002-12-01 08:36:39 +00:00
return rX;
2002-11-21 05:40:49 +00:00
}
int ZRect::Y() const
{
2002-12-01 08:36:39 +00:00
return rY;
2002-11-21 05:40:49 +00:00
}
int ZRect::Left() const
{
2002-12-01 08:36:39 +00:00
return rX;
2002-11-21 05:40:49 +00:00
}
int ZRect::Right() const
{
2002-12-01 08:36:39 +00:00
return rX+rWidth;
2002-11-21 05:40:49 +00:00
}
int ZRect::Top() const
{
2002-12-01 08:36:39 +00:00
return rY;
2002-11-21 05:40:49 +00:00
}
int ZRect::Bottom() const
{
2002-12-01 08:36:39 +00:00
return rY+rHeight;
2002-11-21 05:40:49 +00:00
}
int ZRect::Width() const
{
2002-12-01 08:36:39 +00:00
return rWidth;
2002-11-21 05:40:49 +00:00
}
int ZRect::Height() const
{
2002-12-01 08:36:39 +00:00
return rHeight;
2002-11-21 05:40:49 +00:00
}
} //namespace ZE