00001 #ifndef __RENDER_TEMPLATES_H
00002 #define __RENDER_TEMPLATES_H
00003
00004
00005
00006
00007 #include "GXStandardDefines.h"
00008 #include "fastmath.h"
00009
00010
00011
00012 template<typename type>
00013 class CGXAutoArray
00014 {
00015 public:
00016 CGXAutoArray()
00017 {
00018 m_pArray=NULL;
00019 m_uCount=0;
00020 }
00021
00022 ~CGXAutoArray()
00023 {
00024 Cleanup();
00025 }
00026
00027
00028 bool Init(UINT uCount)
00029 {
00030 Cleanup();
00031
00032 if(0==uCount)
00033 {
00034 m_pArray=NULL;
00035 return true;
00036 }
00037
00038
00039 m_pArray=new type[uCount];
00040 if(!m_pArray)
00041 {
00042 return false;
00043 }
00044 else
00045 {
00046 m_uCount=uCount;
00047 return true;
00048 }
00049 }
00050
00051 void Cleanup()
00052 {
00053 SAFE_ARRAY_DELETE(m_pArray);
00054 m_uCount=0;
00055 }
00056
00057
00058 inline UINT GetCount()
00059 {
00060 return m_uCount;
00061 }
00062
00063 inline type* GetPtr()
00064 {
00065 return m_pArray;
00066 }
00067
00068 inline bool IsNonNull()
00069 {
00070 return (NULL!=m_pArray);
00071 }
00072
00073 inline bool CopyIn(type* pSrc,UINT uCount)
00074 {
00075 if(uCount>m_uCount)
00076 {
00077 if(!Init(uCount))
00078 {
00079 return false;
00080 }
00081 }
00082
00083 for(UINT uCtr=0;uCtr<uCount;uCtr++)
00084 {
00085 m_pArray[uCtr]=pSrc[uCtr];
00086 }
00087
00088 return true;
00089
00090 }
00091
00092
00093 bool WriteToStream(IStream* pStream)
00094 {
00095 assert(pStream!=NULL);
00096
00097
00098 DWORD dwBytesToBeWritten;
00099 DWORD dwBytesWritten;
00100
00101
00102 dwBytesToBeWritten=sizeof(DWORD);
00103
00104 UINT uData=GetCount();
00105
00106 if(FAILED(pStream->Write(&uData,
00107 dwBytesToBeWritten,
00108 &dwBytesWritten)))
00109 {
00110 return false;
00111 }
00112
00113 if(dwBytesWritten!=dwBytesToBeWritten)
00114 {
00115 return false;
00116 }
00117
00118
00119
00120 dwBytesToBeWritten=sizeof(type)*GetCount();
00121
00122
00123 if(FAILED(pStream->Write(GetPtr(),
00124 dwBytesToBeWritten,
00125 &dwBytesWritten)))
00126 {
00127 return false;
00128 }
00129
00130 if(dwBytesWritten!=dwBytesToBeWritten)
00131 {
00132 return false;
00133 }
00134
00135 return true;
00136 }
00137
00138
00139 bool LoadFromStream(IStream* pStream)
00140 {
00141 assert(NULL!=pStream);
00142
00143 DWORD dwBytesToRead, dwBytesRead;
00144
00145
00146
00147 dwBytesToRead=sizeof(DWORD);
00148 DWORD dwCount;
00149
00150 if(FAILED(pStream->Read(&dwCount, dwBytesToRead, &dwBytesRead)))
00151 {
00152 return false;
00153 }
00154
00155 if(dwBytesRead!=dwBytesToRead)
00156 {
00157 return false;
00158 }
00159
00160
00161
00162 if(!Init(dwCount))
00163 {
00164 return false;
00165 }
00166
00167 dwBytesToRead=sizeof(type)*GetCount();
00168
00169
00170 if(FAILED(pStream->Read(GetPtr(), dwBytesToRead, &dwBytesRead)))
00171 {
00172 return false;
00173 }
00174
00175 if(dwBytesRead!=dwBytesToRead)
00176 {
00177 return false;
00178 }
00179
00180 return true;
00181 }
00182
00183
00184 inline void GetValAt(UINT uIndex, type* pOut)
00185 {
00186 assert(NULL!=pOut);
00187 assert(uIndex<GetCount());
00188
00189 *pOut=m_pArray[uIndex];
00190 }
00191
00192
00193 void GetFilteredVal(float fAmt, type* pOut)
00194 {
00195 assert(pOut);
00196 assert(fAmt<=1.0f);
00197 assert(fAmt>=0.0f);
00198 assert(m_uCount>0);
00199
00200 if(m_uCount==1)
00201 {
00202 *pOut=m_pArray[0];
00203 return;
00204 }
00205
00206 float fCeil=(float)(ceil(fAmt*m_uCount));
00207 float fFloor=(float)(floor(fAmt*m_uCount));
00208
00209 UINT uHighIndex;
00210 UINT uLowIndex;
00211
00212 FloatToInt((int*)&uHighIndex, fCeil);
00213 FloatToInt((int*)&uLowIndex, fFloor);
00214
00215
00216
00217
00218 if(uHighIndex=m_uCount)
00219 {
00220 uHighIndex--;
00221 }
00222
00223
00224 float fLerpAmt=(m_uCount*fAmt)-(float)uLowIndex;
00225
00226 BlakeUtilLerp(fLerpAmt, &(m_pArray[uLowIndex]), &(m_pArray[uHighIndex]), pOut);
00227 }
00228
00229
00230
00231
00232
00233 inline operator type*()
00234 {
00235 return m_pArray;
00236 }
00237
00238
00239 private:
00240 type* m_pArray;
00241 UINT m_uCount;
00242 };
00243
00244
00245
00246 #endif