Skip to content

Commit fdef3ea

Browse files
authored
Share texture type names from model.h (scp-fs2open#7395)
* share texture type names from model.h * signed unsigned mismatch * clang * stop yur whining
1 parent 3f85d61 commit fdef3ea

3 files changed

Lines changed: 64 additions & 57 deletions

File tree

code/model/model.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ struct submodel_instance
165165
//Used by scripting - if you change this, do a search
166166
//to update switch() statement in lua.cpp
167167

168+
inline const SCP_map<int, SCP_string> MODEL_TEXTURE_SUFFIXES = {
169+
{ TM_GLOW_TYPE, "-glow" },
170+
{ TM_SPECULAR_TYPE, "-shine" },
171+
{ TM_NORMAL_TYPE, "-normal" },
172+
{ TM_HEIGHT_TYPE, "-height" },
173+
{ TM_MISC_TYPE, "-misc" },
174+
{ TM_SPEC_GLOSS_TYPE, "-reflect" },
175+
{ TM_AMBIENT_TYPE, "-ao" }
176+
};
177+
178+
inline const SCP_string MODEL_TEXTURE_SUFFIX_TRANS = "-trans"; // -trans is a special case as other suffixes can be appended to it
179+
180+
inline const SCP_string& model_texture_longest_suffix() {
181+
return std::max_element(MODEL_TEXTURE_SUFFIXES.begin(),
182+
MODEL_TEXTURE_SUFFIXES.end(),
183+
[](const std::pair<int, SCP_string>& left, const std::pair<int, SCP_string>& right) {
184+
return left.second.size() < right.second.size();
185+
})->second;
186+
}
187+
168188
#define MAX_REPLACEMENT_TEXTURES MAX_MODEL_TEXTURES * TM_NUM_TYPES
169189

170190
// Goober5000 - since we need something < 0

code/model/modelread.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,10 +2584,10 @@ modelread_status read_model_file_no_subsys(polymodel * pm, const char* filename,
25842584
{
25852585
char tmp_name[127];
25862586
cfread_string_len(tmp_name,127,fp);
2587-
constexpr int max_buffer_size = MAX_FILENAME_LEN - 8; // leave room for the longest suffix, "-reflect"
2587+
const auto max_buffer_size = static_cast<size_t>(MAX_FILENAME_LEN) - model_texture_longest_suffix().size();
25882588
if (strlen(tmp_name) >= max_buffer_size)
25892589
{
2590-
Warning(LOCATION, "Model '%s', texture '%s' filename is too long! Truncating to %d characters.", pm->filename, tmp_name, max_buffer_size - 1);
2590+
Warning(LOCATION, "Model '%s', texture '%s' filename is too long! Truncating to %d characters.", pm->filename, tmp_name, static_cast<int>(max_buffer_size - 1));
25912591
tmp_name[max_buffer_size - 1] = '\0';
25922592
}
25932593
model_load_texture(pm, i, tmp_name);
@@ -3098,7 +3098,7 @@ void model_load_texture(polymodel *pm, int i, const char *file)
30983098
else
30993099
{
31003100
// check if we should be transparent, include "-trans" but make sure to skip anything that might be "-transport"
3101-
if ( (strstr(tmp_name, "-trans") && !strstr(tmp_name, "-transpo")) || strstr(tmp_name, "shockwave") || !strcmp(tmp_name, "nameplate") ) {
3101+
if ((strstr(tmp_name, MODEL_TEXTURE_SUFFIX_TRANS.c_str()) && !strstr(tmp_name, "-transpo")) || strstr(tmp_name, "shockwave") || !strcmp(tmp_name, "nameplate")) {
31023102
tmap->is_transparent = true;
31033103
}
31043104

@@ -3123,7 +3123,7 @@ void model_load_texture(polymodel *pm, int i, const char *file)
31233123
else
31243124
{
31253125
strcpy_s(tmp_name, file);
3126-
strcat_s(tmp_name, "-glow" );
3126+
strcat_s(tmp_name, MODEL_TEXTURE_SUFFIXES.at(TM_GLOW_TYPE).c_str());
31273127
strlwr(tmp_name);
31283128

31293129
tglow->LoadTexture(tmp_name, pm->filename);
@@ -3142,14 +3142,14 @@ void model_load_texture(polymodel *pm, int i, const char *file)
31423142
{
31433143
// look for reflectance map
31443144
strcpy_s(tmp_name, file);
3145-
strcat_s(tmp_name, "-reflect");
3145+
strcat_s(tmp_name, MODEL_TEXTURE_SUFFIXES.at(TM_SPEC_GLOSS_TYPE).c_str());
31463146
strlwr(tmp_name);
31473147

31483148
tspecgloss->LoadTexture(tmp_name, pm->filename);
31493149

31503150
// look for a legacy shine map as well
31513151
strcpy_s(tmp_name, file);
3152-
strcat_s(tmp_name, "-shine");
3152+
strcat_s(tmp_name, MODEL_TEXTURE_SUFFIXES.at(TM_SPECULAR_TYPE).c_str());
31533153
strlwr(tmp_name);
31543154

31553155
tspec->LoadTexture(tmp_name, pm->filename);
@@ -3163,7 +3163,7 @@ void model_load_texture(polymodel *pm, int i, const char *file)
31633163
tnorm->clear();
31643164
} else {
31653165
strcpy_s(tmp_name, file);
3166-
strcat_s(tmp_name, "-normal");
3166+
strcat_s(tmp_name, MODEL_TEXTURE_SUFFIXES.at(TM_NORMAL_TYPE).c_str());
31673167
strlwr(tmp_name);
31683168

31693169
tnorm->LoadTexture(tmp_name, pm->filename);
@@ -3175,7 +3175,7 @@ void model_load_texture(polymodel *pm, int i, const char *file)
31753175
theight->clear();
31763176
} else {
31773177
strcpy_s(tmp_name, file);
3178-
strcat_s(tmp_name, "-height");
3178+
strcat_s(tmp_name, MODEL_TEXTURE_SUFFIXES.at(TM_HEIGHT_TYPE).c_str());
31793179
strlwr(tmp_name);
31803180

31813181
theight->LoadTexture(tmp_name, pm->filename);
@@ -3185,7 +3185,7 @@ void model_load_texture(polymodel *pm, int i, const char *file)
31853185
texture_info *tambient = &tmap->textures[TM_AMBIENT_TYPE];
31863186

31873187
strcpy_s(tmp_name, file);
3188-
strcat_s(tmp_name, "-ao");
3188+
strcat_s(tmp_name, MODEL_TEXTURE_SUFFIXES.at(TM_AMBIENT_TYPE).c_str());
31893189
strlwr(tmp_name);
31903190

31913191
tambient->LoadTexture(tmp_name, pm->filename);
@@ -3194,7 +3194,7 @@ void model_load_texture(polymodel *pm, int i, const char *file)
31943194
texture_info *tmisc = &tmap->textures[TM_MISC_TYPE];
31953195

31963196
strcpy_s(tmp_name, file);
3197-
strcat_s(tmp_name, "-misc");
3197+
strcat_s(tmp_name, MODEL_TEXTURE_SUFFIXES.at(TM_MISC_TYPE).c_str());
31983198
strlwr(tmp_name);
31993199

32003200
tmisc->LoadTexture(tmp_name, pm->filename);

qtfred/src/mission/dialogs/ShipEditor/ShipTextureReplacementDialogModel.cpp

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
#include "ShipTextureReplacementDialogModel.h"
22

33
#include "mission/object.h"
4-
5-
// Sub-texture type suffixes, mirroring the strcat_s calls in modelread.cpp.
6-
// Used both when detecting sub-texture slots in initSubTypes and when parsing
7-
// new_texture strings on dialog reload.
8-
static const SCP_string SUBTEXTURE_SUFFIXES[] = { "misc", "shine", "glow", "normal", "height", "ao", "reflect" };
4+
#include "model/model.h"
5+
6+
namespace {
7+
const SCP_vector<SCP_string>& get_replaceable_texture_types()
8+
{
9+
static const SCP_vector<SCP_string> types = []() {
10+
SCP_vector<SCP_string> out;
11+
out.reserve(MODEL_TEXTURE_SUFFIXES.size());
12+
for (const auto& suffix : MODEL_TEXTURE_SUFFIXES) {
13+
out.emplace_back(suffix.second.substr(1)); // strip leading '-'
14+
}
15+
return out;
16+
}();
17+
return types;
18+
}
19+
20+
bool is_known_subtexture_type(const SCP_string& type)
21+
{
22+
return std::any_of(get_replaceable_texture_types().begin(),
23+
get_replaceable_texture_types().end(),
24+
[&type](const SCP_string& knownType) { return lcase_equal(type, knownType); });
25+
}
26+
}
927

1028
namespace fso {
1129
namespace fred {
@@ -122,7 +140,7 @@ namespace fso {
122140
// Only treat the suffix as a type if it's a known sub-texture type.
123141
// Texture names themselves can contain hyphens (e.g. "fighter01-01a"),
124142
// so we must not blindly strip the last segment.
125-
for (const auto& kt : SUBTEXTURE_SUFFIXES) {
143+
for (const auto& kt : get_replaceable_texture_types()) {
126144
if (lcase_equal(possibleType, kt)) {
127145
type = possibleType;
128146
newText = newText.substr(0, npos);
@@ -188,37 +206,12 @@ namespace fso {
188206
}
189207
void ShipTextureReplacementDialogModel::initSubTypes(polymodel* model, int MapNum)
190208
{
191-
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>("misc", false));
192-
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>("shine", false));
193-
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>("glow", false));
194-
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>("normal", false));
195-
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>("height", false));
196-
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>("ao", false));
197-
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>("reflect", false));
198-
199-
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>("misc", ""));
200-
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>("shine", ""));
201-
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>("glow", ""));
202-
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>("normal", ""));
203-
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>("height", ""));
204-
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>("ao", ""));
205-
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>("reflect", ""));
206-
207-
replaceMap[MapNum].insert(std::pair<SCP_string, bool>("misc", false));
208-
replaceMap[MapNum].insert(std::pair<SCP_string, bool>("shine", false));
209-
replaceMap[MapNum].insert(std::pair<SCP_string, bool>("glow", false));
210-
replaceMap[MapNum].insert(std::pair<SCP_string, bool>("normal", false));
211-
replaceMap[MapNum].insert(std::pair<SCP_string, bool>("height", false));
212-
replaceMap[MapNum].insert(std::pair<SCP_string, bool>("ao", false));
213-
replaceMap[MapNum].insert(std::pair<SCP_string, bool>("reflect", false));
214-
215-
inheritMap[MapNum].insert(std::pair<SCP_string, bool>("misc", true));
216-
inheritMap[MapNum].insert(std::pair<SCP_string, bool>("shine", true));
217-
inheritMap[MapNum].insert(std::pair<SCP_string, bool>("glow", true));
218-
inheritMap[MapNum].insert(std::pair<SCP_string, bool>("normal", true));
219-
inheritMap[MapNum].insert(std::pair<SCP_string, bool>("height", true));
220-
inheritMap[MapNum].insert(std::pair<SCP_string, bool>("ao", true));
221-
inheritMap[MapNum].insert(std::pair<SCP_string, bool>("reflect", true));
209+
for (const auto& type : get_replaceable_texture_types()) {
210+
subTypesAvailable[MapNum].insert(std::pair<SCP_string, bool>(type, false));
211+
currentTextures[MapNum].insert(std::pair<SCP_string, SCP_string>(type, ""));
212+
replaceMap[MapNum].insert(std::pair<SCP_string, bool>(type, false));
213+
inheritMap[MapNum].insert(std::pair<SCP_string, bool>(type, true));
214+
}
222215
char subMap[MAX_FILENAME_LEN];
223216
//init saftly, probly not necessary
224217
for (int j = 1; j < TM_NUM_TYPES; j++) {
@@ -240,18 +233,12 @@ namespace fso {
240233
continue;
241234
}
242235
if (!type.empty()) {
243-
if (type == "trans") {
236+
if (lcase_equal(type, MODEL_TEXTURE_SUFFIX_TRANS.substr(1))) {
244237
// transparency map, not a replaceable subtype
245238
} else {
246-
bool known = false;
247-
for (const auto& kt : SUBTEXTURE_SUFFIXES) {
248-
if (lcase_equal(type, kt)) {
249-
subTypesAvailable[MapNum][kt] = true;
250-
known = true;
251-
break;
252-
}
253-
}
254-
if (!known) {
239+
if (is_known_subtexture_type(type)) {
240+
subTypesAvailable[MapNum][type] = true;
241+
} else {
255242
error_display(1, "Invalid Map type %s. Check your model's texture names or get a programmer", type.c_str());
256243
}
257244
}

0 commit comments

Comments
 (0)