首页 > 解决方案 > SDL-Image:无法打开图像,只有错误消息

问题描述

Game Engine Creation 课程作业,关于 SDL 的问题,目前正在学习 C++ SDL。遇到一个关于 SDL_Image 的问题,无法打开图像。

当我运行代码时它立即关闭

bool InitSDL()
{
    //Setup SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        cout << "SDL did not initialise. Error: " << SDL_GetError();
        return false;
    }
    else
    {
        gWindow = SDL_CreateWindow(
            "GEC_SDL",
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            SCREEN_WIDTH,
            SCREEN_HEIGHT,
            SDL_WINDOW_SHOWN
            );
        if (gWindow == NULL) 
        {
            cout << "Window was not created. Error: " << SDL_GetError();
            return false;
        }
    }

    gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
    if (gRenderer != NULL)
    {
        cout << "Renderer initialised." << endl;
        //Initialise PNG loading.
        int imageFlags = IMG_INIT_PNG;
        if (!(IMG_Init(imageFlags) & imageFlags))
        {
            cout << "SDL_Image could not initialise. Error: " << IMG_GetError;
            return false;
        }
        else {
            cout << "SDL_Image initialised." << endl;
        }
    }
    else
    {
        cout << "Renderer could not initialise. Error: " << SDL_GetError;
        return false;
    }

    gTexture = LoadTextureFromFile("D:/_a'Programs/School/GEC_SDL/GEC_SDL/Images/text.bmp");
    if (gTexture == NULL)
    {
        return false;
    }

    return true;
}

错误信息

尝试完整路径:D:_a'Programs\School\GEC_SDL\GEC_SDL\Images

错误信息

从文件函数加载纹理

SDL_Texture* LoadTextureFromFile(string path)
{
    FreeTexture();

    SDL_Texture* pTexture = NULL;

    //Load the image.
    SDL_Surface* pSurface = IMG_Load( path.c_str() );
    if (pSurface != NULL)
    {
        //Create the texture from the pixels on the surface.
        pTexture = SDL_CreateTextureFromSurface(gRenderer, pSurface);
        if (pTexture == NULL)
        {
            cout << "Unable to create texture from surface. Error: " << SDL_GetError() << endl;
        }
        SDL_FreeSurface(pSurface);
    }
    else
    {
        cout << "Unable to create texture from surface. Error: " << IMG_GetError() << endl;
    }

    return pTexture;
}

我在论坛中搜索了类似的问题并尝试了他们的方法,它们都不起作用,请帮助。

标签: c++c

解决方案


OMG,是的,这是'问题,我重命名了文件夹,现在它可以工作了......

PS:愚蠢的字符集


推荐阅读