How to resize an openGL window created with wglCreateContext?

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2010-04-13T19:25:47Z Indexed on 2010/04/13 19:53 UTC
Read the original article Hit count: 252

Filed under:
|
|

Is it possible to resize an openGL window (or device context) created with wglCreateContext without disabling it? If so how? Right now I have a function which resizes the DC but the only way I could get it to work was to call DisableOpenGL and then re-enable. This causes any textures and other state changes to be lost. I would like to do this without the disable so that I do not have to go through the tedious task of recreating the openGL DC state.

HWND hWnd;
HDC hDC;
void View_setSizeWin32(int width, int height) {
    // resize the window
    LPRECT rec = malloc(sizeof(RECT));
    GetWindowRect(hWnd, rec);
    SetWindowPos(      
        hWnd, 
        HWND_TOP, 
        rec->left, 
        rec->top, 
        rec->left+width, 
        rec->left+height,
        SWP_NOMOVE
    );
    free(rec);

    // sad panda
    DisableOpenGL( hWnd, hDC, hRC );
    EnableOpenGL( hWnd, &hDC, &hRC );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    glOrtho(-(width/2), width/2, -(height/2), height/2, -1.0, 1.0);

    // have fun recreating the openGL state....

}


void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC) {
    PIXELFORMATDESCRIPTOR pfd;
    int format;

    // get the device context (DC)
    *hDC = GetDC( hWnd );

    // set the pixel format for the DC
    ZeroMemory( &pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;

    pfd.iLayerType = PFD_MAIN_PLANE;
    format = ChoosePixelFormat( *hDC, &pfd );
    SetPixelFormat( *hDC, format, &pfd );

    // create and enable the render context (RC)
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}

void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC) {
    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( hRC );
    ReleaseDC( hWnd, hDC );
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about opengl