GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


CreateWindowEX() call always retuns NULL by Lerp^_
1
 
RenderWindow::RenderWindow( LPSTR strTitle, int iX, int iY, int iWidth, 
                            int iHeight, int iBpp, COLORREF clrBackground, bool bFullscreen )
    : m_bActive( true ), m_bUpdate( true ), m_iWidth( iWidth ), m_iHeight( iHeight ),
      m_bFullscreen( bFullscreen ), m_iBpp( iBpp )
{
    ofstream fut( "Temp.txt" );
    HINSTANCE hInst = GetModuleHandle( NULL );
    
    fut << iWidth << " " << iHeight << " " << iBpp << endl;
    RECT rcWindow;
    WNDCLASS m_wc;

    fut << "Start" << endl;
    rcWindow.left    = (long)0;
    rcWindow.right    = (long)m_iWidth;
    rcWindow.top    = (long)0;
    rcWindow.bottom    = (long)m_iHeight;

    m_wc.cbClsExtra        = 0;
    m_wc.cbWndExtra        = 0;
    m_wc.hInstance        = hInst;
    m_wc.hIcon            = NULL;
    m_wc.hCursor        = (HCURSOR)LoadCursor( NULL, IDC_ARROW );
    m_wc.hbrBackground    = NULL;
    m_wc.lpszMenuName    = NULL;
    m_wc.style            = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
    m_wc.lpfnWndProc    = (WNDPROC)GlobalMsgProc;    
    m_wc.lpszClassName    = "dsEngine";

    fut << "Start2" << endl;

    if( !RegisterClass( &m_wc ) )
    {
        fut << "Start3" << endl;
        throw CException( __TIME__, __FILE__, __LINE__, 
            E_CRITICAL, "Could not register window class." );
    }

    fut << "Start4" << endl;
    if( bFullscreen )
    {
        fut << "Start5" << endl;
        DEVMODE dmScreenSettings;
        memset( &dmScreenSettings, 0, sizeof( dmScreenSettings ) );
        
        dmScreenSettings.dmSize = sizeof( dmScreenSettings );
        dmScreenSettings.dmPelsWidth    = m_iWidth;
        dmScreenSettings.dmPelsHeight    = m_iHeight;
        dmScreenSettings.dmBitsPerPel    = m_iBpp;
        dmScreenSettings.dmFields        = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

        LONG lReturnValue;
        lReturnValue = ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN );
        if( lReturnValue != DISP_CHANGE_SUCCESSFUL )
        {
            fut << "Start6" << endl;
            g_DisplaySettingsError( lReturnValue );
        }
    }

    fut << "Start7" << endl;
    if( m_bFullscreen )
    {
        fut << "Start8" << endl;
        dwExStyle = WS_EX_APPWINDOW;
        dwStyle = WS_POPUP;
        ShowCursor( FALSE );
    }
    else
    {
        fut << "Start9" << endl;
        dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
        dwStyle = WS_OVERLAPPEDWINDOW;
    }

    AdjustWindowRectEx( &rcWindow, dwStyle, FALSE, dwExStyle );

    m_hWnd = CreateWindowEx(    dwExStyle,
                                "dsEngine",
                                strTitle,
                                dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
                                iX, iY,
                                rcWindow.right - rcWindow.left,
                                rcWindow.bottom - rcWindow.top,
                                NULL,
                                NULL,
                                hInst,
                                NULL );
    if( !m_hWnd )
    {
        fut << "NULL" << endl;
        fut << GetLastError() << endl;
        Destroy();
        throw CException( __TIME__, __FILE__, __LINE__ );
    }

    if( !SetWindowLong( m_hWnd, GWL_USERDATA, long( this ) ) )
    {
        fut << "SWL" << endl;
        throw CException( __TIME__, __FILE__, __LINE__, 
                E_NORMAL );
    }

    static PIXELFORMATDESCRIPTOR PixelFormatDesc = {
        sizeof( PIXELFORMATDESCRIPTOR ),    // Size of Pixel Format Descriptor
        1,                                    // Version Number
        PFD_DRAW_TO_WINDOW |                
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER |
        PFD_TYPE_RGBA,
        m_iBpp,                                // Color Depth
        0, 0, 0, 0, 0, 0,                    // Color Bits Ignored
        0,                                    // Alpha Buffer
        0,                                    // Shift Bit Ignored
        0,                                    // Accumulation Buffer
        0, 0, 0, 0,                            // Accumlation Bits
        16,                                    // Z-Buffer (Depth Buffer)
        0,                                    // Stencil Buffer
        0,                                    // Auxiliary Buffer
        PFD_MAIN_PLANE,                        // Main Drawing Layer
        0,                                    // Reserved
        0, 0, 0                                // Layer Masks
    };
    
    m_hDC = GetDC( m_hWnd );
    if( m_hDC == NULL )
    {
        fut << "DC NULL" << endl;
        Destroy();
        throw CException( __TIME__, __FILE__, __LINE__, 
                E_CRITICAL, "Could Not Get Window Device Context" );
    }

    uiPixelFormat = ChoosePixelFormat( m_hDC, &PixelFormatDesc );
    if( !uiPixelFormat )
    {
        fut << "Pixel Format not found" << endl;
        Destroy();
        throw CException( __TIME__, __FILE__, __LINE__, 
                E_CRITICAL, "Could Not Find Suitable Pixel Format" );
    }

    if( !SetPixelFormat( m_hDC, uiPixelFormat, &PixelFormatDesc ) )
    {
        Destroy();
        throw CException( __TIME__, __FILE__, __LINE__, 
                E_CRITICAL, "Could Not Set The Pixel Format" );
    }
        

    m_hRC = wglCreateContext( m_hDC );
    if( m_hRC == NULL )
    {
        Destroy();
        throw CException( __TIME__, __FILE__, __LINE__, 
                E_CRITICAL, "Could Not Create GL Rendering Context" );
    }

    if( !wglMakeCurrent( m_hDC, m_hRC ) )
    {
        Destroy();
        throw CException( __TIME__, __FILE__, __LINE__, 
                E_CRITICAL, "Could Not Activate GL Rendering Context" );
    }

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

I pass in 800 x 600 x 32, and this is the exact code from NeHe basically but the CreateWindowEx() calls always returns a NULL to me.