0

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);
}
  • [Output](https://drive.google.com/file/d/0B7XEqelNSvg9WVVNb3diWl9HUHM/edit?usp=sharing) –  Jan 21 '14 at 10:06
  • -1: unclear question. – Dinesh Subedi Jan 21 '14 at 10:22
  • What have you tried? What were the results? What did you expect? Do you get any error? If you get errors, post them here. Nobody here is going to read through your code if you don't show some effort yourself. – Kevin Jan 21 '14 at 13:24
  • Result is in the link I provided, my problem is that I cannot resize the drawing area/surface, zooming I can apply later once I have a good hold on drawing objects. Would prefer an answer that does not involve extra libraries (E.g. GLUT). –  Jan 21 '14 at 14:06
  • Forgot to mention, no compile errors to speak of, only runtime error is the drawing area, beyond that I should in theory be ok. I'll be playing a game for the next 2 hours so take your time checking back. –  Jan 21 '14 at 14:08
  • You should read the advice on creating better example code to be debugged [here](http://stackoverflow.com/help/mcve). It's very difficult to help debug your code with the whole program provided as such. – Mike D Jan 21 '14 at 14:32
  • I've put a screenshot in the link, why would u need to compile, besides this is modified from the default template in Code::Blocks 13.12, I have for-filled all 4 criteria your just being stubborn and not reading it because it more than 10 lines of code, cannot possibly reduce further than this. –  Jan 21 '14 at 17:03
  • I even said the core of the problem was in **zxResizeGL** which is used only in the **WM_SIZE** event, how much more specific do you need me to be? The line I cannot produce because I do not know which line it is, if I did I wouldn't be here now would I? –  Jan 21 '14 at 17:17
  • Oh and if you do answer the question please explain what I was doing wrong, pointless to fix a problem and not learn the cause as well. –  Jan 21 '14 at 17:28

2 Answers2

0

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

0

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().

genpfault
  • 51,148
  • 11
  • 85
  • 139