# extern/windows programming question

This topic is 4459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I'm currently using the windows implementation provided by the folk at nehe in my little shooter game. At the moment i'm trying to create a font class which for defining a new font then being able to print text with the font. The problem is I need access to my Device Context handle but that's in a different file. I've got a few source files windows.cpp which holds all the windows code (the hDC) and i've created a font class in not surprisingly font.h and .cpp. I tried taking the variables from the top of the windows.cpp file and putting them into a windows.h file but that seems to screw everthing up.

THE FONT
unsigned int Font::Create_Font(char *fontName, int fontSize)
{
// Windows font.
HFONT hFont;
unsigned int base;

// Create space for 96 characters.
base = glGenLists(96);

if(stricmp(fontName, "symbol")==0)
{
hFont = CreateFont(   -24,
0,
0,
0,
FW_BOLD,
FALSE,
FALSE,
FALSE,
SYMBOL_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE | DEFAULT_PITCH,
fontName
);
}
else
{
hFont = CreateFont(	-24,
0,
0,
0,
FW_BOLD,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE | DEFAULT_PITCH,
fontName
);
}

if(!hFont) return 0;

SelectObject(hDC, hFont);
wglUseFontBitmaps(hDC, 32, 96, base);

return base;
}

AND THE BIT OF WINDOWS CODE FROM THE TOP OF WINDOWS.CPP

HDC		hDC            =NULL;		// Private GDI Device Context
HGLRC		hRC	       =NULL;		// Permanent Rendering Context
HWND		hWnd	       =NULL;		// Holds Our Window Handle
HINSTANCE	hInstance;			// Holds The Instance Of The Application


One of my mates mentioned that I might be able to do something with externs to make the hDC a totally global variable or something but I haven't got the first clue about that. Cheers for advice and help. Neil [Edited by - bapti on December 4, 2005 11:40:49 AM]

##### Share on other sites
well if I remember the nehe tutorials correctly the hdc is allready a global so yes you declare it as extern and use it in your font file:).

That may offend teh sensibilities of some people on this list but it will let you get going on your font renderer!

Cheers
Chris

##### Share on other sites
sorry to be a pest but I don't really know how to define this extern stuff. This is what I tried failed

extern  HDC	hDC	=NULL;		// Private GDI Device ContextI did see this when I was looking through other posts which had extern in themextern 'C'{}

Do I also have to make changes to my font class and declare a hDC at the top of the font.cpp or something like that? I'm just picking up tid-bits from other posts i've read.

##### Share on other sites
You can't initialize an extern - an extern is like a prototype. So, yes, declare
extern HDC		hDC;
in the top of font.cpp (Thus removing =NULL)

extern 'C' { }
Is actually a way to export C functions and code, ie. in a C++ library, where you want a program written in C to be able to access certain wrapper functions, etc.

##### Share on other sites
It would be better to pass hDC as a parameter to Font::Create_Font.