Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search preloaded random textures in TextureManager::GetRandomTexture #783

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 19 additions & 21 deletions src/libprojectM/Renderer/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ auto TextureManager::LoadTexture(const std::string& fileName, const std::string&
return {newTexture, sampler, name, unqualifiedName};
}

auto TextureManager::GetRandomTexture(const std::string& randomName) -> TextureSamplerDescriptor
auto TextureManager::GetRandomTexture(const std::string& randomName, GLenum type) -> TextureSamplerDescriptor
{
std::string selectedFilename;

Expand All @@ -250,8 +250,11 @@ auto TextureManager::GetRandomTexture(const std::string& randomName) -> TextureS
std::string lowerCaseName(randomName);
std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), tolower);

if (m_scannedTextureFiles.empty())
if (m_textures.empty())
{
#ifdef DEBUG
std::cerr << "[TextureManager] Can't create " << randomName << " with no scanned textures" << std::endl;
#endif
return {};
}

Expand All @@ -261,33 +264,28 @@ auto TextureManager::GetRandomTexture(const std::string& randomName) -> TextureS
prefix = lowerCaseName.substr(7);
}

if (prefix.empty())
std::vector<std::string> filteredTextures;
auto prefixLength = prefix.length();
for (auto it = m_textures.begin(); it != m_textures.end(); it++)
{
// Just pick a random index.
std::uniform_int_distribution<size_t> distribution(0, m_scannedTextureFiles.size() - 1);
selectedFilename = m_scannedTextureFiles.at(distribution(rndEngine)).lowerCaseBaseName;
// Filter texture names based on the prefix and the type
if ((it->first.compare(0, prefixLength, prefix) == 0) &&
(it->second->Type() == type))
filteredTextures.emplace_back(it->first);
}
else
{

std::vector<ScannedFile> filteredFiles;
auto prefixLength = prefix.length();
std::copy_if(m_scannedTextureFiles.begin(), m_scannedTextureFiles.end(),
std::back_inserter(filteredFiles),
[&prefix, prefixLength](const ScannedFile& file) {
return file.lowerCaseBaseName.substr(0, prefixLength) == prefix;
});

if (!filteredFiles.empty())
{
std::uniform_int_distribution<size_t> distribution(0, filteredFiles.size() - 1);
selectedFilename = filteredFiles.at(distribution(rndEngine)).lowerCaseBaseName;
}
if (!filteredTextures.empty())
{
std::uniform_int_distribution<size_t> distribution(0, filteredTextures.size() - 1);
selectedFilename = filteredTextures.at(distribution(rndEngine));
}

// If a prefix was set and no file matched, filename can be empty.
if (selectedFilename.empty())
{
#ifdef DEBUG
std::cerr << "[TextureManager] Random texture " << lowerCaseName << " has no filename" << std::endl;
#endif
return {};
}

Expand Down
6 changes: 4 additions & 2 deletions src/libprojectM/Renderer/TextureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class TextureManager
* @brief Returns a random texture descriptor, optionally using a prefix (after the `randXX_` name).
* Will use the default texture loading logic by calling GetTexture() if a texture was selected.
* @param randomName The filename prefix to filter. If empty, all available textures are matches. Case-insensitive.
* @return A texture descriptor with the random texture and a default sampler, or an empty sampler if no texture could be matched.
* @param type The type of texture to load.
* @return A texture descriptor with the random texture of specified type and a default sampler,
* or an empty sampler if no texture could be matched.
*/
auto GetRandomTexture(const std::string& randomName) -> TextureSamplerDescriptor;
auto GetRandomTexture(const std::string& randomName, GLenum type = GL_TEXTURE_2D) -> TextureSamplerDescriptor;

/**
* @brief Returns a sampler for the given name.
Expand Down