读取图像文件而不指定名称
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
boost::filesystem
。这里显示的小程序列出了目录
files/
中的所有文件,与模式fileN.type
匹配,其中 N 是从 0 开始的,未指定。与
-lboost_filesystem
链接。boost::filesystem
还提供了一个简单的目录迭代器。Use
boost::filesystem
.The tiny program shown here lists all files in the directory
files/
, matching the patternfileN.type
, where N is from 0 and upwards, unspecified.Link with
-lboost_filesystem
.boost::filesystem
also provides a simple directory iterator.对于这种类型的情况,您通常会获得目录中的文件名列表(使用
opendir
/readdir
或FindFirstFile
/FindNextFile
(视情况而定),并循环遍历目录中的每个文件名。给定每个文件名,您可以调用 LoadImage() 并将结果附加到数组中。此技术不需要您提前知道文件名。
For this type of situation, you would typically get a list of the filenames in the directory (with
opendir
/readdir
orFindFirstFile
/FindNextFile
as appropriate), and loop on each filename in the directory. Given each filename, you can callLoadImage()
and append the result to your array.This technique doesn't require that you know the filenames ahead of time.
自动加载该目录中的所有文件怎么样?
只需遍历给定的目录并加载该目录中的所有文件即可。
如果您有多个具有不同前缀的动画,另一种解决方案是使用正则表达式。我建议您使用 boost或 std::tr1::regex ,如下所示:
How about loading all files in that directory automatically?
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:鉴于您当前正在对帧的名称进行硬编码,我将假设您知道/可以控制文件的命名方案。我还假设您希望它们按顺序排列,因为它似乎是动画中的帧。最后,我假设您知道有多少帧,因为您似乎有一个足够大的数组来容纳所有准备就绪并等待的帧。
考虑到问题中提供的文件的名称,您不能只执行 FindFirst / FindNext,因为一旦您超过 10 帧,它们几乎肯定会乱序(考虑到提供的命名方案)。
所以我认为你是对的,最好的方法是循环,但错误的是它比手工做更费力。
大约有 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.
That's about 6 lines of code. So for animations of more than 6 frames, I'd say that was easier.