Jump to content
  • Advertisement
Sign in to follow this  
GotenRulezU

Getting windows to paint after certain messages are recieved from the keyboard.

This topic is 5128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well i'm trying to make something to demonstrate a "game" concept for somethign like pacman. But I can't figure out how to make windows paint when the user presses up down left or write. The array changes but I can't figure out how to make it redraw. Heres my source.
#include<windows.h>
#include<string>
bool gameover = false;
using namespace std;
class CPlayer{
private:
	int x;
	int y;
	int lives;
public:
	CPlayer(){
		x = 0;
		y = 0;
		lives = 0;
	}
	int getX();
	int getY();
	int getLives();
	void warpMove(int newx,int newy);
	void move(int direction);
};
CPlayer player;
int scrn[10][10] = {
	{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}
};
HBITMAP bmps[3];

LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	switch(iMsg){
		case WM_PAINT:
			{
				PAINTSTRUCT ps;
				HDC hdc = BeginPaint(hwnd,&ps);
					HDC imagedc = CreateCompatibleDC(hdc);
				for(int y = 0; y < 10; y++){
					for(int x = 0; x < 10; x++){
						SelectObject(imagedc,bmps[scrn[x][y]]);
						BitBlt(hdc,x*10,y*10,10,10,imagedc,0,0,SRCCOPY);
					}
			}
	DeleteDC(imagedc);
				EndPaint(hwnd,&ps);
			}
			break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;
	case WM_DESTROY:
		PostQuitMessage(0);
		gameover = true;
		break;
	case WM_KEYDOWN:
		switch(wParam){
		case VK_UP:
			player.move(1);
			break;
		case VK_DOWN:
			player.move(2);
			break;
		case VK_RIGHT:
			player.move(3);
			break;
		case VK_LEFT:
			player.move(4);
			break;
		}
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdline,int iCmd){
	bmps[0] = (HBITMAP)LoadImage(hInstance,"image1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	bmps[1] = (HBITMAP)LoadImage(hInstance,"image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	bmps[2] = (HBITMAP)LoadImage(hInstance,"image2.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	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 = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "class";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC;
	RegisterClass(&wc);
	HWND hwnd = CreateWindow("class","my window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,SW_SHOW);
	MSG msg;
	while(!gameover){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
		}
	}
	UnregisterClass("class",hInstance);
	return 0;
}
int CPlayer::getX(){return x;}
int CPlayer::getY(){return y;}
int CPlayer::getLives(){return lives;}
void CPlayer::move(int direction){
	scrn[x][y] = 0;
	if(direction == 1){
		y--;
	}
	if(direction == 2){
		y++;
	}
	if(direction == 3){
		x++;
	}
	if(direction == 4){
		x--;
	}
	scrn[x][y] = 2;
}
void CPlayer::warpMove(int newx,int newy){
	x = newx;
	y = newy;
}

Thanks,

Share this post


Link to post
Share on other sites
Advertisement
this code:


PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd,&ps);
HDC imagedc = CreateCompatibleDC(hdc);
for(int y = 0; y < 10; y++){
for(int x = 0; x < 10; x++){
SelectObject(imagedc,bmps[scrn[x][y]]);
BitBlt(hdc,x*10,y*10,10,10,imagedc,0,0,SRCCOPY);
}
}
DeleteDC(imagedc);
EndPaint(hwnd,&ps);




extract it to a separate method called something similar to Render(); Then just call that function when needed, in your case, while handling the WM_PAINT message and at the end of the move() method.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!