protected virtual void Initialize(Configuration config) { var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(config.Width, config.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = DisplayHandle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SharpDX.Direct3D11.Device.CreateWithSwapChain( DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain); SharpDX.DXGI.Factory factory = swapChain.GetParent <SharpDX.DXGI.Factory>(); factory.MakeWindowAssociation(DisplayHandle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); backBufferView = new RenderTargetView(device, backBuffer); Factory2D = new SharpDX.Direct2D1.Factory(); using (var surface = BackBuffer.QueryInterface <Surface>()) { RenderTarget2D = new RenderTarget(Factory2D, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); } RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive; FactoryDWrite = new SharpDX.DirectWrite.Factory(); SceneColorBrush = new SolidColorBrush(RenderTarget2D, SharpDX.Color.White); }
/// <summary> /// Initialization /// </summary> public virtual void Setup() { form = new RenderForm(Window.Title); form.SetBounds(Window.Left, Window.Top, Window.Width, Window.Height); sw = new Stopwatch(); timing = new GameTime(); SwapChainDescription desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(Window.Width, Window.Height, new Rational(FrameRate, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device1.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, out device, out swapChain); D2DFactory = new SharpDX.Direct2D1.Factory(); factory = swapChain.GetParent <SharpDX.DXGI.Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); D2DRenderTarget = new RenderTarget(D2DFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); }
/// <summary> /// /// </summary> protected void InitializeD3D10(int pWidth, int pHeight) { // SwapChain description var desc = new DXGI.SwapChainDescription() { BufferCount = 1, ModeDescription = new DXGI.ModeDescription(pWidth, pHeight, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = DisplayHandle, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput }; // Create Device and SwapChain SharpDX.Direct3D10.Device1.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, SharpDX.Direct3D10.FeatureLevel.Level_10_0, out mDevice, out mSwapChain); // Ignore all windows events DXGI.Factory factory = mSwapChain.GetParent <DXGI.Factory>(); factory.MakeWindowAssociation(DisplayHandle, DXGI.WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer mBackBuffer = Texture2D.FromSwapChain <Texture2D>(mSwapChain, 0); mBackBufferView = new RenderTargetView(mDevice, mBackBuffer); }
public void InitRendering() { try { lock (_drawLock) { m_Ready = false; ResizeRedraw = true; var desc = new SwapChainDescription { BufferCount = 2, ModeDescription = new ModeDescription(ClientSize.Width, ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput | Usage.Shared }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { SharpDX.Direct3D.FeatureLevel.Level_9_3 }, desc, out device, out swapChain); var d2dFactory = new SharpDX.Direct2D1.Factory(); Factory factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = Resource.FromSwapChain <Texture2D>(swapChain, 0); surface = backBuffer.QueryInterface <Surface>(); d2dRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); var bitmapProperties = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Ignore)); clientArea = new RawRectangleF { Left = 0, Top = 0, Bottom = ClientSize.Height, Right = ClientSize.Width }; factory.Dispose(); backBuffer.Dispose(); m_Ready = true; } } catch (Exception ee) { } }
public MainWindow() : base("Heat Simulator") { ClientSize = new Size(400, 400); MouseClick += MainWindow_MouseClick; MouseMove += MainWindow_MouseMove; MaximizeBox = false; KeyPreview = true; KeyPress += MainWindow_KeyPress; var desc = new SwapChainDescription { BufferCount = 1, ModeDescription = new ModeDescription(Width, Height, new Rational(60, 1), Format.B8G8R8A8_UNorm), IsWindowed = true, OutputHandle = Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { FeatureLevel.Level_12_0, FeatureLevel.Level_11_0 }, desc, out device, out swapChain); factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Resource.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); surface = backBuffer.QueryInterface <Surface>(); factory2d = new Factory2D(); renderTarget2d = new RenderTarget(factory2d, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); sColorBrush = new SolidColorBrush(renderTarget2d, Color4.White); factoryWrite = new FactoryW(); textFormat = new TextFormat(factoryWrite, "Ricty", 25) { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near }; renderTarget2d.TextAntialiasMode = TextAntialiasMode.Cleartype; textArea = new RectangleF(0, 0, Width / 2f, Height / 2f); simulator = new HeatSimulator(); stopWatch = new Stopwatch(); cells = new GridCell[simulator.DivNum, simulator.DivNum]; for (var i = 0; i < simulator.DivNum; i++) { for (var j = 0; j < simulator.DivNum; j++) { cells[i, j] = new GridCell(factory2d, Width, Height, i, j, simulator.DivNum, simulator.State); } } }
public RForm(string text) : base(text) { this.ClientSize = new System.Drawing.Size(screenWidth, screenHeight); desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(this.ClientSize.Width, this.ClientSize.Height, new Rational(144, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = this.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain); d2dFactory = new SharpDX.Direct2D1.Factory(); factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(this.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); surface = backBuffer.QueryInterface <Surface>(); d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); solidColorBrush.Color = Color.White; directInput = new DirectInput(); keyboard = new Keyboard(directInput); keyboard.Properties.BufferSize = 128; keyboard.Acquire(); userInputProcessor = new UserInputProcessor(); scoreTextFormat = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 36); scoreTextArea = new SharpDX.Mathematics.Interop.RawRectangleF(10, 10, 400, 400); livesTextArea = new SharpDX.Mathematics.Interop.RawRectangleF(10, 46, 400, 400); gameOverTextFormat = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 108); gameOverTextArea = new SharpDX.Mathematics.Interop.RawRectangleF(0, 0, screenWidth, screenHeight); gameOverTextFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Center; gameOverTextFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center;; rand = new Random(); ship = new Ship(screenWidth / 2, screenHeight / 2); bullets = new List <Bullet>(); asteroids = new List <Asteroid>(); int count = rand.Next(10, 15); for (int i = 0; i < count; i++) { asteroids.Add(new Asteroid(rand.Next(-25, screenWidth + 25), rand.Next(-25, screenWidth + 25), rand.Next(1, 4) * 10, rand, d2dFactory)); } gameInputTimer = new Stopwatch(); gameInputTimer.Start(); }
public Game(string windowName, int windowWidth, int windowHeight, bool isWindowed = true) { RenderForm = new RenderForm(windowName); RenderForm.Width = windowWidth; RenderForm.Height = windowHeight; RenderForm.AllowUserResizing = false; var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription( (int)(RenderForm.Width), (int)(RenderForm.Height), new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = isWindowed, OutputHandle = RenderForm.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out Device, out SwapChain); Factory2D = new SharpDX.Direct2D1.Factory(); Factory factory = SwapChain.GetParent <Factory>(); factory.MakeWindowAssociation(RenderForm.Handle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(SwapChain, 0); Surface = backBuffer.QueryInterface <Surface>(); RenderTarget2D = new RenderTarget(Factory2D, Surface, new RenderTargetProperties( new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); //WEB_SOCKET Connect(); System.Threading.Tasks.Task.Delay(1000); _gameRender = new GameRender(RenderForm, Factory2D, RenderTarget2D); _directInput = new DirectInput(); _keyboard = new Keyboard(_directInput); _keyboard.Properties.BufferSize = 128; _keyboard.Acquire(); }
private void InitializeForm() { ModeDescription modeDescription = new ModeDescription() { Width = ClientSize.Width, Height = ClientSize.Height, RefreshRate = new Rational(60, 1), Format = Format.R8G8B8A8_UNorm }; SwapChainDescription desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = modeDescription, IsWindowed = true, OutputHandle = Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { SharpDX.Direct3D.FeatureLevel.Level_11_1 }, desc, out SharpDX.Direct3D11.Device device, out SwapChain swapChain); SharpDX.Direct2D1.Factory d2dFactory = new SharpDX.Direct2D1.Factory(); // Ignore all windows events SharpDX.DXGI.Factory factory = swapChain.GetParent <SharpDX.DXGI.Factory>(); factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0); RenderTargetView renderView = new RenderTargetView(device, backBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); RenderTarget d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied))); // Release all resources renderView.Dispose(); backBuffer.Dispose(); device.ImmediateContext.ClearState(); device.ImmediateContext.Flush(); device.Dispose(); factory.Dispose(); RulerRender thisForm = this; Manager = new Manager(ref thisForm, ref d2dRenderTarget, ref swapChain); }
private void InitializeDevices() { try { SwapChainDescription = new DXGI.SwapChainDescription(); SwapChainDescription.BufferCount = 2; SwapChainDescription.SampleDescription = new DXGI.SampleDescription(1, 0); SwapChainDescription.SwapEffect = DXGI.SwapEffect.Discard; SwapChainDescription.Usage = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput; SwapChainDescription.IsWindowed = true; SwapChainDescription.ModeDescription = new DXGI.ModeDescription(GameWindow.Current.WindowParameters.Width, GameWindow.Current.WindowParameters.Height, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm); SwapChainDescription.OutputHandle = GameWindowHandle; D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.BgraSupport, featureLevels, SwapChainDescription, out D3DDefaultDevice, out SwapChain); DXGI.Factory factory = SwapChain.GetParent <DXGI.Factory>(); factory.MakeWindowAssociation(GameWindowHandle, DXGI.WindowAssociationFlags.IgnoreAll); D3DDevice = D3DDefaultDevice.QueryInterface <D3D11.Device1>(); Backbuffer = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(SwapChain, 0); RenderTargetView = new D3D11.RenderTargetView(D3DDevice, Backbuffer); D3DDevice.ImmediateContext.Rasterizer.SetViewport(0, 0, GameWindow.Current.WindowParameters.Width, GameWindow.Current.WindowParameters.Height); D3DDevice.ImmediateContext.OutputMerger.SetTargets(RenderTargetView); DXGIDevice = D3DDevice.QueryInterface <DXGI.Device>(); D2DFactory = new D2D1.Factory1(D2D1.FactoryType.MultiThreaded); D2DDevice = new D2D1.Device(D2DFactory, DXGIDevice); D2DDeviceContext = new D2D1.DeviceContext(D2DDevice, D2D1.DeviceContextOptions.None); RenderTargetSurface = Backbuffer.QueryInterface <DXGI.Surface>(); RenderTarget = new D2D1.RenderTarget(D2DFactory, RenderTargetSurface, new D2D1.RenderTargetProperties(new D2D1.PixelFormat(DXGI.Format.Unknown, D2D1.AlphaMode.Premultiplied))); RenderTarget.AntialiasMode = D2D1.AntialiasMode.PerPrimitive; // Initialize debug drawings brushes DrawingBoundsBrush = new D2D1.SolidColorBrush(RenderTarget, new SharpDX.Color(1f, 1f, 0f)); CollisionBoxesBrush = new D2D1.SolidColorBrush(RenderTarget, new SharpDX.Color(1f, 0f, 0f)); RenderFrame = new RenderFrame(RenderTarget); Clock = Stopwatch.StartNew(); } catch (Exception ex) { throw new DeviceInitializationException("Unable to initialize DirectX device!", ex); } }
public static void Initialize(string name, int width, int height, Color bgColor) { form = new RenderForm(name); form.ClientSize = new System.Drawing.Size(width, height); screenFrame = new Frame(); screenFrame.setTransform(1, 0, 0, 1, 0, 0); screenFrame.addHitbox(new Hitbox(0, 0, width, height)); TCM_Graphics.bgColor = bgColor; //overall begin drawing transformStack = new Stack <RawMatrix3x2>(); var swapDescription = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, swapDescription, out dev, out sc); d2dFactory = new SharpDX.Direct2D1.Factory(); fact = sc.GetParent <Factory>(); fact.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(sc, 0); rtv = new RenderTargetView(dev, backBuffer); Surface surf = backBuffer.QueryInterface <Surface>(); rt = new RenderTarget(d2dFactory, surf, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); brush = new SolidColorBrush(rt, bgColor); }
public RenderTarget CreateRenderTarget(RenderForm form) { var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(2, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain); var d2dFactory = new SharpDX.Direct2D1.Factory(); int width = form.ClientSize.Width; int height = form.ClientSize.Height; var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRectangle() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height - 128 * 2) }); factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); RenderTarget d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); return(d2dRenderTarget); }
private static void Main() { var form = new RenderForm("SharpDX - MiniTri Direct2D - Direct3D 10 Sample"); // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain Device1 device; SwapChain swapChain; Device1.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, desc, FeatureLevel.Level_10_0, out device, out swapChain); var d2dFactory = new SharpDX.Direct2D1.Factory(); int width = form.ClientSize.Width; int height = form.ClientSize.Height; var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRect() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128, height - 128) }); // Ignore all windows events Factory factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); var renderView = new RenderTargetView(device, backBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); var d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); var solidColorBrush = new SolidColorBrush(d2dRenderTarget, new Color4(1, 1, 1, 1)); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // Main loop RenderLoop.Run(form, () => { d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(new Color4(1.0f, 0.0f, 0.0f, 0.0f)); solidColorBrush.Color = new Color4((float)Math.Abs(Math.Cos(stopwatch.ElapsedMilliseconds * .001)), 1, 1, 1); d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null); d2dRenderTarget.EndDraw(); swapChain.Present(0, PresentFlags.None); }); // Release all resources renderView.Release(); backBuffer.Release(); device.ClearState(); device.Flush(); device.Release(); device.Release(); swapChain.Release(); factory.Release(); }
private void _initializeGraphics() { Console.Write("Initializing graphic device... "); var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription( _appConfiguration.Width, _appConfiguration.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = !_appConfiguration.FullScreen, OutputHandle = DisplayHandle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device1.CreateWithSwapChain( DriverType.Hardware, #if DEBUG DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug | DeviceCreationFlags.SingleThreaded, #else DeviceCreationFlags.BgraSupport, #endif desc, out _device, out _swapChain); if (_device == null) throw new SharpDXException("Failed to initialize graphics device."); if (_swapChain == null) throw new SharpDXException("Failed to initialize swap chain."); ToDispose<Device1>(_device); ToDispose<SwapChain>(_swapChain); Factory2D = ToDispose<Factory2D>(new Factory2D()); _factoryDXGI = ToDispose<FactoryDXGI>(_swapChain.GetParent<FactoryDXGI>()); _factoryDXGI.MakeWindowAssociation(DisplayHandle, WindowAssociationFlags.IgnoreAll); _backBuffer = ToDispose<Texture2D>(Texture2D.FromSwapChain<Texture2D>(_swapChain, 0)); _backBufferRenderTargetView = ToDispose<RenderTargetView>(new RenderTargetView(_device, _backBuffer)); Viewport = new Viewport(0, 0, _appConfiguration.Width, _appConfiguration.Height); using (var surface = _backBuffer.QueryInterface<Surface>()) { RenderTarget2D = ToDispose<RenderTarget>( new RenderTarget(Factory2D, surface, new RenderTargetProperties( new PixelFormat( Format.Unknown, AlphaMode.Premultiplied)))); } RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive; _vsync = Config.VSync; ScreenSize = new DrawingSizeF(Viewport.Width, Viewport.Height); Console.WriteLine("done."); }
static void D2d() { var plugfire = new Plugfire(); InitializeXAudio2(); //var fileStream = new NativeFileStream(@"D:\plug17.mp3", NativeFileMode.Open, NativeFileAccess.Read); var embeddedStream = Assembly.GetEntryAssembly().GetManifestResourceStream(typeof(Program), "music.mp3"); var audioPlayer = new AudioPlayer(xaudio2, embeddedStream); audioPlayer.Play(); var form = new RenderForm("SharpDX - MiniCube Direct3D11 Sample"); form.Width = Plugfire.WIDTH * 4; form.Height = Plugfire.HEIGHT * 4; Device device; SwapChain swapChain; var d2dFactory = new SharpDX.Direct2D1.Factory(); var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = false, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain); // Ignore all windows events Factory factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); form.KeyUp += (sender, args) => { if (args.KeyCode == Keys.F5) { swapChain.SetFullscreenState(true, null); } else if (args.KeyCode == Keys.F4) { swapChain.SetFullscreenState(false, null); } else if (args.KeyCode == Keys.Escape) { form.Close(); } }; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Texture2D backBuffer = null; Bitmap bitmap = null; RenderTarget render = null; RenderTargetView renderView = null; backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); //var textBuffer = new Texture2D(device, new Texture2DDescription {Height = 100, Width = 100, Format = Format.R8G8B8A8_UNorm}); renderView = new RenderTargetView(device, backBuffer); //var textView = new RenderTargetView(device, textBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); //Surface textSurface = textBuffer.QueryInterface<Surface>(); render = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); //var textRender = new RenderTarget(d2dFactory, textSurface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); bitmap = new Bitmap(render, new Size2(Plugfire.WIDTH, Plugfire.HEIGHT), new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Ignore))); SharpDX.DirectWrite.Factory FactoryDWrite = new SharpDX.DirectWrite.Factory(); var whiteTextBrush = new SolidColorBrush(render, new RawColor4(1, 1, 1, 1)); var debugTextFormat = new TextFormat(FactoryDWrite, "Arial", 10); var titleTextBrush = new SolidColorBrush(render, new RawColor4(1, 1, 0.3f, 1)); var foamTextFormat = new TextFormat(FactoryDWrite, "Arial", FontWeight.Bold, FontStyle.Normal, 100); var presentsTextFormat = new TextFormat(FactoryDWrite, "Arial", FontWeight.Bold, FontStyle.Normal, 60); var titleTextFormat = new TextFormat(FactoryDWrite, "Arial", FontWeight.Bold, FontStyle.Normal, 80); // Main loop RenderLoop.Run(form, () => { plugfire.Frame(stopwatch.ElapsedMilliseconds, audioPlayer.Position.TotalMilliseconds); bitmap.CopyFromMemory(plugfire.GetRgbImage(), Plugfire.WIDTH * 4); render.BeginDraw(); //render.Clear(Color.Wheat); //solidColorBrush.Color = new Color4(1, 1, 1, (float)Math.Abs(Math.Cos(stopwatch.ElapsedMilliseconds * .001))); //d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null); render.DrawBitmap(bitmap, new RawRectangleF(0, 0, render.Size.Width, render.Size.Height), 1.0f, BitmapInterpolationMode.Linear); //render.DrawText(audioPlayer.Position.ToString(), debugTextFormat, new RawRectangleF(10,10,100,100), whiteTextBrush); //if (plugfire.Mode == 0) //{ // var fade = (float)Math.Min((stopwatch.Elapsed.TotalMilliseconds - 500) / 2000, 1); // titleTextBrush.Color = new RawColor4(fade, fade, fade * 0.3f, 1); // render.DrawText("FOAM", foamTextFormat, new RawRectangleF(500, 100, 2000, 2000), titleTextBrush); // render.DrawText("presents", presentsTextFormat, new RawRectangleF(520, 200, 2000, 2000), titleTextBrush); // render.DrawText("Plugfire Regurgitated", titleTextFormat, new RawRectangleF(250, 400, 2000, 2000), titleTextBrush); //} render.EndDraw(); swapChain.Present(0, PresentFlags.None); }); // Release all resources renderView.Dispose(); backBuffer.Dispose(); device.ImmediateContext.ClearState(); device.ImmediateContext.Flush(); device.Dispose(); device.Dispose(); swapChain.Dispose(); factory.Dispose(); }
public RForm(string text) : base(text) { this.ClientSize = new System.Drawing.Size(screenWidth, screenHeight); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RForm)); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); //SharpDX variable initialization desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(this.ClientSize.Width, this.ClientSize.Height, new Rational(144, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = this.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain); d2dFactory = new SharpDX.Direct2D1.Factory(); factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(this.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); surface = backBuffer.QueryInterface <Surface>(); d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); directInput = new DirectInput(); keyboard = new Keyboard(directInput); keyboard.Properties.BufferSize = 128; keyboard.Acquire(); keys = new KeyboardState(); userInputProcessor = new UserInputProcessor(); gameInputTimer = new Stopwatch(); gameInputTimer.Start(); //Colors boardColor = new SolidColorBrush(d2dRenderTarget, new RawColor4(0.9215686f, 0.4235294f, 0.07058824f, 1.0f)); boardSpotColor = new SolidColorBrush(d2dRenderTarget, new RawColor4(0.1882353f, 0.2431373f, 0.2862745f, 1.0f)); boardValueColor = new SolidColorBrush(d2dRenderTarget, Color.Black); activePieceColors = new List <SolidColorBrush>(); activePieceColors.Add(new SolidColorBrush(d2dRenderTarget, new RawColor4(0.3960784f, 0.3960784f, 0.372549f, 1.0f))); activePieceColors.Add(new SolidColorBrush(d2dRenderTarget, new RawColor4(0.7411765f, 0.5843138f, 0.4f, 1.0f))); activePieceColors.Add(new SolidColorBrush(d2dRenderTarget, new RawColor4(0.9176471f, 0.6784314f, 0.3921569f, 1.0f))); activePieceColors.Add(new SolidColorBrush(d2dRenderTarget, new RawColor4(0.972549f, 0.627451f, 0.1137255f, 1.0f))); activePieceColors.Add(new SolidColorBrush(d2dRenderTarget, new RawColor4(0.9137255f, 0.7411765f, 0.2235294f, 1.0f))); activePieceColors.Add(new SolidColorBrush(d2dRenderTarget, new RawColor4(0.427451f, 0.7843137f, 0.654902f, 1.0f))); activePieceColors.Add(new SolidColorBrush(d2dRenderTarget, new RawColor4(0.6627451f, 0.7686275f, 0.3411765f, 1.0f))); //Gameboard drawing initialization topLeftX = screenWidth / 2 - boardWidth / 2; topLeftY = screenHeight / 2 - boardHeight / 2; boardRect = new RawRectangleF(topLeftX, topLeftY, topLeftX + boardWidth, topLeftY + boardHeight); pieceTextFormat = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 36); pieceTextFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center; pieceTextFormat.ParagraphAlignment = ParagraphAlignment.Center; pieceTextFormat4Digits = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 24); pieceTextFormat4Digits.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center; pieceTextFormat4Digits.ParagraphAlignment = ParagraphAlignment.Center; scoreColor = new SolidColorBrush(d2dRenderTarget, new RawColor4(1f, 1f, 1f, 1f)); scoreTextFormat = new TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 36); //Game variable initialization gsd = new GameStateData(); gridSize = 4; gsd.gridSize = gridSize; gsd.bs.gridSize = gridSize; gsd.bs.GenerateANewPiece(gsd.boardValues); if (gsd.bs.Value == 4) { gsd.score += 4; } moveSuccess = false; //Menu Initialization and font setup TextFormat startMenuText = new TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 36); startMenu = new StartMenu(d2dRenderTarget, startMenuText, screenWidth, screenHeight, "start"); TextFormat gameOverMenuText = new TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 72); gameOverScreen = new GameOverScreen(d2dRenderTarget, gameOverMenuText, screenWidth, screenHeight, gsd, "gameover"); TextFormat settingsMenuText = new TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 36); settingsMenu = new SettingsMenu(d2dRenderTarget, settingsMenuText, screenWidth, screenHeight, gsd, "settings"); TextFormat sureMenuText = new TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 36); areYouSureBox = new AreYouSureBox(d2dRenderTarget, sureMenuText, screenWidth, screenHeight, "areyousure"); //Load high scores and initialize the high score related menus WorkingPath = Directory.GetCurrentDirectory(); if (File.Exists(WorkingPath + @"\HighScores.sco")) { highs = FileUtils.ReadFromXmlFile <HighScores>(WorkingPath + @"\HighScores.sco"); } else { highs = new HighScores(); } viewHighScoresTF = new TextFormat(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Isolated), "Gill Sans", FontWeight.UltraBold, FontStyle.Normal, 30); viewHighScoresTF.WordWrapping = SharpDX.DirectWrite.WordWrapping.NoWrap; viewHighScoresTF.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; viewHighScores = new ViewHighScores(d2dRenderTarget, viewHighScoresTF, screenWidth, screenHeight, highs, WorkingPath, "viewhighscores"); madeHighScoreMenu = new MadeHighScoreMenu(d2dRenderTarget, settingsMenuText, screenWidth, screenHeight, gsd, "madehighscore", highs); //Link all the menus together sceneFlow = new SDXSceneFlow(); sceneFlow.menuList.Add(startMenu); sceneFlow.menuList.Add(gameOverScreen); sceneFlow.menuList.Add(settingsMenu); sceneFlow.menuList.Add(areYouSureBox); sceneFlow.menuList.Add(viewHighScores); sceneFlow.menuList.Add(madeHighScoreMenu); currentMenu = "start"; sceneFlow.activeMenu = sceneFlow.NextMenu(currentMenu); }
/// <summary> /// A Game powered by SharpDX /// </summary> /// <param name="Width">The width of the window</param> /// <param name="Height">The height of the window</param> static public void Initialize(SharpDX_Engine.Utitities.Size Size) { GC.Collect(); form = new RenderForm(); form.StartPosition = FormStartPosition.CenterScreen; form.ClientSize = new System.Drawing.Size((int)Size.width, (int)Size.height); form.MaximizeBox = false; form.FormBorderStyle = FormBorderStyle.FixedSingle; form.FormClosed += form_FormClosed; // SwapChain description SwapChainDescription desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain Device1.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, FeatureLevel.Level_10_0, out device, out swapChain); var d2dFactory = new SharpDX.Direct2D1.Factory(); // Ignore all windows events Factory factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); RenderTargetView renderView = new RenderTargetView(device, backBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); RenderTarget d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); form.SizeChanged += form_SizeChanged; form.GotFocus += form_GotFocus; form.LostFocus += form_LostFocus; form.Move += form_Move; WindowPosition = new Coordinate( form.Location.X + SystemInformation.FixedFrameBorderSize.Width + SystemInformation.DragSize.Width, form.Location.Y + SystemInformation.FixedFrameBorderSize.Height + SystemInformation.CaptionHeight + SystemInformation.DragSize.Height ); TextureManager = new TextureManager(d2dRenderTarget); //x swapChain.IsFullScreen = true; Game.Size = Size; Renderer = new Renderer(d2dRenderTarget); Input = new InputManager(); Sound = new Sound.Sound(); Stopwatch = new Stopwatch(); Stopwatch.Start(); Scene = new DummyScene(); UpdateThread = new Thread(UpdateScene); }
public static void Run(WorldBase world, int wndWidth = 1280, int wndHeight = 720, string windowTitle = "Ants", int targetFamerate = 60) { var form = new RenderForm(windowTitle) { ClientSize = new System.Drawing.Size(wndWidth, wndHeight) }; // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out var device, out var swapChain); var d2dFactory = new SharpDX.Direct2D1.Factory(); int width = form.ClientSize.Width; int height = form.ClientSize.Height; var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRectangle() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height - 128 * 2) }); // Ignore all windows events Factory factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); var renderView = new RenderTargetView(device, backBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); var d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // Main loop RenderLoop.Run(form, () => { d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(Color.Aqua); world.RenderLoop(stopwatch, d2dRenderTarget); d2dRenderTarget.EndDraw(); swapChain.Present(0, PresentFlags.None); }); // Release all resources renderView.Dispose(); backBuffer.Dispose(); device.ImmediateContext.ClearState(); device.ImmediateContext.Flush(); device.Dispose(); device.Dispose(); swapChain.Dispose(); factory.Dispose(); }
public bool Initialize(GameForm pForm, bool pFullScreen) { _form = pForm; try { #if DEBUG Device defaultDevice = new Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); _debug = defaultDevice.QueryInterface <DeviceDebug>(); #else Device defaultDevice = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport); #endif Device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); var dxgiDevice2 = Device.QueryInterface <SharpDX.DXGI.Device2>(); var dxgiFactory2 = dxgiDevice2.Adapter.GetParent <SharpDX.DXGI.Factory2>(); // SwapChain description var desc = new SwapChainDescription1() { Width = 0, Height = 0, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription(1, 0), BufferCount = BUFFER_COUNT, Scaling = Scaling.None, SwapEffect = SwapEffect.FlipSequential, Usage = Usage.RenderTargetOutput }; _swapChain = new SwapChain1(dxgiFactory2, Device, pForm.Handle, ref desc, null); _2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); Context = new SharpDX.Direct2D1.DeviceContext(_2dDevice, DeviceContextOptions.EnableMultithreadedOptimizations); SecondaryContext = new SharpDX.Direct2D1.DeviceContext(_2dDevice, DeviceContextOptions.None); #if DEBUG Factory2D = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded, DebugLevel.Information); #else Factory2D = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded); #endif var dpi = Factory2D.DesktopDpi; var prop = new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), dpi.Height, dpi.Width, BitmapOptions.CannotDraw | BitmapOptions.Target); _backBuffer = _swapChain.GetBackBuffer <Surface>(0); _renderTarget = new Bitmap1(Context, _backBuffer, prop); Context.Target = _renderTarget; // Ignore all windows events using (Factory factory = _swapChain.GetParent <Factory>()) { factory.MakeWindowAssociation(pForm.Handle, WindowAssociationFlags.IgnoreAll); } FactoryDWrite = new SharpDX.DirectWrite.Factory(); return(true); } catch (System.Exception e) { System.Diagnostics.Debug.WriteLine("Error creating render target: " + e.Message); return(false); } }
public override void InitDevice() { Width = Surface.ClientSize.Width; Height = Surface.ClientSize.Height; SwapChainDescription SwapDesc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription( Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm//R8G8B8A8_UNorm ), IsWindowed = true, OutputHandle = Surface.Handle, SampleDescription = new SampleDescription(MSamplesCount, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, }; Device device; SwapChain swapChain; DeviceCreationFlags deviceCreationFlags = DeviceCreationFlags.BgraSupport; if (IsDebugMode) { deviceCreationFlags |= DeviceCreationFlags.Debug; } Device.CreateWithSwapChain( DriverType.Hardware, deviceCreationFlags, //DeviceCreationFlags.None, SwapDesc, out device, out swapChain ); DeviceRef = device; DeviceRef.DebugName = "The Device"; DeviceRef.ImmediateContext.DebugName = "The Context"; CheckFeatures(); SwapChainRef = swapChain; SwapChainRef.DebugName = "The SwapChain"; // Ignore all windows events FactoryDXGI factory = swapChain.GetParent <FactoryDXGI>(); factory.MakeWindowAssociation(Surface.Handle, WindowAssociationFlags.IgnoreAll); ZBufferTextureDescription = new Texture2DDescription { Format = Format.R32_Typeless, ArraySize = 1, MipLevels = 1, Width = Width, Height = Height, SampleDescription = new SampleDescription(MSamplesCount, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil | BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, }; Factory2D = new Factory(FactoryType.SingleThreaded, DebugLevel.Information); FactoryDWrite = new SharpDX.DirectWrite.Factory(); RenderTarget2DProperites = new RenderTargetProperties(new PixelFormat( Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)); }
protected void Initialize() { apple = new Apple() { x = 3, y = 3 }; snake = new Snake() { x = 10, y = 10, dy = 1 }; rng = new Random(); // SwapChain description var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(Constants.FRAMES_PER_SECOND, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain); d2dFactory = new SharpDX.Direct2D1.Factory(); int width = form.ClientSize.Width; int height = form.ClientSize.Height; // Ignore all windows events factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); Surface surface = backBuffer.QueryInterface <Surface>(); d2dRenderTarget = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRectangle() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height - 128 * 2) }); var solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); var directInput = new DirectInput(); keyboard = new Keyboard(directInput); keyboard.Acquire(); apple.Initialize(d2dRenderTarget, Color.Red); snake.Initialize(d2dRenderTarget, Color.Green); }
public void Run() { form = CreateForm(); SwapChainDescription desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(1280, 720, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = DisplayHandle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new[] { FeatureLevelDX.Level_10_0 }, desc, out device, out swapChain); FactoryDX factory = swapChain.GetParent <FactoryDX>(); factory.MakeWindowAssociation(DisplayHandle, WindowAssociationFlags.IgnoreAll); backBuffer = Resource.FromSwapChain <Texture2D>(swapChain, 0); backBufferView = new RenderTargetView(device, backBuffer); Factory2D = new Factory2D(); using (Surface surface = backBuffer.QueryInterface <Surface>()) { RenderTarget2D = new RenderTarget(Factory2D, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied))); } RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive; FactoryDirectWrite = new FactoryDW(); SceneColorBrush = new SolidColorBrush(RenderTarget2D, new RawColor4(1, 1, 1, 1)); Initialize(); bool isFormClosed = false; bool formIsResizing = false; form.MouseClick += HandleMouseClick; form.KeyDown += HandleKeyDown; form.KeyUp += HandleKeyUp; form.FormClosed += (o, args) => { isFormClosed = true; }; form.ResizeBegin += (o, args) => { formIsResizing = true; }; form.ResizeEnd += (o, args) => { formIsResizing = false; HandleResize(o, args); }; form.Resize += (o, args) => { if (form.WindowState != currentFormWindowState) { HandleResize(o, args); } currentFormWindowState = form.WindowState; }; LoadContent(); clock.Start(); BeginRun(); RenderLoop.Run(form, () => { if (isFormClosed) { return; } OnUpdate(); if (!formIsResizing) { Render(); } }); UnloadContent(); EndRun(); Dispose(); }
public PumpkinFaceRenderer(IFaceCamera <System.Drawing.PointF> faceCamera) { renderForm = new RenderForm("Calabasas"); renderForm.AllowUserResizing = true; this.faceCamera = faceCamera; renderForm.KeyPress += OnRenderFormKeyPress; renderForm.MouseClick += OnRenderFormMouseClick; if (this.faceCamera != null) { this.faceCamera.OnFaceChanged += OnFaceChanged; this.faceCamera.OnTrackingFace += OnTrackingFace; } // SwapChain description swapChainDesc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = renderForm.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, swapChainDesc, out device, out swapChain); d2dFactory = new SharpDX.Direct2D1.Factory(); dwFactory = new SharpDX.DirectWrite.Factory(); // Ignore all windows events SharpDX.DXGI.Factory factory = swapChain.GetParent <SharpDX.DXGI.Factory>(); factory.MakeWindowAssociation(renderForm.Handle, WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderTargetView = new RenderTargetView(device, backBuffer); surface = backBuffer.QueryInterface <Surface>(); d2dRenderTarget = new RenderTarget( d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied))); // Initialize a TextFormat TextFormat = new TextFormat(dwFactory, "Calibri", 18); d2dRenderTarget.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype; // Initialize a Brush. SceneColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); // Initialize geometery/drawable primitives. facePointGeometry = new RectangleGeometry(d2dFactory, new SharpDX.Mathematics.Interop.RawRectangleF(-PointSize / 2.0F, -PointSize / 2.0F, PointSize / 2.0F, PointSize / 2.0F)); facePointBrush = new SolidColorBrush(d2dRenderTarget, new SharpDX.Color(facePointPenColor.R, facePointPenColor.G, facePointPenColor.B)); drawingStateBlock = new DrawingStateBlock(d2dFactory); framesPerSecond = new FramesPerSecond(); }