From c5c5708e1207999693f5ccd75d9ea61527052df9 Mon Sep 17 00:00:00 2001 From: dimok321 <15055714+dimok789@users.noreply.github.com> Date: Sun, 26 Sep 2010 18:19:45 +0000 Subject: [PATCH] *Fix for image resource manager --- HBC/META.XML | 4 +- gui.pnproj | 2 +- source/libwiigui/gui_imagedata.cpp | 33 +++++------ source/libwiigui/gui_imagedata.h | 3 +- source/menu.cpp | 21 +++---- source/utils/ResourceManager.cpp | 93 ++++++++++-------------------- source/utils/ResourceManager.h | 22 ++++--- source/utils/StringTools.h | 2 + 8 files changed, 73 insertions(+), 107 deletions(-) diff --git a/HBC/META.XML b/HBC/META.XML index cbfa46fb..6868d63b 100644 --- a/HBC/META.XML +++ b/HBC/META.XML @@ -2,8 +2,8 @@ USB Loader GX USB Loader GX Team - 1.0 r978 - 201009260928 + 1.0 r979 + 201009260941 Loads games from USB-devices 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. diff --git a/gui.pnproj b/gui.pnproj index 88aef2e8..08ffb98f 100644 --- a/gui.pnproj +++ b/gui.pnproj @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/source/libwiigui/gui_imagedata.cpp b/source/libwiigui/gui_imagedata.cpp index d5fecd4b..57bbb414 100644 --- a/source/libwiigui/gui_imagedata.cpp +++ b/source/libwiigui/gui_imagedata.cpp @@ -60,19 +60,7 @@ GuiImageData::GuiImageData(const u8 * img, int imgSize) height = 0; format = GX_TF_RGBA8; - if(ResourceManager::Exists(img, imgSize) == true) - { - GuiImageData * Image = ResourceManager::GetImageData(img, imgSize); - - data = Image->GetImage(); - width = Image->GetWidth(); - height = Image->GetHeight(); - format = Image->GetTextureFormat(); - } - else - { - LoadImage(img, imgSize); - } + LoadImage(img, imgSize); } /** @@ -89,13 +77,17 @@ void GuiImageData::LoadImage(const u8 * img, int imgSize) if(!img) return; - if(data) + ImageData * Image = ResourceManager::GetImageData(img); + if(Image != NULL && Image->data != NULL) { - free(data); - data = NULL; + data = Image->data; + width = Image->width; + height = Image->height; + format = Image->format; + return; } - if (imgSize < 8) + else if (imgSize < 8) { return; } @@ -124,6 +116,13 @@ void GuiImageData::LoadImage(const u8 * img, int imgSize) // IMAGE_TPL 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) diff --git a/source/libwiigui/gui_imagedata.h b/source/libwiigui/gui_imagedata.h index 271c2887..59542a54 100644 --- a/source/libwiigui/gui_imagedata.h +++ b/source/libwiigui/gui_imagedata.h @@ -51,9 +51,8 @@ class GuiImageData int GetHeight() { return height; }; //!Gets the texture format u8 GetTextureFormat() { return format; }; - //!Load a new image into this GuiImageData - void LoadImage(const u8 * img, int imgSize); protected: + void LoadImage(const u8 * img, int imgSize); void LoadPNG(const u8 *img, int imgSize); void LoadBMP(const u8 *img, int imgSize); void LoadJpeg(const u8 *img, int imgSize); diff --git a/source/menu.cpp b/source/menu.cpp index 0873b6e3..cfec1985 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -165,8 +165,7 @@ static void * UpdateGUI(void *arg) for (i = 5; i < 255; i += 10) { if (strcmp(headlessID, "") == 0) mainWindow->Draw(); - Menu_DrawRectangle(0, 0, screenwidth, screenheight, ( GXColor ) - { 0, 0, 0, i}, 1); + Menu_DrawRectangle(0, 0, screenwidth, screenheight, (GXColor) {0, 0, 0, i}, 1); Menu_Render(); } mainWindow->RemoveAll(); @@ -236,7 +235,7 @@ GuiImageData *LoadCoverImage(struct discHdr *header, bool Prefere3D, bool noCove { flag = !flag; 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; } } @@ -253,22 +252,16 @@ GuiImageData *LoadCoverImage(struct discHdr *header, bool Prefere3D, bool noCove ***************************************************************************/ int MainMenu(int menu) { - currentMenu = menu; - //if (strcmp(headlessID,"")!=0)HaltGui(); - //WindowPrompt("Can you see me now",0,"ok"); - - pointer[0] = new GuiImageData(Resources::GetFile("player1_point.png"), Resources::GetFileSize("player1_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")); + pointer[0] = Resources::GetImageData("player1_point.png"); + pointer[1] = Resources::GetImageData("player2_point.png"); + pointer[2] = Resources::GetImageData("player3_point.png"); + pointer[3] = Resources::GetImageData("player4_point.png"); mainWindow = new GuiWindow(screenwidth, screenheight); - - const char * image = Settings.widescreen ? "wbackground.png" : "background.png"; - background = new GuiImageData(Resources::GetFile(image), Resources::GetFileSize(image)); + background = Resources::GetImageData(Settings.widescreen ? "wbackground.png" : "background.png"); bgImg = new GuiImage(background); mainWindow->Append(bgImg); diff --git a/source/utils/ResourceManager.cpp b/source/utils/ResourceManager.cpp index 9dbeee7a..a18881f7 100644 --- a/source/utils/ResourceManager.cpp +++ b/source/utils/ResourceManager.cpp @@ -46,59 +46,50 @@ void ResourceManager::DestroyInstance() ResourceManager::~ResourceManager() { // Delete all images... - std::map::iterator imgitr; + std::map::iterator imgitr; for (imgitr = images.begin(); imgitr != images.end(); imgitr++) { - delete imgitr->second; + if(imgitr->second.data) + free(imgitr->second.data); } images.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::iterator itr = images.find(img); - - return (itr != images.end()); + return ResourceManager::Instance()->InternalGetImageData(img); } -GuiImageData * ResourceManager::GetImageData(const u8 *img, u32 imgSize) -{ - return ResourceManager::Instance()->InternalGetImageData(img, imgSize); -} - -void ResourceManager::Remove(GuiImageData *img) -{ - if(!img) - return; - - ResourceManager::Instance()->InternalRemoveImageData(img->GetImage()); -} - -void ResourceManager::Remove(u8 *img) +void ResourceManager::Remove(u8 * img) { ResourceManager::Instance()->InternalRemoveImageData(img); } -GuiImageData * ResourceManager::InternalGetImageData(const u8 *img, u32 imgSize) +void ResourceManager::InternalAddImageData(const u8 * img, ImageData & Data) { - std::map::iterator itr = images.find(img); - if (itr == images.end()) - { - // Not found, create a new one - GuiImageData *d = new GuiImageData(img, imgSize); - images[img] = d; - imageCount[d->GetImage()] = 1; - return d; + std::map::iterator itr = images.find(img); + if (itr != images.end()) + return; - } - imageCount[itr->second->GetImage()]++; - return itr->second; + images[img] = Data; + imageCount[Data.data] = 1; +} + +ImageData * ResourceManager::InternalGetImageData(const u8 *img) +{ + std::map::iterator itr = images.find(img); + if (itr == images.end()) + return NULL; + + imageCount[itr->second.data]++; + + return &itr->second; } void ResourceManager::InternalRemoveImageData(u8 * img) @@ -112,12 +103,13 @@ void ResourceManager::InternalRemoveImageData(u8 * img) { imageCount.erase(itr); - std::map::iterator iitr; + std::map::iterator 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); break; } @@ -126,32 +118,7 @@ void ResourceManager::InternalRemoveImageData(u8 * img) } else if(img) { + //! This case should actually never accur free(img); } } - -void ResourceManager::InternalRemoveImageData(GuiImageData * img) -{ - std::map::iterator itr = imageCount.find(img->GetImage()); - if (itr != imageCount.end()) - { - itr->second--; - - if (itr->second == 0) // Remove the resource - { - imageCount.erase(itr); - - std::map::iterator iitr; - for (iitr = images.begin(); iitr != images.end(); iitr++) - { - if (iitr->second == img) - { - delete iitr->second; - images.erase(iitr); - break; - } - } - } - } -} - diff --git a/source/utils/ResourceManager.h b/source/utils/ResourceManager.h index 51825e55..320ca910 100644 --- a/source/utils/ResourceManager.h +++ b/source/utils/ResourceManager.h @@ -31,27 +31,33 @@ #include +typedef struct _ImageData +{ + u8 * data; + int width; + int height; + u8 format; +} ImageData; + class ResourceManager { public: static ResourceManager *Instance(); static void DestroyInstance(); - static GuiImageData * GetImageData(const u8 *img, u32 imgSize); - static bool Exists(const u8 *img, u32 imgSize); - static void Remove(GuiImageData * img); + static void AddImageData(const u8 *img, ImageData & data); + static ImageData * GetImageData(const u8 *img); static void Remove(u8 * img); private: - bool InternalExists(const u8 *img, u32 imgSize); - GuiImageData *InternalGetImageData(const u8 *img, u32 imgSize); - void InternalRemoveImageData(GuiImageData *img); - void InternalRemoveImageData(u8 *img); + void InternalAddImageData(const u8 * img, ImageData & Data); + ImageData *InternalGetImageData(const u8 *img); + void InternalRemoveImageData(u8 * img); ~ResourceManager(); static ResourceManager *instance; - std::map images; + std::map images; std::map imageCount; }; diff --git a/source/utils/StringTools.h b/source/utils/StringTools.h index 7fad884b..73bdecbb 100644 --- a/source/utils/StringTools.h +++ b/source/utils/StringTools.h @@ -30,6 +30,8 @@ extern "C" { #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 wchar_t * wfmt(const char * format, ...); bool char2wchar_t(const char * src, wchar_t * dest);