• Advertisement

Archived

This topic is now archived and is closed to further replies.

Need help with HDC over multiple files :/

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay... here's what I've done, it's probably a very simple problem but right now I can't wrap my head around it. I have the main.cpp file of a program Ive made as just the same as nehe's lesson 1 codebase. But I've put the font classes (lesson 13) in a seperate file called font.cpp and font.h. In font.h I declared the necessary functions..
GLvoid BuildFont(GLvoid);
GLvoid KillFont(GLvoid);
GLvoid glPrint(const char *fmt, ...);
   
And then I defined the full functions in font.cpp
GLuint	base;			// Base Display List For The Font Set

GLvoid BuildFont(GLvoid)					// Build Our Bitmap Font
{
	HFONT	font;						// Windows Font ID
	HFONT	oldfont;					// Used For Good House Keeping

	base = glGenLists(96);					// Storage For 96 Characters ( NEW )

	font = CreateFont(	-24,				// Height Of Font ( NEW )
		0,				// Width Of Font
		0,				// Angle Of Escapement
		0,				// Orientation Angle
		FW_BOLD,			// Font Weight
		FALSE,				// Italic
		FALSE,				// Underline
		FALSE,				// Strikeout
		ANSI_CHARSET,			// Character Set Identifier
		OUT_TT_PRECIS,			// Output Precision
		CLIP_DEFAULT_PRECIS,		// Clipping Precision
		ANTIALIASED_QUALITY,		// Output Quality
		FF_DONTCARE|DEFAULT_PITCH,	// Family And Pitch
		"Courier New");				// Font Name
	
	oldfont = (HFONT)SelectObject(hDC, font);		// Selects The Font We Want
	wglUseFontBitmaps(hDC, 32, 96, base);			// Builds 96 Characters Starting At Character 32
	SelectObject(hDC, oldfont);						// Selects The Font We Want
	DeleteObject(font);								// Delete The Font
}

GLvoid KillFont(GLvoid)								// Delete The Font List
{
 	glDeleteLists(base, 96);						// Delete All 96 Characters ( NEW )
}

GLvoid glPrint(const char *fmt, ...)				// Custom GL "Print" Routine
{
	char		text[256];							// Holds Our String
	va_list		ap;									// Pointer To List Of Arguments

	if (fmt == NULL)								// If There's No Text
		return;										// Do Nothing

	va_start(ap, fmt);								// Parses The String For Variables
	vsprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers
	va_end(ap);										// Results Are Stored In Text

	glPushAttrib(GL_LIST_BIT);						// Pushes The Display List Bits		( NEW )
	glListBase(base - 32);							// Sets The Base Character to 32	( NEW )

	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text	( NEW )
	glPopAttrib();									// Pops The Display List Bits	( NEW )
}
   
I've linked in all the necessary libs and headers e.t.c (not shown above). Thing is.... u'll notice that the above code in font.cpp calls to HDC in the buildfont function... this generates an error in the compiler that looks like this. "error C2065: 'hDC' : undeclared identifier" HOWEVER.... if I define it here too, I get multiple hdc errors (already defined e.t.c e.t.c). Any chance someone can help me out with this. I just need to be able to use HDC in main.cpp (for rendering the window) and in font.cpp (for building the font). All help hugely appreciated... and sorry for the large code pastes above, but I felt posting everything was the best idea. (and before anyone says "are you spelling it with right capitals e.t.c..." yes I am just to clear up that). Thanks in advance... ~Cobra~ Edited by - Cobra on February 4, 2002 3:42:06 PM Edited by - Cobra on February 4, 2002 3:43:33 PM

Share this post


Link to post
Share on other sites
Advertisement
you should declare the hDC variable ''extern'' in a header and include that header in any cpp that must use the variable. Then you define the variable in exactly one cpp file.

e.g.
  
// in font.h

#include <windows.h>

extern HDC hDC;
// and other stuff...


  
// in font.cpp

#include "font.h"

HDC hDC = NULL;

Share this post


Link to post
Share on other sites
my suggestion :

in font.h, declare :
extern HDC hDC;

in main.cpp, define :
HDC hDC=NULL;

Make sure that font.cpp does NOT include the file main.cpp (eg via #include "main.cpp").

hth

Share this post


Link to post
Share on other sites
ok Mr Dactylos, you were faster than me from a few seconds

anyhow, definition in main.cpp or in font.cpp do the same. The important thing is NOT to declare it twice, eg you choose main.cpp OR font.cpp, not BOTH.

Share this post


Link to post
Share on other sites
=(

I spoke too soon. Seems my 3d text works fine, but my 2d text doesnt. *sniffle*

3d text works 100% perfectly, but for some reason the 2d text doesnt even show up.. (I''m not accidently overwriting the array with the 3d text or anything like that, it''s just not showing =()

Any idea''s ? (the text code I''m using is all in the first post.

Share this post


Link to post
Share on other sites
Is the 2D text drawn from NeHe''s Bitmap fonts ?
If so, you have to locate the text with glRasterPos2d (instead of glTranslatef) and you have to take care that thisposition is INSIDE the screen, otherwise the position will be set as invalid and OpenGL will not display the text.

Share this post


Link to post
Share on other sites
Thx loads... I didnt know I had to use glRasterPos2f. Much appreciated. Works great now. (hmm now to fix my fps counter.. flashes on and off.. wierd stuff.. but I think I can crack this one on my own.. w00t)

:D

Edited by - Cobra on February 5, 2002 9:20:31 AM

Share this post


Link to post
Share on other sites

  • Advertisement