public void Dispose() { if (stream != null) { stream.Dispose(); stream = null; } if (d2dRenderTarget != null) { d2dRenderTarget.Dispose(); d2dRenderTarget = null; } if (wicBitmap != null) { wicBitmap.Dispose(); wicBitmap = null; } if (d2dFactory != null) { d2dFactory.Dispose(); d2dFactory = null; } if (wicFactory != null) { wicFactory.Dispose(); wicFactory = null; } if (memStream != null) { memStream.Dispose(); } }
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 void Draw(RenderTarget renderTarget, SharpDX.Direct2D1.Factory factory) { var x = this.x * Constants.SPRITE_SIZE + 1; var y = this.y * Constants.SPRITE_SIZE + 1; var rectangleGeometry = new RectangleGeometry(factory, new RectangleF(x, y, width, height)); renderTarget.FillGeometry(rectangleGeometry, brush, null); }
public DXGI2DContext(int width, int height) { this.width = width; this.height = height; wicFactory = new SharpDX.WIC.ImagingFactory(); d2dFactory = new SharpDX.Direct2D1.Factory(); wicBitmap = new SharpDX.WIC.Bitmap(wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppBGR, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT); d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); }
public void Draw(RenderTarget renderTarget, SharpDX.Direct2D1.Factory factory) { foreach (var element in trail) { var x = element.X * Constants.SPRITE_SIZE + 1; var y = element.Y * Constants.SPRITE_SIZE + 1; var rectangleGeometry = new RectangleGeometry(factory, new RectangleF(x, y, width, height)); renderTarget.FillGeometry(rectangleGeometry, brush, null); } }
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(); }
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 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(); }
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(); }
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.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 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))); var solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // Main loop RenderLoop.Run(form, () => { d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(Color.Black); solidColorBrush.Color = new Color4(1, 1, 1, (float)Math.Abs(Math.Cos(stopwatch.ElapsedMilliseconds * .001))); d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null); d2dRenderTarget.EndDraw(); swapChain.Present(0, PresentFlags.None); }); // Release all resources renderView.Dispose(); backBuffer.Dispose(); device.ClearState(); device.Flush(); device.Dispose(); device.Dispose(); swapChain.Dispose(); factory.Dispose(); }
/// <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); }
static void Main() { var form = new RenderForm("FUN STUFF PHYSXS"); // 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 device; SwapChain swapChain; 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(); // 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, Color.White); var sim = new Simulation(); // Main loop RenderLoop.Run(form, () => { d2dRenderTarget.BeginDraw(); int width = form.ClientSize.Width; int height = form.ClientSize.Height; var simScaleX = sim.SimBounds.MaxX - sim.SimBounds.MinX; var simScaleY = sim.SimBounds.MaxY - sim.SimBounds.MinY; var viewScaleX = sim.ViewBounds.MaxX - sim.ViewBounds.MinX; var viewScaleY = sim.ViewBounds.MaxY - sim.ViewBounds.MinY; d2dRenderTarget.Clear(Color.Black); foreach (Body body in sim.State) { var x = Convert.ToSingle((body.Pos.X - sim.SimBounds.MinX) * width / simScaleX); var y = Convert.ToSingle((body.Pos.Y - sim.SimBounds.MinY) * height / simScaleY); var xRad = Convert.ToSingle((body.Radius) * width / viewScaleX); var yRad = Convert.ToSingle((body.Radius) * height / viewScaleY); var elipse = new EllipseGeometry(d2dFactory, new Ellipse(new Vector2(x, y), xRad, yRad)); d2dRenderTarget.FillGeometry(elipse, solidColorBrush, null); } d2dRenderTarget.EndDraw(); sim.Step(); 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(); }
private void InitDirect2DFactory() { Direct2DFactory = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded, DebugLevel.None); }
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); }
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); } }
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); }