Skip to content

Commit

Permalink
Load PSX .din
Browse files Browse the repository at this point in the history
  • Loading branch information
cyxx committed Oct 16, 2020
1 parent e8fb867 commit a010d8c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void Game::clearLevelData() {

freeLevelData();
if (g_hasPsx) {
_res.unloadLevelDataPsx(kResTypePsx_DIN);
_res.unloadLevelDataPsx(kResTypePsx_LEV);
_res.unloadLevelDataPsx(kResTypePsx_SON);
}
Expand Down Expand Up @@ -1204,6 +1205,7 @@ void Game::initLevel(bool keepInventoryObjects) {
_varsTable[kVarConradLife] = 2000;
_res.loadLevelData(_level);
if (g_hasPsx) {
_res.loadLevelDataPsx(_level, kResTypePsx_DIN);
_res.loadLevelDataPsx(_level, kResTypePsx_LEV);
_res.loadLevelDataPsx(_level, kResTypePsx_SON);
}
Expand Down
2 changes: 1 addition & 1 deletion opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ int Game::op_playSound(int argc, int32_t *argv) {

// The flags are not correctly masked in the original engine.
// eg. masking 0x7FFFF instead of 0x7FFF. The sound system uses
// bits 1 and 2, so this was clearly an typo.
// bits 1 and 2, so this was clearly a typo.
//
// cseg01:00038F01 mov cl, byte ptr [ebp+flags+1]
// cseg01:00038F04 test cl, 80h
Expand Down
30 changes: 27 additions & 3 deletions resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Resource::Resource() {
_textIndexesTable = 0;
_vrmLoadingBitmap = 0;
_vagOffsetsTableSize = 0;
memset(_dinOffsetsTable, 0, sizeof(_dinOffsetsTable));
_fileDin = 0;
memset(_levOffsetsTable, 0, sizeof(_levOffsetsTable));
_fileLev = 0;
memset(_sonOffsetsTable, 0, sizeof(_sonOffsetsTable));
Expand Down Expand Up @@ -958,6 +960,8 @@ static const struct {
const char *ext;
int type;
} _resTreeTablePsx[] = {
{ "ANI", kResType_ANI },
{ "STM", kResType_STM },
{ "PAL", kResType_PAL },
{ "SPR", kResType_SPR },
{ "F3D", kResType_F3D },
Expand All @@ -966,6 +970,15 @@ static const struct {

void Resource::loadLevelDataPsx(int level, int resType) {
switch (resType) {
case kResTypePsx_DIN: {
char name[16];
snprintf(name, sizeof(name), "level%d.din", level + 1);
_fileDin = fileOpenPsx(name, kFileType_PSX_LEVELDATA, level + 1);
if (_fileDin) {
readDataOffsetsTable(_fileDin, kResOffsetType_DIN, _dinOffsetsTable, kResPsxDinOffsetsTableSize);
}
}
break;
case kResTypePsx_DTT: {
char name[16];
snprintf(name, sizeof(name), "level%d%c.dtt", level + 1, _languagesPsx[fileLanguage()]);
Expand All @@ -982,7 +995,7 @@ void Resource::loadLevelDataPsx(int level, int resType) {
snprintf(name, sizeof(name), "level%d.lev", level + 1);
_fileLev = fileOpenPsx(name, kFileType_PSX_LEVELDATA, level + 1);
if (_fileLev) {
readDataOffsetsTable(_fileLev, kResOffsetType_LEV, _levOffsetsTable);
readDataOffsetsTable(_fileLev, kResOffsetType_LEV, _levOffsetsTable, kResPsxLevOffsetsTableSize);
if (kLoadPsxData) {
for (uint32_t i = 0; i < ARRAYSIZE(_resLoadDataTablePsx); ++i) {
const uint32_t dataSize = seekDataPsx(_resLoadDataTablePsx[i].ext, _fileLev, kResOffsetType_LEV);
Expand All @@ -1002,7 +1015,7 @@ void Resource::loadLevelDataPsx(int level, int resType) {
snprintf(name, sizeof(name), "level%d.son", level + 1);
_fileSon = fileOpenPsx(name, kFileType_PSX_LEVELDATA, level + 1);
if (_fileSon) {
readDataOffsetsTable(_fileSon, kResOffsetType_SON, _sonOffsetsTable);
readDataOffsetsTable(_fileSon, kResOffsetType_SON, _sonOffsetsTable, kResPsxSonOffsetsTableSize);
loadVAB(_fileSon);
}
}
Expand All @@ -1022,6 +1035,13 @@ void Resource::loadLevelDataPsx(int level, int resType) {

void Resource::unloadLevelDataPsx(int resType) {
switch (resType) {
case kResTypePsx_DIN:
memset(_dinOffsetsTable, 0, sizeof(_dinOffsetsTable));
if (_fileDin) {
fileClose(_fileDin);
_fileDin = 0;
}
break;
case kResTypePsx_LEV:
memset(_levOffsetsTable, 0, sizeof(_levOffsetsTable));
if (_fileLev) {
Expand All @@ -1046,9 +1066,13 @@ void Resource::unloadLevelDataPsx(int resType) {
}
}

void Resource::readDataOffsetsTable(File *fp, int offsetType, ResPsxOffset *offsetsTable) {
void Resource::readDataOffsetsTable(File *fp, int offsetType, ResPsxOffset *offsetsTable, int offsetsSize) {
const int dataSize = fileSize(fp);
const int count = fileReadUint32LE(fp);
if (count != offsetsSize) {
error("Unexpected size %d for Psx offset table type %d", count, offsetType);
return;
}
const uint32_t baseOffset = sizeof(uint32_t) + count * (sizeof(uint32_t) + 4);
for (int i = 0; i < count; ++i) {
offsetsTable[i].offset = fileReadUint32LE(fp) + baseOffset;
Expand Down
7 changes: 6 additions & 1 deletion resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ enum {
};

enum {
kResTypePsx_DIN,
kResTypePsx_DTT,
kResTypePsx_LEV,
kResTypePsx_SON,
kResTypePsx_VRM,
};

enum {
kResOffsetType_DIN,
kResOffsetType_LEV,
kResOffsetType_SON,
};

enum {
kVrmLoadingScreenWidth = 320,
kVrmLoadingScreenHeight = 240,
kResPsxDinOffsetsTableSize = 2, // .NEW, .MAP
kResPsxLevOffsetsTableSize = 11, // .STM, .ANI, .F3D, .P3D, .SPR, .PAL, .MSG, .CMD, .ENV, .KEY, .SNK
kResPsxSonOffsetsTableSize = 3, // .SPU, .VH, .VB
kVagOffsetsTableSize = 256,
Expand Down Expand Up @@ -175,6 +178,8 @@ struct Resource {
File *_fileSon;
ResPsxOffset _levOffsetsTable[kResPsxLevOffsetsTableSize];
File *_fileLev;
ResPsxOffset _dinOffsetsTable[kResPsxDinOffsetsTableSize];
File *_fileDin;
bool _psxCmdData;

Resource();
Expand Down Expand Up @@ -211,7 +216,7 @@ struct Resource {

void loadLevelDataPsx(int level, int resType);
void unloadLevelDataPsx(int resType);
void readDataOffsetsTable(File *fp, int offsetType, ResPsxOffset *offsetsTable);
void readDataOffsetsTable(File *fp, int offsetType, ResPsxOffset *offsetsTable, int offsetsSize);
uint32_t seekDataPsx(const char *ext, File *fp, int offsetType, uint32_t offset = 0);
void loadVAB(File *fp);
void loadVRM(File *fp);
Expand Down

0 comments on commit a010d8c

Please sign in to comment.