gpt4 book ai didi

c++ - SDL 错误 : Invalid Renderer on SDL_CreateTextureFromSurface

转载 作者:行者123 更新时间:2023-11-28 02:17:41 28 4
gpt4 key购买 nike

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

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

}

希望这有助于发现问题。

最佳答案

看起来你的 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");
}

关于c++ - SDL 错误 : Invalid Renderer on SDL_CreateTextureFromSurface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33552443/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com