VirtualBox

Changeset 11395 in vbox


Ignore:
Timestamp:
Aug 13, 2008 3:43:17 PM (16 years ago)
Author:
vboxsync
Message:

Fixed SetPixelFormat failure with NVIDIA drivers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/WINNT/Graphics/OpenGL/test/tstShOpenGL.cpp

    r8387 r11395  
    2121#include <GL/gl.h>
    2222#include <stdio.h>
    23 
    24 
    25 
     23#include <io.h>
     24#include <fcntl.h>
    2625
    2726// Enable OpenGL
     
    2928void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
    3029{
    31         PIXELFORMATDESCRIPTOR pfd;
    32         int format;
    33        
    34         // get the device context (DC)
    35         *hDC = GetDC( hWnd );
    36        
    37         // set the pixel format for the DC
    38         ZeroMemory( &pfd, sizeof( pfd ) );
    39         pfd.nSize = sizeof( pfd );
    40         pfd.nVersion = 1;
    41         pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    42         pfd.iPixelType = PFD_TYPE_RGBA;
    43         pfd.cColorBits = 24;
    44         pfd.cDepthBits = 16;
    45         pfd.iLayerType = PFD_MAIN_PLANE;
    46         format = ChoosePixelFormat( *hDC, &pfd );
    47         SetPixelFormat( *hDC, format, &pfd );
    48        
    49         // create and enable the render context (RC)
    50         *hRC = wglCreateContext( *hDC );
    51         wglMakeCurrent( *hDC, *hRC );
    52        
     30    PIXELFORMATDESCRIPTOR pfd;
     31    int format;
     32   
     33    // get the device context (DC)
     34    *hDC = GetDC( hWnd );
     35   
     36    // set the pixel format for the DC
     37    ZeroMemory( &pfd, sizeof( pfd ) );
     38    pfd.nSize = sizeof( pfd );
     39    pfd.nVersion = 1;
     40    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
     41    pfd.iPixelType = PFD_TYPE_RGBA;
     42    pfd.cColorBits = 24;
     43    pfd.cDepthBits = 16;
     44    pfd.iLayerType = PFD_MAIN_PLANE;
     45    format = ChoosePixelFormat( *hDC, &pfd );
     46    if (!SetPixelFormat( *hDC, format, &pfd ))
     47    {
     48        printf("SetPixelFormat(%d) failed, rc = %u\n", format, (unsigned)GetLastError());
     49    }
     50   
     51    // create and enable the render context (RC)
     52    *hRC = wglCreateContext( *hDC );
     53    if (hRC == INVALID_HANDLE_VALUE)
     54    {
     55        printf("wglCreateContext failed, rc = %d\n", GetLastError());
     56    }
     57
     58    if (!wglMakeCurrent( *hDC, *hRC ))
     59    {
     60        printf("wglMakeCurrent failed, rc = %d\n", GetLastError());
     61    }
    5362}
    5463
     
    5766void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
    5867{
    59         wglMakeCurrent( NULL, NULL );
    60         wglDeleteContext( hRC );
    61         ReleaseDC( hWnd, hDC );
     68    wglMakeCurrent( NULL, NULL );
     69    wglDeleteContext( hRC );
     70    ReleaseDC( hWnd, hDC );
    6271}
    6372
    64 int main(int argc, char **argv)
     73HWND CreateInvisWindow(HINSTANCE hInstance)
     74{
     75    WNDCLASS wc;
     76    HWND hWnd;
     77
     78    // register window class
     79    wc.style = CS_OWNDC;
     80    wc.lpfnWndProc = DefWindowProc;
     81    wc.cbClsExtra = 0;
     82    wc.cbWndExtra = 0;
     83    wc.hInstance = hInstance;
     84    wc.hIcon = NULL;
     85    wc.hCursor = NULL;
     86    wc.hbrBackground = NULL;
     87    wc.lpszMenuName = NULL;
     88    wc.lpszClassName = "ShOpenGL";
     89    RegisterClass( &wc );
     90
     91    // create main window
     92    hWnd = CreateWindow(
     93        "ShOpenGL", "",
     94        WS_POPUPWINDOW,
     95        0, 0, 0, 0,
     96        NULL, NULL, hInstance, NULL );
     97
     98    return hWnd;
     99}
     100
     101void RedirectIOToConsole()
     102{
     103    int hConHandle;
     104    HANDLE StdHandle;
     105    FILE *fp;
     106
     107    AllocConsole();
     108
     109    StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
     110    hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
     111    fp = _fdopen( hConHandle, "w" );
     112    *stdout = *fp;
     113
     114    StdHandle = GetStdHandle(STD_INPUT_HANDLE);
     115    hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
     116    fp = _fdopen( hConHandle, "r" );
     117    *stdin = *fp;
     118}
     119
     120int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
     121                      LPSTR lpszCmdLine,
     122                      int nCmdShow )
    65123{
    66124    HDC hdc;
    67125    HGLRC hglrc;
     126    HWND hWnd;
    68127
    69     EnableOpenGL(0, &hdc, &hglrc);
     128    RedirectIOToConsole();
     129
     130    /* Make dummy window, NVIDIA drivers fail if OpenGL is used with DC from a window owned by different process */
     131    hWnd = CreateInvisWindow(hInstance);
     132    if (hWnd == INVALID_HANDLE_VALUE)
     133    {
     134      printf("CreateWindow failed\n");
     135    }
     136
     137    EnableOpenGL(hWnd, &hdc, &hglrc);
    70138
    71139    printf("GL_VENDOR       %s\n", glGetString(GL_VENDOR));
     
    74142    printf("GL_EXTENSIONS   %s\n", glGetString(GL_EXTENSIONS));
    75143
    76     DisableOpenGL(0, hdc, hglrc);
     144    DisableOpenGL(hWnd, hdc, hglrc);
     145
     146    printf("Press Ender to finish...");
     147    getchar();
     148
    77149    return 0;
    78150}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette