?? asl_bitmap.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 + -