zengine/src/ZE_ZRect.cpp

214 lines
4.7 KiB
C++
Raw Normal View History

2002-11-21 05:40:49 +00:00
/*******************************************************************************
2002-12-29 06:50:19 +00:00
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
2002-11-21 05:40:49 +00:00
2002-12-29 06:50:19 +00:00
Licensed under a BSD-style license.
2002-11-21 05:40:49 +00:00
The maintainer of this library is James Turk (james@conceptofzero.net)
2002-12-29 06:50:19 +00:00
and the home of this Library is http://www.zengine.sourceforge.net
2002-11-21 05:40:49 +00:00
*******************************************************************************/
/*!
\par File Header:
File: ZE_ZRect.cpp <br>
Description: Implementation source file for core ZEngine Rectangle Object. <br>
Author(s): James Turk <br>
2003-01-07 05:44:32 +00:00
$Id: ZE_ZRect.cpp,v 1.5 2003/01/07 05:44:32 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
{
}
2002-12-04 05:22:39 +00:00
ZRect::ZRect(float x, float y, float width, float 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);
2002-12-04 05:22:39 +00:00
glVertex2f(rX, rY);
glVertex2f(rX+rWidth, rY);
glVertex2f(rX+rWidth, rY+rHeight);
glVertex2f(rX, rY+rHeight);
2002-12-01 08:36:39 +00:00
glEnd();
2003-01-07 05:44:32 +00:00
glColor4ub(255,255,255,255);
2002-12-01 08:36:39 +00:00
}
2002-12-04 05:22:39 +00:00
void ZRect::Move(float x, float y)
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
rX = x;
rY = y;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
void ZRect::MoveRel(float xMove, float yMove)
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
rX += xMove;
rY += yMove;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
void ZRect::Resize(float width, float height)
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
rWidth = width;
rHeight = height;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
void ZRect::ResizeRel(float widthChange, float heightChange)
2002-11-21 05:40:49 +00:00
{
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
}
2002-12-04 05:22:39 +00:00
bool ZRect::Contains(float x, float y) const
2002-11-21 05:40:49 +00:00
{
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
{
2002-12-04 05:22:39 +00:00
float tempX=0,tempY=0,tempW=0,tempH=0;
2002-11-21 05:40:49 +00:00
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;
}
2002-12-04 05:22:39 +00:00
float ZRect::X() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rX;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
float ZRect::Y() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rY;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
float ZRect::Left() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rX;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
float ZRect::Right() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rX+rWidth;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
float ZRect::Top() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rY;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
float ZRect::Bottom() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rY+rHeight;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
float ZRect::Width() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rWidth;
2002-11-21 05:40:49 +00:00
}
2002-12-04 05:22:39 +00:00
float ZRect::Height() const
2002-11-21 05:40:49 +00:00
{
2002-12-01 08:36:39 +00:00
return rHeight;
2002-11-21 05:40:49 +00:00
}
} //namespace ZE