tamashii 发表于 2009-11-26 00:18:50

MDX游戏设备框架

呃……里面包含有Direct3D、DirectInput和DirectSound
用Sharp Develop里面的框架改的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

// DirectX 名称空间
using Microsoft.DirectX;
using Direct3D = Microsoft.DirectX.Direct3D;
using DirectSound = Microsoft.DirectX.DirectSound;
using DirectInput = Microsoft.DirectX.DirectInput;

namespace YourGame
{
    public class Game : Form
    {      
      static string title = "游戏框架";   // 游戏标题
      static int screenheight = 600;   // 窗口宽度高度
      static int screenwidth = 800;   // 窗口宽度
      static bool paused = false;       // 暂停游戏
      static bool windowed = false;// 窗口模式
      static bool graphicslost = false;   // 图像设备丢失
      
      Direct3D.Device graphics = null;
      
      DirectSound.Device sound = null;
      
      DirectInput.Device keyboard = null;
      DirectInput.Device mouse = null;
      DirectInput.Device gameinput = null;
      
      public Game()
      {
            // 初始化游戏窗口
            this.ClientSize = new Size(screenwidth, screenheight);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = title;
      }
      
      public void InitializeGraphics()
      {
            // 初始化 3D 环境
            Direct3D.PresentParameters p = new Direct3D.PresentParameters();
            p.SwapEffect = Direct3D.SwapEffect.Discard;
            if (windowed)
            {
                p.Windowed = true;
            }
            else
            {
                Direct3D.Format current = Direct3D.Manager.Adapters.CurrentDisplayMode.Format;
                p.BackBufferCount = 1;
                p.BackBufferFormat = current;
                p.BackBufferHeight = screenheight;
                p.BackBufferWidth = screenwidth;
                p.Windowed = false;
            }
            
            // 建立图像设备
            graphics = new Direct3D.Device(Direct3D.Manager.Adapters.Adapter, Direct3D.DeviceType.Hardware, this, Direct3D.CreateFlags.SoftwareVertexProcessing, p);
            
            // 初始化渲染
            graphics.RenderState.CullMode = Direct3D.Cull.None;
            graphics.RenderState.AlphaBlendEnable = true;
            graphics.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Add;
            graphics.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha;
            graphics.RenderState.SourceBlend = Direct3D.Blend.SourceAlpha;
            
            // 设置事件句柄
            graphics.DeviceLost   += new EventHandler(this.InvalidateDeviceObjects);
            graphics.DeviceReset    += new EventHandler(this.RestoreDeviceObjects);
            graphics.Disposing      += new EventHandler(this.DeleteDeviceObjects);
            graphics.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);
      }
      
      public void InitializeSound()
      {
            // 初始化声音设备
            sound = new DirectSound.Device();
            sound.SetCooperativeLevel(this.Handle, DirectSound.CooperativeLevel.Priority);
      }
      
      public void InitializeInput()
      {
            // 键盘
            keyboard = new DirectInput.Device(DirectInput.SystemGuid.Keyboard);
            keyboard.SetCooperativeLevel(
                this.Handle,
                DirectInput.CooperativeLevelFlags.Foreground |
                DirectInput.CooperativeLevelFlags.NonExclusive);
            // 鼠标
            mouse = new DirectInput.Device(DirectInput.SystemGuid.Mouse);
            mouse.SetCooperativeLevel(
                this.Handle,
                DirectInput.CooperativeLevelFlags.Foreground |
                DirectInput.CooperativeLevelFlags.NonExclusive);
            mouse.SetDataFormat(DirectInput.DeviceDataFormat.Mouse);
            // 手柄
            foreach( DirectInput.DeviceInstance i in
                DirectInput.Manager.GetDevices(
                  DirectInput.DeviceClass.GameControl,
                  DirectInput.EnumDevicesFlags.AttachedOnly))
            {
                gameinput = new DirectInput.Device(i.InstanceGuid);
                gameinput.SetCooperativeLevel(
                  this.Handle,
                  DirectInput.CooperativeLevelFlags.Foreground |
                  DirectInput.CooperativeLevelFlags.NonExclusive);
                gameinput.SetDataFormat(DirectInput.DeviceDataFormat.Joystick);
            }
      }
      
      protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)
      {
      }
      
      protected virtual void RestoreDeviceObjects(object sender, EventArgs e)
      {
      }
      
      protected virtual void DeleteDeviceObjects(object sender, EventArgs e)
      {
      }
      
      protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)
      {
            e.Cancel = true;
      }
      
      /// <summary>
      /// 用来播放场景帧
      /// </summary>
      protected virtual void ProcessFrame()
      {
            if (!paused)
            {
                // TODO : 在此添加帧移动代码
            }
            else
                System.Threading.Thread.Sleep(1);
      }
      
      /// <summary>
      /// 渲染场景
      /// </summary>
      protected virtual void Render()
      {
            if (graphics != null)
            {
                if (graphicslost)
                {
                  try
                  {
                        graphics.TestCooperativeLevel();
                  }
                  catch (Direct3D.DeviceLostException)
                  {
                        return;
                  }
                  catch (Direct3D.DeviceNotResetException)
                  {
                        graphics.Reset(graphics.PresentationParameters);
                  }
                }
                try
                {
                  graphics.Clear(Direct3D.ClearFlags.Target, 0, 1.0f, 0);
                  graphics.BeginScene();
                  
                  // TODO : Add rendering code here.
                  
                  graphics.EndScene();
                  graphics.Present();
                }
                catch (Direct3D.DeviceLostException)
                {
                  graphicslost = true;
                }
            }
      }
      
      /// <summary>
      /// 主循环
      /// </summary>
      public void Run()
      {
            // 当窗体仍然存在时,移动帧,并渲染。
            while (Created) {
                ProcessFrame();
                Render();
                Application.DoEvents();
            }
      }
      
      protected override void OnPaint(PaintEventArgs e)
      {
            this.Render();
      }
      
      protected override void OnKeyPress(KeyPressEventArgs e)
      {
            base.OnKeyPress(e);
            if ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) {
                this.Close();
            }
      }
      
      /// <summary>
      /// 应用程序主接口
      /// </summary>
      static void Main()
      {
            try
            {
                Game game = new Game();
                game.InitializeGraphics();
                game.InitializeSound();
                game.InitializeInput();
                game.Show();
                game.Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString(), title, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
      }
    }
}

貘良了 发表于 2009-11-26 04:45:59

感恩节里多谢LZ分享。
另外问一下,Sharp Develop是什么?

shawind 发表于 2009-11-26 12:04:07

一个免费开源的用来开发.net程序的IDE

tamashii 发表于 2009-11-26 20:29:44

换上了中文注释。
这个框架采用的Swap Effect是Discarding
硬件加速、软件处理方式
前景模式输入设备
页: [1]
查看完整版本: MDX游戏设备框架