SDL font not showing

Pages: 12
closed account (96AX92yv)
hello,
I am quite new to the SLD_ttf lib and I have been following the Lazy Foo' tuts on it but the text is not appearing. I have found out that it is not loading the font as I have added in the code saying if it has failed.
Code:
font = TTF_OpenFont( "lazy.ttf", 28 );
if( font == NULL )
{
return false;
}

i have saved this font in the project (project name/project name/lazy.ttf). I am wondering if any1 knows how to fix this problem

thanks in advance
Have you initialized SDL_ttf? (using TTF_Init() ? )

Also, make sure to have the font in the same directory as the generated executable, not just as your project. So if your generated executable is in, e.g. : " project name/project name/Debug/ ", your font should be :

project name/project name/Debug/lazy.ttf


In VS, which I assume the OP is using due to the typical project path, the working directory is usually not the one the executable is in. By default, it's the directory the project file is in.

What I would suggest doing is creating a file in your code that you know doesn't exist

ofstream out("test");

then checking to see whether that file is created in the same directory where lazy.ttf is residing. If it is, then lazy.ttf is in the correct directory. If it's not, then lazy.ttf is not in the correct directory.
Good point cire, I agree, that would definitely clarify the issue. I am using Visual Studio myself and I have noted that running a compiled program from inside Visual Studio and running it outside of Visual Studio can produce different behavior regarding the working directory.

I find my default working directory for a VS project is specified in Project Properties -> Configuration Properties -> Target Name (which is set as the macro $(ProjectName) by default).

Last edited on
I find my default working directory for a VS project is specified in Project Properties -> Configuration Properties -> Target Name (which is set as the macro $(ProjectName) by default).


Well, that's the name of the executable file. For the Junk project $(ProjectName) is "Junk"

The working directory is found in

Project Properties -> Configuration Properties -> Debugging -> Working Directory

and it is the macro $(ProjectDir) by default. Of course, you can check the value of the macro by clicking on the dropdown list for the option and choosing <edit> then clicking the Macros >> button in the resulting dialog. The macros and what they expand to are all listed. You just have to scroll down to the one you want to find.

And yes, if you run the executable outside of the IDE, the working directory will be the directory the executable is in.
closed account (96AX92yv)
what is the include for ofstream out. it has come up with an error when i added it.
1
2
#include <iostream>
#include <fstream> 


An example from: http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

This example simply opens and closes a file. [...] If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// using ofstream constructors.
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  ofstream outfile ("test.txt");

  // >> i/o operations here <<

  outfile.close();

  return 0;
}
Last edited on
closed account (96AX92yv)
thanks but it is showing that the lazy.ttf is in the right place. Any other ideas for SDL_ttf to open the font?
Try using another TTF file, just to make sure you have a valid ttf file there.
closed account (96AX92yv)
still doesnt work

EDIT:
this also is happening in SDL_image. i have
image = IMG_Load( filename.c_str() );
but it is not loading
Last edited on
For now, try using absolute paths in your loading functions and see if your program works properly.

e.g.
 
font = TTF_OpenFont( "c:\\users\\user1\\desktop\\lazy.ttf", 28 );


If it does, you can then troubleshoot the problem with the relative paths.

Did these functions (SDL_image, SDL_ttf, etc) work for you previously?

closed account (96AX92yv)
this method did not work. I have never got SDL_ttf and image to work but i have been following the lazy foo tuts so i dont know why it is not working
Did you call SDL_Init ?
closed account (96AX92yv)
SDL_Init(SDL_INIT_VIDEO);?

do you need to quit SDL_image and SDL_ttf?
To be sure:
1
2
3
4
5
SDL_Init(SDL_INIT_EVERYTHING);

//...

SDL_Quit();


Yes, you have to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Initialize SDL_ttf
TTF_Init();

//declare a TTF_Font
TTF_Font *font;

//load the font
font = TTF_OpenFont( "c:\\users\\user1\\desktop\\lazy.ttf", 28 );


//...


//Close the font
TTF_CloseFont(font); 

//Quit SDL_ttf 
TTF_Quit();

// and then probably followed by SDL_Quit(); 


I don't think SDL_image has initialization and quit functions.


Also, make sure that you have your libraries probably set up: http://www.lazyfoo.net/SDL_tutorials/lesson03/windows/msvsnet2010e/index.php
Last edited on
I don't think SDL_image has initialization and quit functions.

SDL_image is initialized within SDL_init so it shouldn't require any more function calls.
closed account (96AX92yv)
thanks SDL_TTF works now but SDL_image still doesnt work. is there something like Init that is compatable with image?
Last edited on
closed account (96AX92yv)
ive got another problem with SDL :(
i made the screen bigger (640 x 480 to 1920 x 1080) which worked but then when an object moves outside the original area (640 x 480) the object seems to not get rid of its old position. it still moves but make a long line of where it has previously been. any ideas on how to fix it?
EssGeEich wrote:
SDL_image is initialized within SDL_init so it shouldn't require any more function calls.
Thank you for verifying this EssGeEich, I suspected that much but wasn't sure.

For SDL_image:

1
2
3
4
5
6
7
8
SDL_Surface* mySurface;

mySurface = IMG_Load("myImage.png");

//...

SDL_FreeSurface(mySurface);


ive got another problem with SDL :(
i made the screen bigger (640 x 480 to 1920 x 1080) which worked but then when an object moves outside the original area (640 x 480) the object seems to not get rid of its old position. it still moves but make a long line of where it has previously been. any ideas on how to fix it?


Sounds like the screen is not cleared properly and frames are drawn on top of each other. It probably has to do with how you call SDL_SetVideoMode(). See the SDL_RESIZABLE flag for SDL_SetVideoMode().

http://sdl.beuc.net/sdl.wiki/SDL_SetVideoMode
Last edited on
closed account (96AX92yv)
SDL_image
that is what i have for it but it doesnt work

SDL_SetVideoMode()
i tried all of them but none of them worked

is there any other ways to fix these problems
Pages: 12