Skip to main content
GameDev.net gamedev.net
🔒 Locked

This is how I use SDL text

Started by Davaris Dec 12, 2001 at 3:02 PM 10 replies 3.8k views
Original Post
Davaris
Davaris
I saw a few people were having trouble with it. So until Cone3D writes one of his tutes heres my main file. You need SDL_ttf and you can get it from the library sectin at http://www.libsdl.org/. Thanks for the corrections Drizzt. #include "SDL.h" #include "SDL_ttf.h" SDL_Surface *screen; void DrawIMG(SDL_Surface *img, int x, int y) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_BlitSurface(img, NULL, screen, &dest); } int main( int argc, char* argv[] ) { TTF_Font* font; //initialize systems if (SDL_Init(SDL_INIT_VIDEO) <0) { printf("Unable to init SDL: %s\n", SDL_GetError()); return 1; } //set our at exit function atexit ( SDL_Quit ); //create a window screen = SDL_SetVideoMode ( 640, 480, 16, SDL_FULLSCREEN); if (screen == NULL) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); return 1; } TTF_Init (); // Need the .ttf file in the same directory as // the .exe or give it the full path. font = TTF_OpenFont("Antqua.ttf", 20); TTF_SetFontStyle (font, TTF_STYLE_NORMAL); SDL_Color fg = {255,255,255,0}; SDL_Color bg = {255,0,255,0}; SDL_Surface *text1; SDL_Surface *text2; text1 = TTF_RenderText_Solid(font, "Hello World 1", fg); text2 = TTF_RenderText_Shaded(font, "Hello World 2", fg, bg); int done = 0; //declare event variable SDL_Event event ; //message pump while(done == 0) { DrawIMG(text1, 50, 100); DrawIMG(text2, 200, 200); //look for an event while ( SDL_PollEvent ( &event ) ) { //an event was found if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } SDL_Flip(screen); }//end of message pump //done TTF_CloseFont(font); // Don't forget to release the two text surfaces. // I forgot the name of the function return ( 0 ) ; } Edited by - Davaris on December 12, 2001 4:15:03 PM
"I am a pitbull on the pantleg of opportunity."George W. Bush
Drizzt DoUrden
Drizzt DoUrden
Its cool to see people helping here, it is also cool to see that people are realizing how awsome SDL is.

One thing, you fogot to declare SDL_Surface *screen; , and you forgot to add the DrawIMG() function to your file, yet you make 2 calls to it. Heres the function:


void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

Also, don't forget to add the sdl_ttf.lib to your project.

Edited by - Drizzt DoUrden on December 12, 2001 4:12:10 PM
------------------------------Put THAT in your smoke and pipe it
Drizzt DoUrden
Drizzt DoUrden
This a a neat method. Now how would you display the value of a variable ?
------------------------------Put THAT in your smoke and pipe it
Drizzt DoUrden
Drizzt DoUrden
Thanks, but darnit Null and Void, why do you have to respond so fast. I was on my way back to edit my post (I just figured out that my integer had to be converted to a char).
------------------------------Put THAT in your smoke and pipe it
kajjait
kajjait
Does the text on screen display a little bit "Shaky" to anyone... kinda hurts to look at it a while

*edit* when i run it in a window instead of fullscreen it shows up fine! arg!

Edited by - kajjait on March 5, 2002 8:37:50 PM
johnhattan
johnhattan
One note. If you don''t wanna mess with Freetype and SDL_TTF, you could go with SFont.

http://www.linux-games.com/sfont/

You don''t have the size and color flexibility that you do with TrueType, but you can do nice stuff like shaded and textured text. The fonts that they''ve made for it are quite nice.

---
John Hattan
The Code Zone
Sweet software for a saturnine world

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually
Gaiiden
Gaiiden
I use SFont - much easier to deal with and plenty good for most needs.

_________________________________________________________________

Drew Sikora
A.K.A. Gaiiden

ICQ #: 70449988
AOLIM: DarkPylat

Blade Edge Software
Staff Member, GDNet
Public Relations, Game Institute

3-time Contributing author, [i]Game Design Methods [/i] , Charles River Media (coming April/May 2002)
Online column - [i]Design Corner [/i] at Pixelate

NJ IGDA Chapter - NJ developers unite!! [Chapter Home | Chapter Forum]
Drew Sikora
Executive Producer
GameDev.net
_nomad_
_nomad_
anyone here knows how to make sdl_ttf work with an opengl window?
Dorvo
Dorvo
There were two methods explained to me:

1. When using SDL_SetVideoMode() use the SDL_OPENGLBLIT flag with the others used for OpenGL. (Exactly how you render the text, I don''t know.)

2. Use the SDL_Surface obtained from TTF_RenderText_*() to create an OpenGL texture and putting that texture onto a quad on the screen. (This, I haven''t been able to get working either)

That''s something to think about.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.