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

C++ SDL错误:SDL_CreateTextureFromSurface上的渲染器无效,c++,sdl,renderer,sdl-image,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 specifi

好的,根据我的研究,无效渲染器错误似乎适用于各种情况,我不明白为什么我的代码要创建它。 我已经把它缩小到一个特定的代码区域

//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类中,渲染器完全初始化,只有当我尝试使用它来加载图像时,才会出现无效渲染器错误。如有任何关于如何修复此错误的帮助,我们将不胜感激

编辑:: 下面是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;

            }

希望这有助于找到问题。

看起来您的
呈现程序未初始化,除非您发布的代码位于
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");
}

你能展示Init类中的一些代码吗?添加了一些Init代码以进行澄清。这在修复无效的渲染器部分方面起到了作用,我想既然我已经在main中将渲染器设置为初始化,那么如果我在另一个类中引用它就会起作用。现在,我收到另一个关于实际使用函数显示图像的错误。我会做更多的研究,看看是否能解决它。