【游戏开发】定时器循环播放动画

//Windows窗口头文件
#include <windows.h>
 
//swprintf_s函数包含的头文件
#include <tchar.h>

//PlaySound函数包含的库文件
#pragma comment(lib,"winmm.lib") 
 
//TransparentBlt函数包含的库文件
#pragma  comment(lib,"Msimg32.lib")  

//窗口宽度
#define WINDOW_WIDTH 1132	
 
//窗口高度
#define WINDOW_HEIGHT 708			
				
//窗口标题
#define WINDOW_TITLE L"【游戏开发】定时器循环播放动画"
 
//全局环境设备和全局内存设备
HDC	g_hdc=NULL,g_mdc=NULL; 

//12张位图
HBITMAP g_hSprite[12]; 

//记录目前显示的图号
int g_iNum=0;    

//7种画刷
HBRUSH g_hBrush[7]={0}; 

//6种画刷样式
int g_iBrushStyle[6] = {HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL}; 

//位图
HBITMAP g_hBackGround; 
 
//窗口过程函数
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
 
//初始化
BOOL Game_Init(HWND hwnd);		
 
//绘图
VOID Game_Paint( HWND hwnd);		
 
//释放资源
BOOL Game_CleanUp(HWND hwnd );
 
//主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd)
{
	//定义窗口类
	WNDCLASSEX wndClass = { 0 };
 
	//结构体的字节数大小
	wndClass.cbSize = sizeof( WNDCLASSEX ) ;	
 
	//窗口的样式
	wndClass.style = CS_HREDRAW | CS_VREDRAW;	
 
	//指向窗口过程函数的指针
	wndClass.lpfnWndProc = WndProc;		
 
	//窗口类的附加内存,取0
	wndClass.cbClsExtra		= 0;		
 
	//窗口的附加内存,依然取0
	wndClass.cbWndExtra		= 0;		
 
	//窗口过程的程序的实例句柄
	wndClass.hInstance = hInstance;						
 
	//ico图标
	wndClass.hIcon=(HICON)::LoadImage(NULL,L"天龙八部图标.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
 
	//光标句柄
	wndClass.hCursor = LoadCursor( NULL, IDC_ARROW ); 
 
	//画刷句柄
	wndClass.hbrBackground=(HBRUSH)CreateSolidBrush(RGB(255,255,255));  
 
	//指定菜单资源的名字
	wndClass.lpszMenuName = NULL;					
 
	//指定窗口类的名字
	wndClass.lpszClassName = L"TWJDWQ";
 
	//注册窗口类
	if( !RegisterClassEx( &wndClass ) )			
		return -1;		
 
	//创建窗口
	HWND hwnd = CreateWindow( L"TWJDWQ",WINDOW_TITLE,	
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH,
		WINDOW_HEIGHT, NULL, NULL, hInstance, NULL );
 
	//窗口显示的位置
	MoveWindow(hwnd,55,0,WINDOW_WIDTH,WINDOW_HEIGHT,true);
	
	//显示窗口
	ShowWindow( hwnd, nShowCmd );
 
	//更新窗口
	UpdateWindow(hwnd);	
 
	//初始化
	if (!Game_Init (hwnd)) 
	{
		//消息窗口
		MessageBox(hwnd, L"资源初始化失败", L"消息窗口", 0);
		return FALSE;
	}
 
	//循环播放背景音乐
	PlaySound(L"我行即我道.wav", NULL, SND_FILENAME | SND_ASYNC|SND_LOOP); 
 
	//定义并初始化msg
	MSG msg = { 0 };			
 
	//消息循环
	while( msg.message != WM_QUIT )			
	{
		//查看并派发消息
		if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
		{
			//将虚拟键消息转换为字符消息
			TranslateMessage( &msg );		
 
			//捕捉消息给窗口
			DispatchMessage( &msg );
		}
	}
 
	//注销窗口类
	UnregisterClass(L"TWJDWQ", wndClass.hInstance); 
 
	return 0;  
}
 
//处理窗口消息
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )      
{
	//记录绘制信息
	PAINTSTRUCT paintStruct;			
 
	switch( message )						
	{
	//定时器消息
	case WM_TIMER:			

		//调用绘制函数
		Game_Paint(hwnd);      
		
		break;		

	//绘制
	case WM_PAINT:		
 
		//指定窗口开始绘制(将和绘图有关的信息填充到paintStruct结构体中)
		g_hdc = BeginPaint( hwnd, &paintStruct ); 
 
		//调用绘制函数
		Game_Paint( hwnd);
 
		//结束绘制
		EndPaint( hwnd, &paintStruct );			
 
		//更新窗口
		ValidateRect(hwnd, NULL);		
 
		break;									
 
	//按键
	case WM_KEYDOWN:	
 
		//按键为ESC,销毁窗口,并发送一条WM_DESTROY消息
		if (wParam == VK_ESCAPE)    
			DestroyWindow(hwnd);	
 
		break;									
 
	//销毁
	case WM_DESTROY:
 
		//清理资源
		Game_CleanUp(hwnd);	
 
		//请求终止,并发送一条WM_DESTROY消息
		PostQuitMessage( 0 );			
 
		break;									
 
	//缺省
	default:										
		return DefWindowProc( hwnd, message, wParam, lParam );		
	}
 
	return 0;						
}
 
//初始化
BOOL Game_Init( HWND hwnd )
{
	//环境设备
	g_hdc = GetDC(hwnd); 
 
	//加载1张位图
	g_hBackGround = (HBITMAP)LoadImage(NULL,L"天龙八部壁纸.bmp",IMAGE_BITMAP,WINDOW_WIDTH,WINDOW_HEIGHT,LR_LOADFROMFILE);   
 
	//内存设备
	g_mdc = CreateCompatibleDC(g_hdc);  

	//1种默认画刷,6种样式画刷
	for(int i=0;i<=6;i++)
	{
		if(i==6)
			g_hBrush[i] = CreateSolidBrush(RGB(135,206,250));
		else
			g_hBrush[i] = CreateHatchBrush(g_iBrushStyle[i],RGB(135,206,250));
	}

	//图片名称
	wchar_t filename[20];

	//载入11张位图
	for(int i=0;i<12;i++)      
	{
		//filename的初始化
		memset(filename, 0, sizeof(filename));  

		//“组装”出对应的图片文件名称
		swprintf_s(filename,L"%d.bmp",i); 

		//加载图片
		g_hSprite[i] = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,WINDOW_WIDTH,WINDOW_HEIGHT,LR_LOADFROMFILE);
	}

	//设置初始的显示图号为"0"
	g_iNum = 0;            

	//建立定时器,间隔3秒发出消息
	SetTimer(hwnd,1,3000,NULL);   
 
	//绘制
	Game_Paint(hwnd);
 
	return TRUE;
}
 
//绘制
VOID Game_Paint( HWND hwnd )
{
	//选择图片
	SelectObject(g_mdc,g_hBackGround);
 
	//贴背景图
	BitBlt(g_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,g_mdc,0,0,SRCCOPY);  

	//创建字体
	HFONT hFont=CreateFont(30,0,0,0,0,0,0,0,GB2312_CHARSET,0,0,0,0,L"微软雅黑"); 

	//在环境设备中选择字体
	SelectObject(g_hdc,hFont); 

	//设置文字背景为透明
	SetBkMode(g_hdc, TRANSPARENT);  

	//定义三段文字
	wchar_t text1[]=L"新天龙八部:";
	wchar_t text2[]=L"高山流水觅知音,画地为牢心无悔。 ";
	wchar_t text3[]=L"--------逍遥派";

	//定义门派
	wchar_t emei[]=L"峨眉派";
	wchar_t xiaoyao[]=L"逍遥派";
	wchar_t guigu[]=L"鬼谷派";
	wchar_t tangmen[]=L"唐门派";
	wchar_t wudang[]=L"武当派";
	wchar_t mingjiao[]=L"明教派";
	wchar_t xingxiu[]=L"星宿派";

	//设置文字颜色并输出第一段文字
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,30,150,text1,wcslen(text1));

	//设置文字颜色并输出第二段文字
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,30,200,text2,wcslen(text2));

	//设置文字颜色并输出第三段文字
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,300,250,text3,wcslen(text3));

	//输出“峨眉派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,95,70,emei,wcslen(emei));

	//输出“逍遥派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,245,70,xiaoyao,wcslen(xiaoyao));

	//输出“鬼谷派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,395,70,guigu,wcslen(guigu));

	//输出“唐门派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,545,70,tangmen,wcslen(tangmen));

	//输出“武当派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,695,70,wudang,wcslen(wudang));

	//输出“明教派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,845,70,mingjiao,wcslen(mingjiao));

	//输出“星宿派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,995,70,xingxiu,wcslen(xingxiu));

	//释放字体对象
	DeleteObject(hFont);

	//定义两个x坐标值
	int x1 = 100;
	int x2 = 150;

	//用7种不同的画刷填充矩形
	for(int i=0;i<=6;i++)
	{
		//选用画刷
		SelectObject(g_hdc,g_hBrush[i]); 

		//画出一个封闭的矩形 ,矩形左上角坐标为(x1,70),右下角坐标为(x2,y)
		Rectangle(g_hdc,x1,0,x2,70);	

		//矩形直接的间隔
		x1 += 150;
		x2 += 150;
	}

	//处理图号,循环播放
	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++;                    //将“g_iNum”值加1,为下一次要显示的图号
}
 
//清理资源
BOOL Game_CleanUp( HWND hwnd )
{
	//释放位图
	DeleteObject(g_hBackGround);
 
	//释放内存设备
	DeleteDC(g_mdc);

	//释放画刷
	for (int i=0;i<=6;i++)
	{
		DeleteObject(g_hBrush[i]);
	}

	//删除所建立的定时器 
	KillTimer(hwnd,1);    

	//释放图片资源
	for(int i=0;i<12;i++)
		DeleteObject(g_hSprite[i]);

	//释放环境设备
	ReleaseDC(hwnd,g_hdc);

	return TRUE;
}

阅读更多

没有更多推荐了,返回首页