Skip to content

Commit

Permalink
improve memory management of xmenu
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAaronLopezGarcia committed Jul 28, 2024
1 parent d99f4f7 commit b3a4f42
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
12 changes: 7 additions & 5 deletions extras/menus/xMenu/include/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Entry{
unsigned char* sfo_buffer;

void readHeader();
Image* loadIcon();
void findNameInParam();

void animAppear();
Expand All @@ -67,6 +66,10 @@ class Entry{
Entry(string path);
bool isPops();


Image* loadPic0();
Image* loadPic1();

public:

static Entry* createIfPops(string path);
Expand All @@ -79,10 +82,9 @@ class Entry{
string getEbootName();

Image* getIcon();

Image* getPic0();

Image* getPic1();

void loadIcon();
void unloadIcon();

bool run();

Expand Down
2 changes: 2 additions & 0 deletions extras/menus/xMenu/include/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Menu{
string fullPath(string path, string name);

bool isPOPS(string path);

void loadIcons();

void updateScreen();

Expand Down
29 changes: 17 additions & 12 deletions extras/menus/xMenu/src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand Down
15 changes: 15 additions & 0 deletions extras/menus/xMenu/src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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; i<end; i++){
eboots[i]->loadIcon();
}
}

void Menu::draw(){
blitAlphaImageToScreen(0, 0, 480, 272, common::getBG(), 0, 0);

Expand Down Expand Up @@ -152,6 +165,7 @@ void Menu::moveDown(){
else if (this->index+1 < eboots.size())
this->index++;
updateTextAnim();
loadIcons();
}

void Menu::moveUp(){
Expand All @@ -165,6 +179,7 @@ void Menu::moveUp(){
else
this->index--;
updateTextAnim();
loadIcons();
}

void Menu::control(){
Expand Down

0 comments on commit b3a4f42

Please sign in to comment.