嗨,欧弗拉兄弟!我最近开始学习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‘s在一起,由于某种原因,当我构建应用程序,它立即关闭!应用程序构建成功,但在没有打开窗口的情况下关闭!这真是令人沮丧。
我正在使用XCode5和SDL 2.0.3 :)需要帮助!
编辑:在错误日志中显示,上面写着SDL_LoadBMP(): Failed to load LAND.bmp。bmp保存在根目录中,与main.cpp文件夹相同吗?为什么这个不行?
发布于 2014-04-23 10:19:54
您应该能够使用图像的绝对(完整)路径来测试代码。这将验证代码是否实际工作。
要能够使用具有绝对路径的资源,您应该创建一个生成阶段来复制文件。目标应该设置为“产品目录”。您可以将Subpath保留为空白,或者提供一个目录来放置资源(当您获得大量资源时,这将是有用的),例如纹理。如果提供子路径,则修改代码,使其为textures/LAND.bmp
您还可以使用构建阶段将any 2框架和任何其他(如SDL2_image等)打包到最终的应用程序中。这将允许计算机上没有SDL的用户运行该应用程序。要做到这一点,创建另一个构建阶段,将剥离设置为“框架”,并将子路径保持为空。然后,只需添加任何框架,您想要与应用程序。在Build中要做的另一个设置是将“Runpath搜索路径”(在“链接”下找到)更改为@executeable_path/../Frameworks,以便应用程序知道在哪里找到打包的框架
我有一个教程,介绍如何在Xcode中配置SDL2,以及一个模板,以使其快速实现。
http://zamma.co.uk/how-to-setup-sdl2-in-xcode-osx/
https://stackoverflow.com/questions/23240844
复制相似问题