From a9c641bc9dcca927cad8ee6bc53bd0cb31ef5b7f Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 1 Sep 2003 00:22:27 +0000 Subject: [PATCH] DrawClipped --- include/ZE_ZImage.h | 14 +++++++++++++- src/ZE_ZImage.cpp | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/include/ZE_ZImage.h b/include/ZE_ZImage.h index c1ea002..a6c76e8 100644 --- a/include/ZE_ZImage.h +++ b/include/ZE_ZImage.h @@ -13,7 +13,7 @@ \brief Definition file for ZImage. Definition file for ZImage, the OpenGL version of the ZImage class for ZEngine. -
$Id: ZE_ZImage.h,v 1.22 2003/08/07 05:54:45 cozman Exp $
+
$Id: ZE_ZImage.h,v 1.23 2003/09/01 00:22:27 cozman Exp $
\author James Turk **/ @@ -21,6 +21,7 @@ #define __ze_zimage_h__ #include "ZE_ZEngine.h" +#include "ZE_ZRect.h" namespace ZE { @@ -246,6 +247,17 @@ class ZImage **/ void DrawRotated(float x, float y, float angle) const; + /*! + \brief Draw Clipped Image to screen. + + Image is drawn such that only portions of image which fall within a certain area appear. + \since 0.8.5 + \param x X coord to draw Image to. + \param y Y coord to draw Image to. + \param clipRect Rectangle to clip within. + **/ + void DrawClipped(float x, float y, ZRect clipRect) const; + /*! \brief Flip image over one or both axes. diff --git a/src/ZE_ZImage.cpp b/src/ZE_ZImage.cpp index 766db98..a7c86a8 100644 --- a/src/ZE_ZImage.cpp +++ b/src/ZE_ZImage.cpp @@ -13,7 +13,7 @@ \brief Source file for ZImage. Implementation of ZImage, the Image class for ZEngine. -
$Id: ZE_ZImage.cpp,v 1.41 2003/08/08 03:52:25 cozman Exp $
+
$Id: ZE_ZImage.cpp,v 1.42 2003/09/01 00:22:35 cozman Exp $
\author James Turk **/ @@ -237,6 +237,42 @@ void ZImage::DrawRotated(float x, float y, float angle) const glPopMatrix(); } +void ZImage::DrawClipped(float x, float y, ZRect clipRect) const +{ + ZRect imgRect(x,y,rWidth,rHeight),inRect; + float xDiff,yDiff,nx,ny,nw,nh; + xDiff = rTexMaxX - rTexMinX; + yDiff = rTexMaxY - rTexMinY; + + if(clipRect.Contains(imgRect)) + { + Draw(x,y); + } + else if(clipRect.Intersects(imgRect)) + { + inRect = clipRect.Intersection(imgRect); + + nx = rTexMinX + (inRect.X()-imgRect.X())/imgRect.Width()*xDiff; + ny = rTexMinY + (inRect.Y()-imgRect.Y())/imgRect.Height()*yDiff; + nw = nx + (inRect.Width()/imgRect.Width())*xDiff; + nh = ny + (inRect.Height()/imgRect.Height())*yDiff; + + glColor4ub(255,255,255,rAlpha); //sets the color correctly + Bind(); + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(nx,ny); glVertex2f(inRect.Left(),inRect.Top()); + glTexCoord2f(nw,ny); glVertex2f(inRect.Right(),inRect.Top()); + glTexCoord2f(nx,nh); glVertex2f(inRect.Left(),inRect.Bottom()); + glTexCoord2f(nw,nh); glVertex2f(inRect.Right(),inRect.Bottom()); + glEnd(); + glColor4ub(255,255,255,255); //be responsible, return to standard color state + } + else //doesn't contain nor intersect + { + //draw nothing + } +} + void ZImage::Flip(bool horizontal, bool vertical) { GLfloat temp;