public bool SendFrame(TimeSpan elapsedTime, int frameIndex) { var a = Math.PI * elapsedTime.TotalSeconds; var h = VideoFrameHeight - _ballEllipse.RadiusY; var y = (float)(VideoFrameHeight - Math.Abs(Math.Sin(a) * h)); var pos = new RawVector2(VideoFrameWidth * 0.5f, y); using (var df = TakeNextFrameForSending()) { if (!df.TryGetFrame(out FrameD3D11 frame)) { return(false); } // TODO: Draw bouncing ball. _context2D.Target = frame.Bitmap; _context2D.BeginDraw(); _context2D.Transform = Matrix3x2.Identity; _context2D.DrawBitmap(_backgroundBitmap, new RawRectangleF(0, 0, VideoFrameWidth, VideoFrameHeight), 1, D2D1.BitmapInterpolationMode.NearestNeighbor); _context2D.Transform = BallPosition.HasValue ? Matrix3x2.Translation(BallPosition.Value * new Vector2(VideoFrameWidth, VideoFrameHeight)) : Matrix3x2.Translation(pos); _context2D.FillEllipse(_ballEllipse, _ballBrush); _context2D.EndDraw(); _context2D.Target = null; return(true); } }
/// <summary> /// Run our application until the user quits. /// </summary> public void Run() { // Make window active and hide mouse cursor. window.PointerCursor = null; window.Activate(); // Infinite loop to prevent the application from exiting. while (true) { // Dispatch all pending events in the queue. window.Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessAllIfPresent); // Quit if the users presses Escape key. if (window.GetAsyncKeyState(VirtualKey.Escape) == CoreVirtualKeyStates.Down) { return; } // Set the Direct2D drawing target. d2dContext.Target = d2dTarget; // Clear the target and draw some geometry with the brushes we created. d2dContext.BeginDraw(); d2dContext.Clear(Color.CornflowerBlue); // Calculate the center of the screen int halfWidth = this.swapChain.Description.ModeDescription.Width / 2; int halfHeight = this.swapChain.Description.ModeDescription.Height / 2; // Translate the origin of coordinates for drawing the bitmap filled rectangle d2dContext.Transform = Matrix3x2.Translation(halfWidth - 350, halfHeight); d2dContext.FillRectangle(new RectangleF(0, 0, 700, 70), terrainBrush); // Translate again for drawing the player bitmap d2dContext.Transform = Matrix3x2.Translation(halfWidth, halfHeight - playerBitmap.Size.Height); d2dContext.DrawBitmap(playerBitmap, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear); d2dContext.EndDraw(); // Present the current buffer to the screen. swapChain.Present(1, PresentFlags.None); } }
/// <inheritdoc /> public override void Draw(D2D.DeviceContext deviceContext) { if (this.Size.Width == 0 || this.Size.Height == 0) { return; } if (this.backBitmap == null || this.backBitmap.Size != this.Size) { this.InitializeBackBuffer(deviceContext, this.Size); } this.Draw(); var boundary = new RawRectangleF() { Top = this.Position.Y, Left = this.Position.X, Bottom = this.Position.Y + this.Size.Height, Right = this.Position.X + this.Size.Width, }; if (this.PostEffects?.Any() == true) { this.PostEffects.SetInput(this.backBitmap); using (var output = this.PostEffects.Output) { deviceContext.DrawImage(output, new RawVector2(boundary.Left, boundary.Top), D2D.InterpolationMode.NearestNeighbor); } } else { deviceContext.DrawBitmap(this.backBitmap, boundary, 1, D2D.BitmapInterpolationMode.NearestNeighbor); // deviceContext.DrawImage(this.backBitmap, new RawVector2(boundary.Left, boundary.Top), D2D.InterpolationMode.NearestNeighbor); } }
public bool SendFrame(TimeSpan elapsedTime) { using (var df = TakeNextFrameForSending()) { if (!df.TryGetFrame(out BitmapFrameD2D1 frame)) { return(false); } _context2D.Target = frame.Bitmap; _context2D.BeginDraw(); _context2D.Transform = Matrix3x2.Identity; _context2D.DrawBitmap(_backgroundBitmap, new RawRectangleF(0, 0, VideoFrameWidth, VideoFrameHeight), 1, D2D1.BitmapInterpolationMode.NearestNeighbor); // Draw many balls to simulate high motion for (int i = 0; i < BallCount; ++i) { var a = 2 * Math.PI * elapsedTime.TotalSeconds + i * Math.PI / BallCount; var h = VideoFrameHeight - _ballEllipse.RadiusY; var s = (float)VideoFrameWidth / (BallCount + 1); var y = (float)(VideoFrameHeight - Math.Abs(Math.Sin(a) * h)); var pos = new RawVector2((i + 1) * s, y); // - _ballEllipse.RadiusX, y); _context2D.Transform = MousePosition.HasValue ? Matrix3x2.Translation(MousePosition.Value * new Vector2(VideoFrameWidth, VideoFrameHeight)) : Matrix3x2.Translation(pos); _context2D.FillEllipse(_ballEllipse, _ballBrush); } if (_rulerOptions != null) { _context2D.Transform = Matrix3x2.Identity; // Draw the time markers and current time to measure latency int markerCount = _rulerOptions.MarkerCount; int markerWidth = _rulerOptions.MarkerWidth; int markerHeight = _rulerOptions.MarkerHeight; int markerOffset = markerWidth / 2; var y = VideoFrameHeight - markerHeight; _context2D.FillRectangle(new RectangleF(0, y, VideoFrameWidth, markerHeight), _timeRulerBrush); for (int i = 0; i < markerCount; ++i) { var x = markerOffset + i * (VideoFrameWidth - markerWidth) / markerCount; var b = i % 10 == 0 ? _timeMarkerBrush2 : _timeMarkerBrush1; var r = new RectangleF(x - markerOffset, y, markerWidth, markerHeight); _context2D.FillRectangle(r, b); } // Draw current time { var duration = _rulerOptions.Duration.TotalMilliseconds; var t = elapsedTime.TotalMilliseconds % duration; var i = (float)(t * markerCount / duration); var x = markerOffset + i * (VideoFrameWidth - markerWidth) / markerCount; var r = new RectangleF(x - markerOffset, y, markerWidth, markerHeight); _context2D.FillRectangle(r, _currentTimeBrush); } } _context2D.EndDraw(); _context2D.Target = null; return(true); } }
/// <summary> /// Рисует Битмап (карту битов) на экран. /// </summary> /// <param name="bitmap">Карта битов которую нужно нарисовать.</param> /// <param name="x">Отступ от левого края</param> /// <param name="y">Отступ от верхнего края</param> /// <param name="scale">Масштаб (0.5 - картинка будет в пол размера, 2 - в два раза больше)</param> /// <param name="opacity">Прозрачность картинки. 1 - непрозрачная. 0.5 - полупрозрачная. 0 - невидимая</param> /// <param name="interMode">Как будет находиться цвет пикселя при растяжении или сжатии картинки</param> public void DrawBitmap(Bitmap bitmap, float x = 0, float y = 0, float scale = 1, float opacity = 1, BitmapInterpolationMode interMode = BitmapInterpolationMode.Linear) { _RenderTarget2D.BeginDraw(); _RenderTarget2D.DrawBitmap(bitmap, new SharpDX.Mathematics.Interop.RawRectangleF(x, y, x + bitmap.Size.Width * scale, y + bitmap.Size.Height * scale), opacity, interMode); _RenderTarget2D.EndDraw(); }
public void DrawTexture(TextureVisual visual) { var d2dTexture = visual.Texture as D2DTexture; _deviceContext.DrawBitmap(d2dTexture.D2DBitmap, 1.0f, SharpDX.Direct2D1.InterpolationMode.Linear); }
protected override void OnDraw(SharpDX.Direct2D1.DeviceContext ctx) { ctx.Clear(Color.Transparent); ctx.DrawBitmap(XResource.Bitmaps[CurrentMc], 1.0f, InterpolationMode.Linear); }