RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 2458 | Replies: 14
Reply
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  
Join Date: Apr 2006
Location: Canada
Posts: 4,545
Reputation: John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all 
Rep Power: 17
Solved Threads: 284
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: sdl_ttf not working

  #2  
Apr 1st, 2007
Are you sure you have the SDL TTF runtime libraries installed properly? The code looks fine at first glance, but I don't really have time to run this code on my system right now, so I might in a few hours.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
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

Re: sdl_ttf not working

  #3  
Apr 1st, 2007
Are you sure you have the SDL TTF runtime libraries installed properly?
Apt-get says that I have the following packages installed that relate to ttf:
libfreetype6, libsdl-ttf2.0-0, libsdl-ttf2.0-dev, libttf2, and libttf-dev 
The strange thing is I have compiled The Battle for Wesnoth, Sauerbraten, and Action Cubers fine, all of which use ttf fonts. So is it the code? (sigh) My debian install seems rife with problems...
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 4
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: sdl_ttf not working

  #4  
Apr 1st, 2007
Your header paths imply that they are a in a sub directory of your current directory.
Is that true?
In other words, if your current directory is
*/foo , the program will look for those headers in */foo/SDL
Otherwise the path is incomplete and/or incorrect.

From what you posted , I suspect that htses files may not exisit or are not where the program is looking for them.


do a dbupdate command (HD will churn for a few minutes) then type
locate SDL.h
This will return the path to that header if it exists.
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 4
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: sdl_ttf not working

  #5  
Apr 1st, 2007
Originally Posted by JRM View Post
do a dbupdate command (HD will churn for a few minutes) then type
locate SDL.h
This will return the path to that header if it exists.


Sorry, My dyslexia kicked in for a second...
the command is updatedb
Reply With Quote  
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

Re: sdl_ttf not working

  #6  
Apr 1st, 2007
ok I located the SDL stuff, its in /usr/include/SDL/*.
So it should be in the specified directory. SDL_ttf.h
is in there..I wonder why its acting up..
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 4
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: sdl_ttf not working

  #7  
Apr 1st, 2007
Naw, just fix the paths in the include directive.\
Use the full path names, unless you have some compelling reason to do otherwise...
Reply With Quote  
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

Re: sdl_ttf not working

  #8  
Apr 1st, 2007
Im not sure I understand. All the SDL stuff is in /usr/include/SDL/ . So I would use SDL/foo as my includes, correct?
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,545
Reputation: John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all 
Rep Power: 17
Solved Threads: 284
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: sdl_ttf not working

  #9  
Apr 1st, 2007
Originally Posted by Sturm View Post
Im not sure I understand. All the SDL stuff is in /usr/include/SDL/ . So I would use SDL/foo as my includes, correct?

No. Try changing your inclusions "s with < and >, because the directory that your SDL stuff resides in is a system directory, not a home one.

Although that's very unlikely to be what's causing you problems, as the code probably wouldn't even compile if the headers aren't being included.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 4
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: sdl_ttf not working

  #10  
Apr 1st, 2007
just chang all the include directives to absolute paths.
the first one would read:
#include "/usr/include/SDL/SDL.h"
which is the the absolute path name for THAT header. Do the rest of them using the same directory path. There is no doubt this way...

The compliler should be able to find the headers then. No gurantees that there will be no more problems, but at least that situation is handled.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:28 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC