⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asl_bitmap.h

📁 泡泡堂单机版(含ASL游戏引擎源码 泡泡堂单机版(含ASL游戏引擎源码
💻 H
字号:
//-----------------------------------------------------------------------------
//
//    ____ Azure Star Game Engine 蓝星游戏引擎 ____
//
//    Copyright (c) 2006, 蓝星工作室
//    All rights reserved.
//
//    文件名称: asl_bitmap.h
//    摘    要: 位图类定义
//
//    当前版本: 1.0
//    作    者: 汤  祺
//    创建日期: 2006-7-15
//
//-----------------------------------------------------------------------------

#ifndef ASL_BITMAP_INCLUDE
#define ASL_BITMAP_INCLUDE

#pragma once
#pragma comment(lib, "msimg32.lib")

#include "asl_utils.h"
#include "asl_file.h"

//-----------------------------------------------------------------------------
namespace ASL
{

//-----------------------------------------------------------------------------
// 16位颜色值 COLOR 类型定义及相关函数
//-----------------------------------------------------------------------------

// COLOR定义	
typedef WORD COLOR;

// 常用值
const COLOR clRed		= 0xF800;	// 红色
const COLOR clGreen		= 0x07E0;	// 绿色
const COLOR clBlue		= 0x001F;	// 蓝色
const COLOR clYellow	= 0xFFE0;	// 黄色
const COLOR clFuchsia	= 0xF81F;	// 紫色
const COLOR clAqua		= 0x07FF;	// 青色
const COLOR clBlack		= 0x0000;	// 黑色
const COLOR clWhite		= 0xFFFF;	// 白色

// 将红,绿,蓝三分量转换为COLOR
inline COLOR RGB16(int r, int g, int b)
{
	return COLOR((b & 0xF8) >> 3 | (g & 0xFC) << 3 | (r & 0xF8) << 8);
}

// 将32位GDI颜色值转换为COLOR
inline COLOR RGB16(COLORREF c)
{
	return COLOR( (c << 8 & 0xF800) | (c >> 5 & 0x7E0) | (c >> 19 & 0x1F) );
}

// 取红色分量
inline BYTE GetRed(COLOR rgb16)
{
	return (rgb16 & 0xF800) >> 8;
}

// 取绿色分量
inline BYTE GetGreen(COLOR rgb16)
{
	return (rgb16 & 0x07E0) >> 3;
}

// 取蓝色分量
inline BYTE GetBlue(COLOR rgb16)
{
	return (rgb16 & 0x001F) << 3;
}

// 将COLOR转换为32位GDI颜色值
inline COLORREF GetRGB32(COLOR rgb16)
{
	return RGB(GetRed(rgb16), GetGreen(rgb16), GetBlue(rgb16));
}

// 常量矩形, 用于绘图函数的默认值, 代表使用整个位图
const RECT DEFAULTRECT = { 0, 0, 0, 0 };



//-----------------------------------------------------------------------------
// 类名: ASLBitmap
// 功能: 位图类
//       本类与GDI的HBITMAP兼容,是一个在内存中保存的16位设备无关位图可以使用
//       几乎所有的GDI函数进行绘图操作。对于各种常用的绘图操作函数均使用MMX
//       指令进行了优化。本类完全通过CPU对内存进行操作,与DirectX完全无关。可
//       以通过派生类ASLCanvas使用DirectDraw,实现全屏显示。
//-----------------------------------------------------------------------------
class ASLBitmap
{

// 构造、析构函数
public:	
	ASLBitmap(void);
	~ASLBitmap(void);

// 禁用拷贝构造函数和赋值函数, 无实现
private:	
	ASLBitmap(const ASLBitmap &);
	ASLBitmap& operator=(const ASLBitmap &);


	
// 重要操作
public:
	// 创建空白位图
	void CreateBlank(int nWidth, int nHeight) throw(ASLSimpleException);
	
	// 从bmp格式的文件加载位图
	void LoadBMP(ASLFile *pFile) throw(ASLFileException);
	
	// 销毁位图
	void Destroy(void);	
	
	// 将位图保存为bmp格式的图片
	void SaveToFile(LPCSTR szFileName) const throw(ASLFileException);

	// 将位图绘制到窗口上
	void BlitToWnd(HWND hwnd, int xDest = 0, int yDest = 0, 
				   const RECT &rcSrc = DEFAULTRECT) const;
	
	// 将位图绘制到内存段中
	void BlitToMem565(BYTE *pMem, int nPitch) const;
	void BlitToMem555(BYTE *pMem, int nPitch) const;
	
	// 设置位图的拆分块
	void SetBlock(int numX, int numY);



// 属性操作
public:
	// 是否有nAlpha通道
	inline bool IsAlphaChannel(void) { return (m_pAlpha != NULL); }

	// 设置透明色
	inline void SetColorKey(COLOR cl){ m_nColorKey = GetRGB32(cl); }
	inline void SetColorKey(void)
	{ ASSERT(m_pData != NULL); SetColorKey(m_pData[0]); }
	
	// 取透明色
	inline int GetColorKey(void) const{ return m_nColorKey; }
	
	// 设置指定像素的颜色
	inline void SetPixel(int x, int y, COLOR cl)
	{ ASSERT(m_pData != NULL); m_pData[y * m_nPitch/2 + x] = cl; }
	
	// 取指定像素的颜色
	inline COLOR GetPixel(int x, int y) const 
	{ ASSERT(m_pData != NULL);  return m_pData[y * m_nPitch/2 + x]; }

	// 设置热点
	inline void SetHotspot(POINT pt) { m_ptHot = pt; }
	
	// 取HDC
	inline HDC GetDC(void) const { return m_hDC; }
	
	// 取HBITMAP
	inline HBITMAP GetHandle(void) const { return m_hBitmap; }
	
	// 取位图宽度
	inline int GetWidth(void) const { return m_nWidth; }
	
	// 取位图高度
	inline int GetHeight(void) const { return m_nHeight; }
	
	// 取位图单行所占字节数
	inline int GetPitch(void) const { return m_nPitch; }

	// 取位图数据
	inline COLOR* GetData(void) { return m_pData; }
	inline const COLOR* GetData(void) const { return m_pData; }



// 双目绘图函数(将本位图绘制到另一个位图)
public:
	// 以普通方式将位图绘制到目的位图	
	void Draw(ASLBitmap &bmDest, int xDest, int yDest, 
			  int nSeq) const;
	void Draw(ASLBitmap &bmDest, int xDest, int yDest, 
			  int row, int col) const;
	void Draw(ASLBitmap &bmDest, int xDest, int yDest, 
			  const RECT &rcSrc = DEFAULTRECT) const;
	
	// 以半透明方式将位图绘制到目的位图	
	void DrawAlpha(ASLBitmap &bmDest, int xDest, int yDest, int nAlpha, 
				   int nSeq) const;
	void DrawAlpha(ASLBitmap &bmDest, int xDest, int yDest, int nAlpha, 
				   int row, int col) const;	
	void DrawAlpha(ASLBitmap &bmDest, int xDest, int yDest, int nAlpha, 
				   const RECT &rcSrc = DEFAULTRECT) const;

	// 以快速半透明方式将位图绘制到目的位图(50% - 50%)	
	void DrawAlphaFast(ASLBitmap &bmDest, int xDest, int yDest,
					   int nSeq) const;
	void DrawAlphaFast(ASLBitmap &bmDest, int xDest, int yDest,
					   int row, int col) const;
	void DrawAlphaFast(ASLBitmap &bmDest, int xDest, int yDest, 
					   const RECT &rcSrc = DEFAULTRECT) const;

	// 以nAlpha通道方式将位图绘制到目的位图
	void DrawAlphaChannel(ASLBitmap &bmDest, int xDest, int yDest,
						  int nSeq) const;
	void DrawAlphaChannel(ASLBitmap &bmDest, int xDest, int yDest,
						  int row, int col) const;
	void DrawAlphaChannel(ASLBitmap &bmDest, int xDest, int yDest,
						  const RECT &rcSrc = DEFAULTRECT) const;
	
	// 以色饱和方式将位图绘制到目的位图	
	void DrawAdditive(ASLBitmap &bmDest, int xDest, int yDest, 
					  int nSeq) const;
	void DrawAdditive(ASLBitmap &bmDest, int xDest, int yDest, 
					  int row, int col) const;
	void DrawAdditive(ASLBitmap &bmDest, int xDest, int yDest, 
					  const RECT &rcSrc = DEFAULTRECT) const;

	// 将位图缩放绘制到目的位图	
	void DrawStretch(ASLBitmap &bmDest, int xDest, int yDest, double xScale, 
					 double yScale, int nSeq) const;
	void DrawStretch(ASLBitmap &bmDest, int xDest, int yDest, double xScale, 
					 double yScale, int row, int col) const;
	void DrawStretch(ASLBitmap &bmDest, int xDest, int yDest, double xScale, 
					 double yScale, const RECT &rcSrc = DEFAULTRECT) const;

	// 将位图旋转绘制到目的位图	
	void DrawRotate(ASLBitmap &bmDest, int xDest, int yDest, double angle, 
					int nSeq) const;
	void DrawRotate(ASLBitmap &bmDest, int xDest, int yDest, double angle, 
					int row, int col) const;
	void DrawRotate(ASLBitmap &bmDest, int xDest, int yDest, double angle, 
					const RECT &rcSrc = DEFAULTRECT) const;



// 单目绘图函数(修改位图本身)
public:
	// 以指定颜色清空位图
	void Clear(COLOR cl);
	
	// 输出文字
	void TextOut(COLOR cl, int x, int y, LPCSTR format, ...);
	void TextOut(HFONT font, COLOR cl, int x, int y, LPCSTR format, ...);	

	// 绘制直线
	void Line(COLOR cl, int x1, int y1, int x2, int y2, int size = 1);

	// 以半透明方式绘制直线
	void LineAlpha(COLOR cl, int x1, int y1, int x2, int y2, int nAlpha, 
		int size = 1);

	// 绘制矩形(仅边框)
	void Rect(COLOR cl, int x1, int y1, int x2, int y2, int size = 1);

	// 填充矩形
	void FillRect(COLOR cl, int x1, int y1, int x2, int y2);

	// 绘制椭圆(仅边框)
	void Ellipse(COLOR cl, int x1, int y1, int x2, int y2, int size = 1);

	// 填充椭圆
	void FillEllipse(COLOR cl, int x1, int y1, int x2, int y2);

	// 滤镜效果
	void Filter(COLOR cl);

	// 将位图灰度化
	void ConvertGray(void);

	// 将位图与指定颜色按比例混合(可实现淡入淡出)
	void Mix(COLOR cl, int nAlpha, const RECT &rc = DEFAULTRECT);



//私有函数
private:	
	// 调整绘图位置
	bool _AdjustDrawPos(const ASLBitmap &bmDest, int &xDest, int &yDest, 
						RECT &rcSrc) const;
	
	// 用本位图的大小裁剪矩形
	bool _Clip(RECT &rect) const;



//成员变量
protected:
	HBITMAP	m_hBitmap;			// 位图句柄
	HDC		m_hDC;				// 设备句柄
	int		m_nWidth;			// 位图宽度
	int		m_nHeight;			// 位图高度
	int		m_nPitch;			// 位图每行字节数
	int		m_nBlockW;			// 单块位图宽度
	int		m_nBlockH;			// 单块位图高度
	int		m_nBlockX;			// 位图分块列数
	int		m_nBlockY;			// 位图分块行数
	int		m_nColorKey;		// 颜色键
	RECT	m_rcClip;			// 位图裁剪矩形
	POINT	m_ptHot;			// 位图热点
	COLOR*	m_pData;			// 位图数据指针
	BYTE*	m_pAlpha;			// nAlpha通道

}; // ASLBitmap类定义结束

} // namespace ASL

#endif // ASL_BITMAP_INCLUDE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -