Skip to content

Commit 6942b91

Browse files
committed
- free car models
- ConvertIndexedTextureToRGBA to output RGBA or BGRA
1 parent 4b22c54 commit 6942b91

7 files changed

Lines changed: 107 additions & 75 deletions

File tree

DriverLevelTool/driver_routines/d2_types.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ struct TEXINF {
2121

2222
enum EPageStorage
2323
{
24-
TPAGE_PERMANENT = (1 << 0), // pre-loaded page.
25-
TPAGE_AREADATA = (1 << 1), // demand-loaded
26-
TPAGE_SPECPAGES = (1 << 2), // pre-loaded page However, it doesn't indicate texture compression
27-
PAGE_FLAG_UNK1 = (1 << 3),
28-
PAGE_FLAG_UNK2 = (1 << 4),
24+
TPAGE_PERMANENT = (1 << 0), // permanently loaded into VRAM
25+
TPAGE_AREADATA = (1 << 1), // spooled and uncompressed
26+
TPAGE_SPECPAGES = (1 << 2), // special car texture page
2927
};
3028

3129
typedef struct texpage_pos_t

DriverLevelTool/driver_routines/level.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,30 +578,42 @@ void FreeLevelData()
578578

579579
FreeSpoolData();
580580

581-
for (int i = 0; i < 1536; i++)
581+
for (int i = 0; i < MAX_MODELS; i++)
582582
{
583583
if (g_levelModels[i].model)
584584
free(g_levelModels[i].model);
585585
}
586586

587+
for (int i = 0; i < MAX_CAR_MODELS; i++)
588+
{
589+
if (g_carModels[i].cleanmodel)
590+
free(g_carModels[i].cleanmodel);
591+
592+
if (g_carModels[i].dammodel)
593+
free(g_carModels[i].dammodel);
594+
595+
if (g_carModels[i].lowmodel)
596+
free(g_carModels[i].lowmodel);
597+
}
598+
587599
delete[] g_textureNamesData;
588600

589601
// delete texture data
590602
for (int i = 0; i < g_numTexPages; i++)
591603
{
592-
if (g_pageDatas[i].data)
593-
delete[] g_pageDatas[i].data;
604+
if (g_texPageData[i].data)
605+
delete[] g_texPageData[i].data;
594606

595607
delete[] g_texPages[i].details;
596608
}
597609

598-
delete[] g_pageDatas;
610+
delete[] g_texPageData;
599611
delete[] g_texPagePos;
600612
delete[] g_texPages;
601613
delete[] g_extraPalettes;
602614
delete[] g_overlayMapData;
603615

604-
g_pageDatas = NULL;
616+
g_texPageData = NULL;
605617
g_texPagePos = NULL;
606618
g_texPagePos = NULL;
607619
g_extraPalettes = NULL;

DriverLevelTool/driver_routines/models.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct ModelRef_t
4242
int size;
4343
bool swap;
4444

45-
void* userData;
45+
void* userData; // might contain a hardware model pointer
4646
};
4747

4848
//------------------------------------------------------------------------------------------------------------

DriverLevelTool/driver_routines/regions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void LoadRegionData(IVirtualStream* pFile, RegionModels_t* models, AreaDataStr*
9191
int pageIdx = pages->pageIndexes[i];
9292

9393
// region textures are non-compressed due to loading speeds
94-
LoadTPageAndCluts(pFile, &g_pageDatas[pageIdx], pageIdx, false);
94+
LoadTPageAndCluts(pFile, &g_texPageData[pageIdx], pageIdx, false);
9595

9696
if(pFile->Tell() % 2048)
9797
pFile->Seek(2048 - (pFile->Tell() % 2048),VS_SEEK_CUR);

DriverLevelTool/driver_routines/textures.cpp

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ bool g_originalTransparencyKey = true;
2121

2222
XYPAIR g_permsList[16];
2323
XYPAIR g_specList[16];
24-
texdata_t* g_pageDatas = NULL;
24+
texdata_t* g_texPageData = NULL;
2525
TexPage_t* g_texPages = NULL;
2626
extclutdata_t* g_extraPalettes = NULL;
2727
int g_numExtraPalettes = 0;
2828

29-
// legacy format converter
29+
// 16 bit color to BGRA
3030
TVec4D<ubyte> bgr5a1_ToRGBA8(ushort color)
3131
{
3232
ubyte b = (color & 0x1F) * 8;
@@ -44,6 +44,77 @@ TVec4D<ubyte> bgr5a1_ToRGBA8(ushort color)
4444
return TVec4D<ubyte>(r,g,b,a);
4545
}
4646

47+
// 16 bit color to RGBA
48+
TVec4D<ubyte> rgb5a1_ToRGBA8(ushort color)
49+
{
50+
ubyte r = (color & 0x1F) * 8;
51+
ubyte g = ((color >> 5) & 0x1F) * 8;
52+
ubyte b = ((color >> 10) & 0x1F) * 8;
53+
54+
ubyte a = (color >> 15);
55+
56+
// restore source transparency key
57+
if (g_originalTransparencyKey && color == 0)
58+
{
59+
return TVec4D<ubyte>(255, 0, 255, 0);
60+
}
61+
62+
return TVec4D<ubyte>(r, g, b, a);
63+
}
64+
65+
//-------------------------------------------------------------
66+
// Conversion of indexed palettized texture to 32bit RGBA
67+
//-------------------------------------------------------------
68+
void ConvertIndexedTextureToRGBA(int nPage, uint* dest_color_data, int detail, TEXCLUT* clut, bool outputBGR)
69+
{
70+
texdata_t* page = &g_texPageData[nPage];
71+
72+
if (!(detail < g_texPages[nPage].numDetails))
73+
{
74+
MsgError("Cannot apply palette to non-existent detail! Programmer error?\n");
75+
return;
76+
}
77+
78+
int ox = g_texPages[nPage].details[detail].x;
79+
int oy = g_texPages[nPage].details[detail].y;
80+
int w = g_texPages[nPage].details[detail].width;
81+
int h = g_texPages[nPage].details[detail].height;
82+
83+
if (w == 0)
84+
w = 256;
85+
86+
if (h == 0)
87+
h = 256;
88+
89+
char* textureName = g_textureNamesData + g_texPages[nPage].details[detail].nameoffset;
90+
//MsgWarning("Applying detail %d '%s' (xywh: %d %d %d %d)\n", detail, textureName, ox, oy, w, h);
91+
92+
int tp_wx = ox + w;
93+
int tp_hy = oy + h;
94+
95+
for (int y = oy; y < tp_hy; y++)
96+
{
97+
for (int x = ox; x < tp_wx; x++)
98+
{
99+
ubyte clindex = page->data[y * 128 + x / 2];
100+
101+
if (0 != (x & 1))
102+
clindex >>= 4;
103+
104+
clindex &= 0xF;
105+
106+
TVec4D<ubyte> color = outputBGR ?
107+
bgr5a1_ToRGBA8(clut->colors[clindex]) :
108+
rgb5a1_ToRGBA8(clut->colors[clindex]);
109+
110+
// flip texture by Y because of TGA
111+
int ypos = (TEXPAGE_SIZE_Y - y - 1) * TEXPAGE_SIZE_Y;
112+
113+
dest_color_data[ypos + x] = *(uint*)(&color);
114+
}
115+
}
116+
}
117+
47118
//-------------------------------------------------------------------------------
48119

49120
// unpacks texture, returns new source pointer
@@ -168,8 +239,8 @@ void LoadPermanentTPages(IVirtualStream* pFile)
168239
pFile->Seek( g_levInfo.texdata_offset, VS_SEEK_SET );
169240

170241
// allocate page data
171-
g_pageDatas = new texdata_t[g_numTexPages];
172-
memset(g_pageDatas, 0, sizeof(texdata_t)*g_numTexPages);
242+
g_texPageData = new texdata_t[g_numTexPages];
243+
memset(g_texPageData, 0, sizeof(texdata_t)*g_numTexPages);
173244

174245
//-----------------------------------
175246

@@ -190,7 +261,7 @@ void LoadPermanentTPages(IVirtualStream* pFile)
190261
int tpage = g_permsList[i].x;
191262

192263
// permanents are also compressed
193-
LoadTPageAndCluts(pFile, &g_pageDatas[tpage], tpage, true);
264+
LoadTPageAndCluts(pFile, &g_texPageData[tpage], tpage, true);
194265

195266
pFile->Seek(curOfs + ((g_permsList[i].y + 2047) & -2048), VS_SEEK_SET);
196267
}
@@ -208,9 +279,9 @@ void LoadPermanentTPages(IVirtualStream* pFile)
208279
{
209280
long curOfs = pFile->Tell();
210281
int tpage = g_specList[i].x;
211-
282+
212283
// permanents are compressed
213-
LoadTPageAndCluts(pFile, &g_pageDatas[tpage], tpage, true);
284+
LoadTPageAndCluts(pFile, &g_texPageData[tpage], tpage, true);
214285

215286
pFile->Seek(curOfs + ((g_specList[i].y + 2047) & -2048), VS_SEEK_SET);
216287
}

DriverLevelTool/driver_routines/textures.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct extclutdata_t
4040

4141
//---------------------------------------------------------------------------------------------------------------------------------
4242

43-
extern texdata_t* g_pageDatas;
43+
extern texdata_t* g_texPageData;
4444
extern TexPage_t* g_texPages;
4545
extern extclutdata_t* g_extraPalettes;
4646
extern int g_numExtraPalettes;
@@ -58,10 +58,12 @@ extern char* g_overlayMapData;
5858

5959
TVec4D<ubyte> bgr5a1_ToRGBA8(ushort color);
6060

61-
int GetCarPalIndex(int tpage);
61+
void ConvertIndexedTextureToRGBA(int nPage, uint* dest_color_data, int detail, TEXCLUT* clut, bool outputBGR = true);
6262

6363
//---------------------------------------------------------------------------------------------------------------------------------
6464

65+
int GetCarPalIndex(int tpage);
66+
6567
// unpacks compressed texture
6668
char* unpackTexture(char* src, char* dest);
6769

DriverLevelTool/exporter/export_textures.cpp

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -85,65 +85,14 @@ void SaveTGA(const char* filename, ubyte* data, int w, int h, int c)
8585
fclose(pFile);
8686
}
8787

88-
//-------------------------------------------------------------
89-
// Conversion of indexed palettized texture to 32bit RGBA
90-
//-------------------------------------------------------------
91-
void ConvertIndexedTextureToRGBA(int nPage, uint* dest_color_data, int detail, TEXCLUT* clut)
92-
{
93-
texdata_t* page = &g_pageDatas[nPage];
94-
95-
if (!(detail < g_texPages[nPage].numDetails))
96-
{
97-
MsgError("Cannot apply palette to non-existent detail! Programmer error?\n");
98-
return;
99-
}
100-
101-
int ox = g_texPages[nPage].details[detail].x;
102-
int oy = g_texPages[nPage].details[detail].y;
103-
int w = g_texPages[nPage].details[detail].width;
104-
int h = g_texPages[nPage].details[detail].height;
105-
106-
if (w == 0)
107-
w = 256;
108-
109-
if (h == 0)
110-
h = 256;
111-
112-
char* textureName = g_textureNamesData + g_texPages[nPage].details[detail].nameoffset;
113-
//MsgWarning("Applying detail %d '%s' (xywh: %d %d %d %d)\n", detail, textureName, ox, oy, w, h);
114-
115-
int tp_wx = ox + w;
116-
int tp_hy = oy + h;
117-
118-
for (int y = oy; y < tp_hy; y++)
119-
{
120-
for (int x = ox; x < tp_wx; x++)
121-
{
122-
ubyte clindex = page->data[y * 128 + x / 2];
123-
124-
if (0 != (x & 1))
125-
clindex >>= 4;
126-
127-
clindex &= 0xF;
128-
129-
TVec4D<ubyte> color = bgr5a1_ToRGBA8(clut->colors[clindex]);
130-
131-
// flip texture by Y because of TGA
132-
int ypos = (TEXPAGE_SIZE_Y - y - 1) * TEXPAGE_SIZE_Y;
133-
134-
dest_color_data[ypos + x] = *(uint*)(&color);
135-
}
136-
}
137-
}
138-
13988
int clutSortFunc(extclutdata_t* const& i, extclutdata_t* const& j)
14089
{
14190
return (i->palette - j->palette);
14291
}
14392

14493
void GetTPageDetailPalettes(DkList<TEXCLUT*>& out, int nPage, int detail)
14594
{
146-
texdata_t* page = &g_pageDatas[nPage];
95+
texdata_t* page = &g_texPageData[nPage];
14796

14897
// ofc, add default
14998
out.append(&page->clut[detail]);
@@ -181,7 +130,7 @@ void GetTPageDetailPalettes(DkList<TEXCLUT*>& out, int nPage, int detail)
181130
//-------------------------------------------------------------
182131
void ExportTIM(int nPage, int detail)
183132
{
184-
texdata_t* page = &g_pageDatas[nPage];
133+
texdata_t* page = &g_texPageData[nPage];
185134

186135
if (!(detail < g_texPages[nPage].numDetails))
187136
{
@@ -308,7 +257,7 @@ void ExportTexturePage(int nPage)
308257
// Also saving to LEVEL_texture/PAGE_*
309258
//
310259

311-
texdata_t* page = &g_pageDatas[nPage];
260+
texdata_t* page = &g_texPageData[nPage];
312261

313262
if (!page->data)
314263
return; // NO DATA

0 commit comments

Comments
 (0)