SDL错误:SDL_CreateTextureFromSurface上的渲染器无效

时间:2015-11-05 18:48:48

标签: c++ sdl renderer sdl-image

好的,根据我的研究,似乎无效的渲染器错误适用于各种情况,我迷失了我的代码创建它的原因。 我已将其缩小到特定的代码区域

//If existing texture is there, free's and sets to NULL. Along with iWidth & iHeight = 0;
freetexture();

//final image texture
SDL_Texture* niTexture = NULL;

//Loads image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
    printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
    printf("SpriteSheet :: Loaded\n");
    Init mkey;
    //Color key DOUBLE CHECK IF ERROR CHANGE TO ORIGINAL 0, 0xFF, 0xFF
    SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 50, 96, 166));

    //create texture from surface pixels
    niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
    if (niTexture == NULL)
    {
        printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
    }

特别是在该行,

niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);

导致SDL返回无效的渲染器错误。在我的init类中,渲染器完美初始化,只有当我尝试使用它来加载图像时才会出现Invalid Renderer错误。任何有关如何解决此错误的帮助表示赞赏。

编辑:: 以下是与渲染器相关的Init类的一些代码,

printf("Linear Texture Filtering :: Enabled\n");
        //Create Window
        Window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sw, sh, SDL_WINDOW_SHOWN);
        if (Window == NULL)
        {
            printf("Window failed to be created\n");
            SDLSuccess = false;
        }
        else
        {
            printf("Window :: Created\n");
            //Create VYNC'd renderer for the window
            Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
            if (Renderer == NULL)
            {
                printf("Renderer failed to be created\n");
                SDLSuccess = false;

            }

希望这有助于找到问题。

1 个答案:

答案 0 :(得分:2)

看起来您的Renderer未初始化,除非您发布的代码位于Init类的构造函数中。

你的代码中某处有Init的实例,你的意思是在纹理方法中引用它吗?在尝试使用渲染器之前检查它的值,例如:

if (mkey.Renderer) {
    niTexture = SDL_CreateTextureFromSurface(mkey.Renderer, loadedSurface);
    if (niTexture == NULL)
    {
        printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
    }
} else {
    printf("Renderer is not initialized");
}