当前位置:编程学习 > 网站相关 >>

游戏编程基础008---透明动画

代码下载:/2012/0118/20120118080904609.rar

/************************************************************************/ 
/* 
'透明动画' 是游戏中一定会用到的基本技巧,他通过图案的连续显示及透明来产生
背景图上的动画效果,制作的前提是,必须在暂存dc上完成每一张跑动的透明然后
再贴到窗口上,这样在画面更新的时候才不会出现透明贴图过程中产生闪烁的现象
*/ 
/************************************************************************// 
#include "stdafx.h" 
 
//全局变量声明 
HINSTANCE hInst; 
HBITMAP dra,bg; 
HDC hdc,mdc,bufdc; //mdc存储在贴到窗口上的内容,并在此内存DC上进行透明处理;bufdc选取位图用的DC 
HWND hWnd; 
DWORD tPre,tNow; 
int num,x,y; //x y为每次恐龙图案的贴图坐标;利用贴图坐标值的改变,产生动画前进的效果 
 
 
//全局函数 
void MyPaint(HDC); 
ATOM MyRegisterClass(HINSTANCE hInstance); 
BOOL InitInstance(HINSTANCE, int); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
void MyPaint(HDC hdc); 
 
//****程序入口************************************** 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPrevInstance, 
   www.zzzyk.com                  LPSTR     lpCmdLine, 
                     int       nCmdShow) 

MSG msg; 
 
MyRegisterClass(hInstance); 
 
//运行初始化函数 
if (!InitInstance (hInstance, nCmdShow))  

return FALSE; 

 
//游戏循环 
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); 
while (msg.message != WM_QUIT)  

if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 

TranslateMessage(&msg); 
DispatchMessage(&msg); 

else 

tNow = GetTickCount(); 
if (tNow-tPre >= 100) 

MyPaint(hdc); 


 

 
return msg.wParam; 

 
//****定义及注册窗口类别函数************************* 
ATOM MyRegisterClass(HINSTANCE hInstance) 

WNDCLASSEX wcex; 
 
wcex.cbSize = sizeof(WNDCLASSEX);  
wcex.style = CS_HREDRAW | CS_VREDRAW; 
wcex.lpfnWndProc = (WNDPROC)WndProc; 
wcex.cbClsExtra = 0; 
wcex.cbWndExtra = 0; 
wcex.hInstance = hInstance; 
wcex.hIcon = NULL; 
wcex.hCursor = NULL; 
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
wcex.lpszMenuName = NULL; 
wcex.lpszClassName = "canvas";  //类别名称 
wcex.hIconSm = NULL; 
 
return RegisterClassEx(&wcex); 

 
//****初始化************************************* 
// 加载位图并设定各对象初始值 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 

char filename[20] = ""; 
HBITMAP bmp; 
hInst = hInstance; 
 
hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW, 
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 
 
if (!hWnd) 

return FALSE; 

 
MoveWindow(hWnd,10,10,600,450,true); 
ShowWindow(hWnd, nCmdShow); 
UpdateWindow(hWnd); 
 
hdc = GetDC(hWnd); 
mdc = CreateCompatibleDC(hdc); 
bufdc = CreateCompatibleDC(hdc); 
bmp = CreateCompatibleBitmap(hdc,640,480); 
 
SelectObject(mdc,bmp); 
 
dra = (HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,760,198,LR_LOADFROMFILE); 
bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE); 
 
num = 0;    //显示图号 
x = 640; //贴图起始X坐标 
y = 350;    //贴图起始Y坐标 
 
MyPaint(hdc); 
 
return TRUE; 

 
/************************************************************************/ 
/*
1.恐龙跑动图案透明
2.更新贴图坐标
*/ 
/************************************************************************/ 
void MyPaint(HDC hdc) 

if(num == 8) 
num = 0; 
 
//在mdc中贴上背景图 
SelectObject(bufdc,bg); 
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY); 
 
//在mdc上进行透明处理 
SelectObject(bufdc,dra); 
BitBlt(mdc,x,y,95,99,bufdc,num*95,99,SRCAND); 
BitBlt(mdc,x,y,95,99,bufdc,num*95,0,SRCPAINT); 
 
//最后将图画显示在窗口中 
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY); 
 
tPre = GetTickCount();     //记录此次绘图时间 
num++; 
 
x-=20;    //计算下次贴图坐标 
if(x<=-95) 
x = 640; 

 
 
//****回调函数*********************************** 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 

//PAINTSTRUCT ps; 
//HDC hdc; 
 
switch (message)  

//case WM_PAINT:  
// hdc = BeginPaint(hWnd, &ps); 
// EndPaint(hWnd, &ps); 
// break; 
case WM_DESTROY:   
DeleteDC(mdc); 
DeleteDC(bufdc); 
DeleteObject(dra); 
DeleteObject(bg); 
ReleaseDC(hWnd,hdc); 
PostQuitMessage(0); 
break; 
default:  
return DefWindowProc(hWnd, message, wParam, lParam); 
   } 
   return 0; 
}   


摘自liu_liu213的专栏
补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,