2008-05-22 04:51:37 -07:00
|
|
|
// Copyright (C) 2002-2008 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#ifndef __IRR_DIMENSION2D_H_INCLUDED__
|
|
|
|
#define __IRR_DIMENSION2D_H_INCLUDED__
|
|
|
|
|
|
|
|
#include "irrTypes.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace core
|
|
|
|
{
|
|
|
|
|
|
|
|
//! Specifies a 2 dimensional size.
|
|
|
|
template <class T>
|
|
|
|
class dimension2d
|
|
|
|
{
|
|
|
|
public:
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Default constructor for empty dimension
|
2007-09-16 16:41:55 -07:00
|
|
|
dimension2d() : Width(0), Height(0) {}
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Constructor with width and height
|
2007-05-20 11:03:49 -07:00
|
|
|
dimension2d(const T& width, const T& height)
|
2007-09-16 16:41:55 -07:00
|
|
|
: Width(width), Height(height) {}
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Equality operator
|
|
|
|
bool operator==(const dimension2d<T>& other) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return Width == other.Width && Height == other.Height;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Inequality operator
|
|
|
|
bool operator!=(const dimension2d<T>& other) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return ! (*this == other);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
|
|
|
|
//! Set to new values
|
|
|
|
dimension2d<T>& set(const T& width, const T& height)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
Width = width;
|
|
|
|
Height = height;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Divide width and height by scalar
|
2007-10-17 07:58:59 -07:00
|
|
|
dimension2d<T>& operator/=(const T& scale)
|
|
|
|
{
|
|
|
|
Width /= scale;
|
|
|
|
Height /= scale;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Divide width and height by scalar
|
2007-10-17 07:58:59 -07:00
|
|
|
dimension2d<T> operator/(const T& scale) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return dimension2d<T>(Width/scale, Height/scale);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Multiply width and height by scalar
|
2007-10-17 07:58:59 -07:00
|
|
|
dimension2d<T>& operator*=(const T& scale)
|
|
|
|
{
|
|
|
|
Width *= scale;
|
|
|
|
Height *= scale;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Multiply width and height by scalar
|
2007-10-17 07:58:59 -07:00
|
|
|
dimension2d<T> operator*(const T& scale) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return dimension2d<T>(Width*scale, Height*scale);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get area
|
2008-01-29 16:07:52 -08:00
|
|
|
T getArea() const
|
|
|
|
{
|
|
|
|
return Width*Height;
|
|
|
|
}
|
|
|
|
|
2008-11-03 15:04:54 -08:00
|
|
|
//! Get the optimal size according to some properties
|
|
|
|
/** This is a function often used for texture dimension
|
|
|
|
calculations. The function returns the next larger or
|
|
|
|
smaller dimension which is a power-of-two dimension
|
|
|
|
(2^n,2^m) and/or square (Width=Height).
|
|
|
|
\param requirePowerOfTwo Forces the result to use only
|
|
|
|
powers of two as values.
|
|
|
|
\param requireSquare Makes width==height in the result
|
|
|
|
\param larger Choose whether the result is larger or
|
|
|
|
smaller than the current dimension.
|
|
|
|
\return The optimal dimension under the given
|
|
|
|
constraints. */
|
|
|
|
dimension2d<T> getOptimalSize(
|
|
|
|
bool requirePowerOfTwo=true,
|
|
|
|
bool requireSquare=false,
|
|
|
|
bool larger=true) const
|
|
|
|
{
|
|
|
|
u32 i=1;
|
|
|
|
u32 j=1;
|
|
|
|
if (requirePowerOfTwo)
|
|
|
|
{
|
2008-11-03 16:04:10 -08:00
|
|
|
while (i<(u32)Width)
|
2008-11-03 15:04:54 -08:00
|
|
|
i<<=1;
|
|
|
|
if (!larger && i!=1)
|
|
|
|
i>>=1;
|
2008-11-03 16:04:10 -08:00
|
|
|
while (j<(u32)Height)
|
2008-11-03 15:04:54 -08:00
|
|
|
j<<=1;
|
|
|
|
if (!larger && j!=1)
|
|
|
|
j>>=1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-03 16:04:10 -08:00
|
|
|
i=(u32)Width;
|
|
|
|
j=(u32)Height;
|
2008-11-03 15:04:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (requireSquare)
|
|
|
|
{
|
|
|
|
if ((larger && (i>j)) || (!larger && (i<j)))
|
|
|
|
j=i;
|
|
|
|
else
|
|
|
|
i=j;
|
|
|
|
}
|
|
|
|
return dimension2d<T>((T)i,(T)j);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Width of the dimension.
|
|
|
|
T Width;
|
|
|
|
//! Height of the dimension.
|
|
|
|
T Height;
|
2007-05-20 11:03:49 -07:00
|
|
|
};
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Typedef for an f32 dimension.
|
2007-05-20 11:03:49 -07:00
|
|
|
typedef dimension2d<f32> dimension2df;
|
|
|
|
//! Typedef for an integer dimension.
|
|
|
|
typedef dimension2d<s32> dimension2di;
|
|
|
|
|
|
|
|
} // end namespace core
|
|
|
|
} // end namespace irr
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|