读取图像文件而不指定名称

发布于 2024-08-07 04:24:15 字数 413 浏览 5 评论 0原文

SDL 或 C++ 中是否有任何工具允许您从文件夹中读取图像文件而不指定其名称,例如按顺序读取它们等?如果没有,您是否使用任何技术来完成同样的事情?

做这样的事情:

foo_ani[0] = LoadImage("Animations/foo1.png");
foo_ani[1] = LoadImage("Animations/foo2.png");
foo_ani[2] = LoadImage("Animations/foo3.png");

可能会变得非常乏味,并且不能使用循环,因为文件名每次都是特定的。

我真正想到的唯一方法可能是通过每个循环迭代器修改一个字符串,并将循环编号插入字符串的特定部分(假设这就是文件的标记方式),并使用该字符串作为 LoadImage 参数。这似乎比仅仅做上述工作还要多。

Are there any facilities in SDL or C++ that allow you to read image files in from a folder without specifying their name, like reading them in sequential order, etc.? If not are there any techniques you use to accomplish something along the same lines?

Doing something like this:

foo_ani[0] = LoadImage("Animations/foo1.png");
foo_ani[1] = LoadImage("Animations/foo2.png");
foo_ani[2] = LoadImage("Animations/foo3.png");

can become quite tedious, and a loop can't be used because the file name is specific each time.

The only way I could really think of is maybe having a string that you modify through each loop iterator and insert the loop number into the specific part of the string assuming that's how your files are labeled, and using that string as the LoadImage parameter. That seems like more work though than just doing the above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

讽刺将军 2024-08-14 04:24:15

使用 boost::filesystem

这里显示的小程序列出了目录 files/ 中的所有文件,与模式 fileN.type 匹配,其中 N 是从 0 开始的,未指定。

#include <iostream>
#include <sstream>
#include <string>
#include <boost/filesystem.hpp>

using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char** argv)
{
    fs::path dir ("./files");
    string prefix = "file";
    string suffix = "type";
    int i = 0;

    fs::path file;

    do {
        stringstream ss;
        ss << prefix << i++ << "." << suffix;
        file = fs::path(dir / fs::path(ss.str()));
        if(fs::exists(file)) {
            cout << file.leaf() << " exists." << endl;
        }
    } while(fs::exists(file));
    return 0;
}

-lboost_filesystem 链接。

boost::filesystem 还提供了一个简单的目录迭代器。

Use boost::filesystem.

The tiny program shown here lists all files in the directory files/, matching the pattern fileN.type, where N is from 0 and upwards, unspecified.

#include <iostream>
#include <sstream>
#include <string>
#include <boost/filesystem.hpp>

using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char** argv)
{
    fs::path dir ("./files");
    string prefix = "file";
    string suffix = "type";
    int i = 0;

    fs::path file;

    do {
        stringstream ss;
        ss << prefix << i++ << "." << suffix;
        file = fs::path(dir / fs::path(ss.str()));
        if(fs::exists(file)) {
            cout << file.leaf() << " exists." << endl;
        }
    } while(fs::exists(file));
    return 0;
}

Link with -lboost_filesystem.

boost::filesystem also provides a simple directory iterator.

如日中天 2024-08-14 04:24:15

对于这种类型的情况,您通常会获得目录中的文件名列表(使用 opendir/readdirFindFirstFile/FindNextFile(视情况而定),并循环遍历目录中的每个文件名。给定每个文件名,您可以调用 LoadImage() 并将结果附加到数组中。

此技术不需要您提前知道文件名。

For this type of situation, you would typically get a list of the filenames in the directory (with opendir/readdir or FindFirstFile/FindNextFile as appropriate), and loop on each filename in the directory. Given each filename, you can call LoadImage() and append the result to your array.

This technique doesn't require that you know the filenames ahead of time.

冰葑 2024-08-14 04:24:15

自动加载该目录中的所有文件怎么样?

foo_ani = LoadImages("Animations/");

只需遍历给定的目录并加载该目录中的所有文件即可。

如果您有多个具有不同前缀的动画,另一种解决方案是使用正则表达式。我建议您使用 boost或 std::tr1::regex ,如下所示:

foo_ani = LoadImageSet("Animations/", std::tr1::regex("foo*.png"));

How about loading all files in that directory automatically?

foo_ani = LoadImages("Animations/");

Just traverse the directory given and load all files inside that fit.

Another solution, if you have several animations with different prefix is to use regular expressions. I suggest you use boost for that or std::tr1::regex, like this:

foo_ani = LoadImageSet("Animations/", std::tr1::regex("foo*.png"));
千柳 2024-08-14 04:24:15

鉴于您当前正在对帧的名称进行硬编码,我将假设您知道/可以控制文件的命名方案。我还假设您希望它们按顺序排列,因为它似乎是动画中的帧。最后,我假设您知道有多少帧,因为您似乎有一个足够大的数组来容纳所有准备就绪并等待的帧。

考虑到问题中提供的文件的名称,您不能只执行 FindFirst / FindNext,因为一旦您超过 10 帧,它们几乎肯定会乱序(考虑到提供的命名方案)。

所以我认为你是对的,最好的方法是循环,但错误的是它比手工做更费力。

char* fname = new char[50]; // buffer big enough to hold filenames
int numFrames = 8; // or however many, you seem to know what this value should be

for(int i = 0; i < numFrames;  ++i)
{
    sprint(fname, "Animations/foo%d.png",(i+1));
    foo_ani[i] = LoadImage(fname);
}

delete[] fname;

大约有 6 行代码。所以对于超过 6 帧的动画,我认为这更容易。

Given that you are are currently hard coding the name of the frames, I'm going to assume you know / have control over the naming scheme of the files. I'm also assuming you want them sequentially since it seems to be frames in an animation. Finally I'm assuming you know how many frames there are since you seem to have an array big enough to accommodate them all ready and waiting.

Given the names of the files presented in the question, you can't just do FindFirst / FindNext because once you get past 10 frames, they're almost certainly going to come in out of order (given the naming scheme presented).

So I think that you're right that the best way to do it is in a loop, but wrong that it's more effort than doing it by hand.

char* fname = new char[50]; // buffer big enough to hold filenames
int numFrames = 8; // or however many, you seem to know what this value should be

for(int i = 0; i < numFrames;  ++i)
{
    sprint(fname, "Animations/foo%d.png",(i+1));
    foo_ani[i] = LoadImage(fname);
}

delete[] fname;

That's about 6 lines of code. So for animations of more than 6 frames, I'd say that was easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文