Some More Progress.
881
4
Advertisement
As I said in my last entry it has been a long time since I touched C#. So as I was doing the port of the D3DApp framework for the DirectX 10 Luna Book, I realized quite quickly that I was trying to write C++ in C#. Not going to work. So I took a dive into the SlimDX sample MiniTri to get a general quick refresher on how a C# programmer does things. This sample + the MSDN helped a lot on giving me a nice refresher.
The code is not fully finished yet. I still need to get the font display coded and the GameTimer coded, but none the less I got a fully functional Direct3D 10 window running. The window is cleared to blue and I tacked in the functionality to turn on and off VSync. I hate when the video card transistors scream at high frame rates.
There are two things I learned from sitting down to do this. One it is really a excellent way to reinforce the learning curve of the massive DirectX API. You can't just sit there and copy the code or look at the code and port it. The best way I found to go about things is the read the chapter then implement it on my own in a C# way. The advantage is you actually learn the API not how to copy code and you can get a nice clean framework to use compared to what the author provides.
The second thing I learned is probably the biggest one. Why was a torturing my self with C++ all these years. C# is a very clean and powerful language and just looking at the code you can tell what it does. I will say the code I put together is not perfect yet or finalized in anyway but it is indeed a lot cleaner then the authors C++ code. The main reason I can see that this is so would be the removal of Macros and Preprocessor directives. Not to mention the exclusion of header files.
Now again keep in mind this is not finalized yet still needs some tweaking. Plus some more implementation but here is what I got so far.
The code is not fully finished yet. I still need to get the font display coded and the GameTimer coded, but none the less I got a fully functional Direct3D 10 window running. The window is cleared to blue and I tacked in the functionality to turn on and off VSync. I hate when the video card transistors scream at high frame rates.
There are two things I learned from sitting down to do this. One it is really a excellent way to reinforce the learning curve of the massive DirectX API. You can't just sit there and copy the code or look at the code and port it. The best way I found to go about things is the read the chapter then implement it on my own in a C# way. The advantage is you actually learn the API not how to copy code and you can get a nice clean framework to use compared to what the author provides.
The second thing I learned is probably the biggest one. Why was a torturing my self with C++ all these years. C# is a very clean and powerful language and just looking at the code you can tell what it does. I will say the code I put together is not perfect yet or finalized in anyway but it is indeed a lot cleaner then the authors C++ code. The main reason I can see that this is so would be the removal of Macros and Preprocessor directives. Not to mention the exclusion of header files.
Now again keep in mind this is not finalized yet still needs some tweaking. Plus some more implementation but here is what I got so far.
using System;using System.Drawing;using System.Windows.Forms;using SlimDX;using D3D10 = SlimDX.Direct3D10;using DXGI = SlimDX.DXGI;using SlimDX.Windows;namespace InitDirect3D{ class D3DApp { private RenderForm m_Window; private D3D10.Device m_D3DDevice; private DXGI.SwapChain m_SwapChain; private D3D10.RenderTargetView m_RenderTargetView; private D3D10.DepthStencilView m_DepthStencilView; private D3D10.Texture2D m_DepthStencilBuffer; private int m_VSync; public D3DApp() { m_Window = null; m_D3DDevice = null; m_SwapChain = null; m_RenderTargetView = null; m_DepthStencilView = null; m_DepthStencilBuffer = null; m_VSync = 0; } ~D3DApp() { m_RenderTargetView.Dispose(); m_DepthStencilView.Dispose(); m_DepthStencilBuffer.Dispose(); m_D3DDevice.Dispose(); m_SwapChain.Dispose(); } public void Initialize(string windowCaption) { m_Window = new RenderForm(windowCaption); // Setup the SwapChain and Device DXGI.SwapChainDescription swapDesc = new DXGI.SwapChainDescription() { BufferCount = 1, ModeDescription = new DXGI.ModeDescription(m_Window.ClientSize.Width, m_Window.ClientSize.Height, new Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = m_Window.Handle, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = DXGI.Usage.RenderTargetOutput, Flags = DXGI.SwapChainFlags.None, SwapEffect = DXGI.SwapEffect.Discard }; D3D10.Device.CreateWithSwapChain(null, D3D10.DriverType.Hardware, D3D10.DeviceCreationFlags.Debug, swapDesc, out m_D3DDevice, out m_SwapChain); this.OnResize(); // Disable Alt+Enter FullScreen DXGI.Factory factory = m_SwapChain.GetParent(); factory.SetWindowAssociation(m_Window.Handle, DXGI.WindowAssociationFlags.IgnoreAll); m_Window.Resize += new EventHandler(m_Window_Resize); m_Window.KeyDown += new KeyEventHandler(m_Window_KeyDown); } void m_Window_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.V) { if (m_VSync == 0) { m_VSync = 1; } else { m_VSync = 0; } } } void m_Window_Resize(object sender, EventArgs e) { this.OnResize(); } public void Run() { MessagePump.Run(m_Window, () => { DrawScene(); m_SwapChain.Present(m_VSync, DXGI.PresentFlags.None); }); } private void OnResize() { if (m_RenderTargetView != null) { m_RenderTargetView.Dispose(); } if (m_DepthStencilView != null) { m_DepthStencilView.Dispose(); } if (m_DepthStencilBuffer != null) { m_DepthStencilBuffer.Dispose(); } // Setup the RenderTargetView and DepthStencilBuffer/View D3D10.Texture2D backbuffer = D3D10.Texture2D.FromSwapChain3D10.Texture2D>(m_SwapChain, 0); m_RenderTargetView = new D3D10.RenderTargetView(m_D3DDevice, backbuffer); backbuffer.Dispose(); D3D10.Texture2DDescription depthStencilDesc = new D3D10.Texture2DDescription() { Width = m_Window.ClientSize.Width, Height = m_Window.ClientSize.Height, MipLevels = 1, ArraySize = 1, Format = DXGI.Format.D24_UNorm_S8_UInt, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = D3D10.ResourceUsage.Default, BindFlags = D3D10.BindFlags.DepthStencil, CpuAccessFlags = D3D10.CpuAccessFlags.None, OptionFlags = D3D10.ResourceOptionFlags.None }; m_DepthStencilBuffer = new D3D10.Texture2D(m_D3DDevice, depthStencilDesc); m_DepthStencilView = new D3D10.DepthStencilView(m_D3DDevice, m_DepthStencilBuffer); // Bind views to pipeline m_D3DDevice.OutputMerger.SetTargets(m_DepthStencilView, m_RenderTargetView); // Set the viewport transform. D3D10.Viewport vp = new D3D10.Viewport(0, 0, m_Window.ClientSize.Width, m_Window.ClientSize.Height, 0.0f, 1.0f); m_D3DDevice.Rasterizer.SetViewports(vp); } public virtual void UpdateScene(float dt) { } public virtual void DrawScene() { m_D3DDevice.ClearRenderTargetView(m_RenderTargetView, new Color4(1.0f, 0.0f, 0.0f, 1.0f)); m_D3DDevice.ClearDepthStencilView(m_DepthStencilView, D3D10.DepthStencilClearFlags.Depth | D3D10.DepthStencilClearFlags.Stencil, 1.0f, 0); } }}Advertisement
Advertisement
Advertisement
Discussion