SDL_ttf和OpenGL正在输出看似随机的垃圾和崩溃

SDL_ttf and OpenGL are outputting seemingly random garbage and crashes

本文关键字:随机 崩溃 输出 ttf OpenGL SDL      更新时间:2023-10-16

我正在尝试编写一个使用SDL_ttf在OpenGL中渲染文本的基本程序。我已经看到了大约十几个关于如何使两者协同工作的问题,几乎所有这些问题都提供了与我使用的代码相似的代码。然而,我没有看到任何与我所经历的波动有关的问题。

这很奇怪。如果我使用的是某个字体、某个点大小、某个输出字符串,那么程序就能完美地工作和输出。在其他情况下,程序将运行,但文本表面显示为垃圾。在其他情况下,程序会立即崩溃。

例如,如果我调用TTF_OpenFont("font1.TTF",28),然后用"Testing"作为输出字符串调用TTF_RenderUTF8_Blended(),它就会崩溃。但我尝试了很多事情,结果都非常令人不安。

-如果我删除"g"并尝试输出"Testin",那么程序就会正常工作并按预期输出。O_O

-如果我试图输出"Test",我会得到垃圾。

-如果我试图输出"Tst",程序就会工作。o_o

-如果我试图输出"tst",程序就会崩溃。

此外,当我尝试不同的字体和点大小时,我会遇到垃圾或不同字符串的崩溃。

SDL_ttf函数上的所有错误检查都恢复正常。

换句话说,据我所知,唯一决定程序是否工作的是我传递给SDL_ttf函数的字符串。我完全不知道为什么会出现这种情况(字体目录肯定是准确的)。我所能假设的是,我忽略了某种明显的记忆泄露或其他什么。

话虽如此,这是我的初始化代码:

bool Init() {
if( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
return false;
//SDL_Surface* display_surface is declared outside of this function
if( (display_surface = SDL_SetVideoMode(640 , 480 , 32 , SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL)) == NULL )
return false;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,            8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,          8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,           8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,          8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,          16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,         32);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,      8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,    8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,     8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,    8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,  1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,  2);
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0 ,640 ,480 ,0 ,1 ,-1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//TTF_Font* test_font is declared outside of this function
test_font = TTF_OpenFont( "font1.ttf" , 28 ); 
SDL_Color text_color = { 255 , 255 , 255 };
//SDL_Surface* text_surface is declared outside of this function
text_surface = TTF_RenderUTF8_Blended( test_font, "Testing" , text_color );
//force powers of 2
int w_pow2 = 1;
int h_pow2 = 1;
while( w_pow2 < text_surface->w )
w_pow2 *= 2;
while( h_pow2 < text_surface->h )
h_pow2 *= 2;
text_surface->w = w_pow2;
text_surface->h = h_pow2;
GLuint color_format;
if ( text_surface->format->BytesPerPixel == 4 ) {
if (text_surface->format->Rmask == 0x000000ff) 
color_format = GL_RGBA;
else 
color_format = GL_BGRA;
} 
else if ( text_surface->format->BytesPerPixel == 3 ) {
if ( text_surface->format->Rmask == 0x000000ff) 
color_format = GL_RGB;
else 
color_format = GL_BGR;
} 
glGenTextures( 1 , &texture ); //GLuint texture is declared outside of this function
glBindTexture( GL_TEXTURE_2D , texture );
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D , 0 , text_surface->format->BytesPerPixel , text_surface->w , text_surface->h , 0 , color_format , GL_UNSIGNED_BYTE , text_surface->pixels ); 
return true;
}

我的渲染代码:

void Draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D , texture );
glBegin( GL_QUADS );
glTexCoord2f( 0.0 , 0.0 );
glVertex2f( 0.0 , 0.0 );
glTexCoord2f( 1.0 , 0.0 );
glVertex2f( text_surface->w , 0.0 );
glTexCoord2f( 1.0 , 1.0 );
glVertex2f( text_surface->w , text_surface->h );
glTexCoord2f( 0.0 , 1.0 );
glVertex2f( 0.0 , text_surface->h );
glEnd();
glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );
SDL_GL_SwapBuffers();
}

以及我的清理:

void Cleanup() {
glDeleteTextures( 1 , &texture );
SDL_FreeSurface( text_surface ); 
TTF_CloseFont( test_font ); 
TTF_Quit();
SDL_Quit();
}

我将尽我所能提供尽可能多的额外细节。

TTF_RenderUTF8_Blended()根据TrueType渲染器决定绘制文本的方式生成任意大小的SDL_Surface(text_Surface)。稍后,您的代码单方面重新调整text_surface的宽度和高度变量的大小,而不实际更改图像数据(存储在"像素"中)以反映更改后的尺寸。这将导致glTexImage2D()读取不相关的程序变量数据作为图像数据(最好)和/或非法内存访问(最坏)。

从中检出SDLGL_LoadTextureFromFileBestFit()https://github.com/gpcz/OpenGL-SDL-Code-Warehouse/blob/master/SDLGLTexture.cpp例如如何调整SDL_ Surface的大小以具有二维幂以及如何将其映射到正确的OpenGL纹理坐标。