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

📄 gl.h

📁 仿游戏 Diablo 的源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef __GRAPHIC_LIB
#define __GRAPHIC_LIB

//#include <crtdbg.h>
//#include <stdio.h>
#include <afx.h>

struct ScreenInfo{
	DWORD width;
	DWORD height;
	DWORD colorDepth;
};
// screenInfo defined in bitmap.cpp
extern ScreenInfo screenInfo;	//set by ddraw.cpp
//用于从像素中取得颜色分量
extern DWORD colorMask[6];
//用于alpha,beta非MMX计算,rbMask取红蓝,gMask取绿(cloudwu算法)
extern DWORD rbMask, gMask;

struct PixelInfo{
	int colorRedMask;
	int colorRedPos;
	int redMask;
	int redPos;

	int colorGreenMask;
	int colorGreenPos;
	int greenMask;
	int greenPos;

	int colorBlueMask;
	int colorBluePos;
	int blueMask;
	int bluePos;
};
extern PixelInfo pixelInfo16, pixelInfo15, pixelInfo24, pixelInfo32;

// 
int InitGraphicLibrary( int colorDepth );

/////////////////////////////////////////////////////////////////////
// 调色板

#define PAL_SIZE	256

typedef int RGB;

struct ColorEntry{
	union{
		struct{
		BYTE rgbBlue;
		BYTE rgbGreen;
		BYTE rgbRed;
		BYTE rgbRsvd ;
		};
		DWORD rgb;
	};
};

struct Palette{
	int count;
	ColorEntry palette[ PAL_SIZE ];
	int screenPal[ PAL_SIZE ];	// convert to current pixel mode
	
	Palette();
	void AddRef( void );
	void Release( void );
	void Convert( void );
	void Destroy( void );
	void WriteToDisk( FILE *fp, int num = PAL_SIZE );
	void ReadFromDisk( FILE *fp, int num = PAL_SIZE );
};


/////////////////////////////////////////////////////////////////////
// 内存位图及其操作函数

#define BYTES_PER_PIXEL( a ) (( a + 7 ) >> 3 )
#define COLOR_INDEX( c ) ( c & 0x80000000 )

#define COLORKEY8   0
#define COLORKEY15	0x7c1f
#define COLORKEY16  0xf81f
#define COLORKEY24  0xff00ff
#define COLORKEY32  0xff00ff

class Bitmap;
class Bitmap15;
class Bitmap16;

Bitmap* CreateBitmap( int w, int h );
Bitmap* CreateBitmapEx( int w, int h, int colorDepth );
Bitmap* LoadBmp( char *filename, Palette *pal );
Bitmap* LoadTga( char *filename, Palette *pal );
Bitmap* LoadPicture( char* filename, int convert = 1, Palette* pal = NULL );
int SaveTga( char *filename, Bitmap *bmp );

class Bitmap{
public:
	static int drawMode;	// solid, xor, blend ( not support pattern )
	static int alpha;
	
	enum{
		SolidMode = 0x1,
		XorMode = 0x2,
		AlphaMode = 0x4,
		BetaMode = 0x8
		};
			
	int width, height;
	int colorDepth;
	int clip;	// true or false
	int cl, ct, cr, cb;	//clip rectangle
	void* dat;	//data area
/*	int id;		//ID
	int dx, dy;	// for sub bitmap
*/	char** line;	// line addresses
	int color; 	// current color	
	int colorKey;	// SOURCE and TARGET color key
	int pitch;	// the scanline length in dat ( the same as directdraw )
					//( may be larger than actual length for alignment purpose )
	
	// functions
	Bitmap();

	int  GetClip(){
		return clip;
	};
	
	void SetClip( int l, int t, int r, int b );
	void SetClip( int enable );
	void SetDrawMode( int drawmode, int alpha );

	virtual Bitmap* ConvertFormat( int ) = 0;
	virtual int  MakeColor( RGB color ) = 0;
	virtual void SetColor( RGB color ) = 0;
	virtual void SetColorKey( RGB color ) = 0;

	// color = 0x00rrggbb ( 15,16,24,32 ) / 0x800000cc ( 8bit color )
	virtual DWORD GetPixel( int x, int y ) = 0;
	virtual void PutPixel( int x, int y ) = 0; 
	virtual void Blit( Bitmap* dest, int dx, int dy, int sx, int sy, int w, int h ) = 0;
	virtual void BlitMask( Bitmap* dest, int dx, int dy, int sx, int sy, int w, int h ) = 0;
	virtual void Clear( int l, int t, int w, int h, int color = COLORKEY16 ) = 0;

};

// supports 8 bits color mode 
class Bitmap8 : public Bitmap{ 
public:
	Palette *palette;

	~Bitmap8();
	
	virtual void Blit( Bitmap* dest, int dx, int dy, int sx, int sy, int w, int h );
	virtual void BlitInside8( int x, int y, int sx, int sy, int w, int h );
	virtual void BlitOutside8( Bitmap8* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void BlitMask( Bitmap* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void BlitMasked8( Bitmap8* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void BlitMaskedTo16( Bitmap16* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void Clear( int l, int t, int w, int h, int color = COLORKEY8 );
	virtual Bitmap* ConvertFormat( int );
	virtual DWORD GetPixel( int x, int y );
	virtual void PutPixel( int x, int y ); 
	
	inline virtual int MakeColor( RGB color ){ 
		return 0; 
	};
	inline virtual int  GetR( int pixel ){
		return 0;
	};
	inline virtual int  GetG( int pixel ){
		return 0;
	};
	inline virtual int  GetB( int pixel ){
		return 0;
	};
	virtual void SetColor( RGB color );
	virtual void SetColorKey( RGB color );
};

// 15/16位色位图的基类
class BitmapHi : public Bitmap{
public:

	virtual void Blit( Bitmap* dest, int dx, int dy, int sx, int sy, int w, int h );
	virtual void BlitMask( Bitmap* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void Clear( int l, int t, int w, int h, int color = COLORKEY16 );
	// 辅助函数
	virtual void BlitMasked( Bitmap* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void BlitInside( int x, int y, int sx, int sy, int w, int h );
	virtual void BlitOutside( Bitmap* dest, int x, int y, int sx, int sy, int w, int h );

	BitmapHi();
	~BitmapHi();

	virtual Bitmap* ConvertFormat( int ) = 0;
	virtual DWORD GetPixel( int x, int y );
	virtual void PutPixel( int x, int y ); 
	virtual int  MakeColor( RGB color ) = 0;

	// set current draw color
	inline virtual void SetColor( RGB color )
	{
		Bitmap::color = MakeColor( color );
	};
	// set color key
	inline virtual void SetColorKey( RGB color ){
		colorKey = MakeColor( color );
	};

};

// 565模式的位图
class Bitmap16 : public BitmapHi{
public:

	virtual Bitmap* ConvertFormat( int );

	inline virtual int  MakeColor( RGB color )
	{
		return (((color & 0xf80000)>>8 ) | (color & 0xfc00)>>5 | (color&0xf8)>>3);
	};

};

// 555模式的位图
class Bitmap15 : public BitmapHi{
public:

	virtual Bitmap* ConvertFormat( int );

	inline virtual int  MakeColor( RGB color )
	{
		return (((color & 0xf80000)>>9 ) | (color & 0xf800)>>5 | (color&0xf8)>>3);
	};

};

// supports 24 bits color mode
class Bitmap24 : public Bitmap{
public:

	virtual void Blit( Bitmap* dest, int dx, int dy, int sx, int sy, int w, int h );
	void BlitInside24( int x, int y, int sx, int sy, int w, int h );
	void BlitOutside24( Bitmap24* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void BlitMask( Bitmap* dest, int x, int y, int sx, int sy, int w, int h );
	void BlitMasked24( Bitmap24* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void Clear( int l, int t, int w, int h, int color );
	virtual Bitmap* ConvertFormat( int );
	virtual DWORD GetPixel( int x, int y );
	virtual void PutPixel( int x, int y ); 
	virtual void SetColor( int color );
	virtual void SetColorKey( int color );
	
	inline virtual int MakeColor( RGB color ){
		return ( color & 0xffffff );
	};

	inline virtual int  GetR( int pixel ){
		return (( pixel >> 16 ) & 0xff );
	};

	inline virtual int  GetG( int pixel ){
		return (( pixel >> 8 ) & 0xff );
	};

	inline virtual int  GetB( int pixel ){
		return ( pixel & 0xff );
	};

};

// supports 32 bits color mode
class Bitmap32 : public Bitmap{
public:

	virtual void Blit( Bitmap* dest, int dx, int dy, int sx, int sy, int w, int h );
	void BlitInside32( int x, int y, int sx, int sy, int w, int h );
	void BlitOutside32( Bitmap32* dest, int x, int y, int sx, int sy, int w, int h );
	virtual void BlitMask( Bitmap* dest, int x, int y, int sx, int sy, int w, int h );
	void BlitMasked32( Bitmap32* dest, int x, int y, int sx, int sy, int w, int h );
	virtual Bitmap* ConvertFormat( int );
	virtual void Clear( int l, int t, int w, int h, int color );
	virtual DWORD GetPixel( int x, int y );
	virtual void PutPixel( int x, int y ); 

⌨️ 快捷键说明

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