嗨,各位 Overflowers! 我最近开始学习SDL。我选择简单的 directmedia 层作为我的 C++ 知识的外部 API,因为我发现它可以为游戏开发者提供最直观的增强机制。考虑以下代码:
#include <iostream>
#include "SDL/SDL.h"
using std::cerr;
using std::endl;
int main(int argc, char* args[])
{
// Initialize the SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
cerr << "SDL_Init() Failed: " << SDL_GetError() << endl;
exit(1);
}
// Set the video mode
SDL_Surface* display;
display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (display == NULL)
{
cerr << "SDL_SetVideoMode() Failed: " << SDL_GetError() << endl;
exit(1);
}
// Set the title bar
SDL_WM_SetCaption("SDL Tutorial", "SDL Tutorial");
// Load the image
SDL_Surface* image;
image = SDL_LoadBMP("LAND.bmp");
if (image == NULL)
{
cerr << "SDL_LoadBMP() Failed: " << SDL_GetError() << endl;
exit(1);
}
// Main loop
SDL_Event event;
while(1)
{
// Check for messages
if (SDL_PollEvent(&event))
{
// Check for the quit message
if (event.type == SDL_QUIT)
{
// Quit the program
break;
}
}
// Game loop will go here...
// Apply the image to the display
if (SDL_BlitSurface(image, NULL, display, NULL) != 0)
{
cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
exit(1);
}
//Update the display
SDL_Flip(display);
}
// Tell the SDL to clean up and shut down
SDL_Quit();
return 0;
}
我所做的只是制作了一个屏幕表面,对其进行了双倍缓冲,制作了图像的另一个表面,将两者 blit'ed 在一起,由于某种原因,当我构建应用程序时,它立即关闭了!应用程序构建成功,但随后在没有打开窗口的情况下关闭!这真是令人沮丧。
我正在使用 XCode5
和 SDL 2.0.3
:)
需要帮助!
编辑:在错误日志中显示,它说 SDL_LoadBMP(): Failed to load LAND.bmp
。 bmp保存在根目录下,和main.cpp
文件夹同一个文件夹?为什么这行不通?
最佳答案
您应该能够使用图像的绝对(完整)路径来测试您的代码。这将验证代码是否确实有效。
为了能够使用具有绝对路径的资源,您应该创建一个构建阶段来复制文件。目的地应设置为“产品目录”。您可以将子路径留空或提供一个目录来放置资源(当您获得大量资源时这将很有用),例如纹理。如果您提供子路径,则更改您的代码,使其成为 textures/LAND.bmp
您还可以使用构建阶段来打包 SDL2.framework 和任何其他内容,例如SDL2_image 等与您的最终应用程序。这将允许其计算机上没有 SDL 的用户运行该应用程序。为此,创建另一个构建阶段,将 Detination 设置为“Frameworks”,并将 Subpath 留空。然后只需添加您想要与应用程序打包的任何框架。您希望在build设置中进行的另一项设置是将“运行路径搜索路径”(在“链接”下找到)更改为 @executeable_path/../Frameworks
以便应用程序知道在哪里可以找到打包框架
我有一个关于在 Xcode 中配置 SDL2 的教程以及一个快速完成的模板
关于c++ - SDL 图像渲染困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23240844/