Infinite Window
#1
Posted 27 September 2011 - 07:44 PM
I often create little projects, and I at the moment I have a basic window creation & message loop (handling) set up.
But often, my message loops result in the "infinite window" bug when
moving the window. This is just minor of course, but this always bugs me out.
Does anybody know what actually causes this? Is it because I'm not
double buffering yet?
Here's a link to a screenshot if you don't know what I mean with the "Infinite Window" bug.
http://imageshack.us...nitewindow.jpg/
#2
Posted 27 September 2011 - 09:05 PM
#3
Posted 27 September 2011 - 10:53 PM
-
Currently working on: the 3D engine for Tomb Raider.
#4
Posted 28 September 2011 - 12:54 AM
MSG msg = {};
while(msg.message != WM_QUIT)
{
// Send the message if one is found
if(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
#5
Posted 28 September 2011 - 01:02 AM
It doesn't look like you're doing any rendering yet. Once you do, you'll presumably be waiting on vsync at the end of rendering, so this problem will go away. You could also put a Sleep(0) call in the loop, although it's probably not good to have that in there long-term.
#6
Posted 28 September 2011 - 01:06 AM
Will this make the "infinite window" go away? Because usually when I get to that point in programming, I already use a fullscreen (which I won't in this program)
#7
Posted 28 September 2011 - 01:53 AM
IMO, the right way to do this is to configure your rendering API to wait for vsync, and if the API is built properly this should suspend the thread until the vsync clears, which will have the side effect of reducing the CPU utilization to a more reasonable level.
For reference, here's my message loop:
MSG msg;
while (true)
{
if (g_fRendering)
{
Update();
Render();
while (g_fRendering && PeekMessage(&msg, NULL, 0, 0, true))
DispatchMessage(&msg);
}
else
{
if (!GetMessage(&msg, NULL, 0, 0))
break;
DispatchMessage(&msg);
}
}
The g_fRendering flag gets set to false when you minimize the app, or when it starts shutting down; in that case I use GetMessage, which waits for a message, rather than PeekMessage. Down inside the Render() call I'm eventually calling Present() (since I'm using D3D) and telling it to wait for vsync, which both prevents tearing (usually...) and suspends the thread so that other apps can run.
It's also important to handle *all* available messages after each rendered frame, not just one, hence the PeekMessage in the inner while loop. Otherwise messages can back up and overflow the queue because you don't process them fast enough. Mouse moves especially can generate several messages per frame.
#8
Posted 28 September 2011 - 10:58 AM
-
Currently working on: the 3D engine for Tomb Raider.
#9
Posted 28 September 2011 - 02:37 PM
Reedbeta said:
It's also important to handle *all* available messages after each rendered frame, not just one, hence the PeekMessage in the inner while loop. Otherwise messages can back up and overflow the queue because you don't process them fast enough. Mouse moves especially can generate several messages per frame.
If you do this, how would you then set in a frame limiter? Would you do that inside the scope where Update & Render get called?
#10
Posted 28 September 2011 - 02:49 PM
But as said, I would just stick to SetTimer(), it makes your code clean and simple.
-
Currently working on: the 3D engine for Tomb Raider.
#11
Posted 28 September 2011 - 04:59 PM
_NOISEcore said:
Frame limiting is taken care of by the vsync. I don't have a need to limit to some arbitrary rate like 100 fps; I only ever want to run at the refresh rate (or divisors thereof).
#12
Posted 28 September 2011 - 05:09 PM
-
Currently working on: the 3D engine for Tomb Raider.
#13
Posted 28 September 2011 - 06:10 PM
.oisyn said:
Uhm...what?
#14
Posted 28 September 2011 - 06:36 PM
So I don't really have access to VSYNC (I think), in which I just switch to limiting the framerate to the refresh rate of the screen (or more if desired)
That's why I need to implement the timer :)
#15
Posted 28 September 2011 - 07:36 PM
#16
Posted 28 September 2011 - 09:39 PM
Reedbeta said:
-
Currently working on: the 3D engine for Tomb Raider.
#17
Posted 29 September 2011 - 09:50 AM
'}:+()___ [Smile said:
I actually haven't thought of that. I still need alot of "lessons" on effeciency :)
Thank you !
EDIT: I do have an additional problem atm. Well 2, concerning the same things.
First of all, my message loop looks like this:
MSG msg = {};
while(msg.message != WM_QUIT)
{
if(m_bActive)
{
// If the window is active
while(m_bActive && PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Sleep(0);
}
else
{
if(!GetMessage(&msg,NULL,0,0))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
The problem is, m_bActive gets set to false when WM_ACTIVATE with wParam being WA_INACTIVE, and else it gets set back to true.
When I click away, this works, but when I just minimize it keeps using thesame CPU amount as when activated.
This comes to my second problem, at the moment my program doesn't do that much, but yet it uses 25% of my CPU (dual core so..)
This is probably because of me not returning the remainder of my time slice, but I don't know how to do this. They say with Sleep(0)
but this doesn't seem to do anything.
#18
Posted 29 September 2011 - 11:01 AM
Quote
-
Currently working on: the 3D engine for Tomb Raider.
#19
Posted 29 September 2011 - 02:32 PM
#20
Posted 29 September 2011 - 02:47 PM
And you can implement the wait with Sleep(), but the downside is that you're unable to process messages when you're sleeping. So MsgWaitForMultipleObjects() is, in my opinion, the better choice.
-
Currently working on: the 3D engine for Tomb Raider.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












