/// <summary> /// Fills a Rectangle. /// </summary> /// <param name="color">The Color.</param> /// <param name="rectangle">The Rectangle.</param> public void FillRectangle(Color color, Rectangle rectangle) { var line = new Line(_direct3D9Device) {Antialias = true, Width = rectangle.Height}; line.Begin(); line.Draw( DirectXHelper.ConvertToVertex(new Vector2(rectangle.X, rectangle.Center.Y), new Vector2(rectangle.X + rectangle.Width, rectangle.Center.Y)), DirectXHelper.ConvertColor(color)); line.End(); }
/// <summary> /// Hook for IDirect3DDevice9.EndScene /// </summary> /// <param name="devicePtr">Pointer to the IDirect3DDevice9 instance. Note: object member functions always pass "this" as the first parameter.</param> /// <returns>The HRESULT of the original EndScene</returns> /// <remarks>Remember that this is called many times a second by the Direct3D application - be mindful of memory and performance!</remarks> int EndSceneHook(IntPtr devicePtr) { using (Device device = Device.FromPointer(devicePtr)) { // If you need to capture at a particular frame rate, add logic here decide whether or not to skip the frame try { #region Screenshot Request // Is there a screenshot request? If so lets grab the backbuffer lock (_lockRenderTarget) { if (Request != null) { _lastRequestTime = DateTime.Now; DateTime start = DateTime.Now; try { // First ensure we have a Surface to the render target data into if (_renderTarget == null) { // Create offscreen surface to use as copy of render target data using (SwapChain sc = device.GetSwapChain(0)) { _renderTarget = Surface.CreateOffscreenPlain(device, sc.PresentParameters.BackBufferWidth, sc.PresentParameters.BackBufferHeight, sc.PresentParameters.BackBufferFormat, Pool.SystemMemory); } } #region Prepare lines for overlay if (this.Request.RegionToCapture.Width == 0) { _lineVectors = new SlimDX.Vector2[] { new SlimDX.Vector2(0, 0), new SlimDX.Vector2(_renderTarget.Description.Width - 1, _renderTarget.Description.Height - 1), new SlimDX.Vector2(0, _renderTarget.Description.Height - 1), new SlimDX.Vector2(_renderTarget.Description.Width - 1, 0), new SlimDX.Vector2(0, 0), new SlimDX.Vector2(0, _renderTarget.Description.Height - 1), new SlimDX.Vector2(_renderTarget.Description.Width - 1, _renderTarget.Description.Height - 1), new SlimDX.Vector2(_renderTarget.Description.Width - 1, 0), }; } else { _lineVectors = new SlimDX.Vector2[] { new SlimDX.Vector2(this.Request.RegionToCapture.X, this.Request.RegionToCapture.Y), new SlimDX.Vector2(this.Request.RegionToCapture.Right, this.Request.RegionToCapture.Bottom), new SlimDX.Vector2(this.Request.RegionToCapture.X, this.Request.RegionToCapture.Bottom), new SlimDX.Vector2(this.Request.RegionToCapture.Right, this.Request.RegionToCapture.Y), new SlimDX.Vector2(this.Request.RegionToCapture.X, this.Request.RegionToCapture.Y), new SlimDX.Vector2(this.Request.RegionToCapture.X, this.Request.RegionToCapture.Bottom), new SlimDX.Vector2(this.Request.RegionToCapture.Right, this.Request.RegionToCapture.Bottom), new SlimDX.Vector2(this.Request.RegionToCapture.Right, this.Request.RegionToCapture.Y), }; } #endregion using (Surface backBuffer = device.GetBackBuffer(0, 0)) { // Create a super fast copy of the back buffer on our Surface device.GetRenderTargetData(backBuffer, _renderTarget); // We have the back buffer data and can now work on copying it to a bitmap ProcessRequest(); } } finally { // We have completed the request - mark it as null so we do not continue to try to capture the same request // Note: If you are after high frame rates, consider implementing buffers here to capture more frequently // and send back to the host application as needed. The IPC overhead significantly slows down // the whole process if sending frame by frame. Request = null; } DateTime end = DateTime.Now; this.DebugMessage("EndSceneHook: Capture time: " + (end - start).ToString()); _lastScreenshotTime = (end - start); } } #endregion #region Example: Draw Overlay (after screenshot so we don't capture overlay as well) if (this.ShowOverlay) { #region Draw fading lines based on last screencapture request if (_lastRequestTime != null && _lineVectors != null) { TimeSpan timeSinceRequest = DateTime.Now - _lastRequestTime.Value; if (timeSinceRequest.TotalMilliseconds < 1000.0) { using (Line line = new Line(device)) { _lineAlpha = (float)((1000.0 - timeSinceRequest.TotalMilliseconds) / 1000.0); // This is our fade out line.Antialias = true; line.Width = 1.0f; line.Begin(); line.Draw(_lineVectors, new SlimDX.Color4(_lineAlpha, 0.5f, 0.5f, 1.0f)); line.End(); } } else { _lineVectors = null; } } #endregion #region Draw frame rate using (SlimDX.Direct3D9.Font font = new SlimDX.Direct3D9.Font(device, new System.Drawing.Font("Times New Roman", 16.0f))) { if (_lastFrame != null) { font.DrawString(null, String.Format("{0:N1} fps", (1000.0 / (DateTime.Now - _lastFrame.Value).TotalMilliseconds)), 100, 100, System.Drawing.Color.Red); } _lastFrame = DateTime.Now; } #endregion } #endregion } catch(Exception e) { // If there is an error we do not want to crash the hooked application, so swallow the exception this.DebugMessage("EndSceneHook: Exeception: " + e.GetType().FullName + ": " + e.Message); } // EasyHook has already repatched the original EndScene - so calling EndScene against the SlimDX device will call the original // EndScene and bypass this hook. EasyHook will automatically reinstall the hook after this hook function exits. return device.EndScene().Code; } }
/// <summary> /// Draws a Rectangle. /// </summary> /// <param name="pen">The Pen.</param> /// <param name="rectangle">The Rectangle.</param> public void DrawRectangle(Pen pen, Rectangle rectangle) { var dxPen = pen.Instance as DirectXPen; if (dxPen == null) throw new ArgumentException("DirectX9 expects a DirectXPen as resource."); var line = new Line(_direct3D9Device) {Antialias = true, Width = dxPen.Width}; line.Begin(); line.Draw( DirectXHelper.ConvertToVertex( new Vector2(rectangle.X, rectangle.Y), new Vector2(rectangle.X + rectangle.Width, rectangle.Y), new Vector2(rectangle.X, rectangle.Y + rectangle.Height), new Vector2(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), new Vector2(rectangle.X, rectangle.Y), new Vector2(rectangle.X, rectangle.Y + rectangle.Height), new Vector2(rectangle.X + rectangle.Width, rectangle.Y), new Vector2(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height)), DirectXHelper.ConvertColor(dxPen.Color)); line.End(); }
/// <summary> /// Fills a Ellipse. /// </summary> /// <param name="color">The Color.</param> /// <param name="ellipse">The Ellipse.</param> public void FillEllipse(Color color, Ellipse ellipse) { var line = new Line(_direct3D9Device) {Antialias = false, Width = 2}; line.Begin(); line.Draw(DirectXHelper.ConvertToVertex(ellipse.Points), DirectXHelper.ConvertColor(color)); line.End(); }
/// <summary> /// Draws a Polygon. /// </summary> /// <param name="pen">The Pen.</param> /// <param name="polygon">The Polygon.</param> public void DrawPolygon(Pen pen, Polygon polygon) { var dxPen = pen.Instance as DirectXPen; if (dxPen == null) throw new ArgumentException("DirectX9 expects a DirectXPen as resource."); var line = new Line(_direct3D9Device) {Antialias = true, Width = dxPen.Width}; line.Begin(); line.Draw(DirectXHelper.ConvertToVertex(polygon.Points), DirectXHelper.ConvertColor(dxPen.Color)); line.End(); }
/// <summary> /// Draws a line. /// </summary> /// <param name="token">The token.</param> /// <param name="startPoint">The start point.</param> /// <param name="endPoint">The end point.</param> public void DrawLine(char token, Coordinate startPoint, Coordinate endPoint) { var line = new Line(startPoint, endPoint, token); line.Draw(this); }
private void RenderEngine(object obj) { SolidBrush Background = new SolidBrush(Color.White); int Ticks_1000ms = Environment.TickCount; int Ticks_50ms = Environment.TickCount; Point Acceleration = new Point(0, 1); // 10 px down Point No_Acceleration = new Point(-Acceleration.X * 2, -Acceleration.Y * 2); Pen Arrow = new Pen(new SolidBrush(Color.Green), 2f); Pen TestPoints = new Pen(new SolidBrush(Color.Red), 1f); Pen TestCutts = new Pen(new SolidBrush(Color.Lime), 1f); Font fps_font = new Font("Arial", 12); SolidBrush fps_brush = new SolidBrush(Color.Black); Bitmap RenderedFrame = new Bitmap(Paint_Area.Width, Paint_Area.Height); Graphics f_Handle = Graphics.FromImage(RenderedFrame); //Obstacle Obs = new Obstacle(new Point()) //TestCut = new Point(Paint_Area.Width / 2, Paint_Area.Height / 2); Cross Test = new Cross(new Point(Paint_Area.Width / 2, Paint_Area.Height / 2)); // centered test point Test.Color = Color.Green; Line Mirror = new Line(); Mirror.Start = new Point(Paint_Area.Width / 2 - 100, Paint_Area.Height / 2 - 100); Mirror.End = new Point(Paint_Area.Width / 2 + 100, Paint_Area.Height / 2 + 100); Mirror.Color = Color.Gray; Line Exit = new Line(); Exit.Color = Color.Red; Exit.Start = Test.Position; int fps = 0; int frames_rendered = 0; string info = ""; double Angle = 45; double Length = 300; while(true) { // draw background f_Handle.FillRectangle(Background, Paint_Area); /* // draw balls foreach(Ball ball in Balls) { ball.Draw(f_Handle); } // draw lines foreach(Obstacle obs in Lines) { obs.Draw(f_Handle); } // draw arrow if (ShowArrow) { f_Handle.DrawLine(Arrow, ArrowStart, ArrowEnd); // calculate angle and arrow }*/ f_Handle.DrawString(fps.ToString() + "fps", fps_font, fps_brush, 0, 0); // draw crosses Test.Draw(f_Handle, Paint_Area.Height); Actual.Draw(f_Handle, Paint_Area.Height); The.Start = Actual.Position; The.End = Test.Position; The.Draw(f_Handle, Paint_Area.Height); Mirror.Draw(f_Handle, Paint_Area.Height); // calculate end double beta = Mirror.Angle - The.Angle; double c = The.Lenght * Math.Cos(beta / 360 * 2 * Math.PI); Exit.End = new Point((int)(2 * c * Math.Cos(Mirror.Angle / 360 * 2 * Math.PI) + The.Start.X), (int)(2 * c * Math.Sin(Mirror.Angle / 360 * 2 * Math.PI) + The.Start.Y)); Exit.Draw(f_Handle, Paint_Area.Height); // draw info info = "beta: " + beta.ToString() + "\n"; info += "c: " + c.ToString(); f_Handle.DrawString(info, fps_font, fps_brush, 0, 50); // draw line /* // testpoints f_Handle.DrawLine(TestPoints, TestStart.X - 10, TestStart.Y, TestStart.X + 10, TestStart.Y); f_Handle.DrawLine(TestPoints, TestStart.X, TestStart.Y - 10, TestStart.X, TestStart.Y + 10); f_Handle.DrawLine(TestPoints, TestEnd.X - 10, TestEnd.Y, TestEnd.X + 10, TestEnd.Y); f_Handle.DrawLine(TestPoints, TestEnd.X, TestEnd.Y - 10, TestEnd.X, TestEnd.Y + 10); f_Handle.DrawLine(TestCutts, TestCut.X - 10, TestCut.Y, TestCut.X + 10, TestCut.Y); f_Handle.DrawLine(TestCutts, TestCut.X, TestCut.Y - 10, TestCut.X, TestCut.Y + 10);*/ // draw on screen g_Handle.DrawImage(RenderedFrame, 0, 0,800,600); // check for change if(Environment.TickCount > (Ticks_50ms + 50)) { // refresh ticks Ticks_50ms = Environment.TickCount; // every 50 ms Mirror.Start.X = (int)(-(Length / 2) * Math.Sin(Angle / 360 * 2 * Math.PI) + The.End.X); Mirror.Start.Y = (int)(-(Length / 2) * Math.Cos(Angle / 360 * 2 * Math.PI) + The.End.Y); Mirror.End.X = (int)((Length / 2) * Math.Sin(Angle / 360 * 2 * Math.PI) + The.End.X); Mirror.End.Y = (int)((Length / 2) * Math.Cos(Angle / 360 * 2 * Math.PI) + The.End.Y); // inkrement angle //Angle += 1; } // calculate fps if (Environment.TickCount > (Ticks_1000ms + 1000)) { // refresh ticks Ticks_1000ms = Environment.TickCount; fps = frames_rendered; frames_rendered = 0; } frames_rendered++; /* // move balls every 500 ms if(Environment.TickCount > (Ticks + 10)) { // refresh ticks Ticks = Environment.TickCount; // move balls foreach(Ball ball in Balls) { // move ball ball.Move(Acceleration); // check for exit down if(ball.Position.Y > Paint_Area.Height) { // change direction ball.Movement = new Point(ball.Movement.X, -ball.Movement.Y * 70 / 100); ball.Move(No_Acceleration); } // exit right if ((ball.Position.X > Paint_Area.Width) || (ball.Position.X < 0)) { // change direction ball.Movement = new Point(-ball.Movement.X * 70 / 100, ball.Movement.Y); ball.Move(No_Acceleration); } // check for collision foreach(Obstacle obs in Lines) { obs.calculateCollision(ball); } } // check for finish Balls.RemoveAll(x => (x.Position.X > Paint_Area.Width) || (x.Position.X < 0) || (x.Position.Y > Paint_Area.Height) || (x.Position.Y < 0)); } // check for new balls while(Queued_Balls.Count > 0) { Balls.Add(Queued_Balls.Dequeue()); } */ } }
public static void DrawRectangle(System.Drawing.Rectangle rect, long color, bool fill) { if (fill) { System.Drawing.Rectangle[] regions = new System.Drawing.Rectangle[1] { rect }; GUIGraphicsContext.DX9Device.Clear(ClearFlags.Target, (int)color, 1f, 0, regions); } else { Vector2[] vertexList = new Vector2[2]; vertexList[0].X = (float)rect.Left; vertexList[0].Y = (float)rect.Top; vertexList[1].X = (float)(rect.Left + rect.Width); vertexList[1].Y = (float)rect.Top; using (Line line = new Line(GUIGraphicsContext.DX9Device)) { line.Begin(); line.Draw(vertexList, (int)color); vertexList[0].X = (float)(rect.Left + rect.Width); vertexList[0].Y = (float)rect.Top; vertexList[1].X = (float)(rect.Left + rect.Width); vertexList[1].Y = (float)(rect.Top + rect.Height); line.Draw(vertexList, (int)color); vertexList[0].X = (float)(rect.Left + rect.Width); vertexList[0].Y = (float)(rect.Top + rect.Width); vertexList[1].X = (float)rect.Left; vertexList[1].Y = (float)(rect.Top + rect.Height); line.Draw(vertexList, (int)color); vertexList[0].X = (float)rect.Left; vertexList[0].Y = (float)(rect.Top + rect.Height); vertexList[1].X = (float)rect.Left; vertexList[1].Y = (float)rect.Top; line.Draw(vertexList, (int)color); line.End(); } } }
public static void DrawLine(int x1, int y1, int x2, int y2, long color) { Vector2[] vertexList = new Vector2[2]; vertexList[0].X = (float)x1; vertexList[0].Y = (float)y1; vertexList[1].X = (float)x2; vertexList[1].Y = (float)y2; using (Line line = new Line(GUIGraphicsContext.DX9Device)) { line.Begin(); line.Draw(vertexList, (int)color); line.End(); } }