Sign in to follow this  
Retrothomas

Initializing enums, specifically TTF_Font in SDL

Recommended Posts

Retrothomas    100
I'm having trouble setting up an object/enum for a TTF_Font in SDL. I'm following Lazyfoo's tutorial here:
http://lazyfoo.net/SDL_tutorials/lesson07/index.php

I'm trying to declare a TTF_Font in my header file so I can set the font in the constructor and use it in the draw method. I'm doing this:
TTF_Font* font;

Which doesn't work. I realize TTF_Font is an enum and from what I understand you can't declare enums like you can objects or other variables. So how can I create a variable to hold the font and set it up?

Thanks in advance!

Share this post


Link to post
Share on other sites
Roots    1625
"Which doesn't work" is not helpful information. Is it a compiler error? A run-time error? What messages are printed to the output of your compilation or execution before the failure?


I'm going to take a complete stab in the dark and say the problem is you're not including the SDL_ttf library header file, so its not finding the TTF_Font struct (its a struct, [i]not[/i] an enum). If its not that, then you're not linking the TTF library correctly in your build.


This topic sounds like it belongs in the For Beginners section of the forums. This problem doesn't have anything to do with SDL or SDL_ttf really, at least from what I can gather.

Share this post


Link to post
Share on other sites
Retrothomas    100
Thanks for your reply, Roots. I posted after a long troubleshooting session and my brain had turned to mush! Let me clarify:

The specific error I received while compiling is "[font=Monaco]error: ISO C++ forbids declaration of 'TTF_Font' with no type[/font]"

I'm confident I have the SDL_TTF framework linked properly. I used the same procedure to link that and the SDL_Image framework and SDL_Image works fine. I have the header file included for SDL_TTF because I'm able to call TTF_Init(). If I remove the SDL_ttf header then I can't call TTF_init().

I just solved the problem, though. I had included the SDL_ttf header in another file and not the one for the class I was creating the SDL_ttf struct in.

Sometimes I miss something simple like that after programming for a while. I thought it was already included since it was in a global header but I missed it somehow. Thanks again :)

Share this post


Link to post
Share on other sites
rip-off    10979
The problem is the compiler does not see the declaration of TTF_Font at the point you are using it. So you are including SDL_ttf.h in a manner that prevents the compiler from seeing this declaration when needed - which is in a particular header file. By the time you get inside a source file this is often worked out. See [url="http://www.gamedev.net/topic/602246-c-compilation-bug/page__view__findpost__p__4812398"]here[/url] for an example of how an apparently sane header setup confuses the compiler once the code has been preprocessed.

I recommend reading [url="http://www.gamedev.net/page/resources/_/reference/programming/languages/c-and-c/organizing-code-files-in-c-and-c-r1798"]this article[/url], as structuring your headers and source files correctly should solve this problem.

Share this post


Link to post
Share on other sites
Retrothomas    100
[quote name='rip-off' timestamp='1305716551' post='4812462']
The problem is the compiler does not see the declaration of TTF_Font at the point you are using it. So you are including SDL_ttf.h in a manner that prevents the compiler from seeing this declaration when needed - which is in a particular header file. By the time you get inside a source file this is often worked out. See [url="http://www.gamedev.net/topic/602246-c-compilation-bug/page__view__findpost__p__4812398"]here[/url] for an example of how an apparently sane header setup confuses the compiler once the code has been preprocessed.

I recommend reading [url="http://www.gamedev.net/page/resources/_/reference/programming/languages/c-and-c/organizing-code-files-in-c-and-c-r1798"]this article[/url], as structuring your headers and source files correctly should solve this problem.
[/quote]

Thank you very much! I was doing most of the things in the article (resolving cyclic dependencies, using header files, and using #ifndef) but I tried to include too many things in a global header. I think I'll have to include files more locally in the other classes that need them and that should resolve this type of issue better.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this