Jump to content
  • Advertisement
Sign in to follow this  
phil05

win32 problem while exiting program in a game loop

This topic is 5213 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!