Skip to content

Commit f2a0277

Browse files
committedOct 7, 2024··
Fix texture loading if casing mismatches filename
Always use lower-case unqualified names to find and cache the actual textures.
1 parent f6ff794 commit f2a0277

File tree

8 files changed

+77
-34
lines changed

8 files changed

+77
-34
lines changed
 

‎src/libprojectM/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ add_library(projectM_main OBJECT
2323
ProjectMCWrapper.hpp
2424
TimeKeeper.cpp
2525
TimeKeeper.hpp
26+
Utils.cpp
27+
Utils.hpp
2628
projectM-opengl.h
2729
)
2830

‎src/libprojectM/MilkdropPreset/MilkdropShader.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "PerFrameContext.hpp"
44
#include "PresetState.hpp"
5+
#include "Utils.hpp"
56

67
#include <MilkdropStaticShaders.hpp>
78

@@ -69,8 +70,7 @@ void MilkdropShader::LoadTexturesAndCompile(PresetState& presetState)
6970
baseName = name.substr(3);
7071
}
7172

72-
std::string lowerCaseName(baseName);
73-
std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), tolower);
73+
std::string lowerCaseName = Utils::ToLower(baseName);
7474

7575
// The "main" and "blurX" textures are preset-specific and are not managed by TextureManager.
7676
if (lowerCaseName == "main")

‎src/libprojectM/PresetFactoryManager.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <MilkdropPreset/Factory.hpp>
44

5+
#include <Utils.hpp>
6+
57
#include <algorithm>
68
#include <cassert>
79
#include <iostream>
@@ -135,9 +137,8 @@ auto PresetFactoryManager::ParseExtension(const std::string& filename) -> std::s
135137
if (start == std::string::npos || start >= (filename.length() - 1)) {
136138
return "";
137139
}
138-
std::string ext = filename.substr(start + 1, filename.length());
139-
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
140-
return ext;
140+
141+
return Utils::ToLower(filename.substr(start + 1, filename.length()));
141142
}
142143

143144
} // namespace libprojectM

‎src/libprojectM/Renderer/FileScanner.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "FileScanner.hpp"
22

3+
#include "Utils.hpp"
4+
35
#include <algorithm>
46
#include <cctype>
57

@@ -17,7 +19,7 @@ FileScanner::FileScanner(const std::vector<std::string>& rootDirs, std::vector<s
1719
// Convert all extensions to lower-case.
1820
for (auto& extension : _extensions)
1921
{
20-
std::transform(extension.begin(), extension.end(), extension.begin(), [](unsigned char c) { return std::tolower(c); });
22+
Utils::ToLowerInPlace(extension);
2123
}
2224
}
2325

@@ -59,8 +61,7 @@ void FileScanner::Scan(ScanCallback callback)
5961
}
6062

6163
// Match the lower-case extension of the file with the provided list of valid extensions.
62-
auto extension = entry.path().extension().string();
63-
std::transform(extension.begin(), extension.end(), extension.begin(), [](unsigned char c) { return std::tolower(c); });
64+
auto extension = Utils::ToLower(entry.path().extension().string());
6465
if (std::find(_extensions.begin(), _extensions.end(), extension) != _extensions.end())
6566
{
6667
callback(entry.path().string(), entry.path().stem().string());

‎src/libprojectM/Renderer/TextureManager.cpp

+16-25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "IdleTextures.hpp"
55
#include "MilkdropNoise.hpp"
66
#include "Texture.hpp"
7+
#include "Utils.hpp"
78

89
#include <SOIL2/SOIL2.h>
910

@@ -165,7 +166,6 @@ void TextureManager::PurgeTextures()
165166

166167
auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSamplerDescriptor
167168
{
168-
TextureSamplerDescriptor texDesc;
169169
GLint wrapMode{0};
170170
GLint filterMode{0};
171171
std::string unqualifiedName;
@@ -174,24 +174,22 @@ auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSample
174174

175175
ScanTextures();
176176

177-
std::string lowerCaseFileName(name);
178-
std::transform(lowerCaseFileName.begin(), lowerCaseFileName.end(), lowerCaseFileName.begin(), tolower);
179-
177+
std::string lowerCaseUnqualifiedName = Utils::ToLower(unqualifiedName);
180178
for (const auto& file : m_scannedTextureFiles)
181179
{
182-
if (file.lowerCaseBaseName != unqualifiedName)
180+
if (file.lowerCaseBaseName != lowerCaseUnqualifiedName)
183181
{
184182
continue;
185183
}
186184

187-
texDesc = LoadTexture(file.filePath, name);
185+
auto texture = LoadTexture(file);
188186

189-
if (!texDesc.Empty())
187+
if (texture)
190188
{
191189
#ifdef DEBUG
192190
std::cerr << "Loaded texture " << unqualifiedName << std::endl;
193191
#endif
194-
return texDesc;
192+
return {texture, m_samplers.at({wrapMode, filterMode}), name, unqualifiedName};;
195193
}
196194
}
197195

@@ -203,24 +201,20 @@ auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSample
203201
return {m_placeholderTexture, m_samplers.at({wrapMode, filterMode}), name, unqualifiedName};
204202
}
205203

206-
auto TextureManager::LoadTexture(const std::string& fileName, const std::string& name) -> TextureSamplerDescriptor
204+
auto TextureManager::LoadTexture(const ScannedFile& file) -> std::shared_ptr<Texture>
207205
{
208-
GLint wrapMode;
209-
GLint filterMode;
210206
std::string unqualifiedName;
211207

212-
ExtractTextureSettings(name, wrapMode, filterMode, unqualifiedName);
213-
auto sampler = m_samplers.at({wrapMode, filterMode});
214-
if (m_textures.find(name) != m_textures.end())
208+
if (m_textures.find(file.lowerCaseBaseName) != m_textures.end())
215209
{
216-
return {m_textures.at(name), sampler, name, unqualifiedName};
210+
return m_textures.at(file.lowerCaseBaseName);
217211
}
218212

219213
int width{};
220214
int height{};
221215

222216
unsigned int const tex = SOIL_load_OGL_texture(
223-
fileName.c_str(),
217+
file.filePath.c_str(),
224218
SOIL_LOAD_RGBA,
225219
SOIL_CREATE_NEW_ID,
226220
SOIL_FLAG_MULTIPLY_ALPHA, &width, &height);
@@ -232,10 +226,10 @@ auto TextureManager::LoadTexture(const std::string& fileName, const std::string&
232226

233227
uint32_t memoryBytes = width * height * 4; // RGBA, unsigned byte color channels.
234228
auto newTexture = std::make_shared<Texture>(unqualifiedName, tex, GL_TEXTURE_2D, width, height, true);
235-
m_textures[name] = newTexture;
236-
m_textureStats.insert({name, {memoryBytes}});
229+
m_textures[file.lowerCaseBaseName] = newTexture;
230+
m_textureStats.insert({file.lowerCaseBaseName, {memoryBytes}});
237231

238-
return {newTexture, sampler, name, unqualifiedName};
232+
return newTexture;
239233
}
240234

241235
auto TextureManager::GetRandomTexture(const std::string& randomName) -> TextureSamplerDescriptor
@@ -247,8 +241,7 @@ auto TextureManager::GetRandomTexture(const std::string& randomName) -> TextureS
247241

248242
ScanTextures();
249243

250-
std::string lowerCaseName(randomName);
251-
std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), tolower);
244+
std::string lowerCaseName = Utils::ToLower(randomName);
252245

253246
if (m_scannedTextureFiles.empty())
254247
{
@@ -300,8 +293,7 @@ auto TextureManager::GetRandomTexture(const std::string& randomName) -> TextureS
300293

301294
void TextureManager::AddTextureFile(const std::string& fileName, const std::string& baseName)
302295
{
303-
std::string lowerCaseBaseName(baseName);
304-
std::transform(lowerCaseBaseName.begin(), lowerCaseBaseName.end(), lowerCaseBaseName.begin(), tolower);
296+
std::string lowerCaseBaseName = Utils::ToLower(baseName);
305297

306298
ScannedFile file;
307299
file.filePath = fileName;
@@ -320,8 +312,7 @@ void TextureManager::ExtractTextureSettings(const std::string& qualifiedName, GL
320312
return;
321313
}
322314

323-
std::string lowerQualifiedName(qualifiedName);
324-
std::transform(lowerQualifiedName.begin(), lowerQualifiedName.end(), lowerQualifiedName.begin(), tolower);
315+
std::string lowerQualifiedName = Utils::ToLower(qualifiedName);
325316

326317
// Default mode for user textures is "fw" (bilinear filtering + wrap).
327318
wrapMode = GL_REPEAT;

‎src/libprojectM/Renderer/TextureManager.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class TextureManager
8282

8383
void Preload();
8484

85-
TextureSamplerDescriptor LoadTexture(const std::string& fileName, const std::string& name);
85+
auto LoadTexture(const ScannedFile& file) -> std::shared_ptr<Texture>;
8686

8787
void AddTextureFile(const std::string& fileName, const std::string& baseName);
8888

‎src/libprojectM/Utils.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "Utils.hpp"
2+
3+
#include <algorithm>
4+
5+
namespace libprojectM {
6+
namespace Utils {
7+
8+
auto ToLower(const std::string& str) -> std::string
9+
{
10+
std::string lowerStr(str);
11+
ToLowerInPlace(lowerStr);
12+
return lowerStr;
13+
}
14+
15+
auto ToUpper(const std::string& str) -> std::string
16+
{
17+
std::string upperStr(str);
18+
ToUpperInPlace(upperStr);
19+
return upperStr;
20+
}
21+
22+
void ToLowerInPlace(std::string& str)
23+
{
24+
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
25+
}
26+
27+
void ToUpperInPlace(std::string& str)
28+
{
29+
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
30+
}
31+
32+
} // namespace Utils
33+
} // namespace libprojectM

‎src/libprojectM/Utils.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace libprojectM {
6+
namespace Utils {
7+
8+
auto ToLower(const std::string& str) -> std::string;
9+
auto ToUpper(const std::string& str) -> std::string;
10+
11+
void ToLowerInPlace(std::string& str);
12+
void ToUpperInPlace(std::string& str);
13+
14+
} // namespace Utils
15+
} // namespace libprojectM

0 commit comments

Comments
 (0)
Please sign in to comment.