使用SDL和2D的2D TTF文本渲染OpenGL问题

时间:2012-07-16 21:02:17

标签: opengl 2d sdl true-type-fonts

所以我环顾四周,并没有找到在SDL& amp;中实际实现这一点的最佳方法。 OpenGL的。此时我可以使用TTF在屏幕上显示文本,但它并没有按照应有的方式显示文本。附件是显示文本时引擎的外观的屏幕截图。基本上从我在代码中看到的内容来看,我认为发生的事情是我没有使用我用作窗口的主SDL_Surface来模拟文本纹理。我这样说是因为在照片中我的角色周围有一个红色的碰撞盒正在掩盖我正在渲染文本的文本纹理。关于我能做什么的任何想法?

在我的游戏循环中,我在将所有内容绘制到屏幕之前和之后调用beginDraw()和endDraw()。

图片:http://public.gamedev.net/uploads/monthly_07_2012/post-200874-0-25909300-1342404845_thumb.png

所有引擎代码均可在此处找到:https://github.com/Jevi/SDL_GL_ENGINE

P.S:我要为此发另一篇文章,但如果你们已经在查看我的代码,我也可以问这个问题。我一直注意到引擎的一些内存问题。在任务管理器中,分配给我的程序的内存不断攀升,因为它从大约11000 K开始。

void graphics::SDL_GL_RenderText(float x1, float y1, int width, int height, const char* text, int ptsize, const char* ttfLoc, int r, int g, int b){
SDL_Surface* temp;
SDL_Surface* temp2;
SDL_Rect rect;
TTF_Font* font;
SDL_Color textColor;
unsigned int texture;

font = TTF_OpenFont( ttfLoc , ptsize );
textColor.r = r;
textColor.g = g;
textColor.b = b;

temp = TTF_RenderText_Blended( font, text, textColor );

// width = nextpoweroftwo(width);
// height = nextpoweroftwo(height);

temp2 = SDL_CreateRGBSurface(0, width, height, 32, r, g, b, 0);
SDL_BlitSurface(temp, 0, temp2, 0);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, temp2->w, temp2->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp2->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

/* prepare to render our texture */
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glColor3f(1.0f, 1.0f, 1.0f);

/* Draw a quad at location */
glBegin(GL_QUADS);
glTexCoord2d(0,0);
glVertex2f(x1, y1);
glTexCoord2d(1,0);
glVertex2f(x1 + temp2->w, y1);
glTexCoord2d(1,1);
glVertex2f(x1 + temp2->w, y1 + temp2->h);
glTexCoord2d(0,1);
glVertex2f(x1, y1 + temp2->h);
glEnd();
glFinish();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);

SDL_FreeSurface(temp);
SDL_FreeSurface(temp2);
TTF_CloseFont(font);
glDeleteTextures(1, &texture);

}

void graphics::GL_BeginDraw(){
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //Start rendering phase
glOrtho(0,width,height,0,-1,1); //Set the matrix

}

void graphics::GL_EndDraw(){
glPopMatrix(); //End rendering phase
SDL_GL_SwapBuffers();
glFinish();

}

0 个答案:

没有答案
相关问题