2
votes

I'm working on a win32 c/cpp project involving openGL. I'm just starting and had a few basic questions regarding how a standard win32 program works. Following a tutorial, I made the winmain create a new window, enable openGL for the window, and then enter the main loop where if there are messages, the program handles them, and otherwise, the program moves onto drawing openGL animations. Following that, I simply shut down openGL and destroy the window. I'm not too confused about what's happening in here, but this is where I get lost:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {


switch (message)
{

case WM_CREATE:
    return 0;

case WM_CLOSE:
    PostQuitMessage( 0 );
    return 0;

case WM_DESTROY:
    return 0;

case WM_KEYDOWN:
    switch ( wParam )
    {

    case VK_ESCAPE:
        PostQuitMessage(0);
        return 0;

    }
    return 0;

default:
    return DefWindowProc( hWnd, message, wParam, lParam );

}

though I do see in the winmain that I register this function to my window class via

wc.lpfnWndProc = WndProc;

How exactly does this process work? Can someone explain the pipeline to me - as the winmain method runs, it goes onto drawing the opengl animation, but as soon as a key is pressed, it enters message handling... and then what? How does my winmain method communicate with the WndProc method? What's actually happening from the machine's point of view?

1
The key is in your message loop. That's what results in your window procedures being called. Sounds to me like you need to get hold of Petzold's classic book and get on top of the basics.David Heffernan
@DavidHeffernan charlespetzold.com/pw5 is this the book you're talking about?lululoo

1 Answers

0
votes

In your WinMain there should be a pair of TranslateMessage / DispatchMessage calls. TranslateMessage is responsible for getting keystrokes being delivered correctly and DispatchMessage traverses the window hierachy to deliver the messages to the window that has the input focus, effectively calling the function which pointer was registered as default window message handler (Window Procedure) with the message as parameters.