First off: Hello, I'm new here but I've been reading the forums every now and then and found them tremendously useful :)
And now about the problem...
Before I want to dive into directx and opengl and all that I want to make sure I know enough win32 programming to be sure that won't be a problem for any future games. Thus, it has been my ideal to create an easy wrapper class, that'd make creating windows no more difficult than:
OpenWindow ( some params );
I know there's a tutorial on this site about making such a class, but I want to do things different and before I'm actually going to create the class I am testing with alternative message loops to make the job easier.
I came up with the following:
#include <windows.h>
#define WIN32_LEAN_AND_MEAN
// start the main window loop immediately
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Create a MSG struct instance
MSG msg;
// Create a WNDCLASSEX struct instance
WNDCLASSEX wc;
// Create a window handle
HWND hWnd;
// Fill the members of the WNDCLASSEX struct
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpfnWndProc = DefWindowProc;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hCursor = LoadCursor(hInstance,IDC_ARROW);
wc.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
wc.hIconSm = LoadIcon(hInstance,IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpszClassName = "MyWindow";
wc.lpszMenuName = NULL;
// Register the actual class...
if ( !RegisterClassEx ( &wc ) ) {
MessageBox ( NULL, "Could not register window class!\nThis program will close now.", "Fatal Error", MB_OK|MB_ICONEXCLAMATION );
return 0;
}
// Open a window and show it
hWnd = CreateWindowEx ( WS_EX_OVERLAPPEDWINDOW, "MyWindow", "Test Window", WS_SYSMENU|WS_VISIBLE|WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL );
if ( hWnd == NULL ) {
MessageBox(NULL,"Could not open window display!\nThis program will close now.", "Fatla Error", MB_OK|MB_ICONEXCLAMATION);
}
ShowWindow ( hWnd, nCmdShow );
UpdateWindow ( hWnd );
// Now the ingenious part... The while message loop...
int quit = 0;
while ( quit == 0 )
{
if ( PeekMessage ( &msg, hWnd, 0, 0, PM_REMOVE ) ) {
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
if ( msg.message == WM_QUIT ) {
PostQuitMessage ( 0 );
quit = 1;
}
}
return 0;
}
Basically, the idea is to put the message loop inside the main window loop because I want my future windows program to look like this:
#include "mywinwrapclass.h"
OpenWindow ( "My Window Name", x, y, width, height );
While ( GetWindowMessage() != WM_QUIT ) {
// do some stuff
}
Please note that the GetWindowMessage() function would be 'self-made'.
Now, for the actual problem... I use VS6 and the first codeblock (the 'alternative message loop' test program) runs fine. It opens a window, and when you press the X button it closes. At least the window disappears, about the actual closing i'm not so sure because when I open my task manager I see that the CPU is on 100% and the program (WinLoop.exe) is taking it's full attention and looks far from being closed... :S
Is there something I'm missing? Must I free the window class or something because I didn't use a normal windows callback function?