fs/rap/fsSurfCl.cpp

Go to the documentation of this file.
00001 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00002 /*                          _______      ______    ______          __      */
00003 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~    / ____(_)___ / ___ /)  / ____/___  ____/ /__    */
00004 /*     [fsSurfCl]         / /_  / / __ \\__ \|/  / /   / __ \/ __  / _ \   */
00005 /*       rev. 4          / __/ / / /_/ /__/ /   / /___/ /_/ / /_/ /  __/   */
00006 /*    12th Feb 2007     /_/   /_/ ,___/____/    \____/\____/\__,_/\___/    */
00007 /*     [ ] stable              /_/ (c) 2006-7 Filip STOKLAS (FipS)         */
00008 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~       http://HOLE.4FipS.com/fips_code.php          */
00009 /*                                                                         */
00010 /* This code is free for personal and commercial use. You may redistribute */
00011 /* it by any means. If you use the code for your own projects please give  */
00012 /* me credit. Please send a bug report. Don't alter or remove this header! */
00013 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00014 #include <fs/rap/fsSurfCl.h>
00015 
00016 // >>> COMMON SUPPORT BLOCK (rev. 6)
00017 #if defined(FS_HAS_FSASSERT)
00018 #   include <fs/sys/fsAssert.h>
00019 #else
00020 #   include <cassert>
00021 #   if !defined(FS_ASSERT)
00022 #       define FS_ASSERT(exp) assert(exp)
00023 #   endif
00024 #   if !defined(FS_ASSERT_MSG)
00025 #       define FS_ASSERT_MSG(exp, msg) assert(exp && msg)
00026 #   endif
00027 #   if !defined(FS_VERIFY)
00028 #       define FS_VERIFY(exp) \
00029         { bool bExp = !!(exp); assert(bExp && #exp); bExp; }
00030 #   endif
00031 #   if !defined(FS_VERIFY_MSG)
00032 #       define FS_VERIFY_MSG(exp, msg) \
00033         { bool bExp = !!(exp); assert(bExp && #exp && msg); bExp; }
00034 #   endif
00035 #   if !defined(FS_STATIC_ASSERT)
00036 #       define FS_STATIC_ASSERT(exp) { char error[(exp) ? 1 : 0]; error; }
00037 #   endif
00038 #endif
00039 #if !defined(FS_VERIFY_RETURN)
00040 #   define FS_VERIFY_RETURN(exp) \
00041     if(!(exp)) { FS_ASSERT_MSG(0, #exp "<return>"); return; }
00042 #endif
00043 #if !defined(FS_VERIFY_RETURN_VAL)
00044 #   define FS_VERIFY_RETURN_VAL(exp, ret) \
00045     if(!(exp)) { FS_ASSERT_MSG(0, #exp "<returns:>" #ret); return (ret); }
00046 #endif
00047 // <<< COMMON SUPPORT BLOCK (rev. 6)
00048 
00049 #include <vector>
00050 #include <windows.h>
00051 
00052 // provide detailed memory tracking information
00053 #if defined(FS_HAS_FSMEMMGR)
00054 #   include <fs/sys/fsMemMgr.h>
00055 #endif
00056 //---------------------------------------------------------------------------
00057 using namespace fs::rap;
00058 //---------------------------------------------------------------------------
00059 //--- ANONYMOUS NAMESPACE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00060 //---------------------------------------------------------------------------
00061 namespace {
00062 //---------------------------------------------------------------------------
00063 //--- CLASS SurfClImpl_t >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00064 //---------------------------------------------------------------------------
00065 class SurfClImpl_t
00066 {
00067  public:
00068  
00069     SurfClImpl_t(bool bFitCl, int nSurfW, int nSurfH);
00070     void Clear(const SurfCl_t::PxFmt8888_t &Clr);
00071     int GetW() const;
00072     int GetH() const;
00073     SurfCl_t::PxFmt8888_t * GetData();
00074     
00075     void OnSizeEv(const WinApp_t::SizeEv_t &, WinApp_t &);
00076     void OnDrawEv(const WinApp_t::DrawEv_t &, WinApp_t &);
00077     
00078  private:
00079 
00080     SurfClImpl_t(); 
00081     SurfClImpl_t(const SurfClImpl_t &); 
00082     SurfClImpl_t & operator = (const SurfClImpl_t &); 
00083     
00084     void ReszSurf(int nSurfW, int nSurfH);
00085     
00086     //---
00087 
00088     bool m_bFitCl;
00089     int m_nSurfW, m_nSurfH;
00090     int m_nClW, m_nClH;
00091             
00092     typedef std::vector<SurfCl_t::PxFmt8888_t> Data_t;
00093     Data_t m_Data;
00094 };
00095 //---------------------------------------------------------------------------
00096 SurfClImpl_t::SurfClImpl_t(bool bFitCl, int nSurfW, int nSurfH):
00097 m_bFitCl(bFitCl),
00098 m_nSurfW(0),
00099 m_nSurfH(0),
00100 m_nClW(0),
00101 m_nClH(0)
00102 {
00103     FS_VERIFY_RETURN(nSurfW >= 0 && nSurfH >= 0);
00104     ReszSurf(nSurfW, nSurfH);
00105 }
00106 //---------------------------------------------------------------------------
00107 void SurfClImpl_t::Clear(const SurfCl_t::PxFmt8888_t &Clr)
00108 {
00109     const SurfCl_t::PxFmt8888_t Clr2 = { Clr.r, Clr.g, Clr.b, 0 };
00110     FS_ASSERT(!m_Data.empty());
00111     
00112     if(Clr2.r == Clr2.g && Clr2.r == Clr2.b)
00113     {
00114         ::memset(&m_Data[0], Clr2.r, m_Data.size() * sizeof(Clr2));
00115     }
00116     else
00117     {
00118         //std::fill(m_Data.begin(), m_Data.end(), Clr2);
00119         for(size_t i = 0, n = m_Data.size(); i < n; ++i)
00120             m_Data[i] = Clr2;
00121     }
00122 }
00123 //---------------------------------------------------------------------------
00124 int SurfClImpl_t::GetW() const
00125 {
00126     return m_nSurfW;
00127 }
00128 //---------------------------------------------------------------------------
00129 int SurfClImpl_t::GetH() const
00130 {
00131     return m_nSurfH;
00132 }
00133 //---------------------------------------------------------------------------
00134 SurfCl_t::PxFmt8888_t * SurfClImpl_t::GetData()
00135 {   
00136     // 'empty data' is a valid state...
00137     return !m_Data.empty() ? &m_Data[0] : 0;
00138 }
00139 //---------------------------------------------------------------------------
00140 void SurfClImpl_t::OnSizeEv(const WinApp_t::SizeEv_t &SizeEv, WinApp_t &)
00141 {
00142     FS_VERIFY_RETURN(SizeEv.m_nClW >= 0 && SizeEv.m_nClH >= 0);
00143         
00144     m_nClW = SizeEv.m_nClW;
00145     m_nClH = SizeEv.m_nClH;
00146     
00147     if(m_bFitCl)
00148         ReszSurf(m_nClW, m_nClH);
00149 }
00150 //---------------------------------------------------------------------------
00151 void SurfClImpl_t::OnDrawEv(const WinApp_t::DrawEv_t &, WinApp_t &App)
00152 {
00153     if(m_Data.empty()) // it's a valid state...
00154         return;
00155 
00156     HWND hWnd = static_cast<HWND>(App.GetWinHdle());
00157     FS_ASSERT(hWnd);
00158     
00159     HDC hDC = ::GetDC(hWnd);
00160     FS_ASSERT(hDC);
00161     
00162     //---
00163     
00164     static BITMAPINFO *pBmpInfo = 0;
00165     
00166     static char aBmpInfoBuf[sizeof BITMAPINFO + 16];
00167     ::memset(aBmpInfoBuf, 0, sizeof BITMAPINFO + 16);
00168             
00169     pBmpInfo = reinterpret_cast<BITMAPINFO *>(&aBmpInfoBuf);
00170     pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
00171     pBmpInfo->bmiHeader.biPlanes = 1;
00172     pBmpInfo->bmiHeader.biBitCount = 32;
00173     pBmpInfo->bmiHeader.biCompression = BI_BITFIELDS;
00174     pBmpInfo->bmiHeader.biWidth = m_nSurfW;
00175     pBmpInfo->bmiHeader.biHeight = -m_nSurfH; // '-' means y-mirror
00176     reinterpret_cast<unsigned long *>(pBmpInfo->bmiColors)[0] = 0x000000FF;
00177     reinterpret_cast<unsigned long *>(pBmpInfo->bmiColors)[1] = 0x0000FF00;
00178     reinterpret_cast<unsigned long *>(pBmpInfo->bmiColors)[2] = 0x00FF0000;
00179         
00180     //---
00181     
00182     FS_ASSERT(!m_Data.empty());
00183     ::StretchDIBits(
00184      hDC,
00185      0, 0, m_nClW, m_nClH,
00186      0, 0, m_nSurfW, m_nSurfH,
00187      &m_Data[0],
00188      pBmpInfo,
00189      DIB_RGB_COLORS,
00190      SRCCOPY
00191     );
00192 
00193     //---
00194     
00195     ::ReleaseDC(hWnd, hDC);
00196 }
00197 //---------------------------------------------------------------------------
00198 void SurfClImpl_t::ReszSurf(int nSurfW, int nSurfH)
00199 {
00200     FS_ASSERT(nSurfW >= 0 && nSurfH >= 0);
00201 
00202     m_nSurfW = nSurfW;
00203     m_nSurfH = nSurfH;
00204     
00205     m_Data.resize(m_nSurfW * m_nSurfH);
00206 }
00207 //---------------------------------------------------------------------------
00208 } // anonymous namespace
00209 //---------------------------------------------------------------------------
00210 //--- CLASS SurfCl_t >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00211 //---------------------------------------------------------------------------
00217 SurfCl_t::SurfCl_t(bool bFitCl, int nSurfW, int nSurfH):
00218 m_pImpl(new SurfClImpl_t(bFitCl, nSurfW, nSurfH))
00219 {
00220     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00221 }
00222 //---------------------------------------------------------------------------
00223 SurfCl_t::~SurfCl_t()
00224 {
00225     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00226     delete static_cast<SurfClImpl_t *>(m_pImpl);
00227 }
00228 //---------------------------------------------------------------------------
00229 void SurfCl_t::Clear(const PxFmt8888_t &Clr)
00230 {
00231     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00232     return static_cast<SurfClImpl_t *>(m_pImpl)->Clear(Clr);
00233 }
00234 //---------------------------------------------------------------------------
00235 int SurfCl_t::GetW() const
00236 {
00237     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00238     return static_cast<SurfClImpl_t *>(m_pImpl)->GetW();
00239 }
00240 //---------------------------------------------------------------------------
00241 int SurfCl_t::GetH() const
00242 {
00243     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00244     return static_cast<SurfClImpl_t *>(m_pImpl)->GetH();
00245 }
00246 //---------------------------------------------------------------------------
00247 SurfCl_t::PxFmt8888_t * SurfCl_t::GetData()
00248 {
00249     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00250     return static_cast<SurfClImpl_t *>(m_pImpl)->GetData();
00251 }
00252 //---------------------------------------------------------------------------
00253 void SurfCl_t::OnEvent(const WinApp_t::SizeEv_t &SizeEv, WinApp_t &App)
00254 {
00255     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00256     static_cast<SurfClImpl_t *>(m_pImpl)->OnSizeEv(SizeEv, App);
00257 }
00258 //---------------------------------------------------------------------------
00259 void SurfCl_t::OnEvent(const WinApp_t::DrawEv_t &DrawEv, WinApp_t &App)
00260 {
00261     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00262     static_cast<SurfClImpl_t *>(m_pImpl)->OnDrawEv(DrawEv, App);
00263 }
00264 //---------------------------------------------------------------------------

FipS' Code (Thu Feb 15 22:43:35 2007) - (c) Filip STOKLAS (FipS) - [ www ] [ Guest Book ]