【问题标题】:SDL image render difficultiesSDL 图像渲染困难
【发布时间】:2023-12-16 21:47:01
【问题描述】:

嗨,溢出者们! 我最近开始学习 SDL。我选择简单的直接媒体层作为我的 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;    
}

我所做的只是制作了一个屏幕表面,对其进行双重缓冲,制作了图像的另一个表面,将两者结合在一起,并且由于某种原因,当我构建应用程序时,它会立即关闭!应用程序构建成功,但随后在没有打开窗口的情况下关闭!这真是令人沮丧。

我正在使用 XCode5SDL 2.0.3 :) 需要帮助!

编辑:在错误日志中显示为SDL_LoadBMP(): Failed to load LAND.bmp。 bmp保存在根目录下,和main.cpp文件夹是同一个文件夹?为什么这不起作用?

【问题讨论】:

    标签: c++ xcode macos sdl


    【解决方案1】:

    您应该能够使用图像的绝对(完整)路径来测试您的代码。这将验证代码是否确实有效。

    为了能够使用具有绝对路径的资源,您应该创建一个构建阶段来复制文件。目的地应设置为“产品目录”。您可以将子路径留空或提供一个目录来放置资源(当您获得大量资源时这将很有用),例如纹理。如果您提供子路径,则更改您的代码,使其成为textures/LAND.bmp

    您还可以使用构建阶段来打包 SDL2.framework 和任何其他内容,例如SDL2_image 等与您的最终应用程序。这将允许计算机上没有 SDL 的用户运行该应用程序。为此,请创建另一个构建阶段,并将 Detination 设置为“框架”,并将子路径留空。然后只需添加要与应用程序打包的任何框架。您要在 Build Settings 中进行的另一项设置是将“Runpath Search Paths”(在“Linking”下找到)更改为 @executeable_path/../Frameworks,以便应用程序知道在哪里可以找到打包的框架

    我有一个关于在 Xcode 中配置 SDL2 的教程以及一个快速完成的模板

    http://zamma.co.uk/how-to-setup-sdl2-in-xcode-osx/

    【讨论】:

    • 非常感谢!我能够成功添加构建阶段,并且 bam,它可以工作。非常感谢您的帮助。