From b3a4f42f2f3eb8a3b2941e864deb9db9967a35f7 Mon Sep 17 00:00:00 2001 From: JoseAaronLopezGarcia Date: Sun, 28 Jul 2024 17:00:51 +0200 Subject: [PATCH] improve memory management of xmenu --- extras/menus/xMenu/include/entry.h | 12 +++++++----- extras/menus/xMenu/include/menu.h | 2 ++ extras/menus/xMenu/src/entry.cpp | 29 +++++++++++++++++------------ extras/menus/xMenu/src/menu.cpp | 15 +++++++++++++++ 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/extras/menus/xMenu/include/entry.h b/extras/menus/xMenu/include/entry.h index eee53048..b2e26e57 100644 --- a/extras/menus/xMenu/include/entry.h +++ b/extras/menus/xMenu/include/entry.h @@ -58,7 +58,6 @@ class Entry{ unsigned char* sfo_buffer; void readHeader(); - Image* loadIcon(); void findNameInParam(); void animAppear(); @@ -67,6 +66,10 @@ class Entry{ Entry(string path); bool isPops(); + + Image* loadPic0(); + Image* loadPic1(); + public: static Entry* createIfPops(string path); @@ -79,10 +82,9 @@ class Entry{ string getEbootName(); Image* getIcon(); - - Image* getPic0(); - - Image* getPic1(); + + void loadIcon(); + void unloadIcon(); bool run(); diff --git a/extras/menus/xMenu/include/menu.h b/extras/menus/xMenu/include/menu.h index 72808be4..44fe8ac2 100644 --- a/extras/menus/xMenu/include/menu.h +++ b/extras/menus/xMenu/include/menu.h @@ -50,6 +50,8 @@ class Menu{ string fullPath(string path, string name); bool isPOPS(string path); + + void loadIcons(); void updateScreen(); diff --git a/extras/menus/xMenu/src/entry.cpp b/extras/menus/xMenu/src/entry.cpp index d0870eb2..0de78980 100644 --- a/extras/menus/xMenu/src/entry.cpp +++ b/extras/menus/xMenu/src/entry.cpp @@ -24,7 +24,6 @@ Entry* Entry::createIfPops(string path){ if (ent->isPops()){ ent->findNameInParam(); - ent->icon0 = ent->loadIcon(); free(ent->sfo_buffer); ent->sfo_buffer = NULL; return ent; @@ -49,14 +48,20 @@ void Entry::readHeader(){ fclose(fp); } -Image* Entry::loadIcon(){ - int size = header.icon1_offset - header.icon0_offset; - if (size){ - Image* icon = loadImage(this->path.c_str(), this->header.icon0_offset); - if (icon != NULL) - return icon; +void Entry::loadIcon(){ + if (this->icon0 == NULL){ + int size = header.icon1_offset - header.icon0_offset; + if (size){ + this->icon0 = loadImage(this->path.c_str(), this->header.icon0_offset); + } + } +} + +void Entry::unloadIcon(){ + if (this->icon0){ + freeImage(this->icon0); + this->icon0 = NULL; } - return NULL; } bool Entry::isPops(){ @@ -131,13 +136,13 @@ Image* Entry::getIcon(){ return (icon0)? icon0 : common::getNoIcon(); } -Image* Entry::getPic0(){ +Image* Entry::loadPic0(){ int size = this->header.pic1_offset-this->header.pic0_offset; if (size==0) return NULL; return loadImage(this->path.c_str(), this->header.pic0_offset); } -Image* Entry::getPic1(){ +Image* Entry::loadPic1(){ int size = this->header.snd0_offset-this->header.pic1_offset; if (size == 0) return NULL; return loadImage(this->path.c_str(), this->header.pic1_offset); @@ -147,8 +152,8 @@ bool Entry::run(){ if (common::getConf()->fast_gameboot) return true; - this->pic0 = getPic0(); - this->pic1 = getPic1(); + this->pic0 = loadPic0(); + this->pic1 = loadPic1(); animAppear(); diff --git a/extras/menus/xMenu/src/menu.cpp b/extras/menus/xMenu/src/menu.cpp index ef5346f0..0e689934 100644 --- a/extras/menus/xMenu/src/menu.cpp +++ b/extras/menus/xMenu/src/menu.cpp @@ -23,6 +23,7 @@ Menu::Menu(){ if (common::getConf()->sort_entries){ std::sort(eboots.begin(), eboots.end(), Entry::cmpEntriesForSort); } + loadIcons(); } void Menu::readEbootList(string path){ @@ -71,6 +72,18 @@ string Menu::fullPath(string path, string app){ return ""; } +void Menu::loadIcons(){ + int start = this->start; + int end = min(start+3, (int)eboots.size()); + + if (start-1 >= 0) eboots[start-1]->unloadIcon(); + if (end < eboots.size()) eboots[end]->unloadIcon(); + + for (int i=start; iloadIcon(); + } +} + void Menu::draw(){ blitAlphaImageToScreen(0, 0, 480, 272, common::getBG(), 0, 0); @@ -152,6 +165,7 @@ void Menu::moveDown(){ else if (this->index+1 < eboots.size()) this->index++; updateTextAnim(); + loadIcons(); } void Menu::moveUp(){ @@ -165,6 +179,7 @@ void Menu::moveUp(){ else this->index--; updateTextAnim(); + loadIcons(); } void Menu::control(){