Tech Support > Microsoft Windows > Development Resources > redraw shaking problem !
redraw shaking problem !
Posted by black(flashing vampire) on March 22nd, 2007


Hi all,

i'm trying to make my first windows tetris game but found a severe
problem. that is, everytime i update(redraw) the window it may be
filled with shaking lines. i checked some articles and also i have a
nice book "tricks of windows programming guru", but they both didnt
help.
here're parts of my code:


int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Do something...

while (1)
{
if (PeekMessage(&msg, hwndRoot, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

updateGame();
}

return FALSE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
drawGame();
break;
case WM_KEYDOWN:
keyPressedGame(wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}

return 0;
}

void updateGame()
{
// Do something...

InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
}


as u can c in updateGame function i use InvalidateRect and
UpdateWindow to redraw the game window every cycle. is it anything
wrong ? btw i use GDI to draw objects in my game, does it matter
anyway ?

Posted by Grzegorz Wróbel on March 22nd, 2007


black(flashing vampire) wrote:
Use InvalidateRect(hwnd, NULL, FALSE) instead;

If you do need to redraw the background prior to painting to that on
WM_PAINT or use double buffering.

--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D

Posted by black(flashing vampire) on March 23rd, 2007


On Mar 22, 7:10 pm, Grzegorz Wróbel </dev/n...@localhost.localdomain>
wrote:

the matter is, my application is a mini game. so i have to redraw the
whole window to make sure everythong can be drawn, is there any way to
make it work smoothly except double buffer ?


Posted by Grzegorz Wróbel on March 23rd, 2007


black(flashing vampire) wrote:
That depends how much drawing is done. If the window is small and/or
there is not a lot of drawing done it can look smooth if you don't erase
background prior to painting (ie use NULL window brush or return 1 on
WM_ERASEBACKGROUND or use InvalidateRect(hwnd, NULL, FALSE) only to
force redrawing) but paint the background inside WM_PAIN (while
drawing). If the window is large and the large areas of it have to be
updated you might not be able to avoid it without double buffering.

--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D

Posted by black(flashing vampire) on March 27th, 2007


On Mar 24, 12:13 am, Grzegorz Wróbel </dev/n...@localhost.localdomain>
wrote:
thanks. and another fatal problem is, when i run the game, it works
but will not draw anything after several seconds and just display a
white window, anyone could figure it out what happened and how to sort
it ?


Posted by Grzegorz Wróbel on March 27th, 2007


black(flashing vampire) wrote:
Sounds like you have some infinite loop/deadlock in your code.

--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D

Posted by Scott Seligman [MSFT] on March 27th, 2007


"black(flashing vampire)" <quiteblack@gmail.com> wrote:
Are you running through your message loop? If an application sits in
a function for too long without calling GetMessage/DispatchMessage
occasionally, Windows will change the UI to the "Not Responding" UI.

--
Scott Seligman [MSFT]
This posting is provided AS IS with no warranties, and confers
no rights.

Posted by black(flashing vampire) on March 30th, 2007


to Grzegorz Wróbel:
yes i have an infinite loop but that is one dealing with messages

to
i have PeekMessage, TranslateMessage and DispatchMessage in game loop
like code below.


here's code for the game loop:

// Main loop~
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Game loop~
updateGame();
drawGame();

// Sleep a while~
Sleep(GAME_SLEEP_FRAMES);
}

after several seconds of running the game window rejected drawing
anything, i'm stuck for a few weeks. any help ?

Posted by Scott Seligman [MSFT] on March 30th, 2007


"black(flashing vampire)" <quiteblack@gmail.com> wrote:
Change this "if" to a "while". As it is now, this isn't a proper message
loop, as only one message can be processed per frame. No doubt your
message queue is getting full of something like a bunch of WM_MOUSEMOVE
messages and not having a chance to handle WM_PAINT.

--
Scott Seligman [MSFT]
This posting is provided AS IS with no warranties, and confers
no rights.

Posted by black(flashing vampire) on April 1st, 2007


i changed
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
into this
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
and the game refused to display anything after that. any other
solution ?



Similar Posts