Jump to content
  • Advertisement
Sign in to follow this  
Krisc

Constantly using WM_PAINT?

This topic is 4665 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

Is there any way to make a window automatically redraw itself as fast as possible, ie causing a game rendering loop with Win32 API?
#include <windows.h>
#include <vector>
#include <string>
#include "resource.h"
#include "ball.h"

//====================== GLOBALS =======================
HWND	ghMainWnd = 0;
HINSTANCE ghAppInst = 0;
HBITMAP ghBitMap = 0;
HBITMAP ghBitMap2 = 0;
HBITMAP zBall = 0;
HMENU ghMenu = 0;

ball gBall;


//=========== About Dialog Procedure ===================
BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT msg,WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return true;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hDlg,0);
			return true;
		}
		break;
	}
	return false;
}

//================= Window Procedure ===================
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	HDC bmHDC;
	BITMAP bitmap = {0};

	static int bmWidth = 0;
	static int bmHeight = 0;
	HBITMAP oldLpadBM = 0;
	HBITMAP oldRpadBM = 0;


	char *splayer = "Player 1";
	char *splayer2 = "Player 2";
	char *score[6] = {"0", "1", "2","3","4","5"};

	switch (msg)
	{
	case WM_CREATE:

//========= Creating Left Pad ==================
		ghBitMap = LoadBitmap(ghAppInst, MAKEINTRESOURCE(IDB_LPAD));
		
		GetObject(ghBitMap, sizeof(BITMAP), &bitmap);
	
		bmWidth = bitmap.bmWidth;
		bmHeight= bitmap.bmHeight;

//========= Creating Right Pad ==================		
		ghBitMap2 = LoadBitmap(ghAppInst, MAKEINTRESOURCE(IDB_RPAD));
		
		GetObject(ghBitMap2, sizeof(BITMAP), &bitmap);

		bmWidth = bitmap.bmWidth;
		bmHeight= bitmap.bmHeight;

//========= Creating The Ball ==================		
		zBall = LoadBitmap(ghAppInst, MAKEINTRESOURCE(IDB_zBall));
		
		GetObject(zBall, sizeof(BITMAP), &bitmap);

		gBall.setWidth(bitmap.bmWidth);
		gBall.setHeight(bitmap.bmHeight);

		gBall.setX(312);
		gBall.setY(232);

		gBall.setXV(1);
		gBall.setYV(0);

		return 0;

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case ID_FILE_EXIT:
			DestroyWindow(ghMainWnd);
			return 0;
		
		case ID_HELP_ABOUT:
			DialogBox(ghAppInst, MAKEINTRESOURCE (IDD_ABOUTBOX), hWnd, AboutDlgProc);
			return 0;
		}
		return 0;
		

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		
		TextOut(hdc, 20, 12, splayer, strlen(splayer));
		TextOut(hdc, 560,12, splayer2, strlen(splayer2));
		TextOut(hdc, 20, 28, score[1], strlen(score[1]));
		TextOut(hdc, 560, 28, score[2], strlen(score[2]));		

//======= Left Pad===========
		bmHDC = CreateCompatibleDC(hdc);
		oldLpadBM = (HBITMAP)SelectObject(bmHDC,ghBitMap);
		
		BitBlt(hdc, 10, 200, bmWidth, bmHeight, bmHDC, 0, 0, SRCCOPY);

		SelectObject(bmHDC, oldLpadBM);

		DeleteDC(bmHDC);

//======== Right Pad ===========
		bmHDC = CreateCompatibleDC(hdc);
		oldRpadBM = (HBITMAP)SelectObject(bmHDC,ghBitMap2);
		
		BitBlt(hdc, 600, 200, bmWidth, bmHeight, bmHDC, 0, 0, SRCCOPY);

		SelectObject(bmHDC, oldRpadBM);

		DeleteDC(bmHDC);

//======== The Ball ===========
		bmHDC = CreateCompatibleDC(hdc);
		oldRpadBM = (HBITMAP)SelectObject(bmHDC,zBall);
		
		BitBlt(hdc, gBall.getX(), gBall.getY(), gBall.getWidth(), gBall.getHeight(), bmHDC, 0, 0, SRCCOPY);

		SelectObject(bmHDC, oldRpadBM);

		DeleteDC(bmHDC);

		EndPaint(hWnd, &ps);

//======== Update Ball ========
		gBall.setX(gBall.getX() + gBall.getXV());
		gBall.setY(gBall.getY() + gBall.getYV());

		if((gBall.getX() + 16) > 639)
			gBall.setXV(-1);
		if(gBall.getX() < 1)
			gBall.setXV(1);

		return 0;	
	
	case WM_LBUTTONDOWN:
		MessageBox (0, "Reading left mouse clicks.", "Msg", MB_OK);
		return 0;
	
	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE)
			DestroyWindow(ghMainWnd);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		DeleteObject(ghBitMap);
		DeleteObject(ghBitMap2);
		
		return 0;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

//==================== MAIN ===========================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd)
{

	ghAppInst = hInstance;


	WNDCLASS wc;
	wc.style		= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc	= WndProc;
	wc.cbClsExtra	= 0;
	wc.cbWndExtra	= 0;
	wc.hInstance	= ghAppInst;
	wc.hIcon		= ::LoadIcon(ghAppInst, MAKEINTRESOURCE(IDI_ICON1));
	wc.hCursor		= ::LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName	= 0;
	wc.lpszClassName = "MyWndClassName";
	wc.style		= CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;

	RegisterClass( &wc);

	ghMenu = LoadMenu(ghAppInst, MAKEINTRESOURCE (IDR_MENU1));
	ghMainWnd = ::CreateWindow("MyWndClassName", "Zombie Pong",
		WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, 0, ghMenu, ghAppInst, 0);

	if(ghMainWnd == 0)
	{
		::MessageBox(0, "CreateWindow - Failed", 0, 0);
		return false;
	}

	ShowWindow(ghMainWnd, showCmd);
	UpdateWindow(ghMainWnd);


	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (GetMessage(&msg, 0, 0, 0) )
	{
		TranslateMessage (&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}




Share this post


Link to post
Share on other sites
Advertisement
It is causing some extremely bad blinking problems... any ideas? Thanks for the help. +'s for the help! ;)

Share this post


Link to post
Share on other sites
Also, if you are redrawing the whole window each time, be sure to tell InvalidateRect not to request a background erase.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!