Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tfe/TFE_DarkForces/darkForcesMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
using namespace TFE_Memory;
using namespace TFE_Input;

#ifdef TFE_ESPBOX
void espbox_free_loader_scratch(); // TFE_Jedi/Level/level.cpp
#endif

namespace TFE_DarkForces
{
/////////////////////////////////////////////
Expand Down Expand Up @@ -666,13 +670,27 @@ namespace TFE_DarkForces
handleLevelComplete();
}

#ifdef TFE_ESPBOX
// The level is freed before what comes next starts (here, as in the
// original order below, nothing uses it any more): the next mission's
// cutscene (up to ~1MB) does not fit next to a big level on the
// handhelds (finishing NARSHADA ran out of memory in 'jabba1').
region_clear(s_levelRegion);
bitmap_clearLevelData();
bitmap_setAllocator(s_gameRegion);
level_freeAllAssets();
TFE_A11Y::clearCaptions();

startNextMode();
#else
startNextMode();

region_clear(s_levelRegion);
bitmap_clearLevelData();
bitmap_setAllocator(s_gameRegion);
level_freeAllAssets();
TFE_A11Y::clearCaptions();
#endif
}
} break;
}
Expand Down Expand Up @@ -1371,6 +1389,11 @@ namespace TFE_DarkForces
if (!writeState)
{
agent_restartEndLevelTask();
#ifdef TFE_ESPBOX
// As after a level load: the loaders' read buffers keep the size of the largest
// file they read (up to ~0.7MB) and are not needed while playing.
espbox_free_loader_scratch();
#endif
}

time_pause(JFALSE);
Expand Down
122 changes: 96 additions & 26 deletions tfe/TFE_Jedi/Level/level.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <climits>
#include <cstring>
#include <string>
#include <vector>

#include "level.h"
#include "levelData.h"
Expand Down Expand Up @@ -41,6 +43,15 @@ void espbox_free_sprite_scratch();
void espbox_free_level_scratch();
void espbox_free_inf_scratch();
void espbox_free_model_scratch();
// All of the loaders' scratch read buffers (after a level or a save state is loaded).
void espbox_free_loader_scratch()
{
espbox_free_rtexture_scratch();
espbox_free_sprite_scratch();
espbox_free_level_scratch();
espbox_free_inf_scratch();
espbox_free_model_scratch();
}
#endif

namespace TFE_Jedi
Expand Down Expand Up @@ -85,11 +96,7 @@ namespace TFE_Jedi
inf_load(levelName);
#ifdef TFE_ESPBOX
// The loaders' scratch read buffers (~0.8MB) are not needed while playing.
espbox_free_rtexture_scratch();
espbox_free_sprite_scratch();
espbox_free_level_scratch();
espbox_free_inf_scratch();
espbox_free_model_scratch();
espbox_free_loader_scratch();
#endif
level_loadGoals(levelName);

Expand All @@ -113,6 +120,71 @@ namespace TFE_Jedi
s_palModified = JTRUE;
}

// Loads the level texture of slot `index` (and the base copy kept for serialization).
static bool level_loadTexture(s32 index, const char* textureName)
{
TextureData* tex = bitmap_load(textureName, 1);
if (!tex)
{
TFE_System::logWrite(LOG_WARNING, "level_loadGeometry", "Could not open '%s', using 'default.bm' instead.", textureName);
tex = bitmap_load("default.bm", 1);
if (!tex)
{
TFE_System::logWrite(LOG_ERROR, "level_loadGeometry", "'default.bm' is not a valid BM file!");
assert(0);
return false;
}
}
TextureData** texture = &s_levelState.textures[index];
*texture = tex;
// This version never gets modified, so serialization is simpler.
s_levelState.textures[s_levelState.textureCount + index] = tex;

// Setup an animated texture.
if (tex->uvWidth == BM_ANIMATED_TEXTURE)
{
bitmap_setupAnimatedTexture(texture, index);
}
return true;
}

#ifdef TFE_ESPBOX
// Loads the textures (named in the level's list) that a sector or a wall uses.
static bool level_loadUsedTextures(const std::vector<std::string>& names)
{
const s32 count = s_levelState.textureCount;
std::vector<u8> used(count, 0);
auto mark = [&](TextureData** slot)
{
const ptrdiff_t index = slot ? slot - s_levelState.textures : -1;
if (index >= 0 && index < count) { used[index] = 1; }
};
for (s32 s = 0; s < s_levelState.sectorCount; s++)
{
const RSector* sector = &s_levelState.sectors[s];
mark(sector->floorTex);
mark(sector->ceilTex);
for (s32 w = 0; w < sector->wallCount; w++)
{
const RWall* wall = &sector->walls[w];
mark(wall->midTex);
mark(wall->topTex);
mark(wall->botTex);
mark(wall->signTex);
}
}
s32 loaded = 0;
for (s32 i = 0; i < count; i++)
{
if (!used[i] || names[i].empty()) { continue; }
if (!level_loadTexture(i, names[i].c_str())) { return false; }
loaded++;
}
TFE_System::logWrite(LOG_MSG, "level_loadGeometry", "Loaded the %d of %d listed textures the level uses.", loaded, count);
return true;
}
#endif

JBool level_loadGeometry(const char* levelName)
{
s_levelState.secretCount = 0;
Expand Down Expand Up @@ -217,6 +289,13 @@ namespace TFE_Jedi
memset(s_levelState.textures, 0, 2 * s_levelState.textureCount * sizeof(TextureData**));

// Load Textures.
// TFE_ESPBOX: only the textures the sectors and walls use are loaded, after they are read
// (level_loadUsedTextures() below): a level's list can name many it never draws (JABSHIP
// lists 359 and draws 51: 1.1MB of unused bitmaps). The others' slots stay null, like
// <NoTexture>; INF only copies textures from one sector or wall to another.
#ifdef TFE_ESPBOX
std::vector<std::string> textureNames(s_levelState.textureCount);
#endif
TextureData** texture = s_levelState.textures;
TextureData** texBase = s_levelState.textures + s_levelState.textureCount;
for (s32 i = 0; i < s_levelState.textureCount; i++, texture++, texBase++)
Expand All @@ -226,35 +305,23 @@ namespace TFE_Jedi
if (sscanf(line, " TEXTURE: %s ", textureName) != 1)
{
TFE_System::logWrite(LOG_ERROR, "level_loadGeometry", "Cannot read texture name.");
#ifdef TFE_ESPBOX
textureNames[i] = "default.bm";
#else
*texture = bitmap_load("default.bm", 1);
#endif
}
else if (strcasecmp(textureName, "<NoTexture>") == 0)
{
*texture = nullptr;
}
else
{
TextureData* tex = bitmap_load(textureName, 1);
if (!tex)
{
TFE_System::logWrite(LOG_WARNING, "level_loadGeometry", "Could not open '%s', using 'default.bm' instead.", textureName);
tex = bitmap_load("default.bm", 1);
if (!tex)
{
TFE_System::logWrite(LOG_ERROR, "level_loadGeometry", "'default.bm' is not a valid BM file!");
assert(0);
return false;
}
}
*texture = tex;
// This version never gets modified, so serialization is simpler.
*texBase = tex;

// Setup an animated texture.
if (tex->uvWidth == BM_ANIMATED_TEXTURE)
{
bitmap_setupAnimatedTexture(texture, i);
}
#ifdef TFE_ESPBOX
textureNames[i] = textureName;
#else
if (!level_loadTexture(i, textureName)) { return false; }
#endif
}
}

Expand Down Expand Up @@ -571,6 +638,9 @@ namespace TFE_Jedi
s_levelState.controlSector->id = s_levelState.sectorCount;
s_levelState.controlSector->index = s_levelState.controlSector->id;

#ifdef TFE_ESPBOX
if (!level_loadUsedTextures(textureNames)) { return false; }
#endif
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ namespace TFE_Jedi
EdgePairFixed* flatEdge;
EdgePairFixed flatEdgeList[MAX_SEG];
EdgePairFixed* adjoinEdge;
#ifdef TFE_ESPBOX
// Indexed by s_adjoinSegCount, which wall_addAdjoinSegment() keeps below MAX_ADJOIN_SEG
// for the whole frame (each sector's edges start at the count, which never goes down
// within a frame): MAX_ADJOIN_SEG entries are all it uses (-253KB).
EdgePairFixed adjoinEdgeList[MAX_ADJOIN_SEG];
#else
EdgePairFixed adjoinEdgeList[MAX_ADJOIN_SEG * MAX_ADJOIN_DEPTH];
#endif

RWallSegmentFixed wallSegListDst[MAX_SEG];
RWallSegmentFixed wallSegListSrc[MAX_SEG];
Expand Down
7 changes: 5 additions & 2 deletions tfe/TFE_Jedi/Task/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ namespace TFE_Jedi
TASK_PREALLOCATED_CHUNKS = 1,

#ifdef TFE_ESPBOX
TASK_STACK_SIZE = 8 * 1024, // 8KB of (local context) stack memory per task.
TASK_STACK_CHUNK_SIZE = 16, // 128KB of memory per chunk of 16 tasks with stack memory.
// The stack holds a task's local contexts, one per recursion level (at most
// TASK_MAX_LEVELS): none has an array, the largest is 64 bytes, so 1KB at most.
// (ctxAllocate() asserts that it fits.)
TASK_STACK_SIZE = 2 * 1024, // 2KB of (local context) stack memory per task.
TASK_STACK_CHUNK_SIZE = 16, // 32KB of memory per chunk of 16 tasks with stack memory.
#else
TASK_STACK_SIZE = 32 * 1024, // 32KB of stack memory.
TASK_STACK_CHUNK_SIZE = 64, // 2MB of memory for 64 tasks with stack memory.
Expand Down