幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3819|回复: 3

MDX游戏设备框架

[复制链接]

24

主题

117

帖子

1274

积分

⑥精研

积分
1274
QQ
发表于 2009-11-26 00:18:50 | 显示全部楼层 |阅读模式
呃……里面包含有Direct3D、DirectInput和DirectSound
用Sharp Develop里面的框架改的。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. // DirectX 名称空间
  7. using Microsoft.DirectX;
  8. using Direct3D = Microsoft.DirectX.Direct3D;
  9. using DirectSound = Microsoft.DirectX.DirectSound;
  10. using DirectInput = Microsoft.DirectX.DirectInput;
  11. namespace YourGame
  12. {
  13.     public class Game : Form
  14.     {        
  15.         static string title = "游戏框架";     // 游戏标题
  16.         static int screenheight = 600;     // 窗口宽度高度
  17.         static int screenwidth = 800;     // 窗口宽度
  18.         static bool paused = false;       // 暂停游戏
  19.         static bool windowed = false;  // 窗口模式
  20.         static bool graphicslost = false;     // 图像设备丢失
  21.         
  22.         Direct3D.Device graphics = null;
  23.         
  24.         DirectSound.Device sound = null;
  25.         
  26.         DirectInput.Device keyboard = null;
  27.         DirectInput.Device mouse = null;
  28.         DirectInput.Device gameinput = null;
  29.         
  30.         public Game()
  31.         {
  32.             // 初始化游戏窗口
  33.             this.ClientSize = new Size(screenwidth, screenheight);
  34.             this.FormBorderStyle = FormBorderStyle.FixedSingle;
  35.             this.MaximizeBox = false;
  36.             this.StartPosition = FormStartPosition.CenterScreen;
  37.             this.Text = title;
  38.         }
  39.         
  40.         public void InitializeGraphics()
  41.         {
  42.             // 初始化 3D 环境
  43.             Direct3D.PresentParameters p = new Direct3D.PresentParameters();
  44.             p.SwapEffect = Direct3D.SwapEffect.Discard;
  45.             if (windowed)
  46.             {
  47.                 p.Windowed = true;
  48.             }
  49.             else
  50.             {
  51.                 Direct3D.Format current = Direct3D.Manager.Adapters[0].CurrentDisplayMode.Format;
  52.                 p.BackBufferCount = 1;
  53.                 p.BackBufferFormat = current;
  54.                 p.BackBufferHeight = screenheight;
  55.                 p.BackBufferWidth = screenwidth;
  56.                 p.Windowed = false;
  57.             }
  58.             
  59.             // 建立图像设备
  60.             graphics = new Direct3D.Device(Direct3D.Manager.Adapters[0].Adapter, Direct3D.DeviceType.Hardware, this, Direct3D.CreateFlags.SoftwareVertexProcessing, p);
  61.             
  62.             // 初始化渲染
  63.             graphics.RenderState.CullMode = Direct3D.Cull.None;
  64.             graphics.RenderState.AlphaBlendEnable = true;
  65.             graphics.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Add;
  66.             graphics.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha;
  67.             graphics.RenderState.SourceBlend = Direct3D.Blend.SourceAlpha;
  68.             
  69.             // 设置事件句柄
  70.             graphics.DeviceLost     += new EventHandler(this.InvalidateDeviceObjects);
  71.             graphics.DeviceReset    += new EventHandler(this.RestoreDeviceObjects);
  72.             graphics.Disposing      += new EventHandler(this.DeleteDeviceObjects);
  73.             graphics.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);
  74.         }
  75.         
  76.         public void InitializeSound()
  77.         {
  78.             // 初始化声音设备
  79.             sound = new DirectSound.Device();
  80.             sound.SetCooperativeLevel(this.Handle, DirectSound.CooperativeLevel.Priority);
  81.         }
  82.         
  83.         public void InitializeInput()
  84.         {
  85.             // 键盘
  86.             keyboard = new DirectInput.Device(DirectInput.SystemGuid.Keyboard);
  87.             keyboard.SetCooperativeLevel(
  88.                 this.Handle,
  89.                 DirectInput.CooperativeLevelFlags.Foreground |
  90.                 DirectInput.CooperativeLevelFlags.NonExclusive);
  91.             // 鼠标
  92.             mouse = new DirectInput.Device(DirectInput.SystemGuid.Mouse);
  93.             mouse.SetCooperativeLevel(
  94.                 this.Handle,
  95.                 DirectInput.CooperativeLevelFlags.Foreground |
  96.                 DirectInput.CooperativeLevelFlags.NonExclusive);
  97.             mouse.SetDataFormat(DirectInput.DeviceDataFormat.Mouse);
  98.             // 手柄
  99.             foreach( DirectInput.DeviceInstance i in
  100.                 DirectInput.Manager.GetDevices(
  101.                     DirectInput.DeviceClass.GameControl,
  102.                     DirectInput.EnumDevicesFlags.AttachedOnly))
  103.             {
  104.                 gameinput = new DirectInput.Device(i.InstanceGuid);
  105.                 gameinput.SetCooperativeLevel(
  106.                     this.Handle,
  107.                     DirectInput.CooperativeLevelFlags.Foreground |
  108.                     DirectInput.CooperativeLevelFlags.NonExclusive);
  109.                 gameinput.SetDataFormat(DirectInput.DeviceDataFormat.Joystick);
  110.             }
  111.         }
  112.         
  113.         protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)
  114.         {
  115.         }
  116.         
  117.         protected virtual void RestoreDeviceObjects(object sender, EventArgs e)
  118.         {
  119.         }
  120.         
  121.         protected virtual void DeleteDeviceObjects(object sender, EventArgs e)
  122.         {
  123.         }
  124.         
  125.         protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)
  126.         {
  127.             e.Cancel = true;
  128.         }
  129.         
  130.         /// <summary>
  131.         /// 用来播放场景帧
  132.         /// </summary>
  133.         protected virtual void ProcessFrame()
  134.         {
  135.             if (!paused)
  136.             {
  137.                 // TODO : 在此添加帧移动代码
  138.             }
  139.             else
  140.                 System.Threading.Thread.Sleep(1);
  141.         }
  142.         
  143.         /// <summary>
  144.         /// 渲染场景
  145.         /// </summary>
  146.         protected virtual void Render()
  147.         {
  148.             if (graphics != null)
  149.             {
  150.                 if (graphicslost)
  151.                 {
  152.                     try
  153.                     {
  154.                         graphics.TestCooperativeLevel();
  155.                     }
  156.                     catch (Direct3D.DeviceLostException)
  157.                     {
  158.                         return;
  159.                     }
  160.                     catch (Direct3D.DeviceNotResetException)
  161.                     {
  162.                         graphics.Reset(graphics.PresentationParameters);
  163.                     }
  164.                 }
  165.                 try
  166.                 {
  167.                     graphics.Clear(Direct3D.ClearFlags.Target, 0, 1.0f, 0);
  168.                     graphics.BeginScene();
  169.                     
  170.                     // TODO : Add rendering code here.
  171.                     
  172.                     graphics.EndScene();
  173.                     graphics.Present();
  174.                 }
  175.                 catch (Direct3D.DeviceLostException)
  176.                 {
  177.                     graphicslost = true;
  178.                 }
  179.             }
  180.         }
  181.         
  182.         /// <summary>
  183.         /// 主循环
  184.         /// </summary>
  185.         public void Run()
  186.         {
  187.             // 当窗体仍然存在时,移动帧,并渲染。
  188.             while (Created) {
  189.                 ProcessFrame();
  190.                 Render();
  191.                 Application.DoEvents();
  192.             }
  193.         }
  194.         
  195.         protected override void OnPaint(PaintEventArgs e)
  196.         {
  197.             this.Render();
  198.         }
  199.         
  200.         protected override void OnKeyPress(KeyPressEventArgs e)
  201.         {
  202.             base.OnKeyPress(e);
  203.             if ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) {
  204.                 this.Close();
  205.             }
  206.         }
  207.         
  208.         /// <summary>
  209.         /// 应用程序主接口
  210.         /// </summary>
  211.         static void Main()
  212.         {
  213.             try
  214.             {
  215.                 Game game = new Game();
  216.                 game.InitializeGraphics();
  217.                 game.InitializeSound();
  218.                 game.InitializeInput();
  219.                 game.Show();
  220.                 game.Run();
  221.             }
  222.             catch (Exception ex)
  223.             {
  224.                 MessageBox.Show("错误:" + ex.ToString(), title, MessageBoxButtons.OK, MessageBoxIcon.Hand);
  225.             }
  226.         }
  227.     }
  228. }
复制代码
Tamashii是啥意思? 魂! ======================= 我真是败给C++的面向对象了啊……
回复

使用道具 举报

86

主题

250

帖子

390

积分

版主

Rank: 7Rank: 7Rank: 7

积分
390
发表于 2009-11-26 04:45:59 | 显示全部楼层
感恩节里多谢LZ分享。
另外问一下,Sharp Develop是什么?
回复 支持 反对

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
发表于 2009-11-26 12:04:07 | 显示全部楼层
一个免费开源的用来开发.net程序的IDE
え~え~お!!!
回复 支持 反对

使用道具 举报

24

主题

117

帖子

1274

积分

⑥精研

积分
1274
QQ
 楼主| 发表于 2009-11-26 20:29:44 | 显示全部楼层
换上了中文注释。
这个框架采用的Swap Effect是Discarding
硬件加速、软件处理方式
前景模式输入设备
Tamashii是啥意思? 魂! ======================= 我真是败给C++的面向对象了啊……
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-3-28 23:39 , Processed in 0.024042 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表