Skip to content

Commit

Permalink
Use tex sizes from texture rather than hardcoded values (HarbourMaste…
Browse files Browse the repository at this point in the history
…rs#610)

* Use tex sizes from texture rather than hardcoded values

* Dynamic do action tex sizes

* Remove unused minimap texture keys

* Restore MESSAGE_STATIC_TEX_SIZE

* Use dynamic offsets

* MACRO it up

* Enable SPDLOG in Xcode

* Handle non-existent texture
  • Loading branch information
dcvz authored Aug 1, 2022
1 parent 47ad3bd commit 582f084
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 87 deletions.
36 changes: 32 additions & 4 deletions libultraship/libultraship/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include <iostream>

#define LOAD_TEX(texPath) static_cast<Ship::Texture*>(Ship::GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(texPath).get());

extern "C" {
struct OSMesgQueue;

Expand Down Expand Up @@ -123,8 +125,7 @@ extern "C" {
const std::string* hashStr = Ship::GlobalCtx2::GetInstance()->GetResourceManager()->HashToString(crc);

if (hashStr != nullptr) {
const auto res = static_cast<Ship::Texture*>(Ship::GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(hashStr->c_str()).get());

const auto res = LOAD_TEX(hashStr->c_str());
ModInternal::ExecuteHooks<ModInternal::LoadTexture>(hashStr->c_str(), &res->imageData);

return reinterpret_cast<char*>(res->imageData);
Expand All @@ -151,13 +152,40 @@ extern "C" {
}

char* ResourceMgr_LoadTexByName(char* texPath) {
const auto res = static_cast<Ship::Texture*>(Ship::GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(texPath).get());
const auto res = LOAD_TEX(texPath);
ModInternal::ExecuteHooks<ModInternal::LoadTexture>(texPath, &res->imageData);
return (char*)res->imageData;
}

uint16_t ResourceMgr_LoadTexWidthByName(char* texPath) {
const auto res = LOAD_TEX(texPath);
if (res != nullptr)
return res->width;

SPDLOG_ERROR("Given texture path is a non-existent resource");
return -1;
}

uint16_t ResourceMgr_LoadTexHeightByName(char* texPath) {
const auto res = LOAD_TEX(texPath);
if (res != nullptr)
return res->height;

SPDLOG_ERROR("Given texture path is a non-existent resource");
return -1;
}

uint32_t ResourceMgr_LoadTexSizeByName(char* texPath) {
const auto res = LOAD_TEX(texPath);
if (res != nullptr)
return res->imageDataSize;

SPDLOG_ERROR("Given texture path is a non-existent resource");
return -1;
}

void ResourceMgr_WriteTexS16ByName(char* texPath, size_t index, s16 value) {
const auto res = static_cast<Ship::Texture*>(Ship::GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(texPath).get());
const auto res = LOAD_TEX(texPath);

if (res != nullptr)
{
Expand Down
2 changes: 0 additions & 2 deletions soh/include/z64.h
Original file line number Diff line number Diff line change
Expand Up @@ -1458,8 +1458,6 @@ typedef struct {
/* 0x18 */ s16 (*roomCompassOffsetY)[44]; // dungeon compass icon Y offset by room
/* 0x1C */ u8* dgnMinimapCount; // number of room minimaps
/* 0x20 */ u16* dgnMinimapTexIndexOffset; // dungeon minimap texture index offset
/* 0x24 */ u16* owMinimapTexSize;
/* 0x28 */ u16* owMinimapTexOffset;
/* 0x2C */ s16* owMinimapPosX;
/* 0x30 */ s16* owMinimapPosY;
/* 0x34 */ s16 (*owCompassInfo)[4]; // [X scale, Y scale, X offset, Y offset]
Expand Down
6 changes: 4 additions & 2 deletions soh/soh.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6668,10 +6668,12 @@
"$(inherited)",
"-DENABLE_OPENGL",
"-DNDEBUG",
"-DSPDLOG_ACTIVE_LEVEL=0",
);
OTHER_CPLUSPLUSFLAGS = (
"-DENABLE_OPENGL",
"-DNDEBUG",
"-DSPDLOG_ACTIVE_LEVEL=0",
);
OTHER_LIBTOOLFLAGS = "";
OTHER_REZFLAGS = "";
Expand Down Expand Up @@ -6728,10 +6730,12 @@
"$(inherited)",
"-DENABLE_OPENGL",
"-D_DEBUG",
"-DSPDLOG_ACTIVE_LEVEL=0",
);
OTHER_CPLUSPLUSFLAGS = (
"-DENABLE_OPENGL",
"-D_DEBUG",
"-DSPDLOG_ACTIVE_LEVEL=0",
);
OTHER_LIBTOOLFLAGS = "";
OTHER_REZFLAGS = "";
Expand Down Expand Up @@ -6842,7 +6846,6 @@
"-DENABLE_OPENGL",
"-D_CRT_SECURE_NO_WARNINGS",
"-D_CONSOLE",
"-DSPDLOG_ACTIVE_LEVEL=0",
"-DNDEBUG",
);
OTHER_LDFLAGS = (
Expand Down Expand Up @@ -7031,7 +7034,6 @@
"-DENABLE_OPENGL",
"-D_CRT_SECURE_NO_WARNINGS",
"-D_CONSOLE",
"-DSPDLOG_ACTIVE_LEVEL=0",
"-D_DEBUG",
);
OTHER_LDFLAGS = (
Expand Down
78 changes: 78 additions & 0 deletions soh/soh.xcodeproj/xcshareddata/xcschemes/soh.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6D85CFA44008444FB26F04BA"
BuildableName = "soh.app"
BlueprintName = "soh"
ReferencedContainer = "container:soh.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6D85CFA44008444FB26F04BA"
BuildableName = "soh.app"
BlueprintName = "soh"
ReferencedContainer = "container:soh.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6D85CFA44008444FB26F04BA"
BuildableName = "soh.app"
BlueprintName = "soh"
ReferencedContainer = "container:soh.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
3 changes: 3 additions & 0 deletions soh/soh/GbiWrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ void ResourceMgr_CacheDirectory(const char* resName);
void ResourceMgr_LoadFile(const char* resName);
char* ResourceMgr_LoadFileFromDisk(const char* filePath);
char* ResourceMgr_LoadTexByName(char* texPath);
uint16_t ResourceMgr_LoadTexWidthByName(char* texPath);
uint16_t ResourceMgr_LoadTexHeightByName(char* texPath);
uint32_t ResourceMgr_LoadTexSizeByName(char* texPath);
char* ResourceMgr_LoadTexOrDListByName(char* filePath);
char* ResourceMgr_LoadPlayerAnimByName(char* animPath);
char* ResourceMgr_GetNameByCRC(uint64_t crc, char* alloc);
Expand Down
6 changes: 6 additions & 0 deletions soh/soh/OTRGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ extern "C" char* ResourceMgr_LoadJPEG(char* data, int dataSize)

extern "C" char* ResourceMgr_LoadTexByName(const char* texPath);

extern "C" uint16_t ResourceMgr_LoadTexWidthByName(char* texPath);

extern "C" uint16_t ResourceMgr_LoadTexHeightByName(char* texPath);

extern "C" uint32_t ResourceMgr_LoadTexSizeByName(const char* texPath);

extern "C" char* ResourceMgr_LoadTexOrDListByName(const char* filePath) {
auto res = OTRGlobals::Instance->context->GetResourceManager()->LoadResource(filePath);

Expand Down
3 changes: 3 additions & 0 deletions soh/soh/OTRGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ void ResourceMgr_LoadFile(const char* resName);
char* ResourceMgr_LoadFileFromDisk(const char* filePath);
char* ResourceMgr_LoadJPEG(char* data, int dataSize);
char* ResourceMgr_LoadTexByName(const char* texPath);
uint16_t ResourceMgr_LoadTexWidthByName(char* texPath);
uint16_t ResourceMgr_LoadTexHeightByName(char* texPath);
uint32_t ResourceMgr_LoadTexSizeByName(char* texPath);
char* ResourceMgr_LoadTexOrDListByName(const char* filePath);
char* ResourceMgr_LoadPlayerAnimByName(const char* animPath);
AnimationHeaderCommon* ResourceMgr_LoadAnimByName(const char* path);
Expand Down
28 changes: 4 additions & 24 deletions soh/src/code/z_construct.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ void func_80110990(GlobalContext* globalCtx) {
void func_801109B0(GlobalContext* globalCtx) {
InterfaceContext* interfaceCtx = &globalCtx->interfaceCtx;
u32 parameterSize;
u16 doActionOffset;
u8 temp;

gSaveContext.sunsSongState = SUNSSONG_INACTIVE;
Expand Down Expand Up @@ -49,30 +48,11 @@ void func_801109B0(GlobalContext* globalCtx) {

ASSERT(interfaceCtx->doActionSegment != NULL);

if (gSaveContext.language == LANGUAGE_ENG) {
doActionOffset = 0;
} else if (gSaveContext.language == LANGUAGE_GER) {
doActionOffset = 0x2B80;
} else {
doActionOffset = 0x5700;
}

memcpy(interfaceCtx->doActionSegment, ResourceMgr_LoadTexByName(gAttackDoActionENGTex), 0x180);
memcpy(interfaceCtx->doActionSegment + 0x180, ResourceMgr_LoadTexByName(gCheckDoActionENGTex), 0x180);
//DmaMgr_SendRequest1(interfaceCtx->doActionSegment, (uintptr_t)_do_action_staticSegmentRomStart + doActionOffset, 0x300,
//__FILE__, __LINE__);

if (gSaveContext.language == LANGUAGE_ENG) {
doActionOffset = 0x480;
} else if (gSaveContext.language == LANGUAGE_GER) {
doActionOffset = 0x3000;
} else {
doActionOffset = 0x5B80;
}
uint32_t attackDoActionTexSize = ResourceMgr_LoadTexSizeByName(gAttackDoActionENGTex);
memcpy(interfaceCtx->doActionSegment, ResourceMgr_LoadTexByName(gAttackDoActionENGTex), attackDoActionTexSize);
memcpy(interfaceCtx->doActionSegment + (attackDoActionTexSize / 2), ResourceMgr_LoadTexByName(gCheckDoActionENGTex), attackDoActionTexSize);

memcpy(interfaceCtx->doActionSegment + 0x300, ResourceMgr_LoadTexByName(gReturnDoActionENGTex), 0x180);
//DmaMgr_SendRequest1(interfaceCtx->doActionSegment + 0x300, (uintptr_t)_do_action_staticSegmentRomStart + doActionOffset,
//0x180);
memcpy(interfaceCtx->doActionSegment + attackDoActionTexSize, ResourceMgr_LoadTexByName(gReturnDoActionENGTex), ResourceMgr_LoadTexSizeByName(gReturnDoActionENGTex));

interfaceCtx->iconItemSegment = GAMESTATE_ALLOC_MC(
&globalCtx->state, 0x1000 * ARRAY_COUNT(gSaveContext.equips.buttonItems));
Expand Down
29 changes: 9 additions & 20 deletions soh/src/code/z_map_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,6 @@ static u16 sDgnMinimapTexIndexOffset[10] = {
0, 13, 32, 49, 76, 114, 158, 190, 217, 227,
};

static u16 sOwMinimapTexSize[24] = {
2920, 2560, 1560, 2784, 2976, 2040, 3240, 2336, 2080, 2600, 1792, 1888,
3400, 1792, 1888, 2040, 3120, 2304, 2176, 1888, 1560, 3240, 2600, 3400,
};

static u16 sOwMinimapTexOffset[24] = {
0x0000, 0x0B68, 0x1568, 0x1B80, 0x2660, 0x3200, 0x39F8, 0x46A0, 0x4FC0, 0x57E0, 0x6208, 0x6908,
0x7068, 0x7DB0, 0x84B0, 0x8C10, 0x9408, 0xA038, 0xA938, 0xB1B8, 0xB918, 0xBF30, 0xCBD8, 0xD600,
};

static s16 sOwMinimapPosX[24] = {
216, 216, 218, 202, 202, 250, 216, 234, 234, 216, 234, 234,
216, 234, 234, 250, 216, 234, 234, 234, 218, 80, 80, 216,
Expand Down Expand Up @@ -319,14 +309,13 @@ static u8 sFloorID[10][8] = {
static s16 sSkullFloorIconY[10] = { -47, -47, -33, -47, -47, -5, -19, -47, -99, -99 };

MapData gMapDataTable = {
sFloorTexIndexOffset, sBossFloor, sRoomPalette,
sMaxPaletteCount, sPaletteRoom, sRoomCompassOffsetX,
sRoomCompassOffsetY, sDgnMinimapCount, sDgnMinimapTexIndexOffset,
sOwMinimapTexSize, sOwMinimapTexOffset, sOwMinimapPosX,
sOwMinimapPosY, sOwCompassInfo, sDgnMinimapTexIndexBase,
sDgnCompassInfo, sOwMinimapWidth, sOwMinimapHeight,
sOwEntranceIconPosX, sOwEntranceIconPosY, sOwEntranceFlag,
sFloorCoordY, sSwitchEntryCount, sSwitchFromRoom,
sSwitchFromFloor, sSwitchToRoom, sFloorID,
sSkullFloorIconY,
sFloorTexIndexOffset, sBossFloor, sRoomPalette,
sMaxPaletteCount, sPaletteRoom, sRoomCompassOffsetX,
sRoomCompassOffsetY, sDgnMinimapCount, sDgnMinimapTexIndexOffset,
sOwMinimapPosX, sOwMinimapPosY, sOwCompassInfo,
sDgnMinimapTexIndexBase, sDgnCompassInfo, sOwMinimapWidth,
sOwMinimapHeight, sOwEntranceIconPosX, sOwEntranceIconPosY,
sOwEntranceFlag, sFloorCoordY, sSwitchEntryCount,
sSwitchFromRoom, sSwitchFromFloor, sSwitchToRoom,
sFloorID, sSkullFloorIconY,
};
9 changes: 6 additions & 3 deletions soh/src/code/z_map_exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@ void Map_InitData(GlobalContext* globalCtx, s16 room) {
//(uintptr_t)_map_grand_staticSegmentRomStart + gMapData->owMinimapTexOffset[extendedMapIndex],
//gMapData->owMinimapTexSize[mapIndex], __FILE__, __LINE__);

if (sEntranceIconMapIndex < 24)
memcpy(globalCtx->interfaceCtx.mapSegment, ResourceMgr_LoadTexByName(minimapTableOW[sEntranceIconMapIndex]), gMapData->owMinimapTexSize[mapIndex]);
if (sEntranceIconMapIndex < 24) {
const char* textureName = minimapTableOW[sEntranceIconMapIndex];
memcpy(globalCtx->interfaceCtx.mapSegment, ResourceMgr_LoadTexByName(textureName), ResourceMgr_LoadTexSizeByName(textureName));
}

interfaceCtx->unk_258 = mapIndex;
break;
Expand Down Expand Up @@ -445,7 +447,8 @@ void Map_InitData(GlobalContext* globalCtx, s16 room) {
//((gMapData->dgnMinimapTexIndexOffset[mapIndex] + room) * 0xFF0),
//0xFF0, __FILE__, __LINE__);

memcpy(globalCtx->interfaceCtx.mapSegment, ResourceMgr_LoadTexByName(minimapTableDangeon[gMapData->dgnMinimapTexIndexOffset[mapIndex] + room]), 0xFF0);
const char* textureName = minimapTableDangeon[gMapData->dgnMinimapTexIndexOffset[mapIndex] + room];
memcpy(globalCtx->interfaceCtx.mapSegment, ResourceMgr_LoadTexByName(textureName), ResourceMgr_LoadTexSizeByName(textureName));

R_COMPASS_OFFSET_X = gMapData->roomCompassOffsetX[mapIndex][room];
R_COMPASS_OFFSET_Y = gMapData->roomCompassOffsetY[mapIndex][room];
Expand Down
11 changes: 6 additions & 5 deletions soh/src/code/z_message_PAL.c
Original file line number Diff line number Diff line change
Expand Up @@ -1196,15 +1196,15 @@ void Message_LoadItemIcon(GlobalContext* globalCtx, u16 itemId, s16 y) {
R_TEXTBOX_ICON_YPOS = y + 6;
R_TEXTBOX_ICON_SIZE = 32;
memcpy((uintptr_t)msgCtx->textboxSegment + MESSAGE_STATIC_TEX_SIZE,
ResourceMgr_LoadTexByName(gItemIcons[itemId]), 0x1000);
ResourceMgr_LoadTexByName(gItemIcons[itemId]), ResourceMgr_LoadTexSizeByName(gItemIcons[itemId]));
// "Item 32-0"
osSyncPrintf("アイテム32-0\n");
} else {
R_TEXTBOX_ICON_XPOS = R_TEXT_INIT_XPOS - sIconItem24XOffsets[gSaveContext.language];
R_TEXTBOX_ICON_YPOS = y + 10;
R_TEXTBOX_ICON_SIZE = 24;
memcpy((uintptr_t)msgCtx->textboxSegment + MESSAGE_STATIC_TEX_SIZE,
ResourceMgr_LoadTexByName(gItemIcons[itemId]), 0x900);
ResourceMgr_LoadTexByName(gItemIcons[itemId]), ResourceMgr_LoadTexSizeByName(gItemIcons[itemId]));
// "Item 24"
osSyncPrintf("アイテム24=%d (%d) {%d}\n", itemId, itemId - ITEM_KOKIRI_EMERALD, 84);
}
Expand Down Expand Up @@ -1565,9 +1565,9 @@ void Message_Decode(GlobalContext* globalCtx) {
msgCtx->textboxBackgroundUnkArg = font->msgBuf[msgCtx->msgBufPos + 3] & 0xF;

memcpy((uintptr_t)msgCtx->textboxSegment + MESSAGE_STATIC_TEX_SIZE,
ResourceMgr_LoadTexByName(gRedMessageXLeftTex), 0x900);
ResourceMgr_LoadTexByName(gRedMessageXLeftTex), ResourceMgr_LoadTexSizeByName(gRedMessageXLeftTex));
memcpy((uintptr_t)msgCtx->textboxSegment + MESSAGE_STATIC_TEX_SIZE + 0x900,
ResourceMgr_LoadTexByName(gRedMessageXRightTex), 0x900);
ResourceMgr_LoadTexByName(gRedMessageXRightTex), ResourceMgr_LoadTexSizeByName(gRedMessageXRightTex));

msgCtx->msgBufPos += 3;
R_TEXTBOX_BG_YPOS = R_TEXTBOX_Y + 8;
Expand Down Expand Up @@ -1750,7 +1750,8 @@ void Message_OpenText(GlobalContext* globalCtx, u16 textId) {
// "Text Box Type"
osSyncPrintf("吹き出し種類=%d\n", msgCtx->textBoxType);
if (textBoxType < TEXTBOX_TYPE_NONE_BOTTOM) {
memcpy(msgCtx->textboxSegment, ResourceMgr_LoadTexByName(msgStaticTbl[messageStaticIndices[textBoxType]]), MESSAGE_STATIC_TEX_SIZE);
const char* textureName = msgStaticTbl[messageStaticIndices[textBoxType]];
memcpy(msgCtx->textboxSegment, ResourceMgr_LoadTexByName(textureName), MESSAGE_STATIC_TEX_SIZE);
if (textBoxType == TEXTBOX_TYPE_BLACK) {
msgCtx->textboxColorRed = 0;
msgCtx->textboxColorGreen = 0;
Expand Down
Loading

0 comments on commit 582f084

Please sign in to comment.