Skip to content

Commit

Permalink
Reduce battery usage by only re-drawing 3D screen layer when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Aug 13, 2024
1 parent fabcfb1 commit f8e1bc6
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 247 deletions.
87 changes: 69 additions & 18 deletions quickmenu/arm9/source/graphics/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ int progressBarLength = 0;

float cursorTargetTL = 0.0f, cursorTargetTR = 0.0f, cursorTargetBL = 0.0f, cursorTargetBR = 0.0f;
float cursorTL = 0.0f, cursorTR = 0.0f, cursorBL = 0.0f, cursorBR = 0.0f;
float cursorTLPrev = 0.0f, cursorTRPrev = 0.0f, cursorBLPrev = 0.0f, cursorBRPrev = 0.0f;

extern int spawnedtitleboxes;

Expand Down Expand Up @@ -583,7 +584,7 @@ auto getMenuEntryTexture(MenuEntry entry) {

void vBlankHandler()
{
if (fadeType == true) {
if (fadeType) {
if (!fadeDelay) {
screenBrightness--;
if (screenBrightness < 0) screenBrightness = 0;
Expand All @@ -607,13 +608,79 @@ void vBlankHandler()
}
}

static bool updateFrame = true;
static bool whiteScreenPrev = whiteScreen;
static bool showProgressBarPrev = showProgressBar;
static int progressBarLengthPrev = progressBarLength;
static bool showCursorPrev = showCursor;
static bool startMenuPrev = startMenu;

if (whiteScreenPrev != whiteScreen) {
whiteScreenPrev = whiteScreen;
updateFrame = true;
}

if (showProgressBarPrev != showProgressBar) {
showProgressBarPrev = showProgressBar;
updateFrame = true;
}

if (progressBarLengthPrev != progressBarLength) {
progressBarLengthPrev = progressBarLength;
updateFrame = true;
}

if (showCursorPrev != showCursor) {
showCursorPrev = showCursor;
updateFrame = true;
}

if (startMenuPrev != startMenu) {
startMenuPrev = startMenu;
updateFrame = true;
}

if (!whiteScreen && startMenu) {
for (int i = 0; i < 2; i++) {
if (bnriconisDSi[i] && playBannerSequence(i)) {
updateFrame = true;
}
}
for (int i = 0; i < 7; i++) {
if (moveIconUp[i]) {
iconYpos[i] -= 6;
updateFrame = true;
}
}
}

constexpr float swiftness = 0.25f;
cursorTL += (cursorTargetTL - cursorTL) * swiftness;
cursorBL += (cursorTargetBL - cursorBL) * swiftness;
cursorTR += (cursorTargetTR - cursorTR) * swiftness;
cursorBR += (cursorTargetBR - cursorBR) * swiftness;

{
if (cursorTLPrev != cursorTL) {
cursorTLPrev = cursorTL;
updateFrame = true;
}

if (cursorBLPrev != cursorBL) {
cursorBLPrev = cursorBL;
updateFrame = true;
}

if (cursorTRPrev != cursorTR) {
cursorTRPrev = cursorTR;
updateFrame = true;
}

if (cursorBRPrev != cursorBR) {
cursorBRPrev = cursorBR;
updateFrame = true;
}

if (updateFrame) {
glBegin2D();
{
if (controlBottomBright) SetBrightness(0, screenBrightness);
Expand Down Expand Up @@ -643,10 +710,6 @@ void vBlankHandler()
} else drawIcon(1, 40, iconYpos[0]+6);
if (bnrWirelessIcon[1] > 0) glSprite(207, iconYpos[0]+30, GL_FLIP_NONE, &wirelessicons.images[(bnrWirelessIcon[0]-1) & 31]);
}
// Playback animated icon
if (bnriconisDSi[0]==true) {
playBannerSequence(0);
}
glSprite(33, iconYpos[1], GL_FLIP_NONE, getMenuEntryTexture(MenuEntry::PICTOCHAT));
glSprite(129, iconYpos[2], GL_FLIP_NONE, getMenuEntryTexture(MenuEntry::DOWNLOADPLAY));
glSprite(33, iconYpos[3], GL_FLIP_NONE, getMenuEntryTexture(MenuEntry::GBA));
Expand All @@ -659,10 +722,6 @@ void vBlankHandler()
glSprite(10, iconYpos[4], GL_FLIP_NONE, getMenuEntryTexture(MenuEntry::BRIGHTNESS));
}
if (bnrWirelessIcon[num] > 0) glSprite(207, iconYpos[3]+30, GL_FLIP_NONE, &wirelessicons.images[(bnrWirelessIcon[1]-1) & 31]);
// Playback animated icon
if (bnriconisDSi[1]==true) {
playBannerSequence(1);
}
if (!ms().kioskMode) {
glSprite(117, iconYpos[5], GL_FLIP_NONE, getMenuEntryTexture(MenuEntry::SETTINGS));
}
Expand Down Expand Up @@ -699,14 +758,6 @@ void vBlankHandler()
glEnd2D();
GFX_FLUSH = 0;
}

if (!whiteScreen) {
for (int i = 0; i < 7; i++) {
if (moveIconUp[i]) {
iconYpos[i] -= 6;
}
}
}
}

static void clockNeedleDraw(int angle, u32 length, u16 color) {
Expand Down
5 changes: 4 additions & 1 deletion quickmenu/arm9/source/ndsheaderbanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int currentbnriconframeseq[2] = {0};
* Play banner sequence.
* @param binFile Banner file.
*/
void playBannerSequence(int num)
bool playBannerSequence(int num)
{
if (bnriconframeseq[num][currentbnriconframeseq[num] + 1] == 0x0100) {
// Do nothing if icon isn't animated
Expand Down Expand Up @@ -179,6 +179,9 @@ void playBannerSequence(int num)
if (bnriconframeseq[num][currentbnriconframeseq[num]] == 0x0000) {
currentbnriconframeseq[num] = 0; // Reset sequence
}
return true;
}
}

return false;
}
2 changes: 1 addition & 1 deletion quickmenu/arm9/source/ndsheaderbanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,6 @@ void clearBannerSequence(int num);
* Play banner sequence.
* @param binFile Banner file.
*/
void playBannerSequence(int num);
bool playBannerSequence(int num);

#endif // NDS_HEADER2
9 changes: 9 additions & 0 deletions romsel_aktheme/arm9/source/fileBrowse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ extern touchPosition touch;

extern void bgOperations(bool waitFrame);

static inline void doFrameUpdate(void) {
extern bool updateFrame;
while (updateFrame) {
swiWaitForVBlank();
}
updateFrame = true;
}

int file_count = 0;
static int fileStartPos = 0; // The position of the first thing that is not a directory.

Expand Down Expand Up @@ -513,6 +521,7 @@ void loadIcons(const int screenOffset, std::vector<DirEntry> dirContents) {
}
displayDiskIcon(false);

doFrameUpdate();
updateText(false);
}

Expand Down
148 changes: 84 additions & 64 deletions romsel_aktheme/arm9/source/graphics/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int screenBrightness = 0;
bool lcdSwapped = false;
static bool secondBuffer = false;
bool doubleBuffer = true;
bool updateFrame = true;

static int colonTimer = 0;
static bool showColon = true;
Expand Down Expand Up @@ -1560,22 +1561,87 @@ static void loadPng(const bool top, const std::string filename) {

void vBlankHandler()
{
glBegin2D();
{
if (fadeType) {
screenBrightness--;
if (screenBrightness < 0) screenBrightness = 0;
} else {
screenBrightness++;
if (screenBrightness > 31) screenBrightness = 31;
}
if (ms().macroMode) {
SetBrightness(0, lcdSwapped ? screenBrightness : 31);
SetBrightness(1, !lcdSwapped ? screenBrightness : 31);
} else {
if (controlBottomBright) SetBrightness(0, screenBrightness);
if (controlTopBright) SetBrightness(1, screenBrightness);
if (fadeType) {
screenBrightness--;
if (screenBrightness < 0) screenBrightness = 0;
} else {
screenBrightness++;
if (screenBrightness > 31) screenBrightness = 31;
}
if (ms().macroMode) {
SetBrightness(0, lcdSwapped ? screenBrightness : 31);
SetBrightness(1, !lcdSwapped ? screenBrightness : 31);
} else {
if (controlBottomBright) SetBrightness(0, screenBrightness);
if (controlTopBright) SetBrightness(1, screenBrightness);
}

static bool showdialogboxPrev = showdialogbox;
static int dialogboxHeightPrev = dialogboxHeight;

if (showdialogboxPrev != showdialogbox) {
showdialogboxPrev = showdialogbox;
updateFrame = true;
}

if (showdialogbox && (dialogboxHeightPrev != dialogboxHeight)) {
dialogboxHeightPrev = dialogboxHeight;
updateFrame = true;
}

if (displayIcons && iconsToDisplay > 0) {
for (int i = 0; i < iconsToDisplay; i++) {
if (bnriconisDSi[i] && playBannerSequence(i)) {
updateFrame = true;
}
}
if (iconScaleEnabled) {
if (!iconScaleDelay) {
if (iconScaleLarge) {
iconScale += 110;
if (iconScale == 110) {
iconShift = 1;
} else if (iconScale == 330) {
iconShift = 2;
} else if (iconScale == 550) {
iconShift = 3;
} else if (iconScale == 660) {
iconScaleLarge = false;
}
} else {
iconScale -= 110;
if (iconScale == 330) {
iconShift = 2;
} else if (iconScale == 110) {
iconShift = 1;
} else if (iconScale == 0) {
iconShift = 0;
iconScaleLarge = true;
}
}
updateFrame = true;
}
if (iconScaleDelay++ == 2) {
iconScaleDelay = 0;
}
} else if (ms().ak_zoomIcons) {
if (iconScaleWait++ == 60) {
iconScaleWait = 0;
iconScaleEnabled = true;
}
}
}

// Blink colon once per second
if (colonTimer >= 60) {
colonTimer = 0;
showColon = !showColon;
}

colonTimer++;

if (updateFrame) {
glBegin2D();

// glColor(RGB15(31, 31-(3*blfLevel), 31-(6*blfLevel)));
glColor(RGB15(31, 31, 31));
Expand All @@ -1591,46 +1657,6 @@ void vBlankHandler()
drawIcon(i, 5, 22+(i*38), 0);
}
// if (bnrWirelessIcon > 0) glSprite(24, 12, GL_FLIP_NONE, &wirelessIcons[(bnrWirelessIcon-1) & 31]);
// Playback animated icons
// if (!stopDSiAnim && bnriconisDSi[i]) {
if (bnriconisDSi[i]) {
playBannerSequence(i);
}
// stopDSiAnimNotif = stopDSiAnim;
}
if (iconScaleEnabled) {
if (!iconScaleDelay) {
if (iconScaleLarge) {
iconScale += 110;
if (iconScale == 110) {
iconShift = 1;
} else if (iconScale == 330) {
iconShift = 2;
} else if (iconScale == 550) {
iconShift = 3;
} else if (iconScale == 660) {
iconScaleLarge = false;
}
} else {
iconScale -= 110;
if (iconScale == 330) {
iconShift = 2;
} else if (iconScale == 110) {
iconShift = 1;
} else if (iconScale == 0) {
iconShift = 0;
iconScaleLarge = true;
}
}
}
if (iconScaleDelay++ == 2) {
iconScaleDelay = 0;
}
} else if (ms().ak_zoomIcons) {
if (iconScaleWait++ == 60) {
iconScaleWait = 0;
iconScaleEnabled = true;
}
}
}
if (showdialogbox) {
Expand All @@ -1647,18 +1673,12 @@ void vBlankHandler()
} else {
vblankRefreshCounter++;
}
}
glEnd2D();
GFX_FLUSH = 0;

// Blink colon once per second
if (colonTimer >= 60) {
colonTimer = 0;
showColon = !showColon;
glEnd2D();
GFX_FLUSH = 0;
updateFrame = false;
}

colonTimer++;

if (doubleBuffer) {
dmaCopyHalfWordsAsynch(0, topImageWithText[secondBuffer], BG_GFX_SUB, 0x18000);
dmaCopyHalfWordsAsynch(1, bottomImageWithBar[secondBuffer], BG_GFX, 0x18000);
Expand Down
5 changes: 4 additions & 1 deletion romsel_aktheme/arm9/source/ndsheaderbanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ void clearBannerSequence(int iconnum)
* Play banner sequence.
* @param binFile Banner file.
*/
void playBannerSequence(int iconnum)
bool playBannerSequence(int iconnum)
{
if (bnriconframeseq[iconnum][currentbnriconframeseq[iconnum] + 1] == 0x0100) {
// Do nothing if icon isn't animated
Expand Down Expand Up @@ -649,6 +649,9 @@ void playBannerSequence(int iconnum)
if (bnriconframeseq[iconnum][currentbnriconframeseq[iconnum]] == 0x0000) {
currentbnriconframeseq[iconnum] = 0; // Reset sequence
}
return true;
}
}

return false;
}
2 changes: 1 addition & 1 deletion romsel_aktheme/arm9/source/ndsheaderbanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,6 @@ void clearBannerSequence(int iconnum);
* Play banner sequence.
* @param binFile Banner file.
*/
void playBannerSequence(int iconnum);
bool playBannerSequence(int iconnum);

#endif // NDS_HEADER2
Loading

0 comments on commit f8e1bc6

Please sign in to comment.