Sign in to follow this  
CulDeVu

C++ Win32 freezing when mouse is moved

Recommended Posts

CulDeVu    3034
Hello all

I've been having trouble for a while with this ever since I made a customized Window class to make be able to call functions that would easily do things like go full screen, set up child windows, changing cursors, stuff like that. Ever since then, occasionally, when my computer feels like it, all mouse movements will stop entire programming from running. All painting stops, as do frame counters. Also, much more recently, that same program will stop running just like before when a key is held down. What could be going on, if anyone has encountered this before? Does it even have anything to do with writing a custom window class?

Share this post


Link to post
Share on other sites
Buckeye    10747
[quote]Does it even have anything to do with writing a custom window class?[/quote]
If it does [i]not[/i] occur when you [i]don't[/i] use the custom window class, then there's likely something wrong with that class.

Share this post


Link to post
Share on other sites
yewbie    677
[quote name='djtaylor' timestamp='1302652125' post='4797710']
Hello all

I've been having trouble for a while with this ever since I made a customized Window class to make be able to call functions that would easily do things like go full screen, set up child windows, changing cursors, stuff like that. Ever since then, occasionally, when my computer feels like it, all mouse movements will stop entire programming from running. All painting stops, as do frame counters. Also, much more recently, that same program will stop running just like before when a key is held down. What could be going on, if anyone has encountered this before? Does it even have anything to do with writing a custom window class?
[/quote]

I would check your winproc, Are you handling default messages? your program might be drowning in messages.

Share this post


Link to post
Share on other sites
CulDeVu    3034
[quote name='NicoG' timestamp='1302657162' post='4797739']
Show your WinProc.
[/quote]

I copied this directly out of a book, so it should work perfectly.

[code]
// Route Windows messages to game engine member functions
switch (msg)
{
case WM_CREATE:
// Set the game window and start the game
m_pWindow->SetWindow(hWindow);
GameStart(hWindow);
return 0;

case WM_SETFOCUS:
// Activate the game and update the Sleep status
GameActivate(hWindow);
SetSleep(FALSE);
return 0;

case WM_KILLFOCUS:
// Deactivate the game and update the Sleep status
GameDeactivate(hWindow);
SetSleep(TRUE);
return 0;

case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hWindow, &ps);

// Paint the game
GamePaint(hDC);

EndPaint(hWindow, &ps);
return 0;

case WM_LBUTTONDOWN:
// Handle left mouse button press
MouseButtonDown(LOWORD(lParam), HIWORD(lParam), TRUE);
return 0;

case WM_LBUTTONUP:
// Handle left mouse button release
MouseButtonUp(LOWORD(lParam), HIWORD(lParam), TRUE);
return 0;

case WM_RBUTTONDOWN:
// Handle right mouse button press
MouseButtonDown(LOWORD(lParam), HIWORD(lParam), FALSE);
return 0;

case WM_RBUTTONUP:
// Handle right mouse button release
MouseButtonUp(LOWORD(lParam), HIWORD(lParam), FALSE);
return 0;

case WM_MOUSEMOVE:
// Handle mouse movement
MouseMove(LOWORD(lParam), HIWORD(lParam));
return 0;

case WM_DESTROY:
// End the game and exit the application
GameEnd();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWindow, msg, wParam, lParam);
[/code]

It does work normally when I'm not using the class, but what gets me is that sometimes it does this, and sometimes it doesn't. Like, for example, right now when I run it, it works perfectly, but 5 minutes ago when I ran it, it was messing up. Another thing to note is that I can't get stuff like Setting different cursors to work, and I don't know why...

Share this post


Link to post
Share on other sites
yewbie    677
You need some braces on your case statements like:
[code]
case IDOK: // OK button hit!
{
//Update our last record
UpdateObjectRecord(hwndDlg, MyGame.Info.DialogObjectNumber);
EndDialog(hwndDlg, IDOK); // end this dialog box.
}return(true);
[/code]

If not for function then at least for aesthetics.

Share this post


Link to post
Share on other sites
Evil Steve    2017
What does your message pump look like? I bet you're not processing all messages each frame, and you're just processing one message per tick. Especially if it only does it after a while.

Share this post


Link to post
Share on other sites
yewbie    677
[quote name='Evil Steve' timestamp='1302792983' post='4798420']
What does your message pump look like? I bet you're not processing all messages each frame, and you're just processing one message per tick. Especially if it only does it after a while.
[/quote]

Here is the message pump I use for my games djtaylor
[code] bool bQuit = false;
//message pump

do
{
//look for a message
while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//check that we arent quitting
if(msg.message==WM_QUIT)
bQuit = true;

//translate message
TranslateMessage(&msg);

//dispatch message
DispatchMessage(&msg);
}

if(bQuit == false)
{
MyGame.RunFrame();
}


} while(!bQuit);[/code]

Edit: sorry formating is a bit off there.

Share this post


Link to post
Share on other sites
CulDeVu    3034
[quote name='Evil Steve' timestamp='1302792983' post='4798420']
What does your message pump look like? I bet you're not processing all messages each frame, and you're just processing one message per tick. Especially if it only does it after a while.
[/quote]

This is directly out of my program:

[code]
// Message Pump
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Process the message
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// Make sure the game engine isn't sleeping
if (!GameEngine::GetEngine()->GetSleep())
{
// Check the tick count to see if a game cycle has elapsed
iCurTick = GetTickCount();
if (iCurTick - iStartTick > GameEngine::GetEngine()->GetFrameDelay())
{
iStartTick = GetTickCount();
KeyCheck();
Cycle();
//GameEngine::GetEngine()->SetFPS(1000 / ((DWORD)(GetTickCount()) - iStartTick));
}
}
}
//if (GetTickCount() - iStartTick < GameEngine::GetEngine()->GetFrameDelay())
Sleep(1);
}
return (int)msg.wParam;
[/code]

Is there something wrong with this? Maybe the Sleep() has something to do with it?

EDIT: Sorry about the formatting. GameDev need to acknowledge tab characters :P

Share this post


Link to post
Share on other sites
empirical2    567
You should pump all the messages before doing anything else. Sort of like this:

[code]
while (1)
{
while (PeekMessage(etc))
{
do message stuff
}
do game stuff
}
[/code]

Share this post


Link to post
Share on other sites
yewbie    677
[code] if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{[/code]

Needs to be a while as empirical2 stated, your only processing 1 message a loop, you should process all of them before you continue.

Share this post


Link to post
Share on other sites
CulDeVu    3034
[quote name='empirical2' timestamp='1302816889' post='4798563']
You should pump all the messages before doing anything else
[/quote]

That worked great! But now when I click the X on the window, the window disappears but the program doesn't close (the compile/run buttons are greyed out in the editor and I have to go into Task Manager to end the process). Do I have to modify my WinProc?

Share this post


Link to post
Share on other sites
empirical2    567
[quote name='djtaylor' timestamp='1302912586' post='4798982']
[quote name='empirical2' timestamp='1302816889' post='4798563']
You should pump all the messages before doing anything else
[/quote]

That worked great! But now when I click the X on the window, the window disappears but the program doesn't close (the compile/run buttons are greyed out in the editor and I have to go into Task Manager to end the process). Do I have to modify my WinProc?
[/quote]

Are you checking for the quit message? (The outline I gave was just that).
EDIT: I see you are, however if you simply nested it into the new while loop then it wont be able to break out of the outer loop. So your outer loop can never be broken. Use a variable to record this exit state and test for it.


Rough Example:
[code]
MSG msg;
while (1)
{
while(PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message==WM_QUIT)
{
Running=false;
break;
}
}
if (!Running) break;
//Game stuff here,
}
[/code]

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