Last active
December 25, 2015 04:39
-
-
Save axefrog/6918511 to your computer and use it in GitHub Desktop.
Game Dev Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using SharpDX; | |
using SharpDX.Direct2D1; | |
using SharpDX.Direct3D10; | |
using SharpDX.DirectWrite; | |
using SharpDX.DXGI; | |
using SharpDX.Windows; | |
using AlphaMode = SharpDX.Direct2D1.AlphaMode; | |
using Device1 = SharpDX.Direct3D10.Device1; | |
using DriverType = SharpDX.Direct3D10.DriverType; | |
using Factory = SharpDX.DXGI.Factory; | |
using FeatureLevel = SharpDX.Direct3D10.FeatureLevel; | |
using Resource = SharpDX.Direct3D10.Resource; | |
namespace SharpDx1 | |
{ | |
class HelloWorld | |
{ | |
[STAThread] | |
public static void Main() | |
{ | |
var app = new HelloWorld(); | |
app.Run(); | |
} | |
private void Run() | |
{ | |
var form = new RenderForm("SharpDX : Hello World"); | |
form.Width = 300; | |
form.Height = 200; | |
// So, this swap chain seems to be some kind of buffer manager. | |
// You render into a buffer and then swap that onto the primary | |
// surface once you're finished drawing the picture, and this | |
// prevents flickering. | |
var description = new SwapChainDescription | |
{ | |
BufferCount = 1, | |
ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), | |
IsWindowed = true, // I think this tells the swap chain to render to a window, and not to the whole screen | |
OutputHandle = form.Handle, // This tells the swap chain what window to render to (requires IsWindowed = true) | |
SampleDescription = new SampleDescription(1, 0), // No idea what this is for (yet) | |
SwapEffect = SwapEffect.Discard, // The docs seem to indicate that this value will cause the pixels in the buffer to be discarded after use | |
Usage = Usage.RenderTargetOutput | |
}; | |
Device1 device; | |
SwapChain swapChain; | |
// This would seem to create the device we're going to render to, linking the swap chain | |
// to the device in the process. | |
Device1.CreateWithSwapChain( | |
DriverType.Hardware, | |
DeviceCreationFlags.BgraSupport, | |
description, | |
FeatureLevel.Level_10_0, | |
out device, out swapChain); | |
// This is used for the lifetime of the Direct2D app to create resources used by the app | |
var d2dFactory = new SharpDX.Direct2D1.Factory(); | |
// It seems we need a different kind of factory for accessing DirectWrite resources | |
var writeFactory = new SharpDX.DirectWrite.Factory(); | |
var width = form.ClientSize.Width; | |
var height = form.ClientSize.Height; | |
// Allows DXGI to monitor the window and ignore any events we don't want (e.g. switching to | |
// full screen with ALT-ENTER) | |
var factory = swapChain.GetParent<Factory>(); | |
factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); | |
// Now we get a texture object (like we did in MonoGame!) from the swap chain | |
var backBuffer = Resource.FromSwapChain<Texture2D>(swapChain, 0); | |
// This doesn't seem to get used after this, other than for disposing, which suggests to | |
// me that we're providing something to either the device, the back buffer, or both, and | |
// doing so in order to be able to control the cleanup process later. | |
var renderView = new RenderTargetView(device, backBuffer); | |
// I think this is getting some sort of low level object that provides an interface between | |
// the actual memory used by the back buffer and an object which will do the actual drawing | |
var surface = backBuffer.QueryInterface<Surface>(); | |
// In reference to the previous comment, this would be the object that does the actual | |
// drawing to the back buffer. I'm not really sure about the pixel format stuff, I'll have | |
// to learn more about that in a future exercise. | |
var d2dRenderTarget = new RenderTarget(d2dFactory, surface, | |
new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); | |
var brush = new SolidColorBrush(d2dRenderTarget, Color.White); | |
var rand = new Random(); | |
RenderLoop.Run(form, () => | |
{ | |
// So, here we draw some stuff onto the back buffer | |
d2dRenderTarget.BeginDraw(); | |
d2dRenderTarget.Clear(Color.Black); | |
for (var i = 0; i < 100; i++) | |
{ | |
var x = rand.Next(width - 1); | |
var y = rand.Next(height - 1); | |
d2dRenderTarget.DrawLine(new Vector2(x, y), new Vector2(x + 1, y + 1), brush); | |
} | |
d2dRenderTarget.DrawText("Hello again,\r\nworld!", new TextFormat(writeFactory, "Consolas", 30f), new RectangleF(20, 20, 500, 100), brush); | |
d2dRenderTarget.EndDraw(); | |
// ... and here we flip the buffer onto the actual window | |
swapChain.Present(0, PresentFlags.None); | |
}); | |
// Release all resources | |
renderView.Dispose(); | |
backBuffer.Dispose(); | |
device.ClearState(); | |
device.Flush(); | |
device.Dispose(); | |
device.Dispose(); | |
swapChain.Dispose(); | |
factory.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment