Sign in to follow this  
blackgame

redraw shaking problem !

Recommended Posts

blackgame    122
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: (the title and description previously are not precise enough so i add more details this time)
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);
}
and also i use UpdateWindow and is there anything wrong ? btw i use GDI to draw objects in my game, does it matter anyway ? [Edited by - blackgame on March 21, 2007 4:14:31 AM]

Share this post


Link to post
Share on other sites
DesignerX    174
Can you please post your window procedure and you update (or at least the one that draws the graphics) ?
and when you say shaking do you really mean shaking ?

Share this post


Link to post
Share on other sites
blackgame    122
sure, the game window are filled with shaking lines from time to time after opening that .exe file generated by VC6 compiler. here're some code in detials:


// Window Procedure~
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;
}

// Functions to draw objects~
void drawGame()
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);

switch (iMainState)
{
case MAIN_STATE_GAMEMAIN:
drawGameMain(ps);
break;
}

EndPaint(hwnd, &ps);
}

void drawGameMain(PAINTSTRUCT ps)
{
drawMapBG(ps);
drawRects(ps);
}

void drawMapBG(PAINTSTRUCT ps)
{
RECT rect;

// Border~
rect.left = iMapMinX - (MAP_BD_SIZE << 1);
rect.right = iMapMaxX + (MAP_BD_SIZE << 1);
rect.top = iMapMinY - (MAP_BD_SIZE << 1);
rect.bottom = iMapMaxY + (MAP_BD_SIZE << 1);

FillRect(ps.hdc, &rect, CreateSolidBrush(MAP_COLOR_BD));

// BG~
rect.left = iMapMinX - MAP_BD_SIZE;
rect.right = iMapMaxX + MAP_BD_SIZE;
rect.top = iMapMinY - MAP_BD_SIZE;
rect.bottom = iMapMaxY + MAP_BD_SIZE;

FillRect(ps.hdc, &rect, CreateSolidBrush(MAP_COLOR_BG));
}

void drawRects(PAINTSTRUCT ps)
{
int r = 0;
int c = 0;
int i = 0;

for (r=0; r<MAP_ROW; ++r)
{
for (c=0; c<MAP_COL; ++c)
{
if (map[r][c] != 0)
drawRect(ps.hdc, map[r][c], MAP_X+c*(RECT_W+RECT_SPACE), MAP_Y+r*(RECT_H+RECT_SPACE));
}
}

for (i=0; i<SHAPE_RECT_NUM; ++i)
{
r = iShapeRow + shapesRow[iShapeType-1][iShapeState][i];
c = iShapeCol + shapesCol[iShapeType-1][iShapeState][i];

drawRect(ps.hdc, iShapeType, MAP_X+c*(RECT_W+RECT_SPACE), MAP_Y+r*(RECT_H+RECT_SPACE));
}
}


btw, hwnd is a global variable so all functions could reference it directly.
any help ?

[Edited by - blackgame on March 21, 2007 4:05:14 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster

Hi!

I am not an expert when it comes to WM_PAINT stuff but I would recommend
to call BeginPaint() just once and pass the PAINTSTRUCT as a pointer.
Also don't forget to call EndPaint().


void drawGame()
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);

switch (iMainState)
{
case MAIN_STATE_GAMEMAIN:
drawGameMain(&ps); /// passing the address of 'ps'
break;
}

EndPaint(hwnd, &ps);
}

void drawGameMain(PAINTSTRUCT ps)
{

drawMapBG(&ps);
drawRects(&ps);
}

void drawMapBG(PAINTSTRUCT*)
{
/// ...
}

void drawRects(PAINTSTRUCT*)
{
/// ...
}



Bye,
anon

Share this post


Link to post
Share on other sites
blackgame    122
Quote:
Original post by Anonymous Poster

Hi!

I am not an expert when it comes to WM_PAINT stuff but I would recommend
to call BeginPaint() just once and pass the PAINTSTRUCT as a pointer.
Also don't forget to call EndPaint().

*** Source Snippet Removed ***

Bye,
anon


thanks for mentioning that. but it didnt work although i changed code as you said. any idea ?

[Edited by - blackgame on March 21, 2007 11:56:47 PM]

Share this post


Link to post
Share on other sites
Endar    668
Sorry, I don't have a solution, but just to make sure, it's not actual shaking, it's that the lines are popping in and out of view, yes?

Share this post


Link to post
Share on other sites
blackgame    122
Quote:
Original post by Endar
Sorry, I don't have a solution, but just to make sure, it's not actual shaking, it's that the lines are popping in and out of view, yes?


spot on. actually they are some black lines moves from top to bottom very fast, any help ?

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