Managed Directx编程

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
namespace section3
{
    class Program
    {
        static void Main1(string[] args)
        {
            foreach (AdapterInformation adapter in Manager.Adapters)
            {
                if (adapter.Equals(Manager.Adapters.Default))
                {

                    Console.WriteLine("这是默认适配器");

                }
                Console.WriteLine("" + adapter.Information.ToString() + "\n\nDisplay Modes:\n");
                foreach (DisplayMode display in adapter.SupportedDisplayModes)
                {
                    Console.WriteLine("{0}x{1}:{2}@{3}Hz", display.Width, display.Height, display.Format, display.RefreshRate);

                }
                Console.WriteLine("\n目前的显示模式是:{0}x{1}:{2}@{3}Hz", adapter.CurrentDisplayMode.Width, adapter.CurrentDisplayMode.Height, adapter.CurrentDisplayMode.Format, adapter.CurrentDisplayMode.RefreshRate);
                Console.WriteLine("\n\n");
            }
            Console.WriteLine("==========================================================================");
            AdapterInformation ai = Manager.Adapters.Default;
            Caps caps = Manager.GetDeviceCaps(ai.Adapter, DeviceType.Hardware);
            if (caps.TextureCaps.SupportsSquareOnly)
            {
                Console.WriteLine("Square textures only 支持");
            }
            DeviceType[] deviceTypes = new DeviceType[] { DeviceType.Hardware, DeviceType.Software, DeviceType.Reference };

            Format[] backBufferFormats = new Format[] { Format.A8R8G8B8, Format.X8R8G8B8, Format.A2R10G10B10, Format.R5G6B5, Format.A1R5G5B5, Format.X1R5G5B5 };

            bool[] windowModes = new bool[] { true, false };

            // For each adapter
            foreach (AdapterInformation adapter in Manager.Adapters)
            {
                ArrayList adapterFormats = new ArrayList();

                // Build the list of adapter formats
                foreach (DisplayMode dispMode in adapter.SupportedDisplayModes)
                {
                    if (!adapterFormats.Contains(dispMode.Format))
                        adapterFormats.Add(dispMode.Format);
                }

                foreach (DeviceType deviceType in deviceTypes)
                {
                    foreach (Format adapterFormat in adapterFormats)
                    {
                        foreach (Format backBufferFormat in backBufferFormats)
                        {
                            foreach (bool windowMode in windowModes)
                            {
                                if (Manager.CheckDeviceType(
                                    adapter.Adapter,
                                    deviceType,
                                    adapterFormat,
                                    backBufferFormat,
                                    windowMode))
                                {
                                    // *** This combination is valid!
                                }
                            } // windowMode
                        } // backBufferFormat
                    } // adapterFormat
                } // deviceType
            } // adapter
            caps = Manager.GetDeviceCaps(Manager.Adapters.Default.Adapter,
                        DeviceType.Hardware);

            if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
            {
                if (caps.DeviceCaps.SupportsPureDevice)
                {
                    Console.WriteLine("Pure Device");
                }

                Console.WriteLine("Hardware Vertex Processing");
                Console.WriteLine("Mixed Vertex Processing");
            }

            Console.WriteLine("Software Vertex Processing");
            DepthFormat[] depthStencilFormats = new DepthFormat[]{
                DepthFormat.D16,
                DepthFormat.D15S1,
                DepthFormat.D24X8,
                DepthFormat.D24S8,
                DepthFormat.D24X4S4,
                DepthFormat.D32,
            };
            /*
            foreach (DepthFormat depthStencilFormat in depthStencilFormats)
            {
                if (Manager.CheckDeviceFormat(adapter, deviceType, adapterFormat,
                        Usage.DepthStencil, ResourceType.Surface, depthStencilFormat))
                {
                    if (Manager.CheckDepthStencilMatch(adapter, deviceType,
                                adapterFormat, backbufferFormat, depthStencilFormat))
                    {
                        // This depth stencil format is compatible
                    }
                }
            }
            */
            // Assumes you have:
            //
            // AdapterInformation	adapter;
            // DeviceType		deviceType;
            // CreateFlags		createFlags;
            // DisplayMode		displayMode;
            // Format		backbufferFormat;
            // bool			windowed;

            PresentParameters presentation = new PresentParameters();
            /*
                presentation.BackBufferFormat	= backbufferFormat;
                presentation.BackBufferWidth	= displayMode.Width;
                presentation.BackBufferHeight	= displayMode.Height;
                presentation.SwapEffect		= SwapEffect.Flip;
                presentation.Windowed		= windowed;

                Device device = new Device(
                        adapter.Adapter,
                        DeviceType.Hardware,
                        this,
                        createFlags,
                        presentation);
                        Console.ReadLine();
                    }
                }
             */

        }

        public class MainForm : Form
        {
            Device device = null;

            /// <summary>
            ///	When the form is clicked, we close it and stop
            ///	the application
            /// </summary>
            protected override void OnClick(EventArgs e)
            {
                this.Close();
                base.OnClick(e);
            }


            /// <summary>
            ///	In the constructor we initialize the device using
            ///	the default adapter and the current display mode
            /// </summary>
            public MainForm()
            {
                AdapterInformation adapter = Manager.Adapters.Default;

                PresentParameters presentation = new PresentParameters();
                presentation.BackBufferFormat = adapter.CurrentDisplayMode.Format;
                presentation.BackBufferWidth = adapter.CurrentDisplayMode.Width;
                presentation.BackBufferHeight = adapter.CurrentDisplayMode.Height;
                presentation.SwapEffect = SwapEffect.Flip;
                presentation.Windowed = false;

                // Place it in a try catch block just
                //in case it goes wrong for
                // some reason.
                try
                {
                    // To make things simple, we'll just
                    //use the reference device, and
                    // software vertex processing.
                    //Although this will be extrelemly slow
                    // it doesn't matter in this case.
                    // It will definetly work on
                    // all machines, so it saves us doing
                    //enumeration for now.
                    device = new Device(adapter.Adapter,
                                DeviceType.Reference,
                                this,
                                CreateFlags.SoftwareVertexProcessing,
                                presentation);
                }
                catch (DirectXException e)
                {
                    MessageBox.Show(e.Message);
                    return;
                }
            }


            /// <summary>
            ///	Clear the screen with a blue color
            /// </summary>
            public void Render()
            {
                device.Clear(ClearFlags.Target,System.Drawing.Color.AntiqueWhite, 1.0f, 0);
                device.Present();
            }


            /// <summary>
            ///	The entry point where the main render loop goes
            /// </summary>
            static void Main()
            {
                using (MainForm form = new MainForm())
                {
                    form.Show();

                    while (form.Created)
                    {
                        form.Render();
                        Application.DoEvents();
                    }
                }
            }
        }
    }
}
posted @ 2010-08-07 01:52  麦田里的守望者  阅读(506)  评论(2编辑  收藏  举报