2D游戏引擎(十一)—— 向游戏引擎添加滚动背景支持
来源:互联网 发布:php ci base url 编辑:程序博客网 时间:2024/05/17 06:45
BackgroundLayer类代表一个滚动背景中的一个单独图层,它存储了一个特定图层的位图图像以及这个图层的视口,速度和方向。它派生自Bitmap类。ScrollingBackground类负责将图层合并到一个滚动背景中。它派生自Background类。
以下是程序清单:
- //-----------------------------------------------------------------
- // ScrollingBackground Object
- // C++ Header - ScrollingBackground.h
- //-----------------------------------------------------------------
- #pragma once
- //-----------------------------------------------------------------
- // Include Files
- //-----------------------------------------------------------------
- #include <windows.h>
- #include "Background.h"
- //-----------------------------------------------------------------
- // Custom Data Types
- //-----------------------------------------------------------------
- typedef WORD SCROLLDIR;
- const SCROLLDIR SD_UP = 0,
- SD_RIGHT = 1,
- SD_DOWN = 2,
- SD_LEFT = 3;
- //-----------------------------------------------------------------
- // Background Layer Class
- //-----------------------------------------------------------------
- class BackgroundLayer : Bitmap
- {
- protected:
- // Member Variables
- RECT m_rcViewport;
- int m_iSpeed;
- SCROLLDIR m_sdDirection;
- public:
- // Constructor(s)/Destructor
- BackgroundLayer(HDC hDC, LPTSTR szFileName, int iSpeed,
- SCROLLDIR sdDirection);
- BackgroundLayer(HDC hDC, UINT uiResID, HINSTANCE hInstance, int iSpeed = 0,
- SCROLLDIR sdDirection = SD_LEFT);
- // General Methods
- void Update();
- virtual void Draw(HDC hDC, int x, int y, BOOL bTrans = FALSE,
- COLORREF crTransColor = RGB(255, 0, 255));
- void SetSpeed(int iSpeed) { m_iSpeed = iSpeed; };
- void SetDirection(SCROLLDIR sdDirection)
- { m_sdDirection = sdDirection; };
- void SetViewport(RECT& rcViewport)
- { CopyRect(&m_rcViewport, &rcViewport); };
- };
- //-----------------------------------------------------------------
- // Scrolling Background Class
- //-----------------------------------------------------------------
- class ScrollingBackground : Background
- {
- protected:
- // Member Variables
- int m_iNumLayers;
- BackgroundLayer* m_pLayers[10];
- public:
- // Constructor(s)/Destructor
- ScrollingBackground(int iWidth, int iHeight);
- virtual ~ScrollingBackground();
- // General Methods
- virtual void Update();
- virtual void Draw(HDC hDC, BOOL bTrans = FALSE,
- COLORREF crTransColor = RGB(255, 0, 255));
- void AddLayer(BackgroundLayer* pLayer);
- };
- //-----------------------------------------------------------------
- // ScrollingBackground Object
- // C++ Source - ScrollingBackground.cpp
- //-----------------------------------------------------------------
- //-----------------------------------------------------------------
- // Include Files
- //-----------------------------------------------------------------
- #include "ScrollingBackground.h"
- //-----------------------------------------------------------------
- // BackgroundLayer Constructor(s)/Destructor
- //-----------------------------------------------------------------
- // Create a background layer from a bitmap file
- BackgroundLayer::BackgroundLayer(HDC hDC, LPTSTR szFileName,
- int iSpeed, SCROLLDIR sdDirection)
- : Bitmap(hDC, szFileName), m_iSpeed(iSpeed), m_sdDirection(sdDirection)
- {
- // Set the viewport to the entire layer image by default
- m_rcViewport.left = m_rcViewport.top = 0;
- m_rcViewport.right = m_iWidth;
- m_rcViewport.bottom = m_iHeight;
- }
- // Create a background layer from a bitmap resource
- BackgroundLayer::BackgroundLayer(HDC hDC, UINT uiResID, HINSTANCE hInstance,
- int iSpeed, SCROLLDIR sdDirection)
- : Bitmap(hDC, uiResID, hInstance), m_iSpeed(iSpeed),
- m_sdDirection(sdDirection)
- {
- // Set the viewport to the entire layer image by default
- m_rcViewport.left = m_rcViewport.top = 0;
- m_rcViewport.right = m_iWidth;
- m_rcViewport.bottom = m_iHeight;
- }
- void BackgroundLayer::Draw(HDC hDC, int x, int y, BOOL bTrans,
- COLORREF crTransColor)
- {
- // Draw only the part of the layer seen through the viewport
- if (m_rcViewport.top < 0 && m_rcViewport.left < 0)
- {
- // Draw the split viewport wrapping top to bottom AND left to right
- DrawPart(hDC, x, y,
- m_iWidth + m_rcViewport.left, m_iHeight + m_rcViewport.top,
- -m_rcViewport.left, -m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x - m_rcViewport.left, y,
- 0, m_iHeight + m_rcViewport.top,
- m_rcViewport.right, -m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x, y - m_rcViewport.top,
- m_iWidth + m_rcViewport.left, 0,
- -m_rcViewport.left, m_rcViewport.bottom,
- bTrans, crTransColor);
- DrawPart(hDC, x - m_rcViewport.left, y - m_rcViewport.top,
- 0, 0,
- m_rcViewport.right, m_rcViewport.bottom,
- bTrans, crTransColor);
- }
- else if (m_rcViewport.top < 0 && m_rcViewport.right > m_iWidth)
- {
- // Draw the split viewport wrapping top to bottom AND right to left
- DrawPart(hDC, x, y,
- m_rcViewport.left, m_iHeight + m_rcViewport.top,
- m_iWidth - m_rcViewport.left, -m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x + (m_iWidth - m_rcViewport.left), y,
- 0, m_iHeight + m_rcViewport.top,
- m_rcViewport.right - m_iWidth, -m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x, y - m_rcViewport.top,
- m_rcViewport.left, 0,
- m_iWidth - m_rcViewport.left, m_rcViewport.bottom,
- bTrans, crTransColor);
- DrawPart(hDC, x + (m_iWidth - m_rcViewport.left), y - m_rcViewport.top,
- 0, 0,
- m_rcViewport.right - m_iWidth, m_rcViewport.bottom,
- bTrans, crTransColor);
- }
- else if (m_rcViewport.bottom > m_iHeight && m_rcViewport.left < 0)
- {
- // Draw the split viewport wrapping bottom to top AND left to right
- DrawPart(hDC, x, y,
- m_iWidth + m_rcViewport.left, m_rcViewport.top,
- -m_rcViewport.left, m_iHeight - m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x - m_rcViewport.left, y,
- 0, m_rcViewport.top,
- m_rcViewport.right, m_iHeight - m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x, y + (m_iHeight - m_rcViewport.top),
- m_iWidth + m_rcViewport.left, 0,
- -m_rcViewport.left, m_rcViewport.bottom - m_iHeight,
- bTrans, crTransColor);
- DrawPart(hDC, x - m_rcViewport.left, y + (m_iHeight - m_rcViewport.top),
- 0, 0,
- m_rcViewport.right, m_rcViewport.bottom - m_iHeight,
- bTrans, crTransColor);
- }
- else if (m_rcViewport.bottom > m_iHeight && m_rcViewport.right > m_iWidth)
- {
- // Draw the split viewport wrapping bottom to top AND right to left
- DrawPart(hDC, x, y,
- m_rcViewport.left, m_rcViewport.top,
- m_iWidth - m_rcViewport.left, m_iHeight - m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x + (m_iWidth - m_rcViewport.left), y,
- 0, m_rcViewport.top,
- m_rcViewport.right - m_iWidth, m_iHeight - m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x, y + (m_iHeight - m_rcViewport.top),
- m_rcViewport.left, 0,
- m_iWidth - m_rcViewport.left, m_rcViewport.bottom - m_iHeight,
- bTrans, crTransColor);
- DrawPart(hDC, x + (m_iWidth - m_rcViewport.left), y + (m_iHeight - m_rcViewport.top),
- 0, 0,
- m_rcViewport.right - m_iWidth, m_rcViewport.bottom - m_iHeight,
- bTrans, crTransColor);
- }
- else if (m_rcViewport.top < 0)
- {
- // Draw the split viewport wrapping from top to bottom
- DrawPart(hDC, x, y,
- m_rcViewport.left, m_iHeight + m_rcViewport.top,
- m_rcViewport.right - m_rcViewport.left, -m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x, y - m_rcViewport.top,
- m_rcViewport.left, 0,
- m_rcViewport.right - m_rcViewport.left, m_rcViewport.bottom,
- bTrans, crTransColor);
- }
- else if (m_rcViewport.right > m_iWidth)
- {
- // Draw the split viewport wrapping from right to left
- DrawPart(hDC, x, y,
- m_rcViewport.left, m_rcViewport.top,
- m_iWidth - m_rcViewport.left, m_rcViewport.bottom - m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x + (m_iWidth - m_rcViewport.left), y,
- 0, m_rcViewport.top,
- m_rcViewport.right - m_iWidth, m_rcViewport.bottom - m_rcViewport.top,
- bTrans, crTransColor);
- }
- else if (m_rcViewport.bottom > m_iHeight)
- {
- // Draw the split viewport wrapping from bottom to top
- DrawPart(hDC, x, y,
- m_rcViewport.left, m_rcViewport.top,
- m_rcViewport.right - m_rcViewport.left, m_iHeight - m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x, y + (m_iHeight - m_rcViewport.top),
- m_rcViewport.left, 0,
- m_rcViewport.right - m_rcViewport.left, m_rcViewport.bottom - m_iHeight,
- bTrans, crTransColor);
- }
- else if (m_rcViewport.left < 0)
- {
- // Draw the split viewport wrapping from left to right
- DrawPart(hDC, x, y,
- m_iWidth + m_rcViewport.left, m_rcViewport.top,
- -m_rcViewport.left, m_rcViewport.bottom - m_rcViewport.top,
- bTrans, crTransColor);
- DrawPart(hDC, x - m_rcViewport.left, y,
- 0, m_rcViewport.top,
- m_rcViewport.right, m_rcViewport.bottom - m_rcViewport.top,
- bTrans, crTransColor);
- }
- else
- // Draw the entire viewport at once
- DrawPart(hDC, x, y,
- m_rcViewport.left, m_rcViewport.top,
- m_rcViewport.right - m_rcViewport.left, m_rcViewport.bottom - m_rcViewport.top,
- bTrans, crTransColor);
- }
- void BackgroundLayer::Update()
- {
- switch (m_sdDirection)
- {
- case SD_UP:
- // Move the layer up (slide the viewport down)
- m_rcViewport.top += m_iSpeed;
- m_rcViewport.bottom += m_iSpeed;
- if (m_rcViewport.top > m_iHeight)
- {
- m_rcViewport.bottom = m_rcViewport.bottom - m_rcViewport.top;
- m_rcViewport.top = 0;
- }
- break;
- case SD_RIGHT:
- // Move the layer right (slide the viewport left)
- m_rcViewport.left -= m_iSpeed;
- m_rcViewport.right -= m_iSpeed;
- if (m_rcViewport.right < 0)
- {
- m_rcViewport.left = m_iWidth - (m_rcViewport.right - m_rcViewport.left);
- m_rcViewport.right = m_iWidth;
- }
- break;
- case SD_DOWN:
- // Move the layer down (slide the viewport up)
- m_rcViewport.top -= m_iSpeed;
- m_rcViewport.bottom -= m_iSpeed;
- if (m_rcViewport.bottom < 0)
- {
- m_rcViewport.top = m_iHeight - (m_rcViewport.bottom - m_rcViewport.top);
- m_rcViewport.bottom = m_iHeight;
- }
- break;
- case SD_LEFT:
- // Move the layer left (slide the viewport right)
- m_rcViewport.left += m_iSpeed;
- m_rcViewport.right += m_iSpeed;
- if (m_rcViewport.left > m_iWidth)
- {
- m_rcViewport.right = (m_rcViewport.right - m_rcViewport.left);
- m_rcViewport.left = 0;
- }
- break;
- }
- }
- //-----------------------------------------------------------------
- // ScrollingBackground Constructor(s)/Destructor
- //-----------------------------------------------------------------
- ScrollingBackground::ScrollingBackground(int iWidth, int iHeight)
- : Background(iWidth, iHeight, 0), m_iNumLayers(0)
- {
- }
- ScrollingBackground::~ScrollingBackground()
- {
- }
- //-----------------------------------------------------------------
- // ScrollingBackground General Methods
- //-----------------------------------------------------------------
- void ScrollingBackground::Update()
- {
- // Update the layers
- for (int i = 0; i < m_iNumLayers; i++)
- m_pLayers[i]->Update();
- }
- void ScrollingBackground::Draw(HDC hDC, BOOL bTrans,
- COLORREF crTransColor)
- {
- // Draw the layers
- for (int i = 0; i < m_iNumLayers; i++)
- m_pLayers[i]->Draw(hDC, 0, 0, bTrans, crTransColor);
- }
- void ScrollingBackground::AddLayer(BackgroundLayer* pLayer)
- {
- // Add the new layer (maximum of 10)
- if (m_iNumLayers < 10)
- m_pLayers[m_iNumLayers++] = pLayer;
- }
- 2D游戏引擎(十一)—— 向游戏引擎添加滚动背景支持
- 2D游戏引擎(四)—— 向引擎添加游戏杆支持
- 2D游戏引擎(三)—— 向引擎添加输入支持
- 2D游戏引擎(九)——添加背景支持
- 2d游戏引擎(二)——向引擎添加位图类
- 2D游戏引擎(七)——添加MIDI音乐支持
- 2D游戏引擎(八)——添加动画子画面支持
- 2D游戏引擎(十)—— 增强游戏引擎中的子画面
- 使用 Unity* 游戏引擎向 Android* 应用添加 x86 支持
- 2D游戏引擎(五)——添加子画面类
- 2D游戏引擎(六)——添加子画面管理器
- 3D游戏引擎系列十一
- 游戏引擎剖析(十一)
- 游戏引擎剖析(十一)
- 游戏引擎剖析(十一)
- 游戏引擎剖析(十一)
- 3D游戏引擎编写(2)
- Android 2D游戏引擎
- Delphi连接带密码的Access
- Flicker-Free Displays Using an Off-Screen DC
- 细说暴库的原理与方法
- Spring Framework中的面向方面编程(AOP)
- ubuntu解压命令全览
- 2D游戏引擎(十一)—— 向游戏引擎添加滚动背景支持
- Modbus Rtu 通信协议
- 加法器
- 连接ORACLE数据库 "SQLAllocHandle on SQL_HANDLE_ENV 失败
- JMS基本概念
- 老公娶二房的申請書 & 老婆的批覆
- java 参考资料
- JDK 6 新功能
- 解决HttpUrlConnection乱码问题