C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2003, 03:53 PM   #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.
fusikon is offline   Reply With Quote
Old 02-09-2003, 04:24 PM   #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.
fusikon is offline   Reply With Quote
Old 02-09-2003, 09:07 PM   #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...
fusikon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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