Skip to content

Instantly share code, notes, and snippets.

@xoofx
Created August 8, 2014 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoofx/434db79e5ff1d3c1239c to your computer and use it in GitHub Desktop.
Save xoofx/434db79e5ff1d3c1239c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.ServiceModel.Dispatcher;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml.Media.Media3D;
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX;
using SharpDX.Serialization;
using SharpDX.Toolkit;
using SharpDX.DirectWrite;
using SharpDX.Toolkit.Graphics;
using SharpDX.Toolkit.Input;
using Buffer = SharpDX.Toolkit.Graphics.Buffer;
using Device = SharpDX.Direct3D11.Device;
using RasterizerState = SharpDX.Toolkit.Graphics.RasterizerState;
namespace Adamantium_engine
{
class Engine : Game
{
private GraphicsDeviceManager graphicsDeviceManager;
private BasicEffect basicEffect;
private Buffer<VertexPositionColor> vertices;
private Buffer<int> indices;
private VertexInputLayout inputLayout;
private RasterizerState wireframeRasterizerState;
private RasterizerState solidRasterizerState;
private RasterizerState currentRasterizerState;
private Boolean _modelLoaded;
private SharpDX.DirectWrite.Factory _directWriteFactory;
private TextFormat textFormat;
private SharpDX.Direct2D1.Device d2dDevice;
private SharpDX.Direct2D1.DeviceContext d2dContext;
private SharpDX.Direct2D1.Bitmap1 d2dTarget;
private TextLayout textLayout1;
private SolidColorBrush textBrush;
private SolidColorBrush backgroundBrush;
private SpriteBatch batch;
private SpriteFont font;
/// <summary>
/// Initializes a new instance of the <see cref="Engine class" /> class.
/// </summary>
public Engine(double width, double height)
{
try
{
// Creates a graphics manager. This is mandatory.
graphicsDeviceManager = new GraphicsDeviceManager(this);
graphicsDeviceManager.PreparingDeviceSettings += graphicsDeviceManager_PreparingDeviceSettings;
// Setup the relative directory to the executable directory
// for loading contents with the ContentManager
Content.RootDirectory = "Models";
}
catch (Exception exception)
{
MessageDialog dialog = new MessageDialog(exception.Message + exception.StackTrace);
dialog.ShowAsync();
}
}
void graphicsDeviceManager_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
//graphicsDeviceManager.PreferMultiSampling = true;
//e.GraphicsDeviceInformation.PresentationParameters.MultiSampleCount = MSAALevel.X4;
e.GraphicsDeviceInformation.DeviceCreationFlags = DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug;
//e.GraphicsDeviceInformation.PresentationParameters.DepthStencilFormat = DepthFormat.Depth32;
//e.GraphicsDeviceInformation.GraphicsProfile = SharpDX.Direct3D.FeatureLevel.Level_11_0;
//e.GraphicsDeviceInformation.Adapter = GraphicsAdapter.Default;
//e.GraphicsDeviceInformation.PresentationParameters.RefreshRate = new Rational(60, 1);
//e.GraphicsDeviceInformation.PresentationParameters.MultiSampleCount = MSAALevel.X4;
}
protected override void UnloadContent()
{
//Debug.WriteLine("UnloadContent");
}
protected override void Initialize()
{
try
{
Window.Title = "Adamantium engine demo";
base.Initialize();
RasterizerStateDescription description1 = new RasterizerStateDescription();
description1.CullMode = CullMode.None;
description1.IsMultisampleEnabled = true;
description1.IsAntialiasedLineEnabled = true;
description1.FillMode = SharpDX.Direct3D11.FillMode.Wireframe;
wireframeRasterizerState = RasterizerState.New(GraphicsDevice, description1);
description1.CullMode = CullMode.Back;
description1.FillMode = SharpDX.Direct3D11.FillMode.Solid;
solidRasterizerState = RasterizerState.New(GraphicsDevice, description1);
var device = (SharpDX.Direct3D11.Device)GraphicsDevice;
using (var dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device>())
{
DeviceCreationFlags flags = new DeviceCreationFlags();
d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(dxgiDevice, new CreationProperties() { DebugLevel = DebugLevel.Warning }));
d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None));
}
_directWriteFactory = ToDispose(new SharpDX.DirectWrite.Factory1());
// Create a Bitmap1 from backbuffer and make it the D2D context's target
if (GraphicsDevice == null || GraphicsDevice.Presenter == null ||
GraphicsDevice.Presenter.NativePresenter == null) return;
var swapChain = (SwapChain1)GraphicsDevice.Presenter.NativePresenter;
using (var surface = swapChain.GetBackBuffer<Surface>(0))
{
var properties = new BitmapProperties1
{
BitmapOptions = BitmapOptions.Target | BitmapOptions.CannotDraw,
DpiX = DisplayProperties.LogicalDpi,
DpiY = DisplayProperties.LogicalDpi,
PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)
};
// Resizing the application will produce:
// DXGI ERROR: IDXGISwapChain::ResizeBuffers: Swapchain cannot be resized unless all outstanding buffer references have been released. [ MISCELLANEOUS ERROR #19: ]
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb205075(v=vs.85).aspx#Handling_Window_Resizing
// http://sharpdx.org/forum/5-api-usage/228-d3d11-directwrite-d2d1-directdraw-directwrite-into-a-texture2d
// The responsible of this is the Bitmap1 below, disposing it, releasing it does absolutely nothing
// It seems that Dispose() method is not implemented for this type
// If the following 3 lines are commented out, no error is produced but obviously no 2D content is drawn anymore
// Now the interesting part is that things do work even though this is error is produced, still it'd be nice to have no error
textFormat = new TextFormat(_directWriteFactory, "Segoe UI", 24.0f);
d2dTarget = ToDispose(new Bitmap1(d2dContext, surface, properties));
textBrush = new SolidColorBrush(d2dContext, Color4.White);
d2dContext.Target = d2dTarget;
}
}
catch (Exception exception)
{
MessageDialog dialog = new MessageDialog(exception.Message + exception.StackTrace);
dialog.ShowAsync();
}
}
public void SetWireframe()
{
currentRasterizerState = wireframeRasterizerState;
}
public void SetSolidframe()
{
currentRasterizerState = solidRasterizerState;
}
protected override void Update(GameTime gameTime)
{
//Debug.WriteLine("Update");
// Rotate the cube.
if (_modelLoaded)
{
var time = (float)gameTime.TotalGameTime.TotalSeconds;
basicEffect.World = Matrix.RotationX(time) * Matrix.RotationY(time * .2f) * Matrix.RotationZ(time * .4f);
//basicEffect.World = Matrix.Identity;
basicEffect.Projection = Matrix.PerspectiveFovRH((float)Math.PI / 4.0f,
(float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100000000.0f);
}
// Handle base.Update
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
//Debug.WriteLine("Draw");
// Clears the screen with the Color.CornflowerBlue
GraphicsDevice.Clear(Color.CornflowerBlue);
// Setup the vertices
//GraphicsDevice.SetIndexBuffer(indices, true);
//GraphicsDevice.SetVertexBuffer(vertices);
//GraphicsDevice.SetVertexInputLayout(inputLayout);
////Apply the basic effect technique and draw the rotating cube
//basicEffect.CurrentTechnique.Passes[0].Apply();
//GraphicsDevice.SetRasterizerState(currentRasterizerState);
//GraphicsDevice.DrawIndexed(PrimitiveType.TriangleList, indices.ElementCount);
// Clear the target.
try
{
d2dContext.BeginDraw();
d2dContext.DrawText("Check", textFormat, new RectangleF(10, 10, 400, 200), textBrush);
d2dContext.EndDraw();
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message + exception.StackTrace);
}
// Handle base.Draw
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment