Sdl&opengl (++sdl_ttf)

i have been trying to use opengl within sdl and to use sdl_ttf to render
fonts,for about 20 days…But i couldnt manage it and couldnt find enough
sources…Should I go back to and use glut?–
Just say the words , your wish is my command…

here a code example for using SDL_TTf with OpenGl
It use Unicodde Font rendering but you can change this

SDL_Color color = {255,255,255, 0 };
SDL_Surface *text    = TTF_RenderUNICODE_Blended(pFont,(Uint16*) L"Hallo

world",color);

if( !text )
    return;

int w = PowerOfTwo( text->w );
int h = PowerOfTwo( text->h );

SDL_Surface *surface =  SDL_CreateRGBSurface(
        SDL_SWSURFACE,
        w, h,
        32,

#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);

SDL_Rect area;
area.x = 0;
area.y = 0;
area.w = text->w;
area.h = text->h;

Uint32 saved_flags;
Uint8  saved_alpha;

SDL_SetAlpha(text, 0, 0);
SDL_BlitSurface(text, &area, surface, &area);



glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

GLuint tex;

glActiveTexture(GL_TEXTURE0);
/* Create an OpenGL texture for the image */
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,
         0,
         GL_RGBA,
         w, h,
         0,
         GL_RGBA,
         GL_UNSIGNED_BYTE,
         surface->pixels);

// glBindTexture(GL_TEXTURE_2D,tex);

float fMaxTextX = float(text->w) / w;
float fMaxTextY = float(text->h) / h;

float r = 256 / 256.0; // you should replace thie with varibales like r
/ 256.0
float g = 256 / 256.0;// g / 256.0
float b = 256 / 256.0;// b / 256.0

glBegin(GL_TRIANGLE_STRIP);

  glColor4f (r, g,b,1);
  glTexCoord2f(0,0);
  glVertex2f (x, y);

  glColor4f (r, g,b,1);
  glTexCoord2f(fMaxTextX,0);
  glVertex2f (t->x+text->w, t->y);

  glColor4f (r, g,b,1);
  glTexCoord2f(0,fMaxTextY);
  glVertex2f (x, y+text->h);

  glColor4f (r, g,b,1);
  glTexCoord2f(fMaxTextX,fMaxTextY);
  glVertex2f (x+text->w, y+text->h);

glEnd ();

glDeleteTextures(1, &tex);


SDL_FreeSurface(surface);
SDL_FreeSurface(text);

regards
Sahin

2007/5/7, Engin ?alar :>

i have been trying to use opengl within sdl and to use sdl_ttf to render
fonts,for about 20 days…But i couldnt manage it and couldnt find enough
sources…Should I go back to and use glut?


Just say the words , your wish is my command…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

(hocam saolas?n senden ba?ka cevap veren yok zaten :frowning: )

thank you for your help,do anyone know a 2 or 3 D project that uses sdl_ttf
for font rendering??

2007/5/8, Vardar Sahin :>

here a code example for using SDL_TTf with OpenGl
It use Unicodde Font rendering but you can change this

SDL_Color color = {255,255,255, 0 };
SDL_Surface *text    = TTF_RenderUNICODE_Blended(pFont,(Uint16*)

L"Hallo world",color);

if( !text )
    return;

int w = PowerOfTwo( text->w );
int h = PowerOfTwo( text->h );

SDL_Surface *surface =  SDL_CreateRGBSurface(
        SDL_SWSURFACE,
        w, h,
        32,

#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);

SDL_Rect area;
area.x = 0;
area.y = 0;
area.w = text->w;
area.h = text->h;

Uint32 saved_flags;
Uint8  saved_alpha;

SDL_SetAlpha(text, 0, 0);
SDL_BlitSurface(text, &area, surface, &area);



glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

GLuint tex;

glActiveTexture(GL_TEXTURE0);
/* Create an OpenGL texture for the image */
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,
         0,
         GL_RGBA,
         w, h,
         0,
         GL_RGBA,
         GL_UNSIGNED_BYTE,
         surface->pixels);

// glBindTexture(GL_TEXTURE_2D,tex);

float fMaxTextX = float(text->w) / w;
float fMaxTextY = float(text->h) / h;

float r = 256 / 256.0; // you should replace thie with varibales like
r / 256.0
float g = 256 / 256.0;// g / 256.0
float b = 256 / 256.0;// b / 256.0

glBegin(GL_TRIANGLE_STRIP);

  glColor4f (r, g,b,1);
  glTexCoord2f(0,0);
  glVertex2f (x, y);

  glColor4f (r, g,b,1);
  glTexCoord2f(fMaxTextX,0);
  glVertex2f (t->x+text->w, t->y);

  glColor4f (r, g,b,1);
  glTexCoord2f(0,fMaxTextY);
  glVertex2f (x, y+text->h);

  glColor4f (r, g,b,1);
  glTexCoord2f(fMaxTextX,fMaxTextY);
  glVertex2f (x+text->w, y+text->h);

glEnd ();

glDeleteTextures(1, &tex);


SDL_FreeSurface(surface);
SDL_FreeSurface(text);

regards
Sahin

2007/5/7, Engin ?alar < @Engin_Calar>:

i have been trying to use opengl within sdl and to use sdl_ttf to render
fonts,for about 20 days…But i couldnt manage it and couldnt find enough
sources…Should I go back to and use glut?


Just say the words , your wish is my command…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Just say the words , your wish is my command…

hi,
there’s an on-going project at http://code.google.com/p/project1453/ where we
have a really simple SDLFont class wrapping around SDL_ttf… You may want to
check that too… it simply returns an SDL_Surface; if that’s what you need.
it has a static map<int, TTF_Font*> font variable which it loads for
different font sizes, so that the font file is read only once for each
size… with UTF8 rendering we had no problems displaying turkish characters
(see screenshots at
http://bindortyuzelliuc.wordpress.com/ekran-goruntuleri/ ).

( hocam cok s?per bilmiyrm, opengl ile belki farklidir isler :slight_smile: kolay gelsin )

well, there it is :slight_smile:
–kayaOn Tuesday 08 May 2007 03:17:26 Engin ?alar wrote:

(hocam saolas?n senden ba?ka cevap veren yok zaten :frowning: )

thank you for your help,do anyone know a 2 or 3 D project that uses
sdl_ttf for font rendering??


Kaya O?uz
http://www.kuzeykutbu.org/

(hocam saolas?n senden ba?ka cevap veren yok zaten :frowning: )

thank you for your help,do anyone know a 2 or 3 D project that uses
sdl_ttf for font rendering??

hi,
there’s an on-going project at http://code.google.com/p/
project1453/ where we
have a really simple SDLFont class wrapping around SDL_ttf… You
may want to
check that too… it simply returns an SDL_Surface; if that’s what
you need.
it has a static map<int, TTF_Font*> font variable which it loads for
different font sizes, so that the font file is read only once for each
size… with UTF8 rendering we had no problems displaying turkish
characters
(see screenshots at
http://bindortyuzelliuc.wordpress.com/ekran-goruntuleri/ ).

How well (if at all) does this work for asian-language fonts? I
realize it’s bending the original topic a bit, but that’s where my
needs may really fit is the ability to represent (specifically)
Japanese text. Though I’m not actively working on this project, so
it’s not vital. :slight_smile:

Thanks,
– ScottOn 2007/05/08, at 1:51, Kaya O?uz wrote:

On Tuesday 08 May 2007 03:17:26 Engin ?alar wrote:

Hi,
I have no idea really :slight_smile: The text is rendered with TTF_RenderUTF8_Blended, so
if you can display japanese with sdl_ttf, you probably can with this class.
as i’ve said before, the class is really simple, it just wraps some basic
functions of sdl_ttf, nothing special :slight_smile:

thanks,
–kayaOn Tuesday 08 May 2007 11:34:57 Scott Harper wrote:

How well (if at all) does this work for asian-language fonts? ?I ?
realize it’s bending the original topic a bit, but that’s where my ?
needs may really fit is the ability to represent (specifically) ?
Japanese text. ?Though I’m not actively working on this project, so ?
it’s not vital. :slight_smile:


Kaya O?uz
http://www.kuzeykutbu.org/

is sdl_ttf better than building outline fonts in opengl or bitmap fonts in
opengl?? what is the best way to have fonts in a sdl + opengl application?On 5/8/07, Kaya O?uz wrote:

On Tuesday 08 May 2007 11:34:57 Scott Harper wrote:

How well (if at all) does this work for asian-language fonts? I
realize it’s bending the original topic a bit, but that’s where my
needs may really fit is the ability to represent (specifically)
Japanese text. Though I’m not actively working on this project, so
it’s not vital. :slight_smile:

Hi,
I have no idea really :slight_smile: The text is rendered with TTF_RenderUTF8_Blended,
so
if you can display japanese with sdl_ttf, you probably can with this
class.
as i’ve said before, the class is really simple, it just wraps some basic
functions of sdl_ttf, nothing special :slight_smile:

thanks,
–kaya


Kaya O?uz
http://www.kuzeykutbu.org/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Engin ?alar wrote:

do anyone know a 2 or 3 D project that uses sdl_ttf for font rendering??

http://pipmak.svn.sourceforge.net/viewvc/pipmak/trunk/pipmak/source/terminal.c?view=markup
http://pipmak.svn.sourceforge.net/viewvc/pipmak/trunk/pipmak/source/pipmakLuaLib.c?view=markup#l_1232

(Both may not be the simplest examples as they do some further
processing on the surface generated by SDL_ttf before sending it to
OpenGL - the former adds a shadow, the latter does RGBA->RGBA blitting
(which SDL can’t do).)

-Christian

You mean look at:

http://homepages.paradise.net.nz/henryj/code/index.html#FTGL

Regards–
Kuon

"Don’t press that button."
http://goyman.com/

You mean look at:

http://homepages.paradise.net.nz/henryj/code/index.html#FTGL

I meant: “you might”.

I also wanna point out that we use Freetype in our own
implementation, then we upload each glyph into a texture with subTex
upload. We also build a VBO we upload.

When we draw a glyph, each glyph has the freetype glyph index, and a
vbo index. We place the transform matrix at the right place with the
glyph index, by querying freetype (of course, you can cache all those
values, even kerning if you like), then we use glDrawArray to draw a
piece of the font texture with the right coordinates.

If you need any help, I’m always happy to help.On 9 May 2007, at 12:16 PM, Kuon - Nicolas Goy - ??? (Goyman.com SA) - 675 wrote:


Kuon

"Don’t press that button."
http://goyman.com/

thanks ,now i have more knowledge on SDL-ttf&opengl but i wanna ask that:::
in old my code,as i learned from sdl_ttf documentation , i create two
surfaces :one for screen (to use with SDL_SetVideoMode) and one for text…i
open the font and render it to the second surface and then blit that on the
first.
Then i just see the primitives on the screen that i drew using opengl,but
dont see any text?
what is the problem with that?

is it a must to use textures to render fonts?

2007/5/11, Kuon - Nicolas Goy - ??? (Goyman.com SA) - 675 :

On 9 May 2007, at 12:16 PM, Kuon - Nicolas Goy - ??? (Goyman.com SA) - 675 wrote:

You mean look at:

http://homepages.paradise.net.nz/henryj/code/index.html#FTGL

I meant: “you might”.

I also wanna point out that we use Freetype in our own
implementation, then we upload each glyph into a texture with subTex
upload. We also build a VBO we upload.

When we draw a glyph, each glyph has the freetype glyph index, and a
vbo index. We place the transform matrix at the right place with the
glyph index, by querying freetype (of course, you can cache all those
values, even kerning if you like), then we use glDrawArray to draw a
piece of the font texture with the right coordinates.

If you need any help, I’m always happy to help.


Kuon

"Don’t press that button."
http://goyman.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Just say the words , your wish is my command…

When you pass SDL_OPENGL to SDL_SetVideoMode, you cannot blit to that
surface. Opengl controls it, the only way to draw to it is through
opengl, like uploading the SDL_Surface as a texture and then using it
to draw a quad.

If you check the return value of SDL_BlitSurface it probably returned
an error who’s SDL_GetError() string says something like "cannot blit
to a NULL surface.On 5/11/07, Engin ?alar wrote:

thanks ,now i have more knowledge on SDL-ttf&opengl but i wanna ask that:::
in old my code,as i learned from sdl_ttf documentation , i create two
surfaces :one for screen (to use with SDL_SetVideoMode) and one for text…i
open the font and render it to the second surface and then blit that on the
first.
Then i just see the primitives on the screen that i drew using opengl,but
dont see any text?
what is the problem with that?

is it a must to use textures to render fonts?

thank you,thats the answer i look for…

2007/5/11, Brian <brian.ripoff at gmail.com>:>

When you pass SDL_OPENGL to SDL_SetVideoMode, you cannot blit to that
surface. Opengl controls it, the only way to draw to it is through
opengl, like uploading the SDL_Surface as a texture and then using it
to draw a quad.

If you check the return value of SDL_BlitSurface it probably returned
an error who’s SDL_GetError() string says something like "cannot blit
to a NULL surface.

On 5/11/07, Engin ?alar <@Engin_Calar> wrote:

thanks ,now i have more knowledge on SDL-ttf&opengl but i wanna ask
that:::
in old my code,as i learned from sdl_ttf documentation , i create two
surfaces :one for screen (to use with SDL_SetVideoMode) and one for
text…i
open the font and render it to the second surface and then blit that on
the
first.
Then i just see the primitives on the screen that i drew using
opengl,but
dont see any text?
what is the problem with that?

is it a must to use textures to render fonts?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Just say the words , your wish is my command…

is it a must to use textures to render fonts?

Yea, you must upload the pixel data to OpenGL, the best way is to
use texture, because you upload once to the GPU then you simply ask
the GPU to draw it.

You can also use glDrawPixel, but it’s way slower because you have to
upload the data for each frame, which is a bad solution.

In our game, we draw the 3d scene, then the UI. The UI draws all the
widget, and at the end all the texts.

RegardsOn 11 May 2007, at 1:28 AM, Engin ?alar wrote:


Kuon

"Don’t press that button."
http://goyman.com/