Skip to content

Instantly share code, notes, and snippets.

@ItsSpyce
Created September 11, 2015 03:59
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 ItsSpyce/e34bbe9f7ba57fe5424e to your computer and use it in GitHub Desktop.
Save ItsSpyce/e34bbe9f7ba57fe5424e to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.Windows.Forms;
using SharpDX;
using SharpDX.Direct3D;
using SharpDX.DXGI;
using SharpDX.Windows;
using D3D11 = SharpDX.Direct3D11;
namespace Syhno.Game.Eureka.Engine
{
public class SyhnoGameEngine : IDisposable
{
public SceneEngine SceneEngine { get; set; }
public static SyhnoGameEngine CreateNew()
{
var e = new SyhnoGameEngine();
e.StartGame();
return e;
}
private readonly RenderForm _form;
private const int WIDTH = 800;
private const int HEIGHT = 600;
public SyhnoGameEngine()
{
_form = new RenderForm("Syhno Game")
{
ClientSize = new Size(WIDTH, HEIGHT),
StartPosition = FormStartPosition.CenterScreen
};
SceneEngine = new SceneEngine();
}
public void StartGame()
{
InitializeD3D11();
RenderLoop.Run(_form, HandleRenderCallback);
DisposeObjects();
}
private void HandleRenderCallback()
{
}
// General Direct3D 11 stuff
private static SwapChain _swapChain;
private static D3D11.Device _d3D11Device;
private static D3D11.DeviceContext _d3D11DevCon;
private static D3D11.RenderTargetView _renderTargetView;
// Rainbow background colors
private static Color4 _backgroundColor;
private static int _colorModR = 1;
private static int _colorModG = 1;
private static int _colorModB = 1;
private void InitializeD3D11()
{
// Describe our buffer
var bufferDescription = new ModeDescription()
{
Width = 800,
Height = 600,
RefreshRate = new Rational(60, 1),
Format = Format.R8G8B8A8_UNorm
};
// Describe our swap chain
var swapChainDescription = new SwapChainDescription()
{
ModeDescription = bufferDescription,
SampleDescription = new SampleDescription(1, 0),
Usage = Usage.RenderTargetOutput,
BufferCount = 1,
OutputHandle = _form.Handle,
IsWindowed = true
};
// Create our device with swap chain and get the device context
D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None,
swapChainDescription, out _d3D11Device, out _swapChain);
_d3D11DevCon = _d3D11Device.ImmediateContext;
// Create our render target view and set it
using (var backBuffer = _swapChain.GetBackBuffer<D3D11.Texture2D>(0))
{
_renderTargetView = new D3D11.RenderTargetView(_d3D11Device, backBuffer);
}
_d3D11DevCon.OutputMerger.SetRenderTargets(_renderTargetView);
}
public void Dispose()
{
DisposeObjects();
}
public void DisposeObjects()
{
_swapChain.Dispose();
_d3D11Device.Dispose();
_d3D11DevCon.Dispose();
_renderTargetView.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment