Advertisement
imk0tter

GFXControl

Jul 26th, 2021
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. using System.Windows.Forms;
  10. using System.Windows.Media.Imaging;
  11. using GFXLibrary;
  12.  
  13. using System.Threading;
  14. using System.Drawing.Imaging;
  15.  
  16. namespace GFXLibrary
  17. {
  18.     [Docking(DockingBehavior.Ask)]
  19.     public class GFXControl : Control, IUpdatable, IGFXObject
  20.     {
  21.         UpdateEventThread updateEventThread;
  22.  
  23.         Bitmap m_DrawBufferBitmap;
  24.  
  25.         BufferedGraphicsContext m_BufferedGraphicsContext;
  26.         BufferedGraphics m_BufferedGraphics;
  27.  
  28.         protected Rectangle m_DrawArea;
  29.  
  30.         bool m_Initialized;
  31.         public GFXControl() : base()
  32.         {
  33.             updateEventThread = UpdateEventThread.GetUpdateEventThread(OnUpdate);
  34.             m_Initialized = false;
  35.  
  36.             this.SetStyle(ControlStyles.UserPaint, true);
  37.         }
  38.         public bool Initialized
  39.         {
  40.             get { return m_Initialized; }
  41.             private set { m_Initialized = value; }
  42.         }
  43.         public void Initialize()
  44.         {
  45.             m_DrawArea = new Rectangle(0, 0, this.Width, this.Height);
  46.  
  47.             if (Initialized)
  48.             {
  49.                 m_DrawBufferBitmap.Dispose();
  50.             }
  51.                
  52.             m_DrawBufferBitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);
  53.             m_DrawBufferBitmap.MakeTransparent();
  54.  
  55.             m_BufferedGraphicsContext = BufferedGraphicsManager.Current;
  56.             m_BufferedGraphics = m_BufferedGraphicsContext.Allocate(Graphics.FromImage(m_DrawBufferBitmap), m_DrawArea);
  57.  
  58.  
  59.             Initialized = true;
  60.  
  61.         }
  62.         public Graphics GetGraphics()
  63.         {
  64.             return m_BufferedGraphics.Graphics;
  65.         }
  66.         public void RenderToScreen()
  67.         {
  68.             lock (this)
  69.             {
  70.                 var gfx = this.CreateGraphics();
  71.  
  72.                 //gfx.Clear(Color.Transparent);
  73.  
  74.                 //gfx.DrawImage(m_DrawBufferBitmap, m_DrawArea);
  75.  
  76.                 m_BufferedGraphics.Render(gfx);
  77.  
  78.                 m_BufferedGraphics.Graphics.Clear(Color.Transparent);
  79.             }
  80.         }
  81.  
  82.         ~GFXControl()
  83.         {
  84.             updateEventThread.RemoveEvent(OnUpdate);
  85.             updateEventThread = null;
  86.         }
  87.  
  88.         public void OnCreate(bool init)
  89.         {
  90.             if (init)
  91.             {
  92.                 updateEventThread.AddEvent(OnUpdate);
  93.             }
  94.             else
  95.             {
  96.                 updateEventThread.RemoveEvent(OnUpdate);
  97.             }
  98.         }
  99.  
  100.         public virtual void OnUpdate(object sender, UpdateEventArgs e)
  101.         {
  102.         }
  103.  
  104.         protected override void OnPaint(PaintEventArgs e)
  105.         {
  106.         }
  107.  
  108.         private void InitializeComponent()
  109.         {
  110.             this.SuspendLayout();
  111.             //
  112.             // GFXControl
  113.             //
  114.             this.Move += new System.EventHandler(this.GFXControl_Move);
  115.             this.Resize += new System.EventHandler(this.GFXControl_Resize);
  116.             this.ResumeLayout(false);
  117.  
  118.         }
  119.  
  120.         private void GFXControl_Resize(object sender, EventArgs e)
  121.         {
  122.             Initialize();
  123.         }
  124.  
  125.         private void GFXControl_Move(object sender, EventArgs e)
  126.         {
  127.             Initialize();
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133. //--------------------------------------------------------------------------------------
  134.  
  135. using System;
  136. using System.Collections.Generic;
  137. using System.Linq;
  138. using System.Text;
  139. using System.Threading.Tasks;
  140.  
  141. using GFXLibrary;
  142.  
  143. using System.Drawing;
  144. using System.Windows.Forms;
  145. using System.ComponentModel;
  146.  
  147. namespace GFXLibrary_Test
  148. {
  149.     public class FPSControl : GFXControl, IUpdatable
  150.     {
  151.         public FPSControl() : base()
  152.         {
  153.             this.SetStyle(ControlStyles.UserPaint, true);
  154.             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  155.  
  156.             this.BackColor = Color.Transparent;
  157.         }
  158.         public override void OnUpdate(object sender, UpdateEventArgs e)
  159.         {
  160.             if (this.Initialized)
  161.             {
  162.                 lock (this)
  163.                 {
  164.                     //Console.WriteLine("Drawing to buffer!");
  165.                     var gfx = this.GetGraphics();
  166.                     gfx.DrawString(GetFPS(), SystemFonts.DefaultFont, new SolidBrush(Color.FromArgb(255,255,255,255)), 0, 0);
  167.                     this.RenderToScreen();
  168.                 }
  169.             }
  170.         }
  171.  
  172.         long startTicks = 0;
  173.         public string GetFPS()
  174.         {
  175.             long now = DateTime.Now.Ticks;
  176.             TimeSpan ts = new TimeSpan(now - startTicks);
  177.             startTicks = now;
  178.  
  179.             return "FPS: " + Math.Floor(1000 / ts.TotalMilliseconds);
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement