4 Replies - 601 Views - Last Post: 25 May 2014 - 11:41 PM Rate Topic: -----

#1 flaminsnowman99  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 15-December 12

Updating Window In Game Loop

Posted 25 May 2014 - 09:17 PM

So I am working on making a game. The first thing I did was make a window class.
This correctly makes the window and displays it.

Now, I run my game loop and it just freezes the window and shows the Application has stopped working message box. I believe this is because the Window is not actually being updated.

My game loop however, looks like this.

bool run = true;
while(run) {
    Window::Render();

    if (Window::IsCloseRequested())
         run = false;
}

// Render method
void Window::Render(void) {
    MSG msg = {0};
    while(GetMessage(&msg, NULL, 0, 0) > 0) {
	if(msg.message == WM_QUIT)
	    break;
	TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}




So I'm really not sure how to fix this. Any help is greatly appreciated.
Thanks.

This post has been edited by flaminsnowman99: 25 May 2014 - 09:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Updating Window In Game Loop

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • Posts: 21,038
  • Joined: 05-May 12

Re: Updating Window In Game Loop

Posted 25 May 2014 - 09:25 PM

The problem is that your Render() function is actually misnamed. It doesn't go and render a frame. What it does in actually pump messages until it gets the WM_QUIT message.

Even if you don't call Render() you still run into problems. You your while condition is always set to true. Your setting run to false is not going to magically make the the while condition become false.

I've got the feeling that you are copying and pasting code and cobbling them together instead of having a coherent design. I suggest stepping away from your project for a few hours and draw things out on paper or a whiteboard instead of just throwing code together.
Was This Post Helpful? 0
  • +
  • -

#3 flaminsnowman99  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 15-December 12

Re: Updating Window In Game Loop

Posted 25 May 2014 - 09:36 PM

My original Render method was

void Window::Render() {
    InvalidateRect(m_hWnd, NULL, true);
    UpdateWindow(m_hWnd);
}


This, however, caused the same problem. What would be the correct way to do this?

And as much as it may seem like I'm throwing code together randomly, I learned Java a while back and I'm now learning C++ and making a Window from the two is significantly different. I'm trying to follow the same basic steps of how I did it in java.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • Posts: 21,038
  • Joined: 05-May 12

Re: Updating Window In Game Loop

Posted 25 May 2014 - 09:39 PM

With that original Render() you are just trying to force the window to redraw. You aren't pumping any Windows messages, and so modern versions of Windows thinks that your program is dead because a normal Windows program would be pumping messages.
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1456
  • View blog
  • Posts: 4,712
  • Joined: 20-March 10

Re: Updating Window In Game Loop

Posted 25 May 2014 - 11:41 PM

Well first of all I think your main problem is you dont show enough code for anyone to make a judgement on...

secondly creating a window in Java is as you say significantly different so why are you following the same steps..
you shouldn't be if you think about it.

third Render functions should actually render something how are you rendering ? with directX or simple gdi/gdi+ functions...

to start you off some code about how to construct a WinMain with a Window class.

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// create an instance of the derived window class (inherited from CBaseWindow)
	CDerivedWindow mainWnd(hInstance);

	// make this derived window a little different than CBaseWindow
    WNDCLASSEX wcx; 
 
    // Fill in the window class structure with default parameters 
    wcx.cbSize = sizeof(WNDCLASSEX);							// size of structure 
    wcx.style = CS_HREDRAW | CS_VREDRAW;						// redraw if size changes 
    wcx.lpfnWndProc = CBaseWindow::stWinMsgHandler;				// points to window procedure 
    wcx.cbClsExtra = 0;											// no extra class memory 
    wcx.cbWndExtra = 0;											// no extra window memory 
    wcx.hInstance = hInstance;									// handle to instance 
    wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);				// predefined app. icon 
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);					// predefined arrow 
    wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);	// white background brush 
    wcx.lpszMenuName = NULL;									// name of menu resource 
    wcx.lpszClassName = "DerivedWindow";						// name of window class 
    wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);				// small class icon 

	// register the window
	if (mainWnd.RegisterWindow(&wcx))
	{
		DWORD dwError = 0;
		DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
		RECT rc;

		rc.top = 100;
		rc.left = 100;
		rc.right = 740;
		rc.bottom = 580;

		// create the window and start the message loop
		// we will get kicked out of the message loop when the window closes
		if (mainWnd.Create(dwStyle, &rc))
		{
			// message loop
			MSG msg;
			BOOL bRet;

			while((bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
			{ 
				if (bRet == -1)
				{
					// handle the error and possibly exit
					break;
				}
				else
				{
					TranslateMessage(&msg); 
					DispatchMessage(&msg); 

					if (mainWnd.IsWindowClosed()) 
						break;
				}
			}
		}
		else
			dwError = GetLastError();
	}

	return 0;
}




notice the while getmessage thing...

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1