在OpenGL中使用SDL_ttf [英] Using SDL_ttf with OpenGL

查看:141
本文介绍了在OpenGL中使用SDL_ttf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenGL和SDL在程序中创建一个窗口.

I'm using OpenGL and SDL to create a window in my program.

如何在OpenGL窗口中使用SDL_ttf?

How do I use SDL_ttf with an OpenGL window?

例如,我想加载字体并渲染一些文本.我想使用SDL OpenGL表面绘制文本.

For example I want to load a font and render some text. I want to draw the text using an SDL OpenGL surface.

推荐答案

方法如下:

  1. 初始化SDL和SDL_ttf,并使用SDL_SetVideoMode()创建一个窗口.确保您通过了SDL_OPENGL标志.
  2. 初始化OpenGL场景(glViewport()glMatrixMode()等).
  3. 使用SDL_ttf渲染文本,例如TTF_RenderUTF8_Blended().渲染函数返回一个SDL_surface,您必须通过将指向数据的指针(surface->pixels)传递给OpenGL以及数据格式,将其转换为OpenGL纹理.像这样:

  1. Initialize SDL and SDL_ttf, and create a window using SDL_SetVideoMode(). Make sure you pass the SDL_OPENGL flag.
  2. Initialize your OpenGL scene (glViewport(), glMatrixMode() etc.).
  3. Render your text with SDL_ttf using e.g. TTF_RenderUTF8_Blended(). The render functions return an SDL_surface, which you have to convert into an OpenGL texture by passing a pointer to the data (surface->pixels) to OpenGL as well as the format of the data. Like this:

colors = surface->format->BytesPerPixel;
if (colors == 4) {   // alpha
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGBA;
    else
        texture_format = GL_BGRA;
} else {             // no alpha
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGB;
    else
        texture_format = GL_BGR;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); 
glTexImage2D(GL_TEXTURE_2D, 0, colors, surface->w, surface->h, 0,
                    texture_format, GL_UNSIGNED_BYTE, surface->pixels);

  • 然后,您可以使用glBindTexture()等在OpenGL中使用纹理.完成绘制后,请确保调用SDL_GL_SwapBuffers().

  • Then you can use the texture in OpenGL using glBindTexture() etc. Make sure to call SDL_GL_SwapBuffers() when you're done with drawing.

    这篇关于在OpenGL中使用SDL_ttf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆