📄 gengine.h
字号:
#ifndef _GENGINE_H
#define _GENGINE_H
#include "gl.h"
#include "../common.h"
#include <ddraw.h>
#include <dinput.h>
// 主画面的尺寸
#define WIN_WIDTH 640
#define WIN_HEIGHT 352
// tile的尺寸
#define TILE_WIDTH 64
#define TILE_HEIGHT 32
//主画面的尺寸(地图坐标)
#define SCENE_WIDTH 10
#define SCENE_HEIGHT 22
//方向
#define DIR_UP 0
#define DIR_UPLEFT 1
#define DIR_LEFT 2
#define DIR_DOWNLEFT 3
#define DIR_DOWN 4
#define DIR_DOWNRIGHT 5
#define DIR_RIGHT 6
#define DIR_UPRIGHT 7
#define DIR_UNKNOWN 8
// directdraw
extern LPDIRECTDRAW lpdd;
extern LPDIRECTDRAWSURFACE lpDDSScreen;//DirectDraw主表面
extern DDSURFACEDESC ddsd;
extern CRITICAL_SECTION dDrawCritical;
extern Bitmap* screen; // 实际屏幕,即为DirectDraw主表面
extern Bitmap* backScreen; // 作图时操作的"屏幕"
// directinput
#define KEYBUFFERSIZE 32 // 键盘缓冲区的大小
extern LPDIRECTINPUT lpDI;
extern LPDIRECTINPUTDEVICE lpDIKeyboard;
extern LPDIRECTINPUTDEVICE lpDIMouse;
/////////////////////////////////////////////////////////////////////
extern void (*UpdateScreen)( void ); // 输出到DirectDraw主表面
int InitDDraw( void );
void RestoreDDraw( void );
void DestroyDDraw( void );
int InitGraphicEngine( DWORD w, DWORD h, DWORD colordepth );
int ExitGraphicEngine( void );
int InitDInput( HINSTANCE hInst );
void DestroyDInput( void );
void DInputSync( int active );
DWORD DInputReadkey( void );
int LockScreen( void );
int UnlockScreen( void );
/////////////////////////////////////////////////////////////////////
// 鼠标维护
#define MOUSE_L_DOWN 1
#define MOUSE_R_DOWN 2
extern int volatile mouseX, mouseY; // used by the external code
extern DWORD volatile mousePos; // high word for X, low word for Y
extern int volatile mouseB; // mouse button state
extern int mouseCursorFroze;
void MouseTimerHandler( void );
int InitMouse( void );
void MouseExit( void );
void MouseSetCursor( DWORD type );
void MouseSetFocusPos( int x, int y );
void MouseSetPos( int x, int y );
void MouseSetRange( int l, int t, int r, int b );
void MouseBeforeUpdatescreen( void );
void MouseOn( void );
void MouseOff( void );
/////////////////////////////////////////////////////////////////////
// 光影效果处理
extern Bitmap8 *brightness; // 640x352,合成后的亮度掩图
extern int brighterInstalled;
int InitBrighter( void );
inline int GetBrightness( int x, int y )
{// 获取(x,y)处的亮度
if( x < 0 ) x = 0;
else if( x >= WIN_WIDTH ) x = WIN_WIDTH - 1;
if( y < 0 ) y = 0;
else if( y >= WIN_HEIGHT ) y = WIN_HEIGHT - 1;
return brightness->GetPixel( x/2, y/2 );
};
void CopyGroundBrightnessMMX( int sx, int sy );
int testBrighter( void );
/////////////////////////////////////////////////////////////////////
class Anything{
public:
Bitmap** pic; //图片
int frames; //图片数目
int width, height; //图片的尺寸
int baseX, baseY; //图片的基点
Anything();
~Anything();
};
//静止的完全不变化的物体
class StillObj : public Anything{
public:
virtual int LoadPicture( char *entryName, int level );
virtual void Draw( Bitmap* dest, int, int, int ) = 0;
};
class Ground : public StillObj{
public:
virtual int LoadPicture( int level );
virtual void Draw( Bitmap* dest, int x, int y, int picno );
};
class WallBase : public StillObj{
public:
virtual int LoadPicture( int level );
virtual void Draw( Bitmap* dest, int x, int y, int picno );
};
class Wall : public StillObj{
public:
static short bright[32];
virtual int LoadPicture( int level );
virtual void Draw( Bitmap* dest, int x, int y, int picno );
};
/////////////////////////////////////////////////////////////////////
#define MAZE_SIZE 48
//路径搜索
#define MaxWayLen 40
class Path{
BYTE wayPoint[MaxWayLen];
int len;
int head;
public:
Path();
void Clear( void ){
len = head;
};
bool IsEmpty( void ){
return head == len;
};
int GetWayPoint( void ){
return wayPoint[head++];
};
int FindPath( int sx, int sy, int dx, int dy );
};
//
class Object;
struct Layer{
Layer *next;
Object *obj;
};
//迷宫地图
/*struct Maze{
BYTE plot; //地面图片
BYTE wall; //墙壁编码,0表示没有墙
BYTE wallAttr; //当墙上有火把时,值为动画的帧号
unsigned wallTrans : 1; //墙壁透明
unsigned wallTorch : 1; //墙上有火把
unsigned occupied : 1; //被占用
unsigned blocked : 1; //阻挡
unsigned upToDate : 1; //最近更新
unsigned pending : 1; //
unsigned reserved : 2; //
Layer *layer; //精灵链表
};*/
struct Maze{
unsigned sprite : 11;
unsigned obj : 11;
unsigned occupied : 1;
unsigned isWall : 1;
unsigned plot : 8;
};
extern Maze maze[MAZE_SIZE][MAZE_SIZE];
void MapGenerate( void );
// cache负责画出地面,必要时带有明暗效果
struct Cache{
enum{
NoRefresh = 0,
InRefreshTop = 1,
InRefreshTopLeft = 2,
InRefreshLeft = 3,
InRefreshBottomLeft = 4,
InRefreshBottom = 5,
InRefreshBottomRight= 6,
InRefreshRight = 7,
InRefreshTopRight = 8
}status;
int refreshStep;
Ground floor;
WallBase wallBase;
int sceneX, sceneY;
int mapX, mapY; //cache中心对应的map数组下标
Bitmap* ground;
// 刷新
void RefreshTop( void );
void RefreshTopLeft( void );
void RefreshLeft( void );
void RefreshBottomLeft( void );
void RefreshBottom( void );
void RefreshBottomRight( void );
void RefreshRight( void );
void RefreshTopRight( void );
int InitCache( int level, int mapx, int mapy );
void Redraw( int x, int y, int r, int t, int w, int h );
void DrawGround( int deltax, int deltay );
};
extern Cache cache;
// scene负责墙壁及其他精灵的显示
struct Scene{
int status; //方向
int refreshStep;
Wall wall;
int x, y; //屏幕的右上角的下标
int w, h; //宽和高
int deltaX, deltaY;
int moveX, moveY;
int centerX, centerY;
void Draw( void );
int InitScene( int level, int cx, int cy );
void Update( void );
void UpdateStatus( int dir );
void SetCenter( int cx, int cy ); //设置中心点(地图坐标)
void PixelToPlot( int mx, int my, int *px, int *py );
};
extern Scene scene;
/////////////////////////////////////////////////////////////////////
enum SpriteAction{ saNothing = 0, saStand = 0, saFight, saWalk, saSpell, saBeHit };
enum InterType{ itHit = 0, itCharge };
enum ControlType{ ctNothing = 0, ctMouse, ctKey };
//动作的有关数据
struct ActionInfo{
SpriteAction action;
WORD destx, desty;
//Object* target;
DWORD dir;
};
//动作栈,用来保存动作序列
class ActionStack : public CObject{
protected:
int top;
ActionInfo element[8];
public:
ActionStack();
ActionInfo* Pop( void );
int Push( ActionInfo& );
ActionInfo* GetTop( void );
void Clear( void );
};
//交互数据
struct Interdata{
InterType what;
DWORD info[2];
void *data;
};
//用户操作事件
struct ControlEvent{
ControlType what;
DWORD info[4];
};
/////////////////////////////////////////////////////////////////////
//鬼怪精灵主角
class Object : public CObject{
public:
DWORD attrib;
WORD id;
WORD mx, my; //在地图中的位置
int LoginMaze( void );
void LogoutMaze( void );
int LoginCache( void );
void LogoutCache( void );
char* GetDescription( void );
void Interact( Interdata *in = NULL );
void UpdateStatus( );
virtual void Update();
};
//保存所有精灵的指针
#define MAX_OBJECTS 2048
class ObjectArray{
Object *obj[MAX_OBJECTS];
int tail;
public:
ObjectArray();
WORD Login( Object* ptr );
void Logout( WORD id );
inline void DrawSprite( WORD id ){
obj[id]->Update();
};
};
extern ObjectArray objects;
//主角
class Hero : public Object{
public:
DWORD timer; //计时器,以帧为单位
int deltax, deltay; //相对地图块中心的偏移
WORD destx, desty; //目标位置(地图坐标)
SpriteAction saAction; //当前动作 107
DWORD dir; //运动方向,方向编码:2 6
Path path; //路径(方向码) 345
ActionStack asStack; //动作序列
Object *target; //目标物体
WORD clickDelay; //鼠标click延时
Animate *stand[8], *fight[8], *walk[8], *behit[8]; //图片
RleBitmap *currentFrame;
public:
Hero( int x, int y );
int LoadPicture( void );
void UpdateStatus( ControlEvent *ce );
virtual void Update( );
protected:
void Stand( ControlEvent *ce );
void Walk( ControlEvent *ce );
void Fight( ControlEvent *ce );
void BeHit( ControlEvent *ce );
};
extern Hero *warrior;
int testGraphicEngine( void );
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -