0

好吧,根据我的研究,无效的渲染器错误似乎适用于各种情况,我不知道为什么我的代码会创建它。我已将其缩小到特定的代码区域

//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;

            }

希望这有助于发现问题。

4

1 回答 1

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");
}
于 2015-11-05T20:01:35.617 回答