wg_resource_handle.cpp

00001 // wg_resource_handle.cpp
00002 //
00003 // Resource Handle implementation
00004 //
00005 //
00006 // Copyright (c) 2002-2004 Rob Wiskow
00007 // rob-dev@boxedchaos.com
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022 //
00023 
00024 #include "wgui_include_config.h"
00025 #include "wg_resource_handle.h"
00026 #include "wg_error.h"
00027 #include "wutil_debug.h"
00028 
00029 namespace wGui
00030 {
00031 
00032 std::map<TResourceId, unsigned int> CResourceHandle::m_RefCountMap;
00033 std::map<TResourceId, SDL_Surface*> CBitmapResourceHandle::m_BitmapMap;
00034 std::map<TResourceId, std::wstring> CStringResourceHandle::m_StringMap;
00035 std::map<TResourceId, SDL_Cursor*> CCursorResourceHandle::m_SDLCursorMap;
00036 TResourceId CResourceHandle::m_NextUnusedResourceId = 10000;
00037 
00038 
00039 CResourceHandle::CResourceHandle(TResourceId resId) :
00040      m_ResourceId(resId)
00041 {
00042      if (m_ResourceId == AUTO_CREATE_RESOURCE_ID)
00043      {
00044           while (m_RefCountMap.find(m_NextUnusedResourceId) != m_RefCountMap.end())
00045           {
00046                ++m_NextUnusedResourceId;
00047           }
00048           m_ResourceId = m_NextUnusedResourceId;
00049           ++m_NextUnusedResourceId;
00050      }
00051      if (m_RefCountMap.find(m_ResourceId) == m_RefCountMap.end() || m_RefCountMap[m_ResourceId] == 0)
00052      {
00053           m_RefCountMap[m_ResourceId] = 0;
00054      }
00055      ++m_RefCountMap[m_ResourceId];
00056 }
00057 
00058 
00059 CResourceHandle::CResourceHandle(const CResourceHandle& resHandle)
00060 {
00061      m_ResourceId = resHandle.m_ResourceId;
00062      ++m_RefCountMap[m_ResourceId];
00063 }
00064 
00065 
00066 CResourceHandle::~CResourceHandle(void)
00067 {
00068      if (GetRefCount() > 0)
00069      {
00070           --m_RefCountMap[m_ResourceId];
00071      }
00072      else
00073      {
00074           throw(Wg_Ex_App(L"CResourceHandle::~CResourceHandle : Trying to decrement refcount of zero!"));
00075      }
00076 }
00077 
00078 
00079 CBitmapResourceHandle::~CBitmapResourceHandle(void)
00080 {
00081      if (GetRefCount() == 1 && m_BitmapMap.find(m_ResourceId) != m_BitmapMap.end())
00082      {
00083           SDL_FreeSurface(m_BitmapMap[m_ResourceId]);
00084           m_BitmapMap.erase(m_ResourceId);
00085      }
00086 }
00087 
00088 
00089 SDL_Surface* CBitmapResourceHandle::Bitmap(void) const
00090 {
00091      return (m_BitmapMap.find(m_ResourceId) != m_BitmapMap.end()) ? m_BitmapMap[m_ResourceId] : 0;
00092 }
00093 
00094 
00095 CBitmapFileResourceHandle::CBitmapFileResourceHandle(std::wstring sFilename) :
00096      CBitmapResourceHandle(AUTO_CREATE_RESOURCE_ID),
00097      m_sFilename(sFilename)
00098 {
00099      if (m_BitmapMap.find(m_ResourceId) == m_BitmapMap.end())
00100      {
00101           SDL_Surface* pSurface = SDL_LoadBMP(stdex::ToMbString(m_sFilename).c_str());
00102           if (!pSurface)
00103           {
00104                throw(Wg_Ex_App(L"CBitmapFileResourceHandle::CBitmapFileResourceHandle - Unable to load bitmap"));
00105           }
00106           m_BitmapMap[m_ResourceId] = pSurface;
00107      }
00108 }
00109 
00110 
00111 CStringResourceHandle::~CStringResourceHandle(void)
00112 {
00113      if (GetRefCount() == 1 && m_StringMap.find(m_ResourceId) != m_StringMap.end())
00114      {
00115           m_StringMap.erase(m_ResourceId);
00116      }
00117 }
00118 
00119 
00120 const std::wstring CStringResourceHandle::String(void) const
00121 {
00122      return (m_StringMap.find(m_ResourceId) != m_StringMap.end()) ? m_StringMap[m_ResourceId] : L"";
00123 }
00124 
00125 
00126 CCursorResourceHandle::~CCursorResourceHandle(void)
00127 {
00128      if (GetRefCount() == 1 && m_SDLCursorMap.find(m_ResourceId) != m_SDLCursorMap.end())
00129      {
00130           SDL_FreeCursor(m_SDLCursorMap[m_ResourceId]);
00131           m_SDLCursorMap.erase(m_ResourceId);
00132      }
00133 }
00134 
00135 
00136 SDL_Cursor* CCursorResourceHandle::Cursor(void) const
00137 {
00138      return (m_SDLCursorMap.find(m_ResourceId) != m_SDLCursorMap.end()) ? m_SDLCursorMap[m_ResourceId] : 0;
00139 }
00140 
00141 }
00142 

Generated on Wed May 16 23:11:26 2007 for wGui by  doxygen 1.5.1