Answer the question
In order to leave comments, you need to log in
What is the right way to get rid of nested loops?
Good day to all!
In a good effort to leave neat code behind, I rewrite the fragment of reading the contents of files in the subdirectories of the directory:
>Католог текстур
|
|--> Тип текстуры (Diffuse)
| |
| |-->Текстура_0.dds
| |-->Текстура_1.dds
| |-->Текстура_N.dds
|
|--> Тип текстуры (Normal)
| |
| |-->Текстура_0.dds
| |-->Текстура_1.dds
| |-->Текстура_N.dds
|
|--> Тип текстуры (Typename)
|
|-->Текстура_0.dds
|-->Текстура_1.dds
|-->Текстура_N.dds
ResourcesManager::ResourcesManager(QObject *parent) : QObject(parent)
{
QDir root(":/graphic/");
QStringList subdirs = root.entryList(QDir::NoDotAndDotDot|QDir::AllDirs);
qDebug() << subdirs ;
parceAllRes(root);
}
void ResourcesManager::parceAllRes(QDir root)
{
QDir textureSubdir(root.path()+"/textures/");
QStringList texturesTypes = textureSubdir.entryList(QDir::NoDotAndDotDot|QDir::AllDirs);
qDebug() << texturesTypes;
foreach (QString textureType, texturesTypes) {
QDir textureTypeSubdir(root.path()+"/textures/"+textureType);
QStringList texturesType = textureTypeSubdir.entryList();
foreach (QString textureFile, texturesType) {
qDebug() << "Type - " << textureType << " filename - " << textureFile;
}
}
}
foreach (QString textureType, texturesTypes) {
QDir textureTypeSubdir(root.path()+"/textures/"+textureType);
QStringList texturesType = textureTypeSubdir.entryList();
foreach (QString textureFile, texturesType) {
qDebug() << "Type - " << textureType << " filename - " << textureFile;
}
}
Answer the question
In order to leave comments, you need to log in
Traversing directories, even with reading files in these directories, is best done through recursion.
I don't see much problem in your code.
The format of the file paths is strictly defined, so no recursion is needed.
All work is divided into 2 steps:
1. Determine what types of textures are available.
2. Download all textures for each type.
I would break this logic into 2 separate functions. The first one returns a list of available types, where the type is given by enum, and the function inside matches the text name of the folder with enum.
The second function takes a texture type as input and loads all available files of that type.
Well, the main load function:
QMap<TextureType, QList<Texture>> loadTextures()
{
QMap<TextureType, QList<Texture>> result;
for (auto type : getAvailableTextureTypes()) {
result.insert(type, loadTexturesForType(type));
}
return result;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question