gpt4 book ai didi

c++ - SDL Surface Being used without being initialized 错误

转载 作者:行者123 更新时间:2023-11-30 01:23:15 26 4
gpt4 key购买 nike

我正在使用 SDL 加载图像并将其分配给 OpenGL 四边形。我找到了一个教程,告诉我如何去做,但是当我去运行程序时,它说 SDL Surface 变量(代码中的表面)正在使用而没有被初始化。我不知道是不是因为我正在使用类,因为我已经使用我从其他语言获得的知识来找到解决方案但没有运气。

这是一些代码:实体类(到目前为止):

//d2_entity.h
class d2Entity
{
public:
bool LoadImage(const char* fileName);
void DrawImage();
void Clear();

private:
GLuint *texture;
GLenum textureFormat;
GLint noColours;
};

//d2_entity.cpp
bool d2Entity::LoadImageW(const char* fileName)
{
SDL_Surface *surface;

if((surface == IMG_Load(fileName)))
{
// Check if image size is a power of 2
if((surface->w & (surface->w - 1)) != 0)
cout << "Opps! '" << fileName << "'s' width is not a power of 2!";
if((surface->h & (surface->h -1)) != 0)
cout << "Opps! '" << fileName << "'s' height is not a power of 2!";

// Get the No. of channels
d2Entity::noColours = surface->format->BitsPerPixel;
// Contains a alpha channel
if(d2Entity::noColours == 4)
{
if(surface->format->Rmask == 0x000000ff)
d2Entity::textureFormat = GL_RGBA;
else
d2Entity::textureFormat = GL_BGRA;
}
// No alpha channel
else if(d2Entity::noColours == 3)
{
if(surface->format->Rmask == 0x000000ff)
d2Entity::textureFormat = GL_RGB;
else
d2Entity::textureFormat = GL_BGR;
}
else
cout << "Opps! The image '" << fileName << "' is not truecolour!" << endl;

// Create an OpenGL texture
glGenTextures(1, d2Entity::texture);
glBindTexture(GL_TEXTURE_2D, *d2Entity::texture);
// Set texture streching properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, d2Entity::noColours, surface->w, surface->h, 0, d2Entity::textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
}
else
{
cout << "Opps! The entity image could not be loaded!" << endl;
SDL_Quit();
return false;
}

if(surface)
SDL_FreeSurface(surface);

return true;
}

最佳答案

SDL_Surface *surface;    
if((surface == IMG_Load(fileName)))

您从未初始化过 surface,然后您尝试将其与 IMG_Load 的结果进行比较。我很确定你打算改为分配它。

SDL_Surface *surface;
if((surface = IMG_Load(fileName)))

关于c++ - SDL Surface Being used without being initialized 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251776/

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