【游戏程序设计】动画演示-计时器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_38304645/article/details/82826842

运行结果:

源代码:

#include <windows.h>
#include <tchar.h>													//使用swprintf_s函数所需的头文件
#pragma comment(lib, "winmm.lib")									//调用PlaySound函数所需库文件t

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"动画演示_计时器"
//全局设备环境句柄g_hdc与全局内存DC句柄g_mdc,并声明了位图数组g_hSprite用来储存各人物位图
HINSTANCE hInst;
HDC g_hdc;
HDC g_mdc;
HBITMAP g_hSprite[12];												//12张图片DC
int g_iNum;

int  MyWindowClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyDraw(HWND);

/*****************************************************************************************************************
在不同的应用程序中,在此处添加相关的全局变量
******************************************************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstace,
									LPSTR lpCmdLine, int nCmdShow) 
{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
	MyWindowClass(hInstance);
	PlaySound(L"sound.wav", NULL, SND_FILENAME| SND_ASYNC| SND_LOOP);	//循环播放背景音乐
	if(!InitInstance(hInstance, nCmdShow))
		return FALSE;
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

int MyWindowClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = NULL;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = L"gamebase";
	wcex.hIconSm = NULL;

	return RegisterClassEx(&wcex); 
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hInst = hInstance;
	HWND hwnd = CreateWindow(L"gamebase", WINDOW_TITLE, WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
	if(!hwnd)
		return FALSE;
	MoveWindow(hwnd, 10, 10, WINDOW_WIDTH, WINDOW_HEIGHT, true);
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	g_hdc = GetDC(hwnd);
	g_mdc = CreateCompatibleDC(g_hdc);
	//载入各个位图,并建立定时器,间隔0.09秒发送WM_TIMER消息
	wchar_t filename[20];
	for(int i = 0; i != 12; ++i)
	{
		//调用swprintf_s函数,组装出对应的图片文件名称
		swprintf_s(filename, L"%d.bmp", i); 
		g_hSprite[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, WINDOW_WIDTH, WINDOW_HEIGHT, LR_LOADFROMFILE);
	}
	g_iNum = 0;												//图片号
	SetTimer(hwnd, 1, 90, NULL);
	MyDraw(hwnd);
	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;

	switch(message)
	{
	case WM_TIMER:
		MyDraw(hwnd);
		break;
	case WM_KEYDOWN:
		if(wParam == VK_ESCAPE)
			DestroyWindow(hwnd);											//销毁窗口
		break;
/**************************************************************************************************************
在退出程序前,往往在此处删除创建的相关资源
***************************************************************************************************************/
	case WM_DESTROY:
		KillTimer(hwnd, 1);													//删除定时器
		for(int i = 0; i != 12; ++i)										//删除位图资源
			DeleteObject(g_hSprite[i]);
		DeleteDC(g_mdc);													//删除内存DC
		ReleaseDC(hwnd, g_hdc);												//释放设备环境
		PostQuitMessage(0);													//在消息队列中放入0
		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}
/***************************************************************************************************************
在函数MyDraw()中进行相关绘制工作
****************************************************************************************************************/
void MyDraw(HWND hwnd)
{
	if(g_iNum == 12)														//判断是否超过最大图号,保证循环播放图片
		g_iNum = 0;
	SelectObject(g_mdc, g_hSprite[g_iNum]);									//选择对应的位图
	BitBlt(g_hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, g_mdc, 0, 0, SRCCOPY);
	++g_iNum;
}

猜你喜欢

转载自blog.csdn.net/baidu_38304645/article/details/82826842