RSS Forums RSS
Please support our C++ advertiser: Programming Forums

sdl_ttf not working

Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation: Sturm is on a distinguished road 
Rep Power: 4
Solved Threads: 23
Sturm's Avatar
Sturm Sturm is offline Offline
Veteran Poster

sdl_ttf not working

  #1  
Apr 1st, 2007
I am using debian linux and I cannot seem to get sdl_ttf working. It compiles fine but the program quits due to error checking in the code. It returns false at the ttf_init() part. Do not know if this makes a difference but I got sdl and ttf via apt.
Heres my compilation command :

g++ lesson07.cpp -o test -lSDL -lSDL_image -lSDL_ttf -lfreetype

heres my code (actually its a tutorials code. I do not claim that I made it or whatever)
  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2007) and may not be redestributed without written permission.*/
  2.  
  3. //The headers
  4. #include "SDL/SDL.h"
  5. #include "SDL/SDL_image.h"
  6. #include "SDL/SDL_ttf.h"
  7. #include <string>
  8.  
  9. //Screen attributes
  10. const int SCREEN_WIDTH = 640;
  11. const int SCREEN_HEIGHT = 480;
  12. const int SCREEN_BPP = 32;
  13.  
  14. //The surfaces
  15. SDL_Surface *background = NULL;
  16. SDL_Surface *message = NULL;
  17. SDL_Surface *screen = NULL;
  18.  
  19. //The event structure
  20. SDL_Event event;
  21.  
  22. //The font that's going to be used
  23. TTF_Font *font = NULL;
  24.  
  25. //The color of the font
  26. SDL_Color textColor = { 255, 255, 255 };
  27.  
  28. SDL_Surface *load_image( std::string filename )
  29. {
  30. //The image that's loaded
  31. SDL_Surface* loadedImage = NULL;
  32.  
  33. //The optimized surface that will be used
  34. SDL_Surface* optimizedImage = NULL;
  35.  
  36. //Load the image
  37. loadedImage = IMG_Load( filename.c_str() );
  38.  
  39. //If the image loaded
  40. if( loadedImage != NULL )
  41. {
  42. //Create an optimized surface
  43. optimizedImage = SDL_DisplayFormat( loadedImage );
  44.  
  45. //Free the old surface
  46. SDL_FreeSurface( loadedImage );
  47.  
  48. //If the surface was optimized
  49. if( optimizedImage != NULL )
  50. {
  51. //Color key surface
  52. SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
  53. }
  54. }
  55.  
  56. //Return the optimized surface
  57. return optimizedImage;
  58. }
  59.  
  60. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
  61. {
  62. //Holds offsets
  63. SDL_Rect offset;
  64.  
  65. //Get offsets
  66. offset.x = x;
  67. offset.y = y;
  68.  
  69. //Blit
  70. SDL_BlitSurface( source, clip, destination, &offset );
  71. }
  72.  
  73. bool init()
  74. {
  75. //Initialize all SDL subsystems
  76. if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  77. {
  78. return false;
  79. }
  80.  
  81. //Set up the screen
  82. screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  83.  
  84. //If there was in error in setting up the screen
  85. if( screen == NULL )
  86. {
  87. return false;
  88. }
  89.  
  90. //Initialize SDL_ttf
  91. if( TTF_Init() == -1 )
  92. {
  93. return false;
  94. }
  95.  
  96. //Set the window caption
  97. SDL_WM_SetCaption( "TTF Test", NULL );
  98.  
  99. //If everything initialized fine
  100. return true;
  101. }
  102.  
  103. bool load_files()
  104. {
  105. //Load the background image
  106. background = load_image( "background.png" );
  107.  
  108. //Open the font
  109. font = TTF_OpenFont( "lazy.ttf", 28 );
  110.  
  111. //If there was a problem in loading the background
  112. if( background == NULL )
  113. {
  114. return false;
  115. }
  116.  
  117. //If there was an error in loading the font
  118. if( font == NULL )
  119. {
  120. return false;
  121. }
  122.  
  123. //If everything loaded fine
  124. return true;
  125. }
  126.  
  127. void clean_up()
  128. {
  129. //Free the surfaces
  130. SDL_FreeSurface( background );
  131. SDL_FreeSurface( message );
  132.  
  133. //Close the font that was used
  134. TTF_CloseFont( font );
  135.  
  136. //Quit SDL_ttf
  137. TTF_Quit();
  138.  
  139. //Quit SDL
  140. SDL_Quit();
  141. }
  142.  
  143. int main( int argc, char* args[] )
  144. {
  145. //Quit flag
  146. bool quit = false;
  147.  
  148. //Initialize
  149. if( init() == false )
  150. {
  151. return 1;
  152. }
  153.  
  154. //Load the files
  155. if( load_files() == false )
  156. {
  157. return 1;
  158. }
  159.  
  160. //Render the text
  161. message = TTF_RenderText_Solid( font, "The quick brown fox jumps over the lazy hound", textColor );
  162.  
  163. //If there was an error in rendering the text
  164. if( message == NULL )
  165. {
  166. return 1;
  167. }
  168.  
  169. //Apply the images to the screen
  170. apply_surface( 0, 0, background, screen );
  171. apply_surface( 0, 200, message, screen );
  172.  
  173. //Update the screen
  174. if( SDL_Flip( screen ) == -1 )
  175. {
  176. return 1;
  177. }
  178.  
  179. //While the user hasn't quit
  180. while( quit == false )
  181. {
  182. //While there's events to handle
  183. while( SDL_PollEvent( &event ) )
  184. {
  185. //If the user has Xed out the window
  186. if( event.type == SDL_QUIT )
  187. {
  188. //Quit the program
  189. quit = true;
  190. }
  191. }
  192. }
  193.  
  194. //Free surfaces and font then quit SDL_ttf and SDL
  195. clean_up();
  196.  
  197. return 0;
  198. }
AddThis Social Bookmark Button
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:39 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC