2010-05-07 04:47:10 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2009, 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;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
ImageCache::ImageCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImageCache::~ImageCache()
|
|
|
|
{
|
2010-05-20 16:31:55 -05:00
|
|
|
while (fBitmaps.CountItems()) {
|
|
|
|
BBitmap* bit = fBitmaps.ValueFor(0);
|
2010-05-07 04:47:10 -05:00
|
|
|
delete bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BBitmap*
|
|
|
|
ImageCache::GetImage(BString which, BString name)
|
|
|
|
{
|
2010-05-20 16:31:55 -05:00
|
|
|
if (fInstance == NULL)
|
|
|
|
fInstance = new ImageCache();
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
// Loads the bitmap if found
|
|
|
|
bool found;
|
2010-05-20 16:31:55 -05:00
|
|
|
BBitmap* bitmap = fInstance->fBitmaps.ValueFor(name, &found);
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
bitmap = LoadImage(which.String(), name.String());
|
|
|
|
if (bitmap)
|
2010-05-20 16:31:55 -05:00
|
|
|
fInstance->fBitmaps.AddItem(name, bitmap);
|
2010-05-07 04:47:10 -05:00
|
|
|
return bitmap;
|
|
|
|
} else
|
|
|
|
return bitmap;
|
2010-05-20 16:31:55 -05:00
|
|
|
|
2010-05-07 04:47:10 -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-07 04:47:10 -05:00
|
|
|
|
2010-05-20 16:31:55 -05:00
|
|
|
fInstance->fBitmaps.AddItem(name, which);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ImageCache::DeleteImage(BString name)
|
|
|
|
{
|
2010-05-20 16:31:55 -05:00
|
|
|
if (fInstance == NULL)
|
|
|
|
fInstance = new ImageCache();
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-05-20 16:31:55 -05:00
|
|
|
BBitmap* bitmap = fInstance->fBitmaps.ValueFor(name);
|
|
|
|
if (bitmap) {
|
|
|
|
fInstance->fBitmaps.RemoveItemFor(name);
|
2010-05-07 04:47:10 -05:00
|
|
|
delete bitmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ImageCache::Release()
|
|
|
|
{
|
2010-05-20 16:31:55 -05:00
|
|
|
if (fInstance != NULL) {
|
|
|
|
delete fInstance;
|
|
|
|
fInstance = NULL;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2010-05-07 04:47:10 -05:00
|
|
|
return bitmap;
|
|
|
|
}
|