Skip to content

Commit ef97b4b

Browse files
committed
- update driver_routines
1 parent f22dad8 commit ef97b4b

File tree

8 files changed

+131
-25
lines changed

8 files changed

+131
-25
lines changed

Diff for: routines/level.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ ELevelFormat CDriverLevelLoader::DetectLevelFormat(IVirtualStream* pFile)
3333

3434
switch (lump.type)
3535
{
36+
case LUMP_PERMANENTPAGES:
37+
MsgInfo("Detected old 'Driver 1 DEMO' LEV file\n");
38+
pFile->Seek(curPos, VS_SEEK_SET);
39+
return LEV_FORMAT_DRIVER1_OLD;
3640
case LUMP_MODELS:
3741
case LUMP_MAP:
3842
case LUMP_TEXTURENAMES:
@@ -138,6 +142,10 @@ void CDriverLevelLoader::ProcessLumps(IVirtualStream* pFile)
138142
{
139143
// Lumps shared between formats
140144
// almost identical
145+
case LUMP_PERMANENTPAGES:
146+
if (m_textures)
147+
m_textures->LoadPermanentTPagesD1Demo(pFile);
148+
break;
141149
case LUMP_MODELS:
142150
DevMsg(SPEW_WARNING, "LUMP_MODELS ofs=%d size=%d\n", pFile->Tell(), lump.size);
143151
if(m_models)
@@ -310,12 +318,18 @@ bool CDriverLevelLoader::Load(IVirtualStream* pStream)
310318
if (m_textures)
311319
m_textures->SetFormat(m_format);
312320

321+
if (m_format == LEV_FORMAT_DRIVER1_OLD)
322+
{
323+
ProcessLumps(pStream);
324+
return true;
325+
}
326+
313327
LUMP curLump;
314328
pStream->Read(&curLump, sizeof(curLump), 1);
315329

316330
if (curLump.type != LUMP_LUMPDESC)
317331
{
318-
MsgError("Not a LEV file!\n");
332+
MsgError("Not a valid LEV file!\n");
319333
return false;
320334
}
321335

Diff for: routines/level.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
enum LevLumpType
1414
{
1515
// known lumps indexes
16+
LUMP_PERMANENTPAGES = 0, // Driver 1 old format of texture pages
1617
LUMP_MODELS = 1, // level models
1718
LUMP_MAP = 2, // map info
1819

@@ -55,6 +56,7 @@ enum ELevelFormat
5556

5657
LEV_FORMAT_INVALID = 0,
5758

59+
LEV_FORMAT_DRIVER1_OLD, // driver 1, old lump version
5860
LEV_FORMAT_DRIVER1, // driver 1
5961
LEV_FORMAT_DRIVER2_ALPHA16, // driver 2 alpha 1.6 format
6062
LEV_FORMAT_DRIVER2_RETAIL, // driver 2 retail format

Diff for: routines/models.cpp

+36-11
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,19 @@ void CDriverLevelModels::LoadCarModelsLump(IVirtualStream* pFile, int size)
8989
int modelCount;
9090
pFile->Read(&modelCount, sizeof(int), 1);
9191

92+
if (!modelCount)
93+
return;
94+
9295
DevMsg(SPEW_NORM, " all car models count: %d\n", modelCount);
9396

9497
// read entries
9598
carmodelentry_t model_entries[MAX_CAR_MODELS];
9699
pFile->Read(&model_entries, sizeof(carmodelentry_t), MAX_CAR_MODELS);
97100

101+
// a kind of validation for older formats since they have duplicate car model lumps
102+
if ((uint)model_entries[0].cleanOffset > 100000)
103+
return;
104+
98105
// position
99106
int r_ofs = pFile->Tell();
100107

@@ -170,6 +177,18 @@ void CDriverLevelModels::LoadModelNamesLump(IVirtualStream* pFile, int size)
170177
sz += len + 1;
171178
} while (sz < size);
172179

180+
// assign model names to existing models if they were loaded
181+
// (for older D1 LEV versions)
182+
183+
for (int i = 0; i < m_numModelsInPack; i++)
184+
{
185+
ushort mappedId;
186+
pFile->Read(&mappedId, 1, sizeof(ushort));
187+
188+
ModelRef_t& ref = m_levelModels[i];
189+
ref.name = GetModelNameByIndex(i);
190+
}
191+
173192
delete[] modelnames;
174193
}
175194

@@ -353,20 +372,21 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
353372
int ptype = polyType & 31;
354373
int extFlags = (polyType >> 5) & 7;
355374

375+
out->type = ptype;
356376
out->page = 0xFF;
357377
out->detail = 0xFF;
358378
out->flags = 0;
359-
out->color = CVECTOR{ 255 };
360-
361-
*(uint*)&out->color = 0;
379+
out->color = CVECTOR{ 255, 0, 255, 0 };
362380

363381
// TODO: D1 and D2 to have different decoding routines
364382

365383
switch (ptype)
366384
{
367385
case 1:
368386
// what a strange face type. Hardcoded?
369-
*(uint*)out->vindices = *(uint*)&polyList[3];
387+
*(uint*)out->vindices = *(uint*)&polyList[4];
388+
*(uint*)&out->color = *(uint*)&polyList[8];
389+
out->flags = FACE_IS_QUAD | FACE_RGB;
370390
break;
371391
case 0:
372392
case 8:
@@ -384,9 +404,9 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
384404
case 19:
385405
{
386406
// F4
387-
*(uint*)out->vindices = *(uint*)&polyList[1];
388-
*(uint*)out->uv = *(uint*)&polyList[4];
389-
*(uint*)&out->color = *(uint*)&polyList[8];
407+
*(uint*)out->vindices = *(uint*)&polyList[4];
408+
*(uint*)out->uv = *(uint*)&polyList[8];
409+
*(uint*)&out->color = *(uint*)&polyList[12];
390410

391411
// FIXME: read colours
392412

@@ -435,8 +455,6 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
435455
out->page = pft4->texture_set;
436456
out->detail = pft4->texture_id;
437457

438-
//SwapValues(out->uv[2], out->uv[3]);
439-
440458
out->flags = FACE_IS_QUAD | FACE_TEXTURED;
441459

442460
break;
@@ -496,11 +514,18 @@ int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
496514
}
497515
}
498516

499-
if (out->page == 255)
517+
// triangles are hacked to be quads for PSX. We don't need that
518+
if (out->flags && FACE_IS_QUAD && out->vindices[2] == out->vindices[3])
519+
{
520+
out->flags &= ~FACE_IS_QUAD;
521+
}
522+
523+
if (out->page == 255 && (out->flags & FACE_TEXTURED))
500524
{
501525
out->flags &= ~FACE_TEXTURED;
502526
out->flags |= FACE_RGB;
527+
out->color.pad = 128;
503528
}
504529

505-
return PolySizes[*polyList & 0x1f];
530+
return PolySizes[*polyList & 31];
506531
}

Diff for: routines/models.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef void (*OnCarModelFreed_t)(CarModelData_t* tp);
2727

2828
struct dpoly_t
2929
{
30+
ubyte type;
3031
ubyte flags;
3132
ubyte page;
3233
ubyte detail;

Diff for: routines/regions.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ void CBaseLevelMap::LoadSpoolInfoLump(IVirtualStream* pFile)
382382
DevMsg(SPEW_NORM, "Region spool count %d (size=%d bytes)\n", m_numRegionSpools, regionsInfoSize);
383383

384384
m_regionSpoolInfo = (Spool*)malloc(regionsInfoSize);
385-
pFile->Read(m_regionSpoolInfo, 1, regionsInfoSize);
385+
if(regionsInfoSize > 0)
386+
pFile->Read(m_regionSpoolInfo, 1, regionsInfoSize);
386387

387388
m_areaDataStates = new bool[m_numAreas];
388389
memset(m_areaDataStates, 0, m_numAreas);

Diff for: routines/regions_d2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ sdPlane* CDriver2LevelRegion::SdGetCell(const VECTOR_NOPAD& cPosition, int& sdLe
121121

122122
// initial surface
123123
if (*surface == -1)
124-
return nullptr;
124+
return &g_defaultPlane;
125125

126126
const bool simplerMethod = m_owner->m_format == LEV_FORMAT_DRIVER2_ALPHA16;
127127

Diff for: routines/textures.cpp

+72-10
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "textures.h"
1212
#include "level.h"
1313

14-
15-
1614
//-------------------------------------------------------------------------------
1715

1816
// unpacks texture, returns new source pointer
@@ -339,6 +337,69 @@ void CDriverLevelTextures::LoadPermanentTPages(IVirtualStream* pFile)
339337
}
340338
}
341339

340+
void CDriverLevelTextures::LoadPermanentTPagesD1Demo(IVirtualStream* pFile)
341+
{
342+
long lumpOffset = pFile->Tell() + 8;
343+
344+
DevMsg(SPEW_NORM, "Loading OLD FORMAT permanent texture pages\n");
345+
346+
347+
int numTpages = 0;
348+
int textureCount = 0;
349+
pFile->Read(&numTpages, 1, sizeof(numTpages));
350+
pFile->Read(&textureCount, 1, sizeof(textureCount));
351+
352+
if (numTpages == 0 || numTpages > 127)
353+
{
354+
return;
355+
}
356+
357+
TEXPAGE_POS tpage_position[128];
358+
pFile->Read(tpage_position, numTpages + 1, sizeof(XYPAIR));
359+
360+
m_texPages = new CTexturePage[numTpages];
361+
m_numPermanentPages = numTpages;
362+
m_numTexPages = numTpages;
363+
364+
int totalTexturesRead = 0;
365+
// for each tpage read TEXINF array
366+
for (int i = 0; i < numTpages; ++i)
367+
{
368+
CTexturePage& tp = m_texPages[i];
369+
int detailCount;
370+
pFile->Read(&detailCount, 1, sizeof(detailCount));
371+
372+
tp.m_id = i;
373+
tp.m_tp = tpage_position[i];
374+
tp.m_owner = this;
375+
tp.m_details = new TexDetailInfo_t[detailCount];
376+
tp.m_numDetails = detailCount;
377+
378+
for (int j = 0; j < detailCount; ++j)
379+
{
380+
TexDetailInfo_t& detailInfo = tp.m_details[j];
381+
detailInfo.pageNum = i;
382+
detailInfo.detailNum = j;
383+
detailInfo.numExtraCLUTs = 0;
384+
memset(detailInfo.extraCLUTs, 0, sizeof(detailInfo.extraCLUTs));
385+
386+
pFile->Read(&detailInfo.info, 1, sizeof(detailInfo.info));
387+
totalTexturesRead++;
388+
}
389+
}
390+
391+
Msg("Loading permanent texture pages (%d, tex: %d, read %d)\n", numTpages, textureCount, totalTexturesRead);
392+
DevMsg(SPEW_NORM, "Loading permanent texture pages (%d)\n", numTpages);
393+
394+
// load permanent pages
395+
for (int i = 0; i < numTpages; i++)
396+
{
397+
pFile->Seek(lumpOffset + tpage_position[i].offset, VS_SEEK_SET);
398+
// permanents are also compressed
399+
m_texPages[i].LoadTPageAndCluts(pFile, false);
400+
}
401+
}
402+
342403
//-------------------------------------------------------------
343404
// parses texture info lumps. Quite simple
344405
//-------------------------------------------------------------
@@ -354,7 +415,7 @@ void CDriverLevelTextures::LoadTextureInfoLump(IVirtualStream* pFile)
354415
DevMsg(SPEW_NORM, "Texture amount: %d\n", numTextures);
355416

356417
// read array of texutre page info
357-
TEXPAGE_POS* tpage_position = new TEXPAGE_POS[numPages + 1];
418+
TEXPAGE_POS tpage_position[128];
358419
pFile->Read(tpage_position, numPages+1, sizeof(TEXPAGE_POS));
359420

360421
// read page details
@@ -379,9 +440,6 @@ void CDriverLevelTextures::LoadTextureInfoLump(IVirtualStream* pFile)
379440
pFile->Read(m_specList, 16, sizeof(XYPAIR));
380441

381442
DevMsg(SPEW_NORM,"Special/Car TPages = %d\n", m_numSpecPages);
382-
383-
// not needed
384-
delete tpage_position;
385443
}
386444

387445
//-------------------------------------------------------------
@@ -418,6 +476,10 @@ void CDriverLevelTextures::LoadOverlayMapLump(IVirtualStream* pFile, int lumpSiz
418476
//-------------------------------------------------------------
419477
void CDriverLevelTextures::LoadPalletLump(IVirtualStream* pFile)
420478
{
479+
// temporary not working. Maybe it's incorrect
480+
if (m_format < LEV_FORMAT_DRIVER1)
481+
return;
482+
421483
ushort* clutTablePtr;
422484
int total_cluts;
423485

@@ -436,7 +498,7 @@ void CDriverLevelTextures::LoadPalletLump(IVirtualStream* pFile)
436498
{
437499
PALLET_INFO info;
438500

439-
if (m_format == LEV_FORMAT_DRIVER1)
501+
if (m_format <= LEV_FORMAT_DRIVER1)
440502
{
441503
PALLET_INFO_D1 infod1;
442504
pFile->Read(&infod1, 1, sizeof(info) - sizeof(int));
@@ -476,7 +538,7 @@ void CDriverLevelTextures::LoadPalletLump(IVirtualStream* pFile)
476538
added_cluts++;
477539

478540
// only in D1 we need to check count
479-
if (m_format == LEV_FORMAT_DRIVER1)
541+
if (m_format <= LEV_FORMAT_DRIVER1)
480542
{
481543
if (added_cluts >= total_cluts)
482544
break;
@@ -525,7 +587,7 @@ const char* CDriverLevelTextures::GetTextureDetailName(TEXINF* info) const
525587
}
526588

527589
// unpacks RNC2 overlay map segment into RGBA buffer (32x32)
528-
void CDriverLevelTextures::GetOverlayMapSegmentRGBA(TVec4D<ubyte>* destination, int index) const
590+
void CDriverLevelTextures::GetOverlayMapSegmentRGBA(TVec4D<ubyte>* destination, int index, bool bgra /*= false*/) const
529591
{
530592
// 8 bit texture so...
531593
char mapBuffer[16 * 32];
@@ -556,7 +618,7 @@ void CDriverLevelTextures::GetOverlayMapSegmentRGBA(TVec4D<ubyte>* destination,
556618

557619
colorIndex &= 0xf;
558620

559-
destination[y * 32 + x] = rgb5a1_ToBGRA8(clut[colorIndex]);
621+
destination[y * 32 + x] = bgra ? rgb5a1_ToBGRA8(clut[colorIndex]) : rgb5a1_ToRGBA8(clut[colorIndex]);
560622
}
561623
}
562624
}

Diff for: routines/textures.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class CDriverLevelTextures
112112
// loaders
113113
void LoadTextureInfoLump(IVirtualStream* pFile);
114114
void LoadPermanentTPages(IVirtualStream* pFile);
115+
void LoadPermanentTPagesD1Demo(IVirtualStream* pFile);
115116
void LoadTextureNamesLump(IVirtualStream* pFile, int size);
116117
void LoadOverlayMapLump(IVirtualStream* pFile, int size);
117118
void LoadPalletLump(IVirtualStream* pFile);
@@ -127,7 +128,7 @@ class CDriverLevelTextures
127128
const char* GetTextureDetailName(TEXINF* info) const;
128129

129130
// unpacks RNC2 overlay map segment into RGBA buffer (32x32)
130-
void GetOverlayMapSegmentRGBA(TVec4D<ubyte>* destination, int index) const;
131+
void GetOverlayMapSegmentRGBA(TVec4D<ubyte>* destination, int index, bool bgra = false) const;
131132
int GetOverlayMapSegmentCount() const;
132133

133134
protected:

0 commit comments

Comments
 (0)