*Fix for image resource manager

This commit is contained in:
dimok321 2010-09-26 18:19:45 +00:00
parent 1c4f46e8e5
commit c5c5708e12
8 changed files with 73 additions and 107 deletions

View File

@ -2,8 +2,8 @@
<app version="1"> <app version="1">
<name> USB Loader GX</name> <name> USB Loader GX</name>
<coder>USB Loader GX Team</coder> <coder>USB Loader GX Team</coder>
<version>1.0 r978</version> <version>1.0 r979</version>
<release_date>201009260928</release_date> <release_date>201009260941</release_date>
<short_description>Loads games from USB-devices</short_description> <short_description>Loads games from USB-devices</short_description>
<long_description>USB Loader GX is a libwiigui based USB iso loader with a wii-like GUI. You can install games to your HDDs and boot them with shorter loading times. <long_description>USB Loader GX is a libwiigui based USB iso loader with a wii-like GUI. You can install games to your HDDs and boot them with shorter loading times.
The interactive GUI is completely controllable with WiiMote, Classic Controller or GC Controller. The interactive GUI is completely controllable with WiiMote, Classic Controller or GC Controller.

File diff suppressed because one or more lines are too long

View File

@ -60,19 +60,7 @@ GuiImageData::GuiImageData(const u8 * img, int imgSize)
height = 0; height = 0;
format = GX_TF_RGBA8; format = GX_TF_RGBA8;
if(ResourceManager::Exists(img, imgSize) == true) LoadImage(img, imgSize);
{
GuiImageData * Image = ResourceManager::GetImageData(img, imgSize);
data = Image->GetImage();
width = Image->GetWidth();
height = Image->GetHeight();
format = Image->GetTextureFormat();
}
else
{
LoadImage(img, imgSize);
}
} }
/** /**
@ -89,13 +77,17 @@ void GuiImageData::LoadImage(const u8 * img, int imgSize)
if(!img) if(!img)
return; return;
if(data) ImageData * Image = ResourceManager::GetImageData(img);
if(Image != NULL && Image->data != NULL)
{ {
free(data); data = Image->data;
data = NULL; width = Image->width;
height = Image->height;
format = Image->format;
return;
} }
if (imgSize < 8) else if (imgSize < 8)
{ {
return; return;
} }
@ -124,6 +116,13 @@ void GuiImageData::LoadImage(const u8 * img, int imgSize)
// IMAGE_TPL // IMAGE_TPL
LoadTPL(img, imgSize); LoadTPL(img, imgSize);
} }
ImageData NewImage;
NewImage.data = data;
NewImage.width = width;
NewImage.height = height;
NewImage.format = format;
ResourceManager::AddImageData(img, NewImage);
} }
void GuiImageData::LoadPNG(const u8 *img, int imgSize) void GuiImageData::LoadPNG(const u8 *img, int imgSize)

View File

@ -51,9 +51,8 @@ class GuiImageData
int GetHeight() { return height; }; int GetHeight() { return height; };
//!Gets the texture format //!Gets the texture format
u8 GetTextureFormat() { return format; }; u8 GetTextureFormat() { return format; };
//!Load a new image into this GuiImageData
void LoadImage(const u8 * img, int imgSize);
protected: protected:
void LoadImage(const u8 * img, int imgSize);
void LoadPNG(const u8 *img, int imgSize); void LoadPNG(const u8 *img, int imgSize);
void LoadBMP(const u8 *img, int imgSize); void LoadBMP(const u8 *img, int imgSize);
void LoadJpeg(const u8 *img, int imgSize); void LoadJpeg(const u8 *img, int imgSize);

View File

@ -165,8 +165,7 @@ static void * UpdateGUI(void *arg)
for (i = 5; i < 255; i += 10) for (i = 5; i < 255; i += 10)
{ {
if (strcmp(headlessID, "") == 0) mainWindow->Draw(); if (strcmp(headlessID, "") == 0) mainWindow->Draw();
Menu_DrawRectangle(0, 0, screenwidth, screenheight, ( GXColor ) Menu_DrawRectangle(0, 0, screenwidth, screenheight, (GXColor) {0, 0, 0, i}, 1);
{ 0, 0, 0, i}, 1);
Menu_Render(); Menu_Render();
} }
mainWindow->RemoveAll(); mainWindow->RemoveAll();
@ -236,7 +235,7 @@ GuiImageData *LoadCoverImage(struct discHdr *header, bool Prefere3D, bool noCove
{ {
flag = !flag; flag = !flag;
delete Cover; delete Cover;
Cover = new (std::nothrow) GuiImageData(Resources::GetFile(Prefere3D ? "nocover.png" : "nocoverFlat.png"), Resources::GetFileSize(Prefere3D ? "nocover.png" : "nocoverFlat.png")); Cover = Resources::GetImageData(Prefere3D ? "nocover.png" : "nocoverFlat.png");
if (Cover && Cover->GetImage()) break; if (Cover && Cover->GetImage()) break;
} }
} }
@ -253,22 +252,16 @@ GuiImageData *LoadCoverImage(struct discHdr *header, bool Prefere3D, bool noCove
***************************************************************************/ ***************************************************************************/
int MainMenu(int menu) int MainMenu(int menu)
{ {
currentMenu = menu; currentMenu = menu;
//if (strcmp(headlessID,"")!=0)HaltGui(); pointer[0] = Resources::GetImageData("player1_point.png");
//WindowPrompt("Can you see me now",0,"ok"); pointer[1] = Resources::GetImageData("player2_point.png");
pointer[2] = Resources::GetImageData("player3_point.png");
pointer[0] = new GuiImageData(Resources::GetFile("player1_point.png"), Resources::GetFileSize("player1_point.png")); pointer[3] = Resources::GetImageData("player4_point.png");
pointer[1] = new GuiImageData(Resources::GetFile("player2_point.png"), Resources::GetFileSize("player2_point.png"));
pointer[2] = new GuiImageData(Resources::GetFile("player3_point.png"), Resources::GetFileSize("player3_point.png"));
pointer[3] = new GuiImageData(Resources::GetFile("player4_point.png"), Resources::GetFileSize("player4_point.png"));
mainWindow = new GuiWindow(screenwidth, screenheight); mainWindow = new GuiWindow(screenwidth, screenheight);
const char * image = Settings.widescreen ? "wbackground.png" : "background.png"; background = Resources::GetImageData(Settings.widescreen ? "wbackground.png" : "background.png");
background = new GuiImageData(Resources::GetFile(image), Resources::GetFileSize(image));
bgImg = new GuiImage(background); bgImg = new GuiImage(background);
mainWindow->Append(bgImg); mainWindow->Append(bgImg);

View File

@ -46,59 +46,50 @@ void ResourceManager::DestroyInstance()
ResourceManager::~ResourceManager() ResourceManager::~ResourceManager()
{ {
// Delete all images... // Delete all images...
std::map<const u8 *, GuiImageData *>::iterator imgitr; std::map<const u8 *, ImageData>::iterator imgitr;
for (imgitr = images.begin(); imgitr != images.end(); imgitr++) for (imgitr = images.begin(); imgitr != images.end(); imgitr++)
{ {
delete imgitr->second; if(imgitr->second.data)
free(imgitr->second.data);
} }
images.clear(); images.clear();
imageCount.clear(); imageCount.clear();
} }
bool ResourceManager::Exists(const u8 *img, u32 imgSize) void ResourceManager::AddImageData(const u8 *img, ImageData & Data)
{ {
return ResourceManager::Instance()->InternalExists(img, imgSize); ResourceManager::Instance()->InternalAddImageData(img, Data);
} }
bool ResourceManager::InternalExists(const u8 *img, u32 imgSize) ImageData * ResourceManager::GetImageData(const u8 *img)
{ {
std::map<const u8 *, GuiImageData *>::iterator itr = images.find(img); return ResourceManager::Instance()->InternalGetImageData(img);
return (itr != images.end());
} }
GuiImageData * ResourceManager::GetImageData(const u8 *img, u32 imgSize) void ResourceManager::Remove(u8 * img)
{
return ResourceManager::Instance()->InternalGetImageData(img, imgSize);
}
void ResourceManager::Remove(GuiImageData *img)
{
if(!img)
return;
ResourceManager::Instance()->InternalRemoveImageData(img->GetImage());
}
void ResourceManager::Remove(u8 *img)
{ {
ResourceManager::Instance()->InternalRemoveImageData(img); ResourceManager::Instance()->InternalRemoveImageData(img);
} }
GuiImageData * ResourceManager::InternalGetImageData(const u8 *img, u32 imgSize) void ResourceManager::InternalAddImageData(const u8 * img, ImageData & Data)
{ {
std::map<const u8 *, GuiImageData *>::iterator itr = images.find(img); std::map<const u8 *, ImageData>::iterator itr = images.find(img);
if (itr == images.end()) if (itr != images.end())
{ return;
// Not found, create a new one
GuiImageData *d = new GuiImageData(img, imgSize);
images[img] = d;
imageCount[d->GetImage()] = 1;
return d;
} images[img] = Data;
imageCount[itr->second->GetImage()]++; imageCount[Data.data] = 1;
return itr->second; }
ImageData * ResourceManager::InternalGetImageData(const u8 *img)
{
std::map<const u8 *, ImageData>::iterator itr = images.find(img);
if (itr == images.end())
return NULL;
imageCount[itr->second.data]++;
return &itr->second;
} }
void ResourceManager::InternalRemoveImageData(u8 * img) void ResourceManager::InternalRemoveImageData(u8 * img)
@ -112,12 +103,13 @@ void ResourceManager::InternalRemoveImageData(u8 * img)
{ {
imageCount.erase(itr); imageCount.erase(itr);
std::map<const u8 *, GuiImageData *>::iterator iitr; std::map<const u8 *, ImageData>::iterator iitr;
for (iitr = images.begin(); iitr != images.end(); iitr++) for (iitr = images.begin(); iitr != images.end(); iitr++)
{ {
if (iitr->second->GetImage() == img) if (iitr->second.data == img)
{ {
delete iitr->second; if(iitr->second.data)
free(iitr->second.data);
images.erase(iitr); images.erase(iitr);
break; break;
} }
@ -126,32 +118,7 @@ void ResourceManager::InternalRemoveImageData(u8 * img)
} }
else if(img) else if(img)
{ {
//! This case should actually never accur
free(img); free(img);
} }
} }
void ResourceManager::InternalRemoveImageData(GuiImageData * img)
{
std::map<u8 *, int>::iterator itr = imageCount.find(img->GetImage());
if (itr != imageCount.end())
{
itr->second--;
if (itr->second == 0) // Remove the resource
{
imageCount.erase(itr);
std::map<const u8 *, GuiImageData *>::iterator iitr;
for (iitr = images.begin(); iitr != images.end(); iitr++)
{
if (iitr->second == img)
{
delete iitr->second;
images.erase(iitr);
break;
}
}
}
}
}

View File

@ -31,27 +31,33 @@
#include <map> #include <map>
typedef struct _ImageData
{
u8 * data;
int width;
int height;
u8 format;
} ImageData;
class ResourceManager class ResourceManager
{ {
public: public:
static ResourceManager *Instance(); static ResourceManager *Instance();
static void DestroyInstance(); static void DestroyInstance();
static GuiImageData * GetImageData(const u8 *img, u32 imgSize); static void AddImageData(const u8 *img, ImageData & data);
static bool Exists(const u8 *img, u32 imgSize); static ImageData * GetImageData(const u8 *img);
static void Remove(GuiImageData * img);
static void Remove(u8 * img); static void Remove(u8 * img);
private: private:
bool InternalExists(const u8 *img, u32 imgSize); void InternalAddImageData(const u8 * img, ImageData & Data);
GuiImageData *InternalGetImageData(const u8 *img, u32 imgSize); ImageData *InternalGetImageData(const u8 *img);
void InternalRemoveImageData(GuiImageData *img); void InternalRemoveImageData(u8 * img);
void InternalRemoveImageData(u8 *img);
~ResourceManager(); ~ResourceManager();
static ResourceManager *instance; static ResourceManager *instance;
std::map<const u8 *, GuiImageData *> images; std::map<const u8 *, ImageData> images;
std::map<u8 *, int> imageCount; std::map<u8 *, int> imageCount;
}; };

View File

@ -30,6 +30,8 @@
extern "C" { extern "C" {
#endif #endif
//! fmt and wfmt can only be used once at a session and the strings needs
//! to be copied afterwards. A second use overwrites the first string.
const char * fmt(const char * format, ...); const char * fmt(const char * format, ...);
const wchar_t * wfmt(const char * format, ...); const wchar_t * wfmt(const char * format, ...);
bool char2wchar_t(const char * src, wchar_t * dest); bool char2wchar_t(const char * src, wchar_t * dest);