Irrlicht 3D Engine: dimension2d.h Source File

| Irrlicht 3D Engine |

dimension2d.h
Go to the documentation of this file.

    00001 // Copyright (C) 2002-2012 Nikolaus Gebhardt
    00002 // This file is part of the "Irrlicht Engine".
    00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
    00004
    00005 #ifndef __IRR_DIMENSION2D_H_INCLUDED__
    00006 #define __IRR_DIMENSION2D_H_INCLUDED__
    00007
    00008 #include "irrTypes.h"
    00009 #include "irrMath.h" // for irr::core::equals()
    00010
    00011 namespace irr
    00012 {
    00013 namespace core
    00014 {
    00015     template <class T>
    00016     class vector2d;
    00017
    00019     template <class T>
    00020     class dimension2d
    00021     {
    00022         public:
    00024             dimension2d() : Width(0), Height(0) {}
    00026             dimension2d(const T& width, const T& height)
    00027                 : Width(width), Height(height) {}
    00028
    00029             dimension2d(const vector2d<T>& other); // Defined in vector2d.h
    00030
    00032             template <class U>
    00033             explicit dimension2d(const dimension2d<U>& other) :
    00034                 Width((T)other.Width), Height((T)other.Height) { }
    00035
    00036             template <class U>
    00037             dimension2d<T>& operator=(const dimension2d<U>& other)
    00038             {
    00039                 Width = (T) other.Width;
    00040                 Height = (T) other.Height;
    00041                 return *this;
    00042             }
    00043
    00044
    00046             bool operator==(const dimension2d<T>& other) const
    00047             {
    00048                 return core::equals(Width, other.Width) &&
    00049                         core::equals(Height, other.Height);
    00050             }
    00051
    00053             bool operator!=(const dimension2d<T>& other) const
    00054             {
    00055                 return ! (*this == other);
    00056             }
    00057
    00058             bool operator==(const vector2d<T>& other) const;  // Defined in vector2d.h
    00059
    00060             bool operator!=(const vector2d<T>& other) const
    00061             {
    00062                 return !(*this == other);
    00063             }
    00064
    00066             dimension2d<T>& set(const T& width, const T& height)
    00067             {
    00068                 Width = width;
    00069                 Height = height;
    00070                 return *this;
    00071             }
    00072
    00074             dimension2d<T>& operator/=(const T& scale)
    00075             {
    00076                 Width /= scale;
    00077                 Height /= scale;
    00078                 return *this;
    00079             }
    00080
    00082             dimension2d<T> operator/(const T& scale) const
    00083             {
    00084                 return dimension2d<T>(Width/scale, Height/scale);
    00085             }
    00086
    00088             dimension2d<T>& operator*=(const T& scale)
    00089             {
    00090                 Width *= scale;
    00091                 Height *= scale;
    00092                 return *this;
    00093             }
    00094
    00096             dimension2d<T> operator*(const T& scale) const
    00097             {
    00098                 return dimension2d<T>(Width*scale, Height*scale);
    00099             }
    00100
    00102             dimension2d<T>& operator+=(const dimension2d<T>& other)
    00103             {
    00104                 Width += other.Width;
    00105                 Height += other.Height;
    00106                 return *this;
    00107             }
    00108
    00110             dimension2d<T> operator+(const dimension2d<T>& other) const
    00111             {
    00112                 return dimension2d<T>(Width+other.Width, Height+other.Height);
    00113             }
    00114
    00116             dimension2d<T>& operator-=(const dimension2d<T>& other)
    00117             {
    00118                 Width -= other.Width;
    00119                 Height -= other.Height;
    00120                 return *this;
    00121             }
    00122
    00124             dimension2d<T> operator-(const dimension2d<T>& other) const
    00125             {
    00126                 return dimension2d<T>(Width-other.Width, Height-other.Height);
    00127             }
    00128
    00130             T getArea() const
    00131             {
    00132                 return Width*Height;
    00133             }
    00134
    00136
    00150             dimension2d<T> getOptimalSize(
    00151                     bool requirePowerOfTwo=true,
    00152                     bool requireSquare=false,
    00153                     bool larger=true,
    00154                     u32 maxValue = 0) const
    00155             {
    00156                 u32 i=1;
    00157                 u32 j=1;
    00158                 if (requirePowerOfTwo)
    00159                 {
    00160                     while (i<(u32)Width)
    00161                         i<<=1;
    00162                     if (!larger && i!=1 && i!=(u32)Width)
    00163                         i>>=1;
    00164                     while (j<(u32)Height)
    00165                         j<<=1;
    00166                     if (!larger && j!=1 && j!=(u32)Height)
    00167                         j>>=1;
    00168                 }
    00169                 else
    00170                 {
    00171                     i=(u32)Width;
    00172                     j=(u32)Height;
    00173                 }
    00174
    00175                 if (requireSquare)
    00176                 {
    00177                     if ((larger && (i>j)) || (!larger && (i<j)))
    00178                         j=i;
    00179                     else
    00180                         i=j;
    00181                 }
    00182
    00183                 if ( maxValue > 0 && i > maxValue)
    00184                     i = maxValue;
    00185
    00186                 if ( maxValue > 0 && j > maxValue)
    00187                     j = maxValue;
    00188
    00189                 return dimension2d<T>((T)i,(T)j);
    00190             }
    00191
    00193
    00196             dimension2d<T> getInterpolated(const dimension2d<T>& other, f32 d) const
    00197             {
    00198                 f32 inv = (1.0f - d);
    00199                 return dimension2d<T>( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d));
    00200             }
    00201
    00202
    00204             T Width;
    00206             T Height;
    00207     };
    00208
    00210     typedef dimension2d<f32> dimension2df;
    00212     typedef dimension2d<u32> dimension2du;
    00213
    00215
    00217     typedef dimension2d<s32> dimension2di;
    00218
    00219
    00220 } // end namespace core
    00221 } // end namespace irr
    00222
    00223 #endif
    00224