// Renders a shape to the backbuffer public void Render(TargetBase render) { var d3dContext = render.DeviceManager.ContextDirect3D; // Bind the vertex buffer and input layout d3dContext.InputAssembler.SetVertexBuffers(0, vertexBinding); d3dContext.InputAssembler.InputLayout = style.layout; // Set the topology if different from the previous topology if (d3dContext.InputAssembler.PrimitiveTopology != topology) { d3dContext.InputAssembler.PrimitiveTopology = topology; } // Set the vertex shader if different from the current vertex shader if (d3dContext.VertexShader.Get() != style.vertexShader) { d3dContext.VertexShader.Set(style.vertexShader); } // Set the pixel shader if different from the current pixel shader if (d3dContext.PixelShader.Get() != style.pixelShader) { d3dContext.PixelShader.Set(style.pixelShader); } d3dContext.PixelShader.SetShaderResource(0, textureView); // Draw the actual shape d3dContext.Draw(vertexCount, 0); }
public virtual void Render(TargetBase target) { if (!Show) { return; } frameCount++; var timeElapsed = (double)clock.ElapsedTicks / Stopwatch.Frequency;; totalTime += timeElapsed; if (totalTime >= 1.0f) { measuredFPS = (double)frameCount / totalTime; frameCount = 0; totalTime = 0.0; } var context2D = target.DeviceManager.ContextDirect2D; context2D.BeginDraw(); context2D.Transform = Matrix.Identity; context2D.DrawText(string.Format("{0:F2} FPS ({1:F1} ms)", measuredFPS, timeElapsed * 1000.0), textFormat, new RectangleF(8, 8, 8 + 256, 8 + 16), sceneColorBrush); context2D.EndDraw(); clock.Restart(); }
public virtual void Render(TargetBase target) { if (!Show) return; _gameManager.Update(_clock.ElapsedMilliseconds, target, _deviceManager); }
public virtual void OnRender(TargetBase targetBase) { lock (lockObject) { if (isVideoStopped) { return; } if (mediaEngineEx != null) { long pts; if (mediaEngineEx.OnVideoStreamTick(out pts)) { var backBuffer = OutputVideoTexture ?? targetBase.BackBuffer; if (backBuffer != null) { var desc = backBuffer.Description; var region = new Rectangle(0, 0, desc.Width, desc.Height); mediaEngineEx.TransferVideoFrame(backBuffer, null, region, (ColorBGRA)BackgroundColor); } } } } }
public void Render(TargetBase render) { foreach (var shape in shapeList) { if(shape!=null) shape.Render(render); } }
public void Render(TargetBase render) { game.PushWorldMatrix(); game.world *= transformation; game.updateConstantBuffer(); model.Render(render); game.PopWorldMatrix(); }
public void Render(TargetBase render) { game.PushWorldMatrix(); game.world *= Matrix.Identity * Matrix.RotationZ(rotationAngle) * Matrix.Translation(pos); game.updateConstantBuffer(); model.Render(render); game.PopWorldMatrix(); }
protected override void CreateSizeDependentResources(TargetBase renderBase) { base.CreateSizeDependentResources(renderBase); if (SwapChain != null) { SwapChain.MatrixTransform = Matrix3x2.Scaling(1f / panel.CompositionScaleX, 1f / panel.CompositionScaleY); } }
public void Render(TargetBase render) { background.Render(render); foreach (var obj in backgroundObjects) { obj.Render(render); } }
public override void drawContent(TargetBase target) { int y = random.Next(0, size.Height); context.BeginDraw(); // Dim context.FillRectangle(new RectangleF(0, 0, size.Width, size.Height), dimmingBrush); context.DrawLine(new DrawingPointF(0, y), new DrawingPointF(size.Width, y), lineBrush); context.EndDraw(); }
public void Render(TargetBase render) { game.PushWorldMatrix(); game.world *= Matrix.Translation(new Vector3(Constants.boundaryLeft, Constants.boundaryBottom, 1)); game.updateConstantBuffer(); updateLighting(); model.Render(render); game.PopWorldMatrix(); }
public override void drawContent(TargetBase target) { int x = random.Next(0, size.Width); int y = random.Next(0, size.Height); context.BeginDraw(); // Dim context.FillRectangle(new RectangleF(0, 0, size.Width, size.Height), dimmingBrush); context.FillRectangle(new RectangleF(x,y,x+2,y+2), pixelBrush); context.EndDraw(); }
public void Render(TargetBase target) { if (_Nodes.Count == 0) { return; } var d3dContext = target.DeviceManager.ContextDirect3D; d3dContext.OutputMerger.SetTargets(target.DepthStencilView, target.RenderTargetView); d3dContext.ClearDepthStencilView(target.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); foreach (var node in _Nodes) { node.Render(target, this.TargetView); } }
public void Render(TargetBase target, TargetView targetView) { //background colour var immlColour = _Context.GetBackgroundColorOrDefault(Imml.Drawing.Color3.White); var bgColour = new Color4(immlColour.R, immlColour.G, immlColour.B, 1); target.DeviceManager.ContextDirect3D.ClearRenderTargetView(target.RenderTargetView, bgColour); var camera = _Context.GetActiveCamera(); if (camera != null) { var lookAt = new Vector3( (float)Math.Sin(camera.WorldRotation.X) + camera.WorldPosition.X, camera.WorldPosition.Y, (float)Math.Cos(camera.WorldRotation.Z) + camera.WorldPosition.Z); targetView.View = Matrix.LookAtLH(camera.WorldPosition.ToSharpDxVector(), lookAt, Vector3.UnitY); } }
public virtual void Render(TargetBase target) { if (!Show) return; if (_effectGraph == null) return; var context2D = target.DeviceManager.ContextDirect2D; context2D.BeginDraw(); if (EnableClear) context2D.Clear(Colors.Black); context2D.Clear(Colors.Transparent); context2D.DrawImage(_effectGraph.Output, InterpolationMode.Linear, CompositeMode.DestinationAtop); context2D.EndDraw(); }
public virtual void Update(DeviceContext context, TargetBase target, DeviceManager deviceManager, Point screenSize, float dt, float elapsedTime) { context.DrawRectangle(screenSize.ApplyTo(GamesParams.Margin0.ToRectangleWith(GamesParams.Margin1)), GamesParams.ObstaclesColor, 2); _gameTime += dt; if (!_gameManager.IsTraining) { const float u = 1.00f; if (_gameTime < u && !AfterStartFreeze) { var op = 1 - _gameTime / u; var hideBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Colors.White, new BrushProperties { Opacity = op }); context.FillRectangle(screenSize.ApplyTo(new RectangleF(0, 0, 1, 1)), hideBrush); } else { AfterStartFreeze = true; } if (_gameTime > _gameMaxTime - u) { var op = 1 - (_gameMaxTime - _gameTime) / u; var hideBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Colors.White, new BrushProperties { Opacity = op }); context.FillRectangle(screenSize.ApplyTo(new RectangleF(0, 0, 1, 1)), hideBrush); } float bar; bar = _gameManager.IsTraining ? 1 : _gameTime / _gameMaxTime; _gameManager.Page.SetTimeRectangleWidth(bar); _gameManager.RedrawPointsAndLifes(); if (_gameTime > _gameMaxTime) _gameManager.Win(100); } }
public override void drawContent(TargetBase target) { var rangePartitioner = Partitioner.Create(0, size.Height); Parallel.ForEach(rangePartitioner, (range, loopState) => { for (int y = range.Item1 ; y < range.Item2 ; y++) { for (int x = 0; x < size.Width; x++) { float val = view.pixelAt(x, y); int intVal = (int)(255 * val); data[y * size.Width + x] = 0 | intVal << 16 | intVal << 8 | intVal; } } }); buf.CopyFromMemory<int>(data, size.Width*sizeof(int)); context.BeginDraw(); context.DrawBitmap(buf, new RectangleF(0,0,drawingSize.Width, drawingSize.Height), 1.0f, BitmapInterpolationMode.Linear); context.EndDraw(); view.stepTime(); }
public virtual void Render(TargetBase target) { if (!Show) return; frameCount++; var timeElapsed = (double)clock.ElapsedTicks / Stopwatch.Frequency; ; totalTime += timeElapsed; if (totalTime >= 1.0f) { measuredFPS = (double)frameCount / totalTime; frameCount = 0; totalTime = 0.0; } var context2D = target.DeviceManager.ContextDirect2D; context2D.BeginDraw(); context2D.Transform = Matrix.Identity; context2D.DrawText(string.Format("{0:F2} FPS ({1:F1} ms)", measuredFPS, timeElapsed * 1000.0), textFormat, new RectangleF(8, 8, 8 + 256, 8 + 16), sceneColorBrush); context2D.EndDraw(); clock.Restart(); }
private void UpdateSize(TargetBase target) { var localSize = new Size2((int)target.RenderTargetSize.Width, (int)target.RenderTargetSize.Height); if (localSize != screenSize) { screenSize = localSize; bitmapSource.ScaleSource = new Vector2((float)screenSize.Width / imageSize.Width, (float)screenSize.Height / imageSize.Height); } }
public virtual void Render(TargetBase target) { if (!Show) return; if (_rippleEffect == null) return; UpdateSize(target); var context2D = target.DeviceManager.ContextDirect2D; Update(); context2D.BeginDraw(); if (EnableClear) context2D.Clear(Color.CornflowerBlue); context2D.DrawImage(_rippleEffect); context2D.EndDraw(); }
protected virtual void CreateSizeDependentResources(TargetBase renderBase) { var d3dDevice = DeviceManager.DeviceDirect3D; var d3dContext = DeviceManager.ContextDirect3D; var d2dContext = DeviceManager.ContextDirect2D; d2dContext.Target = null; SafeDispose(ref renderTargetView); SafeDispose(ref depthStencilView); SafeDispose(ref bitmapTarget); // If the swap chain already exists, resize it. if (swapChain != null) { swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None); } // Otherwise, create a new one. else { // SwapChain description var desc = CreateSwapChainDescription(); // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device // First, retrieve the underlying DXGI Device from the D3D Device. // Creates the swap chain using (var dxgiDevice2 = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>()) using (var dxgiAdapter = dxgiDevice2.Adapter) using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>()) { swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc)); // Ensure that DXGI does not queue more than one frame at a time. This both reduces // latency and ensures that the application will only render after each VSync, minimizing // power consumption. dxgiDevice2.MaximumFrameLatency = 1; } } // Obtain the backbuffer for this window which will be the final 3D rendertarget. using (var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0)) { // Create a view interface on the rendertarget to use on bind. renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, backBuffer)); // Cache the rendertarget dimensions in our helper class for convenient use. var backBufferDesc = backBuffer.Description; RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height); } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)RenderTargetSize.Width, Height = (int)RenderTargetSize.Height, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Create a viewport descriptor of the full window size. var viewport = new SharpDX.Direct3D11.Viewport((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f); // Set the current viewport using the descriptor. d3dContext.Rasterizer.SetViewports(viewport); // Now we set up the Direct2D render target bitmap linked to the swapchain. // Whenever we render to this bitmap, it will be directly rendered to the // swapchain associated with the window. var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1( new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), DeviceManager.Dpi, DeviceManager.Dpi, SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw); // Direct2D needs the dxgi version of the backbuffer surface pointer. // Get a D2D surface from the DXGI back buffer to use as the D2D render target. using (var dxgiBackBuffer = swapChain.GetBackBuffer <SharpDX.DXGI.Surface>(0)) bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties)); // So now we can set the Direct2D render target. d2dContext.Target = BitmapTarget2D; // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces. d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale; }
void _SurfaceImageSourceTarget_OnRender(TargetBase render) { _ContextRenderer.Render(render); }
public virtual void Render(TargetBase target) { if (!Show) return; var context2D = target.DeviceManager.ContextDirect2D; context2D.BeginDraw(); if (EnableClear) context2D.Clear(Colors.Black); var sizeX = (float)target.RenderTargetBounds.Width; var sizeY = (float)target.RenderTargetBounds.Height; var globalScaling = Matrix.Scaling(Math.Min(sizeX, sizeY)); var centerX = (float)(target.RenderTargetBounds.X + sizeX / 2.0f); var centerY = (float)(target.RenderTargetBounds.Y + sizeY / 2.0f); if (textFormat == null) { // Initialize a TextFormat textFormat = new TextFormat(target.DeviceManager.FactoryDirectWrite, "Calibri", 96 * sizeX / 1920) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center }; } if (pathGeometry1 == null) { var sizeShape = sizeX / 4.0f; // Creates a random geometry inside a circle pathGeometry1 = new PathGeometry1(target.DeviceManager.FactoryDirect2D); var pathSink = pathGeometry1.Open(); var startingPoint = new DrawingPointF(sizeShape * 0.5f, 0.0f); pathSink.BeginFigure(startingPoint, FigureBegin.Hollow); for (int i = 0; i < 128; i++) { float angle = (float)i / 128.0f * (float)Math.PI * 2.0f; float R = (float)(Math.Cos(angle) * 0.1f + 0.4f); R *= sizeShape; DrawingPointF point1 = new DrawingPointF(R * (float)Math.Cos(angle), R * (float)Math.Sin(angle)); if ((i & 1) > 0) { R = (float)(Math.Sin(angle * 6.0f) * 0.1f + 0.9f); R *= sizeShape; point1 = new DrawingPointF(R * (float)Math.Cos(angle + Math.PI / 12), R * (float)Math.Sin(angle + Math.PI / 12)); } pathSink.AddLine(point1); } pathSink.EndFigure(FigureEnd.Open); pathSink.Close(); } context2D.TextAntialiasMode = TextAntialiasMode.Grayscale; float t = clock.ElapsedMilliseconds / 1000.0f; context2D.Transform = Matrix.RotationZ((float)(Math.Cos(t * 2.0f * Math.PI * 0.5f))) * Matrix.Translation(centerX, centerY, 0); context2D.DrawText("SharpDX\nDirect2D1\nDirectWrite", textFormat, new RectangleF(-sizeX / 2.0f, -sizeY / 2.0f, +sizeX/2.0f, sizeY/2.0f), sceneColorBrush); float scaling = (float)(Math.Cos(t * 2.0 * Math.PI * 0.25) * 0.5f + 0.5f) * 0.5f + 0.5f; context2D.Transform = Matrix.Scaling(scaling) * Matrix.RotationZ(t * 1.5f) * Matrix.Translation(centerX, centerY, 0); context2D.DrawGeometry(pathGeometry1, sceneColorBrush, 2.0f); context2D.EndDraw(); }
public void Render(CommonDX.TargetBase target) { var d2dContext = _d2dContext; //target.DeviceManager.ContextDirect2D; var d2dDevice = target.DeviceManager.DeviceDirect2D; var d3dContext = target.DeviceManager.ContextDirect3D; var d3dDevice = target.DeviceManager.DeviceDirect3D; if (NumberFramesToRender > 0) { //TurnOffRenderingBecauseThereAreRenderableEffects(); _useStagingBitmap = false; var _tempTarget = d2dContext.Target; d2dContext.Target = _stagingBitmap; d2dContext.BeginDraw(); //if (_doClear) { //d2dContext.Clear(Color.White); d2dContext.Clear(new Color4(0, 0, 0, 0)); // _doClear = false; //} foreach (var renderTree in _renderTree.OrderBy(x => x.Order)) { if (renderTree.Type == eRenderType.Effect && renderTree.EffectDTO.IsRenderable && !renderTree.HasLinkedEffects) //effects { if (renderTree.EffectDTO.Effect != null) { d2dContext.Transform = //Matrix.Identity Matrix.Scaling(renderTree.EffectDTO.MainScale) * Matrix.Translation(renderTree.EffectDTO.MainTranslation) //* Matrix.Scaling(_globalScale) //* Matrix.Translation(_globalTranslation) ; d2dContext.DrawImage(renderTree.EffectDTO.Effect); } } else if (renderTree.Type == eRenderType.Text && renderTree.TextDTO.IsRenderable) //text { d2dContext.Transform = Matrix.Scaling(renderTree.TextDTO.MainScale) * Matrix.Translation(renderTree.TextDTO.MainTranslation) //* Matrix.Scaling(_globalScale) //* Matrix.Translation(_globalTranslation) ; d2dContext.DrawText(renderTree.TextDTO.Text, renderTree.TextDTO.TextFormat, renderTree.TextDTO.LayoutRect, renderTree.TextDTO.Brush); } else if (renderTree.Type == eRenderType.Media && renderTree.MediaDTO.IsRenderable) //video/audio { } else if (renderTree.Type == eRenderType.Shape && renderTree.ShapeDTO.IsRenderable) //Geometry { d2dContext.Transform = //Matrix.Identity Matrix.Scaling(renderTree.ShapeDTO.MainScale) * Matrix.Translation(renderTree.ShapeDTO.MainTranslation) //* Matrix.Scaling(_globalScale) //* Matrix.Translation(_globalTranslation) ; if (renderTree.ShapeDTO.Type == 2) { d2dContext.FillGeometry(renderTree.ShapeDTO.Shape, renderTree.ShapeDTO.Brush); } else { d2dContext.DrawGeometry( renderTree.ShapeDTO.Shape, renderTree.ShapeDTO.Brush, renderTree.ShapeDTO.StrokeWidth, new SharpDX.Direct2D1.StrokeStyle( _deviceManager.FactoryDirect2D, //target.DeviceManager.FactoryDirect2D, new SharpDX.Direct2D1.StrokeStyleProperties() { DashStyle = (SharpDX.Direct2D1.DashStyle)renderTree.ShapeDTO.DashStyleIndex, DashOffset = renderTree.ShapeDTO.DashOffset, StartCap = SharpDX.Direct2D1.CapStyle.Square, EndCap = SharpDX.Direct2D1.CapStyle.Square, DashCap = SharpDX.Direct2D1.CapStyle.Square, MiterLimit = renderTree.ShapeDTO.MiterLimit })); } } else if (renderTree.Type == eRenderType.ShapePath && renderTree.ShapePathDTO.IsRenderable) //ShapePath Geometry { d2dContext.Transform = //Matrix.Identity Matrix.Scaling(renderTree.ShapePathDTO.MainScale) * Matrix.RotationX(renderTree.ShapePathDTO.MainRotation.X) * Matrix.RotationY(renderTree.ShapePathDTO.MainRotation.Y) * Matrix.RotationZ(renderTree.ShapePathDTO.MainRotation.Z) * Matrix.Translation(renderTree.ShapePathDTO.MainTranslation) //* Matrix.Scaling(_globalScale) //* Matrix.Translation(_globalTranslation) ; d2dContext.DrawGeometry( renderTree.ShapePathDTO.Shapes[0], renderTree.ShapePathDTO.Brush, renderTree.ShapePathDTO.StrokeWidth, new SharpDX.Direct2D1.StrokeStyle( _deviceManager.FactoryDirect2D,//target.DeviceManager.FactoryDirect2D, new SharpDX.Direct2D1.StrokeStyleProperties() { DashStyle = (SharpDX.Direct2D1.DashStyle)renderTree.ShapePathDTO.DashStyleIndex, DashOffset = renderTree.ShapePathDTO.DashOffset, StartCap = SharpDX.Direct2D1.CapStyle.Square, EndCap = SharpDX.Direct2D1.CapStyle.Square, DashCap = SharpDX.Direct2D1.CapStyle.Square, MiterLimit = renderTree.ShapePathDTO.MiterLimit })); } } d2dContext.Target = _tempTarget; d2dContext.EndDraw(); NumberFramesToRender--; } else { _useStagingBitmap = true; } d2dContext.BeginDraw(); d2dContext.Clear(Color.White); if (_useStagingBitmap) { d2dContext.Transform = Matrix.Scaling(_globalScale) * Matrix.Translation(_globalTranslation); d2dContext.DrawImage(_stagingBitmap); } //SELECTED TILE _drawSelectedTile(d2dContext); //DESIGNER SURFACE REGION //_drawDesktopOutline(d2dContext); //DEBUGGING INFO //_drawDebuggingInfo(d2dContext); d2dContext.EndDraw(); }
public void Begin(TargetBase render) { //var d3dContext = render.DeviceManager.ContextDirect3D; //var d2dContext = render.DeviceManager.ContextDirect2D; //m_d3dContext = d3dContext; m_renderTargetSize = new Vector2((float)render.RenderTargetSize.Width, (float)render.RenderTargetSize.Height); m_width = (float)render.RenderTargetSize.Width; m_height = (float)render.RenderTargetSize.Height; m_dpi = Windows.Graphics.Display.DisplayProperties.LogicalDpi; m_view = Matrix.LookAtLH(new Vector3(0, 0, -1), new Vector3(0, 0, 0), Vector3.UnitY); m_projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, m_width / (float)m_height, 0.1f, 2.0f); m_viewProj = Matrix.Multiply(m_view, m_projection); }
// Draw the model in the position having undergone the local transformation. public void Render(TargetBase render) { game.PushWorldMatrix(); game.world *= transformation * Matrix.RotationZ(rotationAngle) * Matrix.Translation(pos); game.updateConstantBuffer(); updateLighting(); model.Render(render); game.PopWorldMatrix(); }
public virtual void OnRender(TargetBase targetBase) { lock (lockObject) { if (isVideoStopped) return; if (mediaEngineEx != null) { long pts; if (mediaEngineEx.OnVideoStreamTick(out pts)) { var backBuffer = OutputVideoTexture ?? targetBase.BackBuffer; if (backBuffer != null) { var desc = backBuffer.Description; var region = new Rectangle(0, 0, desc.Width, desc.Height); mediaEngineEx.TransferVideoFrame(backBuffer, null, region, (ColorBGRA)BackgroundColor); } } } } }
public void Render(TargetBase target) { var d3dContext = target.DeviceManager.ContextDirect3D; var d2dContext = target.DeviceManager.ContextDirect2D; // Set targets (This is mandatory in the loop) d3dContext.OutputMerger.SetTargets(target.DepthStencilView, target.RenderTargetView); // Clear the views d3dContext.ClearDepthStencilView(target.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); if (EnableClear) { d3dContext.ClearRenderTargetView(target.RenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); } spriteBatch.Begin(target); for (int i = 0; i < 50; i++) { spriteBatch.Draw(_textureDot, _currentPointerPosition * new Vector2(1, -1)); } spriteBatch.End(); }
public abstract void drawContent(TargetBase target);
public virtual void Render(TargetBase render) { if (!RenderOn) return; var d3dContext = render.DeviceManager.ContextDirect3D; var d3dDevice = render.DeviceManager.DeviceDirect3D; float width = (float)render.RenderTargetSize.Width; float height = (float)render.RenderTargetSize.Height; // Prepare matrices //var view = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY); //var proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, width / (float)height, 0.1f, 100.0f); var view = Matrix.LookAtLH(new Vector3(0, 0, -3), new Vector3(0, 0, 0), Vector3.UnitY); var proj = Matrix.PerspectiveFovLH(0.925f, 1.0f, 0.1f, 100.0f); var viewProj = Matrix.Multiply(view, proj); var time = (float)(clock.ElapsedMilliseconds / 1000.0); // Set targets (This is mandatory in the loop) d3dContext.OutputMerger.SetTargets(render.DepthStencilView, render.RenderTargetView); // Clear the views d3dContext.ClearDepthStencilView(render.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); if (EnableClear) { d3dContext.ClearRenderTargetView(render.RenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); } //if (RenderOn) //{ foreach (var rDto in _renderTree.Where(x => x.D3DPrimitiveDTO.Project.IsRenderable)) { // Calculate WorldViewProj //var worldViewProj = Matrix.Scaling(Scale) * Matrix.RotationX(time) * Matrix.RotationY(time * 2.0f) * Matrix.RotationZ(time * .7f) * viewProj; var worldViewProj = Matrix.Scaling(rDto.D3DPrimitiveDTO.Project.ScaleX, rDto.D3DPrimitiveDTO.Project.ScaleY, rDto.D3DPrimitiveDTO.Project.ScaleZ) * Matrix.RotationX(rDto.D3DPrimitiveDTO.Project.RotationX) * Matrix.RotationY(rDto.D3DPrimitiveDTO.Project.RotationY) * Matrix.Translation(rDto.D3DPrimitiveDTO.Project.TranslationX, rDto.D3DPrimitiveDTO.Project.TranslationY, rDto.D3DPrimitiveDTO.Project.TranslationZ) //* Matrix.Scaling(_globalScaleX, _globalScaleY, 1) //* Matrix.RotationX(_globalRotationX) //* Matrix.RotationY(_globalRotationY) //* Matrix.Translation(_globalTranslationX, _globalTranslationY, _globalTranslationZ) * viewProj; worldViewProj.Transpose(); // Setup the pipeline d3dContext.InputAssembler.SetVertexBuffers(0, rDto.D3DPrimitiveDTO.VertexBufferBinding); d3dContext.InputAssembler.InputLayout = rDto.D3DPrimitiveDTO.Layout; d3dContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; d3dContext.VertexShader.SetConstantBuffer(0, rDto.D3DPrimitiveDTO.ConstantBuffer); d3dContext.VertexShader.Set(rDto.D3DPrimitiveDTO.VertexShader); d3dContext.PixelShader.SetShaderResource(0, rDto.D3DPrimitiveDTO.TextureView); d3dContext.PixelShader.SetSampler(0, rDto.D3DPrimitiveDTO.Sampler); d3dContext.PixelShader.Set(rDto.D3DPrimitiveDTO.PixelShader); // Update Buffers d3dContext.UpdateSubresource(ref worldViewProj, rDto.D3DPrimitiveDTO.ConstantBuffer, 0); d3dContext.OutputMerger.BlendState = rDto.D3DPrimitiveDTO.BlendState; // Draw the cube d3dContext.Draw(rDto.D3DPrimitiveDTO.VertexCount, 0); } //} }
/// <summary> /// Renders the screen. /// </summary> /// <param name="target">The Direct2D drawing target.</param> public virtual void Render(TargetBase target) { Point drawingPosition = new Point(0, 0); SurfaceImageSourceTarget surfaceImageSourceTarget = target as SurfaceImageSourceTarget; if (surfaceImageSourceTarget != null) { drawingPosition = surfaceImageSourceTarget.DrawingPosition; } IRenderableScreenCopy screenCopy = this.screen.GetScreenCopy(); var context2D = target.DeviceManager.ContextDirect2D; context2D.BeginDraw(); context2D.Transform = Matrix.Identity; context2D.Clear(this.GetColor(ScreenColor.DefaultBackground)); // 1. Paint backgrounds { RectangleF rect = new RectangleF(); var lines = screenCopy.Cells; for (int y = 0; y < lines.Length; y++) { var cols = lines[y]; rect.Top = drawingPosition.Y + (y * this.physicalFontMetrics.CellHeight); rect.Bottom = rect.Top + this.physicalFontMetrics.CellHeight; ScreenColor currentBackgroundColor = cols.Length > 0 ? cols[0].BackgroundColor : ScreenColor.DefaultBackground; ScreenColor cellBackgroundColor; int blockStart = 0; for (int x = 0; x <= cols.Length; x++) // loop once above the upper bound { var cell = cols[x < cols.Length ? x : x - 1]; bool isCursor = !screenCopy.CursorHidden && y == screenCopy.CursorRow && x == screenCopy.CursorColumn; cellBackgroundColor = isCursor ? ScreenColor.CursorBackground : cell.BackgroundColor; if (cellBackgroundColor != currentBackgroundColor || x == cols.Length) { rect.Left = drawingPosition.X + (blockStart * this.physicalFontMetrics.CellWidth); rect.Right = drawingPosition.X + (x * this.physicalFontMetrics.CellWidth); Brush backgroundBrush = this.GetBrush(context2D, this.GetColor(currentBackgroundColor)); if (currentBackgroundColor == ScreenColor.CursorBackground && !screenCopy.HasFocus) { rect.Right = rect.Right - 1.0f; context2D.DrawRectangle(rect, backgroundBrush); } else { context2D.FillRectangle(rect, backgroundBrush); } blockStart = x; currentBackgroundColor = cellBackgroundColor; } } } } // 2. Paint foregrounds { RectangleF rect = new RectangleF(); var lines = screenCopy.Cells; for (int y = 0; y < lines.Length; y++) { var cols = lines[y]; rect.Top = drawingPosition.Y + (y * this.physicalFontMetrics.CellHeight); rect.Bottom = rect.Top + this.physicalFontMetrics.CellHeight; ScreenColor currentForegroundColor = cols.Length > 0 ? cols[0].ForegroundColor : ScreenColor.DefaultForeground; ScreenCellModifications currentCellModifications = cols.Length > 0 ? cols[0].Modifications : ScreenCellModifications.None; bool currentCellUCSWIDE = cols.Length > 0 ? cols[0].Character == CjkWidth.UCSWIDE : false; ScreenColor cellForegroundColor; int blockStart = 0; for (int x = 0; x <= cols.Length; x++) // loop once above the upper bound { var cell = cols[x < cols.Length ? x : x - 1]; bool isCursor = !screenCopy.CursorHidden && y == screenCopy.CursorRow && x == screenCopy.CursorColumn; cellForegroundColor = isCursor && screenCopy.HasFocus ? ScreenColor.CursorForeground : cell.ForegroundColor; if (currentCellUCSWIDE || cellForegroundColor != currentForegroundColor || cell.Modifications != currentCellModifications || x == cols.Length) { rect.Left = drawingPosition.X + (blockStart * this.physicalFontMetrics.CellWidth); rect.Right = drawingPosition.X + (x * this.physicalFontMetrics.CellWidth); Brush foregroundBrush = this.GetBrush(context2D, this.GetColor(currentForegroundColor)); TextFormat textFormat = this.textFormatNormal; if (currentCellModifications.HasFlag(ScreenCellModifications.Bold)) { textFormat = this.textFormatBold; } string text = new string(cols.Skip(blockStart).Take(x - blockStart).Select(c => char.IsWhiteSpace(c.Character) ? ' ' : c.Character).Where(ch => ch != CjkWidth.UCSWIDE).ToArray()).TrimEnd(); if (text.Length > 0) { context2D.DrawText(text, textFormat, rect, foregroundBrush, DrawTextOptions.Clip); } if (currentCellModifications.HasFlag(ScreenCellModifications.Underline)) { var point1 = new Vector2(rect.Left, rect.Bottom - 1.0f) + drawingPosition; var point2 = new Vector2(rect.Right, rect.Bottom - 1.0f) + drawingPosition; context2D.DrawLine(point1, point2, foregroundBrush); } blockStart = x; currentForegroundColor = cellForegroundColor; } currentCellUCSWIDE = cell.Character == CjkWidth.UCSWIDE; } } } context2D.EndDraw(); }
public override void Update(DeviceContext context, TargetBase target, DeviceManager deviceManager, Point screenSize, float dt, float elapsedTime) { if (AfterStartFreeze) { // Player moving _playerPos = _playerPos.Add(_playerDir.Mul(_playerSpd).Mul(dt)).Clamp(GamesParams.Margin0.Add(_padSize.Half()), GamesParams.Margin1.Sub(_padSize.Half())); // Ball moving _ballPos = _ballPos.Add(_ballDir.Mul(_ballSpd).Mul(dt)); // Ball collision with borders if (!_ballPos.IsInside(GamesParams.Margin0.Add(_ballSize.Half()), GamesParams.Margin1.Sub(_ballSize.Half()))) { _ballDir.Y = -_ballDir.Y; if (_ballPos.X < GamesParams.MarginX0 + 0.001f) { NewGame(); _gameManager.Die(); return; } if (_ballPos.X > GamesParams.MarginX1 - 0.001f) { _gameManager.Win(500); _ballPos = new Point(0.5f, 0.5f); _ballDir = new Point(1.0f, 0.0f).AddNoise().Normalise(); return; } } _ballPos = _ballPos.Clamp(GamesParams.Margin0.Add(_ballSize.Half()), GamesParams.Margin1.Sub(_ballSize.Half())); // Ball collision with player pad if (_ballPos.IsInside(_playerPos.Sub(_padSize.Half()).Sub(_ballSize.Half()), _playerPos.Add(_padSize.Half()).Add(_ballSize.Half()))) { _ballDir.X = 1; _ballDir.Y = _ballPos.Sub(_playerPos).Y/_padSize.Half().Y; _ballDir = _ballDir.Normalise(); _ballSpd += _ballSpdInc; _gameManager.AddPoints(75); } // Ball collision with enemy pad if (_ballPos.IsInside(_enemyPos.Sub(_padSize.Half()).Sub(_ballSize.Half()), _enemyPos.Add(_padSize.Half()).Add(_ballSize.Half()))) { _ballDir.X = -1; _ballDir.Y = _ballPos.Sub(_enemyPos).Y/_padSize.Half().Y; _ballDir = _ballDir.Normalise(); _ballSpd += _ballSpdInc; } // Enemy moving if (Math.Abs(_ballPos.Y - _enemyPos.Y) < _padSize.Div(3).Y) { _enemyDir.Y = 0; } else { if (_ballPos.Y > _enemyPos.Y) _enemyDir.Y = 1; else _enemyDir.Y = -1; _enemyPos = _enemyPos.Add(_enemyDir.Mul(_enemySpd).Mul(dt)).Clamp(GamesParams.Margin0.Add(_padSize.Half()), GamesParams.Margin1.Sub(_padSize.Half())); } } // Drawing var playerBox = _playerPos.ToBox(_padSize); var enemyBox = _enemyPos.ToBox(_padSize); var ballBox = _ballPos.ToBox(_ballSize); context.FillRectangle(screenSize.ApplyTo(playerBox), GamesParams.PlayerColor); context.FillRectangle(screenSize.ApplyTo(enemyBox), GamesParams.EnemyColor); context.FillRectangle(screenSize.ApplyTo(ballBox), GamesParams.AdditionalColor); base.Update(context, target, deviceManager, screenSize, dt, elapsedTime); }
protected virtual void CreateSizeDependentResources(TargetBase renderBase) { var d3dDevice = DeviceManager.DeviceDirect3D; var d3dContext = DeviceManager.ContextDirect3D; var d2dContext = DeviceManager.ContextDirect2D; d2dContext.Target = null; SafeDispose(ref renderTargetView); SafeDispose(ref depthStencilView); SafeDispose(ref bitmapTarget); // If the swap chain already exists, resize it. if (swapChain != null) { swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None); } // Otherwise, create a new one. else { // SwapChain description var desc = CreateSwapChainDescription(); // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device // First, retrieve the underlying DXGI Device from the D3D Device. // Creates the swap chain using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>()) using (var dxgiAdapter = dxgiDevice2.Adapter) using (var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>()) { swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc)); // Ensure that DXGI does not queue more than one frame at a time. This both reduces // latency and ensures that the application will only render after each VSync, minimizing // power consumption. dxgiDevice2.MaximumFrameLatency = 1; } } // Obtain the backbuffer for this window which will be the final 3D rendertarget. using (var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(swapChain, 0)) { // Create a view interface on the rendertarget to use on bind. renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, backBuffer)); // Cache the rendertarget dimensions in our helper class for convenient use. var backBufferDesc = backBuffer.Description; RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height); } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)RenderTargetSize.Width, Height = (int)RenderTargetSize.Height, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Create a viewport descriptor of the full window size. var viewport = new SharpDX.Direct3D11.Viewport((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f); // Set the current viewport using the descriptor. d3dContext.Rasterizer.SetViewports(viewport); // Now we set up the Direct2D render target bitmap linked to the swapchain. // Whenever we render to this bitmap, it will be directly rendered to the // swapchain associated with the window. var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1( new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), DeviceManager.Dpi, DeviceManager.Dpi, SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw); // Direct2D needs the dxgi version of the backbuffer surface pointer. // Get a D2D surface from the DXGI back buffer to use as the D2D render target. using (var dxgiBackBuffer = swapChain.GetBackBuffer<SharpDX.DXGI.Surface>(0)) bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties)); // So now we can set the Direct2D render target. d2dContext.Target = BitmapTarget2D; // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces. d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale; }
public virtual void Render(TargetBase render) { var d3dContext = render.DeviceManager.ContextDirect3D; float width = (float)render.RenderTargetSize.Width; float height = (float)render.RenderTargetSize.Height; // Prepare matrices var view = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY); var proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, width / (float)height, 0.1f, 100.0f); var viewProj = Matrix.Multiply(view, proj); var time = (float)(clock.ElapsedMilliseconds / 1000.0); // Set targets (This is mandatory in the loop) d3dContext.OutputMerger.SetTargets(render.DepthStencilView, render.RenderTargetView); // Clear the views d3dContext.ClearDepthStencilView(render.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); if (EnableClear) { d3dContext.ClearRenderTargetView(render.RenderTargetView, Color.Black); } if (ShowCube) { // Calculate WorldViewProj var worldViewProj = Matrix.Scaling(Scale) * Matrix.RotationX(time) * Matrix.RotationY(time * 2.0f) * Matrix.RotationZ(time * .7f) * viewProj; worldViewProj.Transpose(); // Setup the pipeline d3dContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding); d3dContext.InputAssembler.InputLayout = layout; d3dContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; d3dContext.VertexShader.SetConstantBuffer(0, constantBuffer); d3dContext.VertexShader.Set(vertexShader); d3dContext.PixelShader.SetShaderResource(0, textureView); d3dContext.PixelShader.SetSampler(0, sampler); d3dContext.PixelShader.Set(pixelShader); // Update Constant Buffer d3dContext.UpdateSubresource(ref worldViewProj, constantBuffer, 0); // Draw the cube d3dContext.Draw(36, 0); } }