Skip to main content
gamedev.net
🔒 Locked

extern/windows programming question

Started by bapti Dec 4, 2005 at 10:38 AM 3 replies 450+ views
Original Post
bapti
bapti
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]
chollida1
chollida1
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
CheersChris
bapti
bapti
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.
crusadingknight
crusadingknight
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.
JohnBolton
JohnBolton
It would be better to pass hDC as a parameter to Font::Create_Font.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.