Skip to content

Commit aaa2e03

Browse files
committed
Move struct functions out of structs to save memory
This saves memory because the struct pointer is no longer passed to these functions. Also fixed the 2nd custom state text becoming blue when creating a new state. Also made a small optimization in drawCheatsHandleCustomStateAction.
1 parent e3ba97d commit aaa2e03

File tree

7 files changed

+259
-269
lines changed

7 files changed

+259
-269
lines changed

ttyd-tools/rel/include/global.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,13 +1056,6 @@ struct ManageCustomStates
10561056
uint8_t TotalEntries;
10571057
bool StateWasSelected;
10581058
uint8_t SelectedState;
1059-
1060-
// Do not inline resizeStateMemory, createCustomState, nor deleteCustomState
1061-
CustomStateStruct *resizeStateMemory(
1062-
CustomStateStruct *state, uint32_t numEntries, int32_t incrementAmount);
1063-
1064-
char *createCustomState();
1065-
uint32_t deleteCustomState(uint32_t stateIndex);
10661059
};
10671060

10681061
struct CustomStateSettingsStruct
@@ -1223,9 +1216,6 @@ struct SetCustomText
12231216
delete[] (Buffer);
12241217
}
12251218
}
1226-
1227-
// Do not inline customTextInit
1228-
void customTextInit(const char *initialText, uint32_t maxTextSize);
12291219
};
12301220

12311221
extern MenuVars MenuVar;

ttyd-tools/rel/include/menufunctions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ uint32_t marioSpecialMovesButtonControls();
4949
uint32_t partnerChangeYoshiColorButtonControls();
5050
uint32_t followersOptionsButtonControls();
5151

52+
void customTextInit(const char *initialText, uint32_t maxTextSize);
5253
uint32_t setCustomTextButtonControls(char *textOut, uint32_t textSize, bool applyNullTerminator);
5354

5455
void adjustMenuItemBoundsMain(int32_t valueChangedBy, int32_t lowerBound, int32_t upperBound);
@@ -114,6 +115,10 @@ bool cheatsManageTimer(uint32_t buttonInput);
114115
int32_t convertDoubleToString(char *strOut, int32_t totalLength, int32_t decimalCount, double value);
115116
float *getMarioCoordinateFromIndex(int32_t index);
116117

118+
CustomStateStruct *resizeStateMemory(CustomStateStruct *state, uint32_t numEntries, int32_t incrementAmount);
119+
char *createCustomState();
120+
uint32_t deleteCustomState(uint32_t stateIndex);
121+
117122
bool checkForDPADInput();
118123
uint32_t checkButtonSingleFrame();
119124
// void correctPageSingleColumn(uint32_t button, uint8_t &currentPage)

ttyd-tools/rel/source/commonfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ ttyd::memory::SmartAllocationData *allocFromSmartHeap(uint32_t size, uint32_t gr
235235
// Allocate the memory
236236
ttyd::memory::SmartAllocationData *SmartData = ttyd::memory::smartAlloc(size, group);
237237

238-
// Set up a temporary local variable to use for getting memory
238+
// Set up a temporary local variable to use for getting the memory
239239
void *Ptr = SmartData->pMemory;
240240

241241
// Clear the memory, as this is not done automatically

ttyd-tools/rel/source/draw.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4605,7 +4605,10 @@ void drawCheatsManageCustomStates()
46054605
break;
46064606
}
46074607

4608-
bool CurrentOptionCheck = (tempSelectedOption != 0) && (tempCurrentMenuOption == i);
4608+
bool CurrentOptionCheck = (tempSelectedOption != 0) &&
4609+
(tempSelectedOption != CREATE_CUSTOM_STATE) &&
4610+
(tempCurrentMenuOption == i);
4611+
46094612
Color = getSelectedTextColor(CurrentOptionCheck);
46104613

46114614
char *tempBuf = strncpy(
@@ -4633,25 +4636,24 @@ void drawCheatsHandleCustomStateAction()
46334636
// Make sure the player is currently in the game
46344637
if (checkIfInGame())
46354638
{
4636-
char *NewStateName = CustomState.createCustomState();
4639+
char *NewStateName = createCustomState();
46374640
if (NewStateName)
46384641
{
46394642
strncpy(NewStateName, NewName, NameSize);
4640-
closeSecondaryMenu();
46414643
}
46424644
else // Failsafe; shouldn't ever run
46434645
{
4644-
closeSecondaryMenu();
46454646
MenuVar.FunctionReturnCode = MAX_STATES_EXIST;
46464647
MenuVar.Timer = secondsToFrames(3);
46474648
}
46484649
}
46494650
else
46504651
{
4651-
closeSecondaryMenu();
46524652
MenuVar.FunctionReturnCode = STATES_CREATE_NOT_IN_GAME;
46534653
MenuVar.Timer = secondsToFrames(3);
46544654
}
4655+
4656+
closeSecondaryMenu();
46554657
break;
46564658
}
46574659
case RENAME_CUSTOM_STATE:

ttyd-tools/rel/source/global.cpp

Lines changed: 0 additions & 249 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
#include "global.h"
22
#include "memcard.h"
33

4-
#include <ttyd/msgdrv.h>
5-
#include <ttyd/memory.h>
6-
#include <ttyd/mario_pouch.h>
7-
#include <ttyd/mario.h>
8-
#include <ttyd/mario_motion.h>
9-
#include <ttyd/seq_mapchange.h>
10-
114
#include <cstring>
125

136
namespace mod {
@@ -2223,246 +2216,4 @@ void setInitialSettings()
22232216
FrameCounter.ButtonCombo[RESET] = PAD_L | PAD_DPAD_RIGHT;
22242217
}
22252218

2226-
void SetCustomText::customTextInit(const char *initialText, uint32_t maxTextSize)
2227-
{
2228-
char *tempBuffer = reinterpret_cast<char *>(
2229-
clearMemory(Buffer, CUSTOM_TEXT_BUFFER_SIZE));
2230-
2231-
if (initialText)
2232-
{
2233-
#ifdef TTYD_JP
2234-
// Custom text doesn't currently support Japanese characters
2235-
if (ttyd::msgdrv::_ismbblead(initialText[0]))
2236-
{
2237-
// Text starts with a Japanese character
2238-
CurrentIndex = 0;
2239-
}
2240-
else
2241-
{
2242-
#endif
2243-
uint32_t MaxIndex = maxTextSize - 1;
2244-
if (MaxIndex > (CUSTOM_TEXT_BUFFER_SIZE - 1))
2245-
{
2246-
MaxIndex = (CUSTOM_TEXT_BUFFER_SIZE - 1);
2247-
}
2248-
2249-
uint32_t Length = strlen(initialText);
2250-
if (Length > MaxIndex)
2251-
{
2252-
Length = MaxIndex;
2253-
}
2254-
2255-
CurrentIndex = static_cast<uint8_t>(Length);
2256-
strncpy(tempBuffer, initialText, Length);
2257-
#ifdef TTYD_JP
2258-
}
2259-
#endif
2260-
}
2261-
else
2262-
{
2263-
CurrentIndex = 0;
2264-
}
2265-
}
2266-
2267-
CustomStateStruct * ManageCustomStates::resizeStateMemory(
2268-
CustomStateStruct *state, uint32_t numEntries, int32_t incrementAmount)
2269-
{
2270-
uint32_t CurrentStatesSize = sizeof(CustomStateStruct) * numEntries;
2271-
2272-
// Allocate temporary memory to hold the states
2273-
// Allocate the memory on the smart heap to avoid fragmentation
2274-
ttyd::memory::SmartAllocationData *SmartData =
2275-
allocFromSmartHeap(CurrentStatesSize,
2276-
ttyd::memory::SmartAllocationGroup::kNone);
2277-
2278-
// Set up a temporary local variable to use for getting the memory
2279-
CustomStateStruct *tempStateSmart = reinterpret_cast<CustomStateStruct *>(SmartData->pMemory);
2280-
2281-
// Copy the states from the old memory to the temporary memory
2282-
memcpy(tempStateSmart, state, CurrentStatesSize);
2283-
2284-
// Free the old memory
2285-
delete[] (state);
2286-
2287-
// Allocate new memory to hold the states
2288-
state = new CustomStateStruct[numEntries + incrementAmount];
2289-
2290-
// Copy the states from the temporary memory to the new memory
2291-
memcpy(state, tempStateSmart, CurrentStatesSize);
2292-
2293-
// Free the temporary memory
2294-
ttyd::memory::smartFree(SmartData);
2295-
return state;
2296-
}
2297-
2298-
char *ManageCustomStates::createCustomState()
2299-
{
2300-
uint32_t tempTotalEntries = TotalEntries;
2301-
CustomStateStruct *tempState = State;
2302-
2303-
if (tempTotalEntries == 0)
2304-
{
2305-
// No custom states exist, so create one
2306-
if (tempState)
2307-
{
2308-
delete[] (tempState);
2309-
}
2310-
2311-
tempState = new CustomStateStruct;
2312-
State = tempState;
2313-
2314-
tempTotalEntries = 1;
2315-
TotalEntries = tempTotalEntries;
2316-
}
2317-
else if (tempTotalEntries >= CUSTOM_STATES_MAX_COUNT)
2318-
{
2319-
// Maximum number of states have been made
2320-
return nullptr;
2321-
}
2322-
else
2323-
{
2324-
tempState = resizeStateMemory(tempState, tempTotalEntries, 1);
2325-
State = tempState;
2326-
2327-
tempTotalEntries++;
2328-
TotalEntries = tempTotalEntries;
2329-
}
2330-
2331-
// Set the info for the new state
2332-
tempState = &tempState[tempTotalEntries - 1];
2333-
2334-
// Back up the inventory
2335-
uint32_t PouchPtr = reinterpret_cast<uint32_t>(ttyd::mario_pouch::pouchGetPtr());
2336-
2337-
// Standard items
2338-
memcpy(
2339-
tempState->StandardItems,
2340-
reinterpret_cast<void *>(PouchPtr + 0x192),
2341-
sizeof(tempState->StandardItems));
2342-
2343-
// Important items
2344-
memcpy(
2345-
tempState->ImportantItems,
2346-
reinterpret_cast<void *>(PouchPtr + 0xA0),
2347-
sizeof(tempState->ImportantItems));
2348-
2349-
// Badges
2350-
memcpy(
2351-
tempState->Badges,
2352-
reinterpret_cast<void *>(PouchPtr + 0x1FA),
2353-
sizeof(tempState->Badges));
2354-
2355-
// Equipped badges
2356-
memcpy(
2357-
tempState->EquippedBadges,
2358-
reinterpret_cast<void *>(PouchPtr + 0x38A),
2359-
sizeof(tempState->EquippedBadges));
2360-
2361-
// Stored items
2362-
memcpy(
2363-
tempState->StoredItems,
2364-
reinterpret_cast<void *>(PouchPtr + 0x1BA),
2365-
sizeof(tempState->StoredItems));
2366-
2367-
// Back up some of Mario's data
2368-
CustomStateMarioVars *MarioVars = &tempState->MarioVars;
2369-
MarioVars->currentHP = *reinterpret_cast<int16_t *>(PouchPtr + 0x70);
2370-
MarioVars->maxHP = *reinterpret_cast<int16_t *>(PouchPtr + 0x72);
2371-
MarioVars->currentFP = *reinterpret_cast<int16_t *>(PouchPtr + 0x74);
2372-
MarioVars->maxFP = *reinterpret_cast<int16_t *>(PouchPtr + 0x76);
2373-
MarioVars->maxHPEnteringBattle = *reinterpret_cast<int16_t *>(PouchPtr + 0x8E);
2374-
MarioVars->maxFPEnteringBattle = *reinterpret_cast<int16_t *>(PouchPtr + 0x90);
2375-
MarioVars->currentSP = *reinterpret_cast<int16_t *>(PouchPtr + 0x7A);
2376-
MarioVars->maxSP = *reinterpret_cast<int16_t *>(PouchPtr + 0x7C);
2377-
MarioVars->availableBP = *reinterpret_cast<int16_t *>(PouchPtr + 0x92);
2378-
MarioVars->maxBP = *reinterpret_cast<int16_t *>(PouchPtr + 0x94);
2379-
MarioVars->rank = *reinterpret_cast<int16_t *>(PouchPtr + 0x88);
2380-
MarioVars->level = *reinterpret_cast<int16_t *>(PouchPtr + 0x8A);
2381-
MarioVars->starPowersObtained = *reinterpret_cast<uint16_t *>(PouchPtr + 0x8C);
2382-
MarioVars->starPoints = *reinterpret_cast<int16_t *>(PouchPtr + 0x96);
2383-
MarioVars->coins = *reinterpret_cast<int16_t *>(PouchPtr + 0x78);
2384-
2385-
// Back up the partner data
2386-
memcpy(
2387-
tempState->PartyData,
2388-
reinterpret_cast<void *>(PouchPtr + sizeof(ttyd::mario_pouch::PouchPartyData)),
2389-
sizeof(tempState->PartyData));
2390-
2391-
// Back up the sequence position
2392-
tempState->SequencePosition = getSequencePosition();
2393-
2394-
// Back up which partner is currently out
2395-
ttyd::mario::Player *player = ttyd::mario::marioGetPtr();
2396-
uint32_t PartnerPtr = reinterpret_cast<uint32_t>(getPartnerPointer());
2397-
if (PartnerPtr)
2398-
{
2399-
tempState->PartnerOut = player->prevFollowerId[0];
2400-
}
2401-
2402-
// Back up which follower is currently out
2403-
uint32_t FollowerPtr = reinterpret_cast<uint32_t>(getFollowerPointer());
2404-
if (FollowerPtr)
2405-
{
2406-
tempState->FollowerOut = player->prevFollowerId[1];
2407-
}
2408-
2409-
// Back up if Mario is currently using Boat Mode or not
2410-
if (player->currentMotionId == ttyd::mario_motion::MarioMotions::kShip)
2411-
{
2412-
tempState->MarioIsShip = true;
2413-
}
2414-
else
2415-
{
2416-
tempState->MarioIsShip = false;
2417-
}
2418-
2419-
// Back up the current map
2420-
strncpy(
2421-
tempState->CurrentMap,
2422-
ttyd::seq_mapchange::NextMap,
2423-
sizeof(tempState->CurrentMap));
2424-
2425-
// Back up the current loading zone
2426-
strncpy(
2427-
tempState->CurrentBero,
2428-
ttyd::seq_mapchange::NextBero,
2429-
sizeof(tempState->CurrentBero));
2430-
2431-
// Return a pointer to the new state's name
2432-
return tempState->StateName;
2433-
}
2434-
2435-
uint32_t ManageCustomStates::deleteCustomState(uint32_t stateIndex)
2436-
{
2437-
CustomStateStruct *tempState = State;
2438-
uint32_t tempTotalEntries = TotalEntries;
2439-
2440-
// Decrement TotalEntries now since it'll be used several times in the following code
2441-
tempTotalEntries--;
2442-
TotalEntries = tempTotalEntries;
2443-
2444-
// Check if there are other states left after deleting the selected one
2445-
if (tempTotalEntries > 0)
2446-
{
2447-
// If deleting the last entry, don't do any moving beforehand
2448-
if (stateIndex < tempTotalEntries)
2449-
{
2450-
// Move all of the states under the one being deleted up by one slot
2451-
uint32_t CopySize = sizeof(CustomStateStruct) * (tempTotalEntries - stateIndex);
2452-
memcpy(&tempState[stateIndex], &tempState[stateIndex + 1], CopySize);
2453-
}
2454-
2455-
State = resizeStateMemory(tempState, tempTotalEntries, 0);
2456-
}
2457-
else
2458-
{
2459-
// There are no more states left
2460-
delete[] (tempState);
2461-
State = nullptr;
2462-
}
2463-
2464-
// Return the total number of entries
2465-
return tempTotalEntries;
2466-
}
2467-
24682219
}

ttyd-tools/rel/source/menu.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ void menuCheckButton()
16261626
{
16271627
if (tempTotalEntries < CUSTOM_STATES_MAX_COUNT)
16281628
{
1629-
CustomText.customTextInit(nullptr, 0);
1629+
customTextInit(nullptr, 0);
16301630
MenuVar.SecondaryMenuOption = 0;
16311631
MenuVar.MenuSelectedOption = CurrentMenuOptionCheck;
16321632
MenuVar.SelectedOption = CurrentMenuOptionCheck;
@@ -1676,7 +1676,7 @@ void menuCheckButton()
16761676
}
16771677
case DELETE_CUSTOM_STATE:
16781678
{
1679-
uint32_t TotalEntries = CustomState.deleteCustomState(tempCurrentMenuOption);
1679+
uint32_t TotalEntries = deleteCustomState(tempCurrentMenuOption);
16801680
if (TotalEntries == 0)
16811681
{
16821682
closeSecondaryMenu();
@@ -1704,7 +1704,7 @@ void menuCheckButton()
17041704
sizeof(CustomState.State[tempCurrentMenuOption].StateName);
17051705

17061706
// Add 1 to StateNameSize since it's not null terminated
1707-
CustomText.customTextInit(StateName, StateNameSize + 1);
1707+
customTextInit(StateName, StateNameSize + 1);
17081708

17091709
MenuVar.SecondaryMenuOption = 0;
17101710
MenuVar.MenuSelectedOption = tempSelectedOption;
@@ -1932,7 +1932,7 @@ void menuCheckButton()
19321932
HandledYoshiOption = true;
19331933

19341934
// char NewYoshiName[11]; // One byte for NULL
1935-
CustomText.customTextInit(ttyd::mario_pouch::pouchGetYoshiName(), 11);
1935+
customTextInit(ttyd::mario_pouch::pouchGetYoshiName(), 11);
19361936

19371937
MenuVar.MenuSelectedOption = STATS_PARTNER_DISPLAY_YOSHI_NAME;
19381938
MenuVar.SecondaryMenuOption = 0;

0 commit comments

Comments
 (0)