TTF_RenderText_Solid 错误

TTF_RenderText_Solid error

我目前在使用 SDL_TTF 和 OpenGL 渲染文本时遇到一个奇怪的错误。

问题是当我使用 TTF_RenderText_Blended 时,所有文本都显示良好并且没有任何问题

但是当我想切换到 TTF_RenderText_Solid "buggy" 时显示黑色矩形,我不知道指定如果从表面创建正确的纹理时 SDL_TTF 或 OpenGL 出现问题

从 textInfo(font,size) 加载表面的函数

void TextSprite::loadSprite(const std::string& text, textInfo* info){
SDL_Surface* tmpSurface = nullptr;

tmpSurface = TTF_RenderText_Solid(info->font,text.c_str(), *_color);
if (tmpSurface==nullptr){
    ErrorManager::systemError("Cannot make a text texture");
}
createTexture(tmpSurface);

}

从 SDL_Surface

创建 OpenGL 纹理的函数
void TextSprite::createTexture(SDL_Surface* surface){
glGenTextures(1,&_textureID);
glBindTexture(GL_TEXTURE_2D,_textureID);

int Mode = GL_RGB;
if (surface->format->BytesPerPixel==4){
    Mode = GL_RGBA;
}
glTexImage2D(GL_TEXTURE_2D,0,Mode,surface->w,surface->h,0,Mode,GL_UNSIGNED_BYTE,surface->pixels);

//Wrapping
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

//Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);

glBindTexture(GL_TEXTURE_2D,0);
_rect.w = surface->w;
_rect.h = surface->h;

SDL_FreeSurface(surface);

}

感谢您的帮助。

TTF_RenderText_Solid() outputs 8-bit palettized surfaces,而不是 TextSprite::createTexture() 操作的 32 位 ARGB 表面。