This is how I use SDL text
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
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
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
quote:Original post by Drizzt DoUrden
Now how would you display the value of a variable ?
By using the standard C or C++ libraries. An example in C:
int SomeValue = 12345;char SomeString[12];sprintf(SomeString,"%d",SomeValue);
[Resist Windows XP''s Invasive Production Activation Technology!]
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).
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
*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
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
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, <a href="http://www.charlesriver.com/titles/larameegames.html">Game Design Methods</a> , Charles River Media (coming April/May 2002)
Online column - Design Corner at Pixelate
NJ IGDA Chapter - NJ developers unite!! [Chapter Home | Chapter Forum]
_________________________________________________________________
Drew Sikora
A.K.A. Gaiiden
ICQ #: 70449988
AOLIM: DarkPylat
Blade Edge Software
Staff Member, GDNet
Public Relations, Game Institute
3-time Contributing author, <a href="http://www.charlesriver.com/titles/larameegames.html">Game Design Methods</a> , Charles River Media (coming April/May 2002)
Online column - Design Corner at Pixelate
NJ IGDA Chapter - NJ developers unite!! [Chapter Home | Chapter Forum]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement