SDL Error: Invalid Renderer on SDL_CreateTextureFromSurface

SDL Error: Invalid Renderer on SDL_CreateTextureFromSurface

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

//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 return 出现无效渲染器错误。在我的初始化 class 中,渲染器完美初始化,只有当我尝试使用它加载图像时,我才会收到无效渲染器错误。感谢任何有关如何修复此错误的帮助。

编辑:: 这是 Init Class 中与渲染器相关的一些代码,

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;

            }

希望这有助于找到问题。

您的 Renderer 似乎未初始化,除非您发布的代码位于 Init class 的构造函数中。

您的代码中是否已有 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");
}