Windows help.

Started by
3 comments, last by SSJCORY 20 years, 3 months ago
I''m working on a pong clone but when i draw the paddle, it extends when you move it and makes the paddle bigger instead of moving it. Can someone help me?

#define WIN32_LEAN_AND_MEAN
#include<windows.h>
int ballx;
int bally;
HINSTANCE gInstance;
HWND ghwnd;
void initWindow();
void killWindow();
void initBall();
void initPaddle();
void moveBall();
void movePaddle();
void drawBall();
void drawPaddle();
void MessageHandler();
const int height = 480;
const int width = 640;
const int paddlewidth = 40;
const int paddleheight = 10;
const int ballradius = 5;
int pb = 440 , pt = 430, pl = 300, pr = 340;
int topy;
int topl;
bool gameover = false;
MSG msg;
HDC hdc;
RECT rect;
RECT winrect;
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
	switch(msg){
	case WM_DESTROY:
		gameover = true;
		break;
	case WM_KEYDOWN:
		switch(wParam){
		case 37:
			pl -= 10;
			pr -= 10;
			break;
		case 39:
			pl += 10;
			pr += 10;
			break;
		}
		break;
	}
	return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR CmdLine,int iCmd){
	gInstance = hInstance;
	hdc = GetDC(ghwnd);
	GetClientRect(ghwnd,&winrect);
	initWindow();
	initBall();
	initPaddle();
	while(!gameover){
		MessageHandler();
		FillRect(hdc,&winrect,(HBRUSH)GetStockObject(WHITE_BRUSH));
		moveBall();
		movePaddle();
		drawBall();
		drawPaddle();
	}
	killWindow();
	return msg.wParam;
}
void initWindow(){
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = gInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "Class";
	wc.lpszMenuName = NULL;
	wc.style = NULL;
	RegisterClass(&wc);
	ghwnd = CreateWindow("Class","Pong",WS_OVERLAPPEDWINDOW,0,0,width,height,NULL,NULL,gInstance,NULL);
	ShowWindow(ghwnd,SW_SHOW);
	UpdateWindow(ghwnd);
}
void killWindow(){
}
void initBall(){
	ballx = 2;
	bally = 2;
}
void initPaddle(){
}
void drawBall(){	
}
void drawPaddle(){	
	rect.bottom = pb;
	rect.top = pt;
	rect.left = pl;
	rect.right = pr;
	FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
}
void moveBall(){
}
void movePaddle(){
}
void MessageHandler(){
	MSG msg;
	if(PeekMessage(&msg,ghwnd,0,0,PM_REMOVE)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}
Thanks, Cory Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
I don''t think it makes your paddle bigger. It is probably moving it and just not redrawing/refreshing the background so the old paddle is still there.
<< I''m working on a pong clone but when i draw the paddle, it extends when you move it and makes the paddle bigger instead of moving it. Can someone help me? >>

Yeah, you need to remember to erase the old paddle position IF you are using no back buffer. Or else it will look like your paddle is getting larger. This is how I did my breakout. Goes without saying, you also have to erase the old ball position.

VazBreak.cpp

However, if you use a back buffer there is no need to erase the old paddle position since the back buffer gets wiped each frame, and you draw to the off screen dc each frame, then copy the entire back_dc to your front_dc each frame. Here is how to do that....

How to set up back buffer

Good to see you do something in GDI, much less set up required.

Phil P
AlienCharm is right; you''re paddle is not getting bigger, the old paddle is just not being erased. One way to fix this is to draw a white paddle over your paddle, then draw your paddle again.
void drawPaddle(){		FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));	rect.bottom = pb;	rect.top = pt;	rect.left = pl;	rect.right = pr;	FillRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));}



This will get you going, but this solution isn''t perfect; you''ll see some flickering on the screen. You''ll have to learn about backbuffering before you can fix this. Do a search for backbuffer or Blt and you should be able to find a tutorial to help, but don''t get lost in the details. good luck
Problem solved instead of clearing the old paddle in the draw paddle i did it in the move paddle. So that there would be no flickering.


Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|

This topic is closed to new replies.

Advertisement