Question

This is all the code I have. The functions begining with zx are just so that when I'm done I can quickly put together a custom library based on those functions. The function I need help with is zxResizeGL which is used only on the WM_SIZE event atm.

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

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);

/* Necessary value we need access to */
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define ZX_THIS_INSTANCE ((HINSTANCE)&__ImageBase)
/* We define 2 here so dev can retrieve the FPN b4 it is multiplied by 100 */
#define ZX__PERCENT( NUM, OF ) ( ( NUM ) / ( OF ) )
#define ZX_PERCENT( NUM, OF ) ( ZX__PERCENT( NUM, OF ) * 100 )

WNDCLASSEX zxGetOSWindowClassEX( void );

BOOL zxInitOSClasses( void );
HWND zxNewOSWindow( int w, int h, int x, int y, char const *text );    
HDC   hDC = NULL;
HGLRC hRC = NULL;

void zxResizeGL(int width, int height )
{
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, width, 0, height, -1.0, 1.0);
   glFlush();
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hwnd;
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0,
          width = 256,
         height = width,
              w = ZX__PERCENT( width - 20, width ),
              h = w;
    if ( !zxInitOSClasses() )
      return 0;

    hwnd = zxNewOSWindow( 480, 480, 0, 0, "OpenGL Sample" );

    ShowWindow(hwnd, nCmdShow);

    /* enable OpenGL for the window */
    EnableOpenGL(hwnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            switch ( msg.message )
            {
            case WM_QUIT:
              bQuit = TRUE;
              break;
            case WM_SIZE:
              /* This is where I'm trying to peform the resize */
               width = LOWORD( msg.lParam );
              height = HIWORD( msg.lParam );
                   w = ZX__PERCENT( width  - 20, width  );
                   h = ZX__PERCENT( height - 20, height );
              zxResizeGL( width, height );
              break;
            default:
              TranslateMessage(&msg);
              DispatchMessage(&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */

            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();
            glRotatef(theta, 0.0f, 0.0f, 1.0f);

            glBegin(GL_TRIANGLES);

                glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f( 0.0f,   h );
                glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(    w,  -h );
                glColor3f(0.0f, 0.0f, 1.0f);   glVertex2f(   -w,  -h );

            glEnd();

            glPopMatrix();

            SwapBuffers(hDC);

            theta += 1.0f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL(hwnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow(hwnd);

    return msg.wParam;
}

The defines for the functions broken off from main code.

WNDCLASSEX zxGetOSWindowClassEX( void )
{
  WNDCLASSEX    wx = {0};
         wx.cbSize = sizeof( WNDCLASSEX );
          wx.style = CS_OWNDC;
    wx.lpfnWndProc = WindowProc;
      wx.hInstance = ZX_THIS_INSTANCE;
          wx.hIcon = LoadIcon(   NULL, IDI_APPLICATION );
        wx.hCursor = LoadCursor( NULL, IDC_ARROW );
  wx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  wx.lpszClassName = "zxOSWindow";
        wx.hIconSm = LoadIcon(   NULL, IDI_APPLICATION );
  return wx;
}

BOOL zxInitOSClasses( void )
{
  WNDCLASSEX wx = zxGetOSWindowClassEX();
  if ( !RegisterClassEx( &wx ) )
    return 0;
  return 1;
}

HWND zxNewOSWindow( int w, int h, int x, int y, char const *text )
{
  WNDCLASSEX wx = zxGetOSWindowClassEX();
  return CreateWindowEx(
    0, wx.lpszClassName, text,
    WS_OVERLAPPEDWINDOW,
    x, y, w, h, NULL, NULL,
    wx.hInstance, NULL );
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
        break;

        case WM_DESTROY:
            return 0;

        case WM_KEYDOWN:
        {
            switch (wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                break;
            }
        }
        break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
    PIXELFORMATDESCRIPTOR pfd;

    int iFormat;

    /* 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;

    iFormat = ChoosePixelFormat(*hDC, &pfd);

    SetPixelFormat(*hDC, iFormat, &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);
}
Was it helpful?

Solution

Microsoft says WM_SIZE messages are sent (not posted) and thus come in via WindowProc().

PeekMessage() can only retrieve posted messages.

Handle WM_SIZE in your WindowProc().

OTHER TIPS

After having tried a break point I noticed I was not receiving the WM_SIZE event, will investigate alternate ways to perform resize. For now I will put the zxResizeGL in the drawing loop

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top