📄 gameloophandler.cs
字号:
using System;
using System.Collections;
using System.Windows.Forms;
namespace DDGameHelper
{
#region GameLoop 事件参数类定义
/// <summary>
/// GameLoop 相关事件参数
/// </summary>
public class EventGameLoopArgs : EventArgs
{
/// <summary>
/// 游戏所属 Form (输入输出 bind )
/// </summary>
public Form OwnerForm;
/// <summary>
/// 每个循环消耗(经过)的时间,以 0.0001 秒为单位
/// </summary>
public double LoopDuration;
/// <summary>
/// 游戏循环计数器
/// </summary>
public long Counter;
/// <summary>
/// 当前游戏循环使用的 DXTimer
/// </summary>
public DXTimer DXTimeR;
/// <summary>
/// 游戏循环中要用到的 DirectDraw 显示输出处理者
/// </summary>
public DDHandler DDHandleR;
/// <summary>
/// 游戏循环中要用到的 DirectInput 用户输入处理者
/// </summary>
public DIHandler DIHandleR;
/// <summary>
/// DirectSound处理者
/// </summary>
public DSHandler DSHandleR;
/// <summary>
/// 限速模式
/// </summary>
public bool IsSpeedLimitMode;
}
#endregion
#region 委托定义
/// <summary>
/// 所有循环开始前
/// </summary>
public delegate void On_Loop_Before(Object sender, EventGameLoopArgs e);
/// <summary>
/// 每一次 GameLoop 循环当中发生
/// </summary>
public delegate void On_Loop_Progress(Object sender, EventGameLoopArgs e);
/// <summary>
/// 所有循环结束后
/// </summary>
public delegate void On_Loop_After(Object sender, EventGameLoopArgs e);
/// <summary>
/// 游戏所属 Form 获取到焦点时发生
/// </summary>
public delegate void On_Loop_Form_GetFocus(Object sender, EventGameLoopArgs e);
/// <summary>
/// 游戏所属 Form 失去焦点时发生
/// </summary>
public delegate void On_Loop_Form_LostFocus(Object sender, EventGameLoopArgs e);
/// <summary>
/// 游戏所属 Form 重绘时发生
/// </summary>
public delegate void On_Loop_Form_Paint(Object sender, EventGameLoopArgs e);
#endregion
/// <summary>
/// 游戏主循环
/// </summary>
public class GameLoopHandler
{
#region 事件定义(按照其生命周期排列)
/// <summary>
/// 所有循环开始前
/// </summary>
public event On_Loop_Before On_Loop_Before;
/// <summary>
/// 每个 GameLoop 循环
/// </summary>
public event On_Loop_Progress On_Loop_Progress;
/// <summary>
/// 所有循环结束后
/// </summary>
public event On_Loop_After On_Loop_After;
/// <summary>
/// 循环中Form获取到焦点
/// </summary>
public event On_Loop_Form_GetFocus On_Loop_Form_GetFocus;
/// <summary>
/// 循环中Form失去焦点
/// </summary>
public event On_Loop_Form_LostFocus On_Loop_Form_LostFocus;
/// <summary>
/// 循环中Form重绘
/// </summary>
public event On_Loop_Form_Paint On_Loop_Form_Paint;
#endregion
#region 受保护成员定义
/// <summary>
/// 标明是否为限速模式(每秒小于 100 fps )
/// </summary>
protected bool _isSpeedLimitMode;
/// <summary>
/// 用于挂靠游戏循环输入输出的 form
/// </summary>
protected Form _ownerForm;
/// <summary>
/// 每次循环所消耗的时间 0.0001 秒为单位
/// </summary>
protected double _loopDuration;
/// <summary>
/// 游戏循环中用到的 dxtimer
/// </summary>
protected DXTimer _dXTimer;
/// <summary>
/// 游戏循环计数器
/// </summary>
protected long _counter;
/// <summary>
/// 返回信息
/// </summary>
protected string _msg = string.Empty;
/// <summary>
/// 游戏循环中要用到的 DirectDraw 显示输出处理者
/// </summary>
protected DDHandler _dDHandler;
/// <summary>
/// 游戏循环中要用到的 DirectInput 用户输入处理者
/// </summary>
protected DIHandler _dIHandler;
/// <summary>
/// DirectSound 处理者
/// </summary>
protected DSHandler _dSHandler;
/// <summary>
/// 随机数发生器
/// </summary>
protected Random _r;
/// <summary>
/// GameObject集合
/// </summary>
protected ArrayList _gameObjectCollection;
#endregion
#region 公共成员
/// <summary>
/// 标明是否为限速模式(每秒小于 100 fps )
/// </summary>
public bool IsSpeedLimitMode{get{return _isSpeedLimitMode;}set{_isSpeedLimitMode=value;}}
/// <summary>
/// 用于挂靠游戏循环输入输出的 form
/// </summary>
public Form OwnerForm{get{return _ownerForm;}set{_ownerForm=value;}}
/// <summary>
/// 每次循环所消耗的时间 0.0001 秒为单位
/// </summary>
public double LoopDuration{get{return _loopDuration;}set{_loopDuration=value;}}
/// <summary>
/// 游戏循环中用到的 dxtimer
/// </summary>
public DXTimer DXTimer{get{return _dXTimer;}set{_dXTimer=value;}}
/// <summary>
/// 游戏循环计数器
/// </summary>
public long Counter{get{return _counter;}set{_counter=value;}}
/// <summary>
/// 游戏循环中要用到的 DirectDraw 显示输出处理者
/// </summary>
public DDHandler DDHandler{get{return _dDHandler;}set{_dDHandler=value;}}
/// <summary>
/// 游戏循环中要用到的 DirectInput 用户输入处理者
/// </summary>
public DIHandler DIHandler{get{return _dIHandler;}set{_dIHandler=value;}}
/// <summary>
/// DirectSound 处理者
/// </summary>
public DSHandler DSHandler{get{return _dSHandler;}set{_dSHandler=value;}}
/// <summary>
/// 随机数发生器
/// </summary>
public Random RandoM{get{return _r;}set{_r = value;}}
/// <summary>
/// GameObject集合
/// </summary>
public ArrayList GameObjectCollection{get{return _gameObjectCollection;}set{_gameObjectCollection = value;}}
#endregion
#region GameLoop 的构造函数
/// <summary>
/// GameLoop 的构造函数(初始化各个 handler 及 form)
/// </summary>
public GameLoopHandler(Form ownerForm)
{
_ownerForm=ownerForm;
_dXTimer=new DXTimer();
_dDHandler=new DDHandler(_ownerForm);
_dIHandler=new DIHandler(_ownerForm);
_dSHandler=new DSHandler(_ownerForm);
_isSpeedLimitMode=true;
_r=new Random();
_gameObjectCollection = new ArrayList();
_ownerForm.GotFocus+=new EventHandler(_ownerForm_GotFocus);
_ownerForm.LostFocus+=new EventHandler(_ownerForm_LostFocus);
_ownerForm.Paint+=new PaintEventHandler(_ownerForm_Paint);
}
#endregion
#region GameLoop 的循环体
/// <summary>
/// 开始循环
/// </summary>
public string Loop()
{
bool IsInit=_dDHandler.Init(ref _msg); //初始化 dd
if(!IsInit)return _msg;
IsInit=_dIHandler.Init(ref _msg); //初始化 di
if(!IsInit)return _msg;
IsInit=_dSHandler.Init(ref _msg); //初始化 ds
if(!IsInit)return _msg;
//大循环前事件
if(On_Loop_Before!=null)On_Loop_Before(this, GetEventGameLoopArgs());
_dXTimer.Start();
while (_ownerForm.Created) //如果 form 一直处理 Created 状态,则一直循环
{
if (!_ownerForm.Focused) //窗体失去交点之后
{
System.Threading.Thread.Sleep(10);
Application.DoEvents(); // form 上的相关事件
continue;
}
if(_isSpeedLimitMode) //限速
{
_loopDuration += _dXTimer.GetElapsedMillisecond();
if (_loopDuration > 10.0)
{
_counter++;
if(On_Loop_Progress!=null)On_Loop_Progress(this, GetEventGameLoopArgs()); //循环中事件
_loopDuration = 0.0;
}
else
{
System.Threading.Thread.Sleep(1);
}
}
else
{
_counter++;
_loopDuration = _dXTimer.GetElapsedMillisecond();
if(On_Loop_Progress!=null)On_Loop_Progress(this, GetEventGameLoopArgs()); //循环中事件
}
Application.DoEvents();
}
//大循环后事件
if(On_Loop_After!=null)On_Loop_After(this, GetEventGameLoopArgs());
return _msg;
}
#endregion
#region 相关事件 bind 及方法调用
/// <summary>
/// 获取当前的GameLoop事件参数
/// </summary>
private EventGameLoopArgs GetEventGameLoopArgs()
{
EventGameLoopArgs e=new EventGameLoopArgs();
e.OwnerForm=_ownerForm;
e.LoopDuration=_loopDuration;
e.DXTimeR=_dXTimer;
e.DIHandleR=_dIHandler;
e.DDHandleR=_dDHandler;
e.DSHandleR=_dSHandler;
e.IsSpeedLimitMode=_isSpeedLimitMode;
e.Counter=_counter;
return e;
}
private void _ownerForm_GotFocus(object sender, EventArgs e)
{
if(On_Loop_Form_GetFocus!=null)On_Loop_Form_GetFocus(this, GetEventGameLoopArgs());
}
private void _ownerForm_LostFocus(object sender, EventArgs e)
{
if(On_Loop_Form_LostFocus!=null)On_Loop_Form_LostFocus(this, GetEventGameLoopArgs());
}
private void _ownerForm_Paint(object sender, PaintEventArgs e)
{
if(On_Loop_Form_Paint!=null)On_Loop_Form_Paint(this, GetEventGameLoopArgs());
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -