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

📄 mainwin.h

📁 源代码演示了在Windows下角色扮演游戏的制作过程
💻 H
字号:
//
// Script player主窗口(MainWindow)
//
//		Copyright (c) 2000-2002 Chihiro.SAKAMOTO (HyperWorks)
//
#ifndef __MainWin_h__
#define __MainWin_h__

#include "Window.h"
#include "dc.h"
#include "Action.h"
#include "Script.h"
#include "Image.h"
#include "DrawImage.h"

//
// 主窗口类
//
class CMainWin: public CWindow {
  public:
	// 常数

	// 动作
	enum	{
		ActionNop,
		ActionScriptDone,
		ActionScript,
	} ;

	// 项目的最大值
	enum	{
		MAX_MENU_ITEM = 8,
		MAX_MENU_TEXT = 60,
	} ;

  protected:
	// 重叠的显示位置
	enum	{
		None = 0,
		Left = 1,
		Right = 2,
		Both = 3,
		Center = 4,
	} ;

	// 消息的显示位置
	enum	{
		MSG_W = MessageFont * MessageWidth / 2 + 20,
		MSG_H = (MessageFont + 2) * MessageLine + 14,
		MSG_X = (WindowWidth - MSG_W) / 2,
		MSG_Y = WindowHeight - MSG_H - 8,
		MSG_TEXTOFFSET_X = MSG_X + 10,
		MSG_TEXTOFFSET_Y = MSG_Y + 7,
		MSG_TEXTSPACE_Y = 2,
		WAITMARK_X = MessageWidth - 2,
		WAITMARK_Y = MessageLine - 1,
	} ;

	// 菜单的显示位置
	enum	{
		MENU_X = 20,
		MENU_Y = MSG_Y - 2,		// 与消息区域不重叠
		MENU_WIDTH = (MAX_MENU_TEXT + 2) * MessageFont / 2,
		MENU_HEIGHT = (MAX_MENU_ITEM + 1) * MessageFont,
		MENU_MIN_WIDTH = 50,
		MENU_FRAME_WIDTH = 10,
		MENU_FRAME_HEIGHT = 10,
		MENU_ITEM_SPACE = 2,
		MENU_ITEM_HEIGHT = MessageFont + MENU_ITEM_SPACE,
	} ;

	// 菜单项目
	struct CMenuItem {
		char		text[MAX_MENU_TEXT];
		int			anser;
		int			length;
		COLORREF	color;
	} ;

	// 画面合成项目
	enum	{
		TextMessage = 1 << 0,
		TextWaitMark = 1 << 1,
		MenuFrame = 1 << 2,
		MenuItemFirst = 3,
	} ;
	static unsigned MenuItem(int n) { return 1 << (MenuItemFirst + n); }

  public:
	CMainWin();
	~CMainWin();

	LRESULT WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam);

	virtual void OnInitSubMenu(HMENU hMenu, UINT id);
	virtual BOOL OnIdle(long count);

	void OnCommand(UINT notifyCode, UINT id, HWND ctrl);

	BOOL SetAction(int action, int param=0);
	bool StartScript(const char *name, int mode=0);
	void StartMainMenu();

	void WriteMessage(const char *msg);
	void ClearMessage();
	void HideWaitMark();
	void ShowMessageWindow();
	void HideMessageWindow(bool update=true);
	void FlipMessageWindow();

	void SetMenuItem(const char *lavel, int anser);
	void OpenMenu();
	void HideMenuWindow(bool update=true);
	void ClearMenuItemCount() { MenuCount = 0; }
	int GetMenuItemCount() const { return MenuCount; }
	int GetMenuAnser(int index) const { return MenuBuffer[index].anser; }
	int GetMenuSelect(CPoint point);
	void SelectMenu(int index, bool select);

	BOOL LoadImageBack(const char *name);

	BOOL LoadImageLeft(const char *name) { return LoadImageOverlap(name, Left); }
	BOOL LoadImageRight(const char *name) { return LoadImageOverlap(name, Right); }
	BOOL LoadImageCenter(const char *name) { return LoadImageOverlap(name, Center); }
	BOOL LoadImageOverlap(const char *name) { return LoadImageOverlap(name, Both); }

	void ClearBack() { ClearImageBack(); }
	void ClearCenter() { HideOverlapLayer(Center); }
	void ClearLeft() { HideOverlapLayer(Left); }
	void ClearRight() { HideOverlapLayer(Right); }
	void ClearOverlap() { HideOverlapLayer(Both); }

	const CRect &GetInvalidRect() const { return InvalidRect; }

	void Repaint(const CRect &rect);

	void UpdateImage(const CRect &rect);

  protected:
	virtual BOOL PreCreateWindow(CREATESTRUCT &cs);

	BOOL OnCreate(CREATESTRUCT *cs);
	void OnFirstAction();
	void OnClose();
	void OnPaint();

	BOOL LoadImageOverlap(const char *name, int position);
	BOOL ClearImageBack();
	void ShowOverlapLayer(int pos);
	void HideOverlapLayer(int pos);
	BOOL Update(bool repaint=true);

	void HideAllLayer(COLORREF pix);

	int MsgX(int x) const { return x * MessageFont / 2 + MSG_TEXTOFFSET_X; }
	int MsgY(int y) const { return y * (MessageFont + MSG_TEXTSPACE_Y) + MSG_TEXTOFFSET_Y; }
	BOOL Kinsoku(const char *p);

	void Mixing(const CRect &rect, unsigned flags = ~0);

	void Invalidate(const CRect &rect) { InvalidRect |= rect; }

	int FormatMessage(const char *msg);

	BOOL IsStopScript() const { return Action->IsScriptRunning(); }

  protected:
	HFONT	hFont;						// 文字的字型

	CAction        *Action;				// 现在选择的动作集
	CAction			NopAction;			// 什么都不做的动作集
	CScriptAction	ScriptAction;		// 执行脚本时所用的动作集

	CDrawImage	MixedImage;				// 合成结果
	CImage		BackLayer;				// 背景
	CImage		OverlapLayer;			// 重叠
	unsigned	OverlapFlags;			// 重叠的状态旗标
	BOOL		TextDisplay;			// 文字显示旗标
	BOOL		WaitMarkShowing;		// 显示等待键盘输入的符号

	CRect		InvalidRect;			// 无效区域
	CRect		TextRect;				// 文字显示区域
	CRect		WaitMarkRect;			// 等待键盘输入符号显示区域
	CRect		MenuRect;				// 菜单显示区域
	CRect		OverlapBounds;			// 重叠的有效区域
	BOOL		BackShow;				// 显示那个背景?
	BOOL		OverlapShow;			// 显示那个重叠?
	BOOL		TextShow;				// 文字显示旗标
	BOOL		MenuShow;				// 菜单显示旗标
	COLORREF	BgColor;				// 当没有背景时填入的色彩

	char	MsgBuffer[MessageLine][MessageWidth + 1];
	int		CurX;
	int		CurY;

	CMenuItem	MenuBuffer[MAX_MENU_ITEM];
	int			MenuCount;

	static const CRect Position[];
} ;

// Inline成员函数

//
// 显示内存上的图像
//
inline void CMainWin::Repaint(const CRect &rect)
{
	MixedImage.Draw(CClientDC(this), rect);
}

//
// 在重绘之前有无效区域,则在内存上的图像会合成
//
inline void CMainWin::UpdateImage(const CRect &rect)
{
	Update();
	Repaint(rect);
}

//
// 从鼠标光标取得菜单项目
//
inline int CMainWin::GetMenuSelect(CPoint point)
{
	if (point.x < MenuRect.left + MENU_FRAME_WIDTH
	 || point.y < MenuRect.top + MENU_FRAME_HEIGHT
	 || point.x >= MenuRect.right - MENU_FRAME_WIDTH
	 || point.y >= MenuRect.bottom - MENU_FRAME_HEIGHT)
		return -1;
	return (point.y - MenuRect.top - MENU_FRAME_WIDTH) / MENU_ITEM_HEIGHT;
}

#endif

⌨️ 快捷键说明

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