本系列文章由@二货梦想家张程 所写,转载请注明出处。
本文章链接:http://blog.csdn.net/terence1212/article/details/44264153
作者:ZeeCoder 微博链接:http://weibo.com/zc463717263
我的邮箱:michealfloyd@126.com 欢迎大家发邮件来和我交流编程心得
you are what you read!与大家共勉!
-------------------------------------------------分割线:ZeeCoder--------------------------------------------
一、循环背景动画
循环背景动画就是不断地进行背景图的裁剪和接合,然后显示在窗口上所产生的一种背景循环滚动的效果。要达到这样的效果我们需要一张接合后看不出团接缝的背景图。其原理如下:
如上图所示,裁剪和接合过程:首先将左图黑色部分裁剪下来贴到右图黑色部分,然后将左图白色部分裁剪下来贴到右图白色部分。重复上述步骤即可完成循环背景动画。
二、多背景循环动画
多背景循环动画的原理和循环背景动画的原理相同,那么在多背景循环动画中,为了达到远近层次以及实际视觉移动效果,需要决定背景贴图的先后顺序,以及滚动的顺序。
上图是最终的效果图,从图中可以看到,整个动画分为四个层次,依次是天空,草地,海岛,人物,其中,海岛和人物需要
完成透明贴图。背景的滚动速度依次是天空<海岛<草地。人物即在原地跑动就可以看到向前奔跑的效果。
三、具体实现
#include "stdafx.h"
#include "MulitBg.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HDC hdc,mdc,bufdc;
HWND hWnd;
HBITMAP g_bg , g_island , g_walker;
DWORD g_tNow , g_tPre;
int g_x0 = 0 , g_x1 = 0 , g_x2 = 0 , g_x4 = 0 , g_num;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
//***************************主函数**********************************
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MULITBG, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
GetMessage(&msg , NULL , NULL , NULL);
while (msg.message != WM_QUIT)
{
if ( PeekMessage( &msg , NULL ,0 ,0 ,PM_REMOVE))//PM_REMOVE消息从队列里除掉
{
TranslateMessage( &msg);
DispatchMessage(&msg);
}
else
{
g_tNow = GetTickCount();
if (g_tNow - g_tPre >= 120)
{
MyPaint(hdc);
}
}
}
return (int) msg.wParam;
}
//***************************窗口类函数**********************************
ATOM MyRegisterClass(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 = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MULITBG));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MULITBG);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//***************************初始化函数**********************************
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP bmp;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(_T("MulitBg"), _T("多背景循环滚动"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd , 10 , 10 , 800 ,600 ,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc , 800 , 600);
SelectObject(mdc , bmp);
g_bg = (HBITMAP)LoadImage(NULL , _T("bg0.bmp") , IMAGE_BITMAP , 800 , 600 , LR_LOADFROMFILE);//天空
g_island = (HBITMAP)LoadImage(NULL , _T("bg.bmp") , IMAGE_BITMAP , 800 , 1200 , LR_LOADFROMFILE);//海岛
g_walker = (HBITMAP)LoadImage(NULL , _T("1.bmp") , IMAGE_BITMAP , 968 , 258 , LR_LOADFROMFILE);//人物跑动
MyPaint(hdc);
return TRUE;
}
//***************************消息响应函数**********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteObject(g_bg);
DeleteObject(g_island);
DeleteObject(g_walker);
ReleaseDC(hWnd , hdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//***************************自定义绘图函数*********************************
void MyPaint(HDC hdc)
{
//贴上天空图
SelectObject(bufdc , g_bg);
BitBlt(mdc , 0 , 0 , g_x0 , 370, bufdc , 800 - g_x0 , 0 , SRCCOPY);
BitBlt(mdc , g_x0 , 0 , 800 - g_x0 , 370 , bufdc , 0 , 0 , SRCCOPY);
//贴上海面图
BitBlt(mdc , 0 , 370 , g_x1 , 230 , bufdc , 800 - g_x1 ,370 , SRCCOPY);
BitBlt(mdc , g_x1 , 370 , 800 - g_x1 , 230 , bufdc , 0 , 370 , SRCCOPY);
//贴上海岛图并透明
SelectObject(bufdc , g_island);
BitBlt(mdc , 0 , 0 , g_x2 , 600 ,bufdc , 800 - g_x2 , 600 , SRCAND);
BitBlt(mdc , g_x2 , 0 , 800 - g_x2 , 600 , bufdc , 0 , 600 , SRCAND);
BitBlt(mdc , 0 , 0 , g_x2 , 600 ,bufdc , 800 - g_x2 , 0 , SRCPAINT);
BitBlt(mdc , g_x2 , 0 , 800- g_x2 , 600 ,bufdc , 0 , 0 , SRCPAINT);
//贴上人物图并透明
SelectObject(bufdc , g_walker);
BitBlt(mdc , 250, 350 ,121 , 129 ,bufdc , g_num*121 , 129 , SRCAND);
BitBlt(mdc ,250 , 350 , 121, 129 , bufdc , g_num*121 , 0 , SRCPAINT);
//将最后的画面显示在窗口上
BitBlt(hdc , 0 , 0 , 800 , 600 , mdc , 0 , 0 ,SRCCOPY);
g_tPre = GetTickCount();//获取当前系统时间
g_x0 += 5 ; //重设天空背景切割宽度
if ( g_x0 >= 800)
{
g_x0 = 0 ;
}
g_x1 += 20;//重设海面背景切割宽度
if ( g_x1 >= 800)
{
g_x1 = 0 ;
}
g_x2 += 10 ;//重设海岛背景切割宽度
if (g_x2 == 800)
{
g_x2 = 0;
}
g_x4 += 5;
if (g_x4 >= 800)
{
g_x4 = 0 ;
}
g_num ++;//重设跑动图的图号
if (g_num == 8)
{
g_num = 0 ;
}
}
笔记六就写到这里。利用bitblt进行动画设计的技巧到这篇笔记也结束了。
偶尔会翻书看后面利用directX SDK来进行3D游戏动画设计,但总觉得学什么东西都不能急于求成,就像一开始以为bitblt这个函
数很简单,但真正在写代码的时候才发现会遇到很多问题,所以在以后的学习过程中,要做到任何一个简单的程序都不能忽视。
最后还是那几句话,欢迎大家发邮件和我交流游戏编程的经验,也希望大牛们能不吝赐教。
---end
本笔记代码已经上传,欢迎下载:【Visual C++】游戏编程学习笔记之六:多背景循环动画---配套代码