[edit: most problems fixed, one problem remaining] Invalidate rectangle follow up, flickering and no movement
-
[edit] I solved the background flickering problem by changing the last argument in the InvalidateRect() function to false. I also got the test rectangle moving. The only problem left is that the white moving rectangle is still flickering. [/edit] I wrote some code, for some reason the rectangle that should work as background is flickering and I can`t get a test rectangle to move. I`m pasting the source bellow, please help me get my code in proper configuration.
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
UpdateGame();
}
}//...
switch (msg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
draw(hdc);
movingR = movingR + 0.1;
RECT ARectangle;
ARectangle.left = 0;
ARectangle.top = 0;
ARectangle.right = 600;
ARectangle.bottom = 600;InvalidateRect(hwnd, &ARectangle, false); UpdateWindow(hwnd); EndPaint(hwnd, &ps); return 0;
//...
void draw(HDC hdc)
{
Gdiplus::Graphics gf(hdc);
Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0));
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0));
Gdiplus::SolidBrush brush2(Gdiplus::Color(255, 255, 255, 255));gf.FillRectangle(&brush, 0, 0, 600, 600); gf.FillRectangle(&brush2,(int)movingR, 200, 100, 100);
}
//...void UpdateGame()
{
//movingR = movingR + 1;
} -
[edit] I solved the background flickering problem by changing the last argument in the InvalidateRect() function to false. I also got the test rectangle moving. The only problem left is that the white moving rectangle is still flickering. [/edit] I wrote some code, for some reason the rectangle that should work as background is flickering and I can`t get a test rectangle to move. I`m pasting the source bellow, please help me get my code in proper configuration.
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
UpdateGame();
}
}//...
switch (msg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
draw(hdc);
movingR = movingR + 0.1;
RECT ARectangle;
ARectangle.left = 0;
ARectangle.top = 0;
ARectangle.right = 600;
ARectangle.bottom = 600;InvalidateRect(hwnd, &ARectangle, false); UpdateWindow(hwnd); EndPaint(hwnd, &ps); return 0;
//...
void draw(HDC hdc)
{
Gdiplus::Graphics gf(hdc);
Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0));
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0));
Gdiplus::SolidBrush brush2(Gdiplus::Color(255, 255, 255, 255));gf.FillRectangle(&brush, 0, 0, 600, 600); gf.FillRectangle(&brush2,(int)movingR, 200, 100, 100);
}
//...void UpdateGame()
{
//movingR = movingR + 1;
}You should use double buffering. Just google for “OpenGL double buffering”.
Mircea
-
[edit] I solved the background flickering problem by changing the last argument in the InvalidateRect() function to false. I also got the test rectangle moving. The only problem left is that the white moving rectangle is still flickering. [/edit] I wrote some code, for some reason the rectangle that should work as background is flickering and I can`t get a test rectangle to move. I`m pasting the source bellow, please help me get my code in proper configuration.
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
UpdateGame();
}
}//...
switch (msg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
draw(hdc);
movingR = movingR + 0.1;
RECT ARectangle;
ARectangle.left = 0;
ARectangle.top = 0;
ARectangle.right = 600;
ARectangle.bottom = 600;InvalidateRect(hwnd, &ARectangle, false); UpdateWindow(hwnd); EndPaint(hwnd, &ps); return 0;
//...
void draw(HDC hdc)
{
Gdiplus::Graphics gf(hdc);
Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0));
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0));
Gdiplus::SolidBrush brush2(Gdiplus::Color(255, 255, 255, 255));gf.FillRectangle(&brush, 0, 0, 600, 600); gf.FillRectangle(&brush2,(int)movingR, 200, 100, 100);
}
//...void UpdateGame()
{
//movingR = movingR + 1;
}Do not call
InvalidateRect
andUpdateWindow
from inside yourWM_PAINT
handler. You should be setting the rectangle's dimensions from outside the handler, and then callingInvalidateRect
to cause the update to happen. Then when you get to the drawing code you get the rectangle details from inside thePaintstruct
. Also using hardcoded values like you show above is not the correct way to do it. -
Do not call
InvalidateRect
andUpdateWindow
from inside yourWM_PAINT
handler. You should be setting the rectangle's dimensions from outside the handler, and then callingInvalidateRect
to cause the update to happen. Then when you get to the drawing code you get the rectangle details from inside thePaintstruct
. Also using hardcoded values like you show above is not the correct way to do it.Richard and Mircea thanks for advice