Sign in to follow this  
phil05

win32 problem while exiting program in a game loop

Recommended Posts

I tried different things for hours, but when I hit escape, it asks me if I want to quit. I click no, and the same message box comes up again asking the same question. It keeps repeating itself in the game loop until I click yes. My game is so sad its forcing the player to quit. I'll provide the entire code below. Thanks for the help.

// PREPROCESSOR DIRECTIVES
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

// GLOBALS
#define WIN32_LEAN_AND_MEAN
const char g_szClassName[] = "myClassWindow";
bool keys[256];
bool Quit = false;		// False by default

// PROTOTYPES
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

// WINMAIN
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{


	WNDCLASSEX wc;
	HWND hWnd;
	MSG	Msg;

	// Background Color on Parent Window
	LOGBRUSH Brush;
	Brush.lbColor	= RGB(0, 90, 0);
	Brush.lbHatch	= 0;
	Brush.lbstyle	= BS_SOLID;
	HBRUSH hBrush	= CreateBrushIndirect(&Brush);

	// Registering Class Window
	wc.cbClsExtra		= 0;
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= hBrush;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hInstance		= hInstance;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= g_szClassName;
	wc.lpszMenuName		= NULL;
	wc.style			= CS_HREDRAW | CS_VREDRAW;

	// Did registering the class window fail? If so, alert user and close application.
	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Registering Class Window Failed!", "Error...", MB_OK | MB_ICONERROR);
		return 0;
	}

	// Creating Parent Window
	if (! (hWnd = CreateWindowEx(NULL, g_szClassName, "My RPG", WS_MINIMIZEBOX | WS_BORDER | 
		WS_CAPTION | WS_VISIBLE | WS_SYSMENU, 0, 0, 1024, 768, NULL, NULL, hInstance, NULL)))
		{
			MessageBox(NULL, "Creating Parent Window Failed!", "Error...", MB_OK | MB_ICONERROR);
			return 0;
		} 

	// Show & Update Window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Message & Game Loop
	while (!Quit)
	{
		if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (Msg.message == WM_QUIT)
			{
				Quit = true;
			}
			else
			{
				TranslateMessage(&Msg);
				DispatchMessage(&Msg);
			}
		}
		else
		{
			// MAIN GAME LOOP AREA

			if (keys[VK_ESCAPE])										// If User hits Escape, set Quit to true.
			{
				if (MessageBox(NULL, "Are you sure you want to exit?", "Exiting Game...", MB_YESNO | MB_ICONQUESTION) == IDYES)
				{
					Quit = true;
				}
				else
				{
					Quit = false;
				}
			}
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////

	return Msg.wParam;
}


// WINDOW PROCEDURE
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_CREATE:	
			{
				RECT cRect;
				GetWindowRect(hWnd, &cRect);
				int xSize = cRect.right - cRect.left;
				int ySize = cRect.bottom - cRect.top;
				int x	  = (GetSystemMetrics(SM_CXSCREEN) - xSize) / 2;
				int y	  = (GetSystemMetrics(SM_CYSCREEN) - ySize) / 2;
				SetWindowPos(hWnd, 0, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
			}
			break;

		case WM_KEYDOWN:
			{
				keys[wParam] = true;
			}
			break;

		case WM_KEYUP:
			{
				keys[wParam] = false;
			}
			break;

		case WM_CLOSE:   DestroyWindow(hWnd);
						 break;
		case WM_DESTROY: PostQuitMessage(WM_QUIT);
						 break;
		default:		 return DefWindowProc(hWnd, Msg, wParam, lParam);
	}

	return 0;
}

Share this post


Link to post
Share on other sites
Perhaps when you close the dialog the next event isn't a key up. It might be a WM_PAINT to redraw the dialog. In which case you're going to bring it up again. That key up event might not get a chance.
You should be easily able to determine this or whatever the real cause is if you put in some breakpoints, and step through the code.
Or try putting:
keys[VK_ESCAPE] = false;
where you bring up the MessageBox.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this