可使用不同目录中的图像执行

发布于 2025-01-05 04:26:50 字数 485 浏览 3 评论 0原文

我有以下目录结构,

       (root)
  /      |        \
bin    resources  src
|        |         |
Debug  dot.bmp   .cpp
|
.exe

我希望 .exe 文件使用 dot.bmp。

这是加载 dot.bmp 的 .cpp 文件中的代码

player_img = il->load_image( "dot.bmp" );

我觉得我需要修改这行代码,但是更改后它到:

player_img = il->load_image( "../resources/dot.bmp" );

我仍然收到一条错误消息,指出图像无法已加载。

我需要改变什么?这是否可能,或者我应该放弃并将图像放在与 .exe 相同的目录中?

I have the following directory structure

       (root)
  /      |        \
bin    resources  src
|        |         |
Debug  dot.bmp   .cpp
|
.exe

I would like the .exe file to use dot.bmp.

Here's the code from the .cpp file that loads dot.bmp

player_img = il->load_image( "dot.bmp" );

I feel like I need to modify this line of code, but after changing it to:

player_img = il->load_image( "../resources/dot.bmp" );

I still get an error saying that the image couldn't be loaded.

What do I need to change? Is this even possible, or should I just give up and put the image in the same directory as the .exe?

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

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

发布评论

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

评论(1

等风也等你 2025-01-12 04:26:50

你需要再下一层才能找到根源。

../../resources/dot.bmp

您的可执行文件位于 bin/Debug 中,但我认为您是在假设它位于 bin 中的情况下进行编码的。

假设您使用的是 Windows,相对路径将相对于当前工作目录,而不是可执行文件所在的目录。通常它们是同一件事,但不一定。

我倾向于使用完全限定的路径并预先设置可执行文件所在的目录。您可以通过调用 GetModuleFileName< 来获取此文件/a> 将 NULL 作为 hModule 参数传递。这将返回可执行文件的完整路径,因此您需要删除文件名部分。

您还需要考虑部署。此结构看起来像您的开发结构,但在部署程序时您可能需要不同的组织。例如,我希望部署时可执行文件位于 bin 目录中。

最后一个想法。假设您的程序需要的图像在编译时已知,那么将它们作为资源链接到可执行文件会更容易。这样您就根本不必担心这些问题,并且可执行文件可以独立存在。

You need to go down one further level in order to get to the root.

../../resources/dot.bmp

Your executable is in bin/Debug but I think you coded under the assumption that it is in bin.

Assuming you are on Windows, the relative path will be relative to the current working directory rather than the directory where the executable resides. Often they are the same thing, but not necessarily.

I would be inclined to use fully-qualified paths and pre-pend the directory where the executable lives. You can obtain this by calling GetModuleFileName passing NULL as the hModule argument. This will return the full path to the executable so you will need to strip off the file name portion.

You will also need to think about deployment. This structure looks like your development structure but you may want a different organisation when you deploy the program. For example, I'd expect the executable to live in the bin directory when deployed.

One final thought. Assuming the images that your program needs is known at compile time it would be much easier to link them into the executable as resources. That way you simply don't have to worry about these issues at all and the executable can stand alone.

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