如前所述,我正在尝试使用在纹理中转换的 SDL_Surface 显示文本,这是我的代码:
if (SDL_Init(SDL_INIT_VIDEO) == 0) {
//Create a surface
switch (method) {
case 2: // load a texture with SDL_Image:
{
surface = IMG_Load("../../../../../data/box3.png");
}
break;
case 3: // load a texture with SDL_TTF:
SDL_Color textColor={ 255, 255, 0, 1 };
if (TTF_Init() == 0){;
TTF_Font *font;
font = TTF_OpenFont("../../../../../data/Bedizen.ttf", 20);
if (font != NULL){
qDebug() << TTF_FontFaceFamilyName(font);
surface = TTF_RenderText_Solid(font, ".....", textColor );
}
else
qDebug() << "Error (Font) : " << TTF_GetError();
}
else
qDebug() << "Error (Font) : " << TTF_GetError();
break;
}
if (surface != NULL){
GLint nbOfColors;
GLenum texture_format = 0;
qDebug("surface : %dx%d / %dbpp / %d", surface->w, surface->h,
surface->format->BytesPerPixel, surface->pitch);
MemoryDump(surface->pixels, surface->pitch, surface->h, surface->format->BytesPerPixel);
// get the number of channels in the SDL surface
nbOfColors = surface->format->BytesPerPixel;
switch (nbOfColors) {
case 1:
texture_format = GL_ALPHA;
break;
case 3: // no alpha channel
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
break;
case 4: // contains an alpha channel
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
break;
default:
qDebug() << "Warning: the image is not truecolor...";
break;
}
glEnable( GL_TEXTURE_2D );
// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &texture );
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture );
// Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, nbOfColors, surface->w, surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, surface->pixels );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
else{
qDebug() << "Error (SDL) : " << SDL_GetError();
}
}
这对 IMG_LOAD() 非常有效,但不适用于 TTF_RenderText_Solid(),所以我设法自己找到它,起初我认为 TTF_RTS() 的表面没有很好的像素映射,但我能够找出,多亏了自制的内存转储,它是好的。