-
Notifications
You must be signed in to change notification settings - Fork 2
/
mat.h
110 lines (90 loc) · 2.97 KB
/
mat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#include "mgs/common/util.h"
#include "mgs/archive/dar/dar.h"
#include "image/pcx/dr_pcx.h"
inline
int findMaterialIdx(char* matName, CArrayList<noesisMaterial_t*>& matList) {
for (int i = 0; i < matList.Num(); i++) {
if (!strcmp(matList[i]->name, matName))
return i;
}
return -1;
}
inline
int findTextureIdx(char* texName, CArrayList<noesisTex_t*>& texList) {
for (int i = 0; i < texList.Num(); i++) {
if (!strcmp(texList[i]->filename, texName))
return i;
}
return -1;
}
inline
uint8_t* findPcx(noeRAPI_t* rapi, uint16_t& strcode, int& size) {
std::filesystem::path p{ rapi->Noesis_GetInputName() };
p = p.parent_path();
for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(p)) {
if (file.path().extension() == ".dar") {
Dar dar = Dar(file.path().u8string());
if (uint8_t* pcx = dar.findFile(strcode, 0x70, size))
return pcx;
}
}
return NULL;
}
inline
noesisTex_t* loadTexture(noeRAPI_t* rapi, uint16_t& strcode) {
int size;
uint8_t* texData = findPcx(rapi, strcode, size);
if (!texData) return NULL;
int width;
int height;
int components;
uint8_t* imageData = drpcx_load_memory(texData, size, false, &width, &height, &components, 4);
int datasize = width * height * 4;
uint8_t* tga = makeTGA(imageData, datasize, width, height);
drpcx_free(imageData);
noesisTex_t* noeTexture = rapi->Noesis_LoadTexByHandler(tga, datasize + 0x12, ".tga");
delete[] tga;
delete[] texData;
return noeTexture;
}
inline
void bindMat(uint16_t strcode, BYTE* fileBuffer, noeRAPI_t* rapi, CArrayList<noesisMaterial_t*>& matList, CArrayList<noesisTex_t*>& texList) {
//set mat name
std::string matStr = intToHexString(strcode);
char matName[7];
strcpy_s(matName, matStr.c_str());
//check if material already exists
int x = findMaterialIdx(matName, matList);
//use existing mat if it does
if (x > -1) {
rapi->rpgSetMaterial(matName);
return;
}
//create material
noesisMaterial_t* noeMat = rapi->Noesis_GetMaterialList(1, false);
noeMat->name = rapi->Noesis_PooledString(matName);
//set tex name
std::string texStr = intToHexString(strcode) + ".tga";
char texName[11];
strcpy_s(texName, texStr.c_str());
//check if texture already exists
int y = findTextureIdx(texName, texList);
//set tex to mat if it does
if (y > -1) {
noeMat->texIdx = y;
matList.Append(noeMat);
rapi->rpgSetMaterial(matName);
return;
}
//load texture
noesisTex_t* noeTexture = loadTexture(rapi, strcode);
if (!noeTexture) return;
noeTexture->filename = rapi->Noesis_PooledString(texName);
//set tex to mat
noeMat->texIdx = texList.Num();
matList.Append(noeMat);
texList.Append(noeTexture);
//set material
rapi->rpgSetMaterial(noeMat->name);
}