2D游戏引擎(九)——添加背景支持
2008-08-01 23:34
531 查看
2d游戏中,基本背景类型为以下4种:
1.纯色背景
2.图像背景
3.动画背景
4.滚动背景
在下面的Background类中,支持前3种背景(其中第3种背景为自定义的:星空背景),第4种背景将在以后展开。
程序清单:
//-----------------------------------------------------------------
// Background Object
// C++ Header - Background.h
//-----------------------------------------------------------------
#pragma once
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include <windows.h>
#include "Bitmap.h"
//-----------------------------------------------------------------
// Background Class
//-----------------------------------------------------------------
class Background
{
protected:
// Member Variables
int m_iWidth, m_iHeight;
COLORREF m_crColor;
Bitmap* m_pBitmap;
public:
// Constructor(s)/Destructor
Background(int iWidth, int iHeight, COLORREF crColor);
Background(Bitmap* pBitmap);
virtual ~Background();
// General Methods
virtual void Update();
virtual void Draw(HDC hDC);
// Accessor Methods
int GetWidth() { return m_iWidth; };
int GetHeight() { return m_iHeight; };
};
//-----------------------------------------------------------------
// Starry Background Class
//-----------------------------------------------------------------
class StarryBackground : Background
{
protected:
// Member Variables
int m_iNumStars; //星星数量
int m_iTwinkleDelay; //星星闪烁延迟
POINT m_ptStars[100]; //位置
COLORREF m_crStarColors[100]; //颜色
public:
// Constructor(s)/Destructor
StarryBackground(int iWidth, int iHeight, int iNumStars = 100,
int iTwinkleDelay = 50);
virtual ~StarryBackground();
// General Methods
virtual void Update();
virtual void Draw(HDC hDC);
};
//-----------------------------------------------------------------
// Background Object
// C++ Source - Background.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "Background.h"
//-----------------------------------------------------------------
// Background Constructor(s)/Destructor
//-----------------------------------------------------------------
Background::Background(int iWidth, int iHeight, COLORREF crColor)
{
// Initialize the member variables
m_iWidth = iWidth;
m_iHeight = iHeight;
m_crColor = crColor;
m_pBitmap = NULL;
}
Background::Background(Bitmap* pBitmap)
{
// Initialize the member variables
m_crColor = 0;
m_pBitmap = pBitmap;
m_iWidth = pBitmap->GetWidth();
m_iHeight = pBitmap->GetHeight();
}
Background::~Background()
{
}
//-----------------------------------------------------------------
// Background General Methods
//-----------------------------------------------------------------
void Background::Update()
{
// Do nothing since the basic background is not animated
}
void Background::Draw(HDC hDC)
{
// Draw the background
if (m_pBitmap != NULL)
m_pBitmap->Draw(hDC, 0, 0);
else
{
RECT rect = { 0, 0, m_iWidth, m_iHeight };
HBRUSH hBrush = CreateSolidBrush(m_crColor);
FillRect(hDC, &rect, hBrush);
DeleteObject(hBrush);
}
}
//-----------------------------------------------------------------
// StarryBackground Constructor
//-----------------------------------------------------------------
StarryBackground::StarryBackground(int iWidth, int iHeight, int iNumStars,
int iTwinkleDelay) : Background(iWidth, iHeight, 0)
{
// Initialize the member variables
m_iNumStars = min(iNumStars, 100);
m_iTwinkleDelay = iTwinkleDelay;
// Create the stars
for (int i = 0; i < iNumStars; i++)
{
m_ptStars[i].x = rand() % iWidth;
m_ptStars[i].y = rand() % iHeight;
m_crStarColors[i] = RGB(128, 128, 128);
}
}
StarryBackground::~StarryBackground()
{
}
//-----------------------------------------------------------------
// StarryBackground General Methods
//-----------------------------------------------------------------
void StarryBackground::Update()
{
// Randomly change the shade of the stars so that they twinkle
int iRGB;
for (int i = 0; i < m_iNumStars; i++)
if ((rand() % m_iTwinkleDelay) == 0)
{
iRGB = rand() % 256;
m_crStarColors[i] = RGB(iRGB, iRGB, iRGB);
}
}
void StarryBackground::Draw(HDC hDC)
{
// Draw the solid black background
RECT rect = { 0, 0, m_iWidth, m_iHeight };
HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
FillRect(hDC, &rect, hBrush);
DeleteObject(hBrush);
// Draw the stars
for (int i = 0; i < m_iNumStars; i++)
SetPixel(hDC, m_ptStars[i].x, m_ptStars[i].y, m_crStarColors[i]);
}
1.纯色背景
2.图像背景
3.动画背景
4.滚动背景
在下面的Background类中,支持前3种背景(其中第3种背景为自定义的:星空背景),第4种背景将在以后展开。
程序清单:
//-----------------------------------------------------------------
// Background Object
// C++ Header - Background.h
//-----------------------------------------------------------------
#pragma once
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include <windows.h>
#include "Bitmap.h"
//-----------------------------------------------------------------
// Background Class
//-----------------------------------------------------------------
class Background
{
protected:
// Member Variables
int m_iWidth, m_iHeight;
COLORREF m_crColor;
Bitmap* m_pBitmap;
public:
// Constructor(s)/Destructor
Background(int iWidth, int iHeight, COLORREF crColor);
Background(Bitmap* pBitmap);
virtual ~Background();
// General Methods
virtual void Update();
virtual void Draw(HDC hDC);
// Accessor Methods
int GetWidth() { return m_iWidth; };
int GetHeight() { return m_iHeight; };
};
//-----------------------------------------------------------------
// Starry Background Class
//-----------------------------------------------------------------
class StarryBackground : Background
{
protected:
// Member Variables
int m_iNumStars; //星星数量
int m_iTwinkleDelay; //星星闪烁延迟
POINT m_ptStars[100]; //位置
COLORREF m_crStarColors[100]; //颜色
public:
// Constructor(s)/Destructor
StarryBackground(int iWidth, int iHeight, int iNumStars = 100,
int iTwinkleDelay = 50);
virtual ~StarryBackground();
// General Methods
virtual void Update();
virtual void Draw(HDC hDC);
};
//-----------------------------------------------------------------
// Background Object
// C++ Source - Background.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "Background.h"
//-----------------------------------------------------------------
// Background Constructor(s)/Destructor
//-----------------------------------------------------------------
Background::Background(int iWidth, int iHeight, COLORREF crColor)
{
// Initialize the member variables
m_iWidth = iWidth;
m_iHeight = iHeight;
m_crColor = crColor;
m_pBitmap = NULL;
}
Background::Background(Bitmap* pBitmap)
{
// Initialize the member variables
m_crColor = 0;
m_pBitmap = pBitmap;
m_iWidth = pBitmap->GetWidth();
m_iHeight = pBitmap->GetHeight();
}
Background::~Background()
{
}
//-----------------------------------------------------------------
// Background General Methods
//-----------------------------------------------------------------
void Background::Update()
{
// Do nothing since the basic background is not animated
}
void Background::Draw(HDC hDC)
{
// Draw the background
if (m_pBitmap != NULL)
m_pBitmap->Draw(hDC, 0, 0);
else
{
RECT rect = { 0, 0, m_iWidth, m_iHeight };
HBRUSH hBrush = CreateSolidBrush(m_crColor);
FillRect(hDC, &rect, hBrush);
DeleteObject(hBrush);
}
}
//-----------------------------------------------------------------
// StarryBackground Constructor
//-----------------------------------------------------------------
StarryBackground::StarryBackground(int iWidth, int iHeight, int iNumStars,
int iTwinkleDelay) : Background(iWidth, iHeight, 0)
{
// Initialize the member variables
m_iNumStars = min(iNumStars, 100);
m_iTwinkleDelay = iTwinkleDelay;
// Create the stars
for (int i = 0; i < iNumStars; i++)
{
m_ptStars[i].x = rand() % iWidth;
m_ptStars[i].y = rand() % iHeight;
m_crStarColors[i] = RGB(128, 128, 128);
}
}
StarryBackground::~StarryBackground()
{
}
//-----------------------------------------------------------------
// StarryBackground General Methods
//-----------------------------------------------------------------
void StarryBackground::Update()
{
// Randomly change the shade of the stars so that they twinkle
int iRGB;
for (int i = 0; i < m_iNumStars; i++)
if ((rand() % m_iTwinkleDelay) == 0)
{
iRGB = rand() % 256;
m_crStarColors[i] = RGB(iRGB, iRGB, iRGB);
}
}
void StarryBackground::Draw(HDC hDC)
{
// Draw the solid black background
RECT rect = { 0, 0, m_iWidth, m_iHeight };
HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
FillRect(hDC, &rect, hBrush);
DeleteObject(hBrush);
// Draw the stars
for (int i = 0; i < m_iNumStars; i++)
SetPixel(hDC, m_ptStars[i].x, m_ptStars[i].y, m_crStarColors[i]);
}
相关文章推荐
- 2D游戏引擎(十一)—— 向游戏引擎添加滚动背景支持
- 2D游戏引擎制作:添加游戏类并修改XML读取类
- 2D游戏引擎(六)——添加子画面管理器
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 5(添加微信支持)
- 2d游戏引擎(二)——向引擎添加位图类
- 支持Box2D,开源HTML5 2D游戏引擎FLAG
- 支持Box2D,开源HTML5 2D游戏引擎FLAG
- 支持Box2D,开源HTML5 2D游戏引擎FLAG
- Cocos2d-x 是一个支持多平台的 2D 手机游戏引擎
- HTML5 2D游戏引擎研发系列 第六章
- 一种2D游戏引擎的设计与实现
- 发布HTML5 2D游戏引擎YEngine2D
- 2D游戏引擎开发
- Phaser开源2d引擎 html5游戏框架中文简介
- 【手游游戏引擎】学习游戏开发的两个方向 Cocos2d-x 和 Unity 3D/2D (一)
- 移动平台的2d游戏引擎(android&iphone)
- 【技术讨论】从弹弹堂说起,如何用2D物理引擎编写一个游戏<一>2011-11-05 10:36
- 2D游戏引擎制作:前言
- 2D的RPG游戏人物角色移动代码(以egret游戏引擎、TypeScript语言为例)
- 大二时写的基于XNA的2D游戏引擎