现在的位置: 首页 > 综合 > 正文

2D游戏引擎(九)——添加背景支持

2013年09月11日 ⁄ 综合 ⁄ 共 4663字 ⁄ 字号 评论关闭

2d游戏中,基本背景类型为以下4种:
       1.纯色背景

2.图像背景

3.动画背景

4.滚动背景

在下面的Background类中,支持前3种背景(其中第3种背景为自定义的:星空背景),第4种背景将在以后展开。

程序清单:

  1. //-----------------------------------------------------------------
  2. // Background Object
  3. // C++ Header - Background.h
  4. //-----------------------------------------------------------------
  5. #pragma once
  6. //-----------------------------------------------------------------
  7. // Include Files
  8. //-----------------------------------------------------------------
  9. #include <windows.h>
  10. #include "Bitmap.h"
  11. //-----------------------------------------------------------------
  12. // Background Class
  13. //-----------------------------------------------------------------
  14. class Background
  15. {
  16. protected:
  17.   // Member Variables
  18.   int       m_iWidth, m_iHeight;
  19.   COLORREF  m_crColor;
  20.   Bitmap*   m_pBitmap;
  21. public:
  22.   // Constructor(s)/Destructor
  23.           Background(int iWidth, int iHeight, COLORREF crColor);
  24.           Background(Bitmap* pBitmap);
  25.   virtual ~Background();
  26.   // General Methods
  27.   virtual void  Update();
  28.   virtual void  Draw(HDC hDC);
  29.   // Accessor Methods
  30.   int GetWidth()  { return m_iWidth; };
  31.   int GetHeight() { return m_iHeight; };
  32. };
  33. //-----------------------------------------------------------------
  34. // Starry Background Class
  35. //-----------------------------------------------------------------
  36. class StarryBackground : Background
  37. {
  38. protected:
  39.   // Member Variables
  40.   int       m_iNumStars;                            //星星数量
  41.   int       m_iTwinkleDelay;                     //星星闪烁延迟
  42.   POINT     m_ptStars[100];                    //位置
  43.   COLORREF  m_crStarColors[100];      //颜色
  44. public:
  45.   // Constructor(s)/Destructor
  46.           StarryBackground(int iWidth, int iHeight, int iNumStars = 100,
  47.             int iTwinkleDelay = 50);
  48.   virtual ~StarryBackground();
  49.   // General Methods
  50.   virtual void  Update();
  51.   virtual void  Draw(HDC hDC);
  52. };

  1. //-----------------------------------------------------------------
  2. // Background Object
  3. // C++ Source - Background.cpp
  4. //-----------------------------------------------------------------
  5. //-----------------------------------------------------------------
  6. // Include Files
  7. //-----------------------------------------------------------------
  8. #include "Background.h"
  9. //-----------------------------------------------------------------
  10. // Background Constructor(s)/Destructor
  11. //-----------------------------------------------------------------
  12. Background::Background(int iWidth, int iHeight, COLORREF crColor)
  13. {
  14.   // Initialize the member variables
  15.   m_iWidth = iWidth;
  16.   m_iHeight = iHeight;
  17.   m_crColor = crColor;
  18.   m_pBitmap = NULL;
  19. }
  20. Background::Background(Bitmap* pBitmap)
  21. {
  22.   // Initialize the member variables
  23.   m_crColor = 0;
  24.   m_pBitmap = pBitmap;
  25.   m_iWidth = pBitmap->GetWidth();
  26.   m_iHeight = pBitmap->GetHeight();
  27. }
  28. Background::~Background()
  29. {
  30. }
  31. //-----------------------------------------------------------------
  32. // Background General Methods
  33. //-----------------------------------------------------------------
  34. void Background::Update()
  35. {
  36.   // Do nothing since the basic background is not animated
  37. }
  38. void Background::Draw(HDC hDC)
  39. {
  40.   // Draw the background
  41.   if (m_pBitmap != NULL)
  42.     m_pBitmap->Draw(hDC, 0, 0);
  43.   else
  44.   {
  45.     RECT    rect = { 0, 0, m_iWidth, m_iHeight };
  46.     HBRUSH  hBrush = CreateSolidBrush(m_crColor);
  47.     FillRect(hDC, &rect, hBrush);
  48.     DeleteObject(hBrush);
  49.   }
  50. }
  51. //-----------------------------------------------------------------
  52. // StarryBackground Constructor
  53. //-----------------------------------------------------------------
  54. StarryBackground::StarryBackground(int iWidth, int iHeight, int iNumStars,
  55.   int iTwinkleDelay) : Background(iWidth, iHeight, 0)
  56. {
  57.   // Initialize the member variables
  58.   m_iNumStars = min(iNumStars, 100);
  59.   m_iTwinkleDelay = iTwinkleDelay;
  60.   // Create the stars
  61.   for (int i = 0; i < iNumStars; i++)
  62.   {
  63.     m_ptStars[i].x = rand() % iWidth;
  64.     m_ptStars[i].y = rand() % iHeight;
  65.     m_crStarColors[i] = RGB(128, 128, 128);
  66.   }
  67. }
  68. StarryBackground::~StarryBackground()
  69. {
  70. }
  71. //-----------------------------------------------------------------
  72. // StarryBackground General Methods
  73. //-----------------------------------------------------------------
  74. void StarryBackground::Update()
  75. {
  76.   // Randomly change the shade of the stars so that they twinkle
  77.   int iRGB;
  78.   for (int i = 0; i < m_iNumStars; i++)
  79.     if ((rand() % m_iTwinkleDelay) == 0)
  80.     {
  81.       iRGB = rand() % 256;
  82.       m_crStarColors[i] = RGB(iRGB, iRGB, iRGB);
  83.     }
  84. }
  85. void StarryBackground::Draw(HDC hDC)
  86. {
  87.   // Draw the solid black background
  88.   RECT    rect = { 0, 0, m_iWidth, m_iHeight };
  89.   HBRUSH  hBrush = CreateSolidBrush(RGB(0, 0, 0));
  90.   FillRect(hDC, &rect, hBrush);
  91.   DeleteObject(hBrush);
  92.   // Draw the stars
  93.   for (int i = 0; i < m_iNumStars; i++)
  94.     SetPixel(hDC, m_ptStars[i].x, m_ptStars[i].y, m_crStarColors[i]);
  95. }

 

抱歉!评论已关闭.