#include "headers.h" CBitmapCache g_BitmapCache; CBitmapCache::CBitmapCache(): m_cache(5, CacheNode::cmp) { } CBitmapCache::~CBitmapCache() { for (int i = 0; i < m_cache.getCount(); ++i) { delete m_cache[i].bitmap; free(m_cache[i].path); } } HBITMAP CBitmapCache::LoadBitmap(const TCHAR *path) { CacheNode search = {0}; search.path = (TCHAR *)path; CacheNode *newNode = m_cache.find(&search); if (newNode) { newNode->refCount++; return newNode->bitmap; } newNode = new CacheNode; newNode->path = _tcsdup(path); char *s = mir_t2a(path); newNode->bitmap = (HBITMAP)CallService(MS_UTILS_LOADBITMAP, 0, (LPARAM)s); mir_free(s); newNode->refCount = 1; m_cache.insert(newNode); return newNode->bitmap; } void CBitmapCache::UnloadBitmap(HBITMAP bmp) { for (int i = 0; i < m_cache.getCount(); ++i) if (m_cache[i].bitmap == bmp) { if (--m_cache[i].refCount == 0) { DeleteObject(m_cache[i].bitmap); free(m_cache[i].path); m_cache.remove(i); } return; } }