Skip to main content
GameDev.net gamedev.net
🔒 Locked

HDC Invalid?

Started by AbsolutDew Dec 6, 2002 at 12:31 PM 3 replies 1.7k views
Original Post
AbsolutDew
AbsolutDew
Hi, I'm trying to write a font class similiar to the one in the opengl game programming book.
      
        void generate( std::string type, int height, int weight )
        {
            base_ = glGenLists( 96 );

	        HFONT font = CreateFont( -height,
						             0, 0, 0,
						             weight,
						             FALSE, FALSE, FALSE,
						             ANSI_CHARSET,
						             OUT_TT_PRECIS,
						             CLIP_DEFAULT_PRECIS,
						             ANTIALIASED_QUALITY,
						             FF_DONTCARE | DEFAULT_PITCH,
						             type.c_str() );


            HDC hdc = wglGetCurrentDC();            
            
	        SelectObject( hdc, font )

	        wglUseFontBitmaps( hdc, 32, 96, base_ )
        }
  
  
Well my problem is that both SelectObject and wglUseFontBitmap fail. GetLastError returns 6, which is invalid handle. Why is this if wglGetCurrentDC executes without error? I dropped the class into the tutorial that covered fonts, and it works fine, so i assume the problem lies somewhere in the initilazation of my engine?
      
engine::engine( std::string title, RECT viewport )
    {
        HINSTANCE hInst = GetModuleHandle( NULL );

        WNDCLASSEX WndClass =
        {
            sizeof( WNDCLASSEX ),               // cbSize

            CS_HREDRAW | CS_VREDRAW,            // style

            WndProc,                            // lpfnWndProc

            0, 0,                               // cbClsExtra, cbWndExtra

            hInst,                              // hInstance

            LoadIcon( NULL, IDI_APPLICATION ),  // hIcon

            LoadCursor( NULL, IDC_ARROW ),      // hCursor

            (HBRUSH)GetStockObject( BLACK_BRUSH ),  // hbrBackground

            NULL,                               // lpszMenuName

            "lpszClassName",                    // lpszClassName

            LoadIcon( NULL, IDI_WINLOGO )       // hIconSm

        };

        RegisterClassEx( &WndClass );

        viewport_ = viewport;

	    hwnd_ = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, // dwExStyle

                                "lpszClassName",            // lpClassName

                                title.c_str(),              // lpWindowName

                                WS_OVERLAPPEDWINDOW,        // dwStyle

                                viewport.left, viewport.top,    // x, y

                                viewport.right, viewport.bottom,    // nWidth, nHeight

		                        NULL,                       // hWndParent

		                        NULL,                       // hMenu

		                        hInst,                      // hInstance

		                        NULL );                     // lpParam



        hdc_ = GetDC( hwnd_ );

        init_gl();

        ShowWindow( hwnd_, SW_SHOW );
        SetForegroundWindow( hwnd_ );
        SetFocus( hwnd_ );
    }

    void engine::init_gl()
    {
        PIXELFORMATDESCRIPTOR descriptor;

        ZeroMemory( &descriptor, sizeof( PIXELFORMATDESCRIPTOR ) );
        descriptor.nSize = sizeof( PIXELFORMATDESCRIPTOR );
        descriptor.nVersion = 1;
        descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        descriptor.iPixelType = PFD_TYPE_RGBA;
        descriptor.cColorBits = 32;
        descriptor.cDepthBits = 16;
        descriptor.iLayerType = PFD_MAIN_PLANE;

        int format = ChoosePixelFormat( hdc_, &descriptor );

        SetPixelFormat( hdc_, format, &descriptor );

        hrc_ = wglCreateContext( hdc_ )

        wglMakeCurrent( hdc_, hrc_ );

        resize( viewport_ );
    
        glShadeModel( GL_SMOOTH );
        glEnable( GL_BLEND );

        glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
        glClearDepth( 1.0f );
        glEnable( GL_DEPTH_TEST );
        glDepthFunc( GL_LEQUAL );

        font_system = new font();
    }
      
Sorry about the length, but im not sure what's causing it. I removed all the error checking to try to hack down the size, but everything was fine except those two functions i mentioned. [edited by - absolutdew on December 6, 2002 1:40:04 PM]
niyaw
niyaw
is your font variable valid after CreateFont call?
VC6-OGL
VC6-OGL
Here is a simple Font Raster:

#include <windows.h>
#include <gl/glut.h>
#include <stdio.h>

GLubyte space[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

GLubyte letters[] [13] = {
{ 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0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, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x7e },
{ 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
{ 0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c },
{ 0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
{ 0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e },
{ 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff },
{ 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
{ 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
{ 0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
{ 0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3 },
{ 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3 },
{ 0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff }
};

GLuint fontOffset;

void makeRasterFont( void ) {
GLuint i, j;

glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );

fontOffset = glGenLists ( 128 );
for ( i = 0, j = ''A''; i < 26; i++, j++ ) {
glNewList ( fontOffset + j, GL_COMPILE );
glBitmap ( 8, 13, 0.0, 2.0, 10.0, 0.0, letters );
glEndList();
}
glNewList ( fontOffset + '' '', GL_COMPILE );
glBitmap ( 8, 13, 0.0, 2.0, 10.0, 0.0, space );
glEndList();
}

void init( void ) {
glShadeModel ( GL_SMOOTH );
makeRasterFont();
printf ( "Program_25 - Text Display.\n" );
}

void printString( char *s ) {
glPushAttrib ( GL_LIST_BIT );
glListBase ( fontOffset );
glCallLists ( strlen( s ), GL_UNSIGNED_BYTE, ( GLubyte * )s );
glPopAttrib();
}

void display( void ) {
GLfloat white[3] = { 1.0, 1.0, 1.0 };

glClear ( GL_COLOR_BUFFER_BIT );
glColor3fv ( white );

glRasterPos2i ( 20, 100 );
printString ( "OPENGL RASTER FONT" );
glRasterPos2i ( 20, 60 );
printString ( "THE QUICK BROWN FOX JUMPS" );
glRasterPos2i ( 20, 40 );
printString ( "OVER A LAZY DOG" );
glFlush();
}

void reshape( int h, int w ) {
glViewport ( 0, 0, ( GLsizei ) w, ( GLsizei ) h);
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
glOrtho ( 0, w, 0, h, -1.0, 1.0 );
glMatrixMode ( GL_MODELVIEW );
}

void keyboard( unsigned char key, int x, int y ) {
switch ( key ) {
case 27:
exit( 0 );
}
}

int main( int argc, char** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize ( 300, 300 );
glutInitWindowPosition ( 100, 100 );
glutCreateWindow ( "Font Raster" );
init();
glutReshapeFunc ( reshape );
glutKeyboardFunc ( keyboard );
glutDisplayFunc ( display );
glutMainLoop();
return 0;
}

Hope that helps.
AbsolutDew
AbsolutDew
Yes, the font is valid.

For some reason I commented out the glHint functions and it works...

If I leave any of them it fails on SelectObject. Why the heck would those calls break it? I''m confused

Jan Wassenberg
Jan Wassenberg
You aren''t supposed to use GDI stuff with double buffered pixel formats. Dunno why it breaks with glHint, though.

I''d recommend (if size isn''t critical) you render the font into a texture beforehand, and then draw with textured quads in a display list: faster and portable.
I can send you some code to get you started, if you''re interested.

HTH
Jan
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

Topic Locked

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

Sign in to reply to this topic.