Chat-O-Matic/application/ImageCache.cpp

100 lines
1.7 KiB
C++
Raw Normal View History

/*
2011-12-03 16:38:03 -06:00
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrea Anzani, andrea.anzani@gmail.com
*/
#include "ImageCache.h"
#include <AppDefs.h>
#include <Bitmap.h>
#include <Debug.h>
#include <TranslationUtils.h>
2010-05-20 16:31:55 -05:00
ImageCache* ImageCache::fInstance = NULL;
ImageCache::ImageCache()
{
}
ImageCache::~ImageCache()
{
2010-05-20 16:31:55 -05:00
while (fBitmaps.CountItems()) {
BBitmap* bit = fBitmaps.ValueFor(0);
delete bit;
}
}
BBitmap*
ImageCache::GetImage(BString which, BString name)
{
2010-05-20 16:31:55 -05:00
if (fInstance == NULL)
fInstance = new ImageCache();
// Loads the bitmap if found
bool found;
2010-05-20 16:31:55 -05:00
BBitmap* bitmap = fInstance->fBitmaps.ValueFor(name, &found);
if (!found) {
bitmap = LoadImage(which.String(), name.String());
if (bitmap)
2010-05-20 16:31:55 -05:00
fInstance->fBitmaps.AddItem(name, bitmap);
return bitmap;
} else
return bitmap;
2010-05-20 16:31:55 -05:00
return NULL;
}
void
ImageCache::AddImage(BString name, BBitmap* which)
{
2010-05-20 16:31:55 -05:00
if (fInstance == NULL)
fInstance = new ImageCache();
2010-05-20 16:31:55 -05:00
fInstance->fBitmaps.AddItem(name, which);
}
void
ImageCache::DeleteImage(BString name)
{
2010-05-20 16:31:55 -05:00
if (fInstance == NULL)
fInstance = new ImageCache();
2010-05-20 16:31:55 -05:00
BBitmap* bitmap = fInstance->fBitmaps.ValueFor(name);
if (bitmap) {
fInstance->fBitmaps.RemoveItemFor(name);
delete bitmap;
}
}
void
ImageCache::Release()
{
2010-05-20 16:31:55 -05:00
if (fInstance != NULL) {
delete fInstance;
fInstance = NULL;
}
}
BBitmap*
ImageCache::LoadImage(const char* fullName, const char* shortName)
{
BBitmap* bitmap = BTranslationUtils::GetBitmap(fullName);
if (!bitmap)
bitmap = BTranslationUtils::GetBitmap('PNG ', shortName);
if (!bitmap)
2010-05-20 16:31:55 -05:00
printf("ImageCache: Can't load bitmap! %s\n", fullName);
return bitmap;
}