Font class, why won't it work?

This is a discussion on Font class, why won't it work? within the Game Programming forums, part of the General Programming Boards category; Hmm ... I thought this would be simple, but so often are the simple things the ones that take up ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    35

    Unhappy Font class, why won't it work?

    Hmm ... I thought this would be simple, but so often are the simple things the ones that take up whole days...

    All I want to do is wrap up my fonts (just Windows fonts) in their own class for a game. Piece of cake! Nothing but a bunch of fonts contained in a class, an Init routine which builds the different fonts, and a destructor that deletes them.

    Everything appears to be fine except that the wrapped-up "CreateFont" function doesn't seem to be working properly. The following is a public member function of the class:

    Code:
    HFONT GameFonts::MakeGameFont(int size, bool italic, char* style, int weight)
    {
       HFONT theFont = CreateFont(size, 0, 0, 0, weight,
                                  italic, false, false, ANSI_CHARSET,
                                  OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
                                  PROOF_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
                                  style);
       return theFont;
    }
    This function is called in the Init routine like so:

    Code:
    bool GameFonts::Init(HDC hdc)
    {
       bool success = true;
       hfScoreFont = MakeGameFont(-20, true, "times new roman", FW_BOLD);
          if(!hfScoreFont) success=false;
       hfLevelFont = MakeGameFont(-15, false, "arial", FW_BOLD);
    ...
    Don't mind all the "bool success" stuff, that's just me trying to fingure out what's going on.

    Well, the bottom line is this: Something is happening. If I go into the "CreateFont" function and put in the values for size, italic, etc (instead of using the values passed in) then it works, but for some reason the function's not taking the values I pass in! All the fonts are exactly the same! What's up with this? This same function worked perfectly when it was global.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    Okay, sorry, update:

    The problem's not with the wrapped function, it's with selecting the fonts. For some reason all the fonts wind up being just one of the fonts, the Menu Items font. Why? Dunno why. I select all the fonts in the same way, like this:

    Code:
       SelectObject(hdc, (HFONT)gameFonts.hfMenuItFont);
    So I've got about 10 different fonts and I'm selecting them in this manner throughout the game, but every single one turns out to be this blasted hfMenuItFont! What's up with this? It's not even as if hfMenyItFont is the first font I select. Then I'd say, Okay, somehow the selecting's not working. But before I even select hfMenuItFont, in the same function, I select the header font, like so:
    Code:
       SelectObject(hdc, (HFONT)gameFonts.hfMenuHdFont);
    So despite the fact that my program has never even seen the hfMenuItFont, the heading winds up being in that font anyway. Now I'm thinking, is there some strange memory corruption going on? What's happening? Here's the entire declaration for the class:
    Code:
    #ifndef GAME_FONTS_H
    #define GAME_FONTS_H
    
    #include <windows.h>
    
    class GameFonts
    {
    public:
       GameFonts(){ }
       ~GameFonts();
       bool Init(HDC);
       HFONT MakeGameFont(int, bool, char *, int=400 );
    
       HDC * p_hdc;
       HFONT hfScoreFont;
       HFONT hfLevelFont;
       HFONT hfAdvLevFont;
       HFONT hfMenuHdFont;
       HFONT hfMenuItFont;
       HFONT hfTinyFont;
       HFONT hfBodyFont;
       HFONT hfFooterFont;
       HFONT hfGetRdyFont;
       HFONT hfGameOvFont;
       HFONT hfOldFont;
    
    };
    
    
    
    #endif // GAME_FONTS_H

    It seems as if the font is being selected only the first/second time, and then the SelectObject calls cease to have any effect. I really have no idea what's going on. I'm assuming that an HFONT is a set size and that assigning stuff to them doesn't change the size of the member data. But maybe I'm wrong.
    Last edited by fusikon; 02-09-2003 at 06:09 PM.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    Yay! Got it!

    The solution was to pass in the address of the font class object, and then deference it:

    Code:
       SelectObject(hdc, gameFonts->hfScoreFont);
    Before I was passing the gameFonts object by value. I hadn't thought that would matter but I guess it did. I guess it would have helped you all if I'd stated that I was passing the object into other functions in the first place...

    Got another question, I'll put up the thread in a moment...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 05:30 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. CEdit::OnChar() won't work for derived class!
    By LuckY in forum Windows Programming
    Replies: 0
    Last Post: 02-28-2003, 01:01 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21