![](/img/trans.png)
[英]SDL2 - Why does SDL_CreateTextureFromSurface() need a renderer*?
[英]SDL Error: Invalid Renderer on SDL_CreateTextureFromSurface
好吧,根据我的研究,似乎Invalid Renderer错误适用于各种情况,而我却迷失了我的代码创建它的原因。 我已将其范围缩小到特定的代码区域
//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;
}
希望这有助于发现问题。
除非您发布的代码在Init
类的构造函数中,否则您的Renderer
似乎尚未初始化。
您打算在纹理方法中引用的代码中是否已有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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.