Skip to content

Commit f86ce07

Browse files
committed
more correct tex header handling
1 parent fff5cb7 commit f86ce07

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

main.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,23 @@ void dds2tex(const char* dds_path)
4141
DDS_HEADER dds_header;
4242
assert(fread(&dds_header, sizeof(DDS_HEADER), 1, dds_file) == 1);
4343

44-
const uint8_t* tex_format;
44+
TEX_HEADER tex_header = {
45+
.magic = tex_magic,
46+
.image_width = dds_header.dwWidth,
47+
.image_height = dds_header.dwHeight,
48+
.unk1 = 1 // this is always set to 1 in original tex files; no idea what it stands for
49+
};
4550
if (memcmp(dds_header.ddspf.dwFourCC, "DXT1", 4) == 0) {
46-
tex_format = tex_dxt1_format;
51+
tex_header.tex_format = tex_format_dxt1;
4752
} else if (memcmp(dds_header.ddspf.dwFourCC, "DXT5", 4) == 0) {
48-
tex_format = tex_dxt5_format;
53+
tex_header.tex_format = tex_format_dxt5;
4954
} else {
5055
fprintf(stderr, "Error: dds file needs to be in either DXT1 or DXT5 format!\n");
5156
fclose(dds_file);
5257
return;
5358
}
5459
if (dds_header.dwMipMapCount > 1) { // this value may be set to 1, which is equivalent to leaving it at 0 (no mipmaps)
60+
tex_header.has_mipmaps = true;
5561
if (dds_header.dwMipMapCount != 32u - __builtin_clz(max(dds_header.dwWidth, dds_header.dwHeight))) {
5662
fprintf(stderr, "Error: DDS mipmap count mismatch; expected %u mipmaps, got %u\n", 32 - __builtin_clz(max(dds_header.dwWidth, dds_header.dwHeight)), dds_header.dwMipMapCount);
5763
fclose(dds_file);
@@ -70,13 +76,6 @@ void dds2tex(const char* dds_path)
7076
fclose(dds_file);
7177
return;
7278
}
73-
TEX_HEADER tex_header = {
74-
.magic = tex_magic,
75-
.image_width = dds_header.dwWidth,
76-
.image_height = dds_header.dwHeight,
77-
.has_mipmaps = dds_header.dwMipMapCount > 1
78-
};
79-
memcpy(tex_header.tex_format, tex_format, 2);
8079
fwrite(&tex_header, sizeof(TEX_HEADER), 1, tex_file);
8180

8281
printf("Info: Converting %ux%u DDS file \"%s\" to TEX file \"%s\".\n", tex_header.image_width, tex_header.image_height, dds_path, tex_path);
@@ -131,9 +130,9 @@ void tex2dds(const char* tex_path)
131130
assert(fread((uint8_t*) &tex_header + 4, sizeof(TEX_HEADER) - 4, 1, tex_file) == 1);
132131

133132
const char* dds_format;
134-
if (memcmp(tex_header.tex_format, tex_dxt1_format, 2) == 0) {
133+
if (tex_header.tex_format == tex_format_dxt1) {
135134
dds_format = "DXT1";
136-
} else if (memcmp(tex_header.tex_format, tex_dxt5_format, 2) == 0) {
135+
} else if (tex_header.tex_format == tex_format_dxt5) {
137136
dds_format = "DXT5";
138137
} else {
139138
fprintf(stderr, "Error: tex file needs to be in either DXT1 or DXT5 format!\n");

tex.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
#include <stdint.h>
22
#include <stdbool.h>
33

4-
const uint8_t tex_dxt1_format[2] = {1, 10};
5-
const uint8_t tex_dxt5_format[2] = {1, 12};
6-
4+
enum tex_format {
5+
tex_format_d32 = 0x1,
6+
tex_format_d24s8 = 0x2,
7+
tex_foramt_d24x8 = 0x3,
8+
tex_format_dxt1 = 0xA,
9+
tex_format_dxt5 = 0xC
10+
};
711
#define tex_magic "TEX"
812

913
typedef struct {
1014
uint8_t magic[4];
1115
uint16_t image_width;
1216
uint16_t image_height;
13-
uint8_t tex_format[2];
1417
uint8_t unk1;
18+
uint8_t tex_format;
19+
uint8_t unk2;
1520
bool has_mipmaps;
1621
} TEX_HEADER;

0 commit comments

Comments
 (0)