Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Media.Imaging;
- using GFXLibrary;
- using System.Threading;
- using System.Drawing.Imaging;
- namespace GFXLibrary
- {
- [Docking(DockingBehavior.Ask)]
- public class GFXControl : Control, IUpdatable, IGFXObject
- {
- UpdateEventThread updateEventThread;
- Bitmap m_DrawBufferBitmap;
- BufferedGraphicsContext m_BufferedGraphicsContext;
- BufferedGraphics m_BufferedGraphics;
- protected Rectangle m_DrawArea;
- bool m_Initialized;
- public GFXControl() : base()
- {
- updateEventThread = UpdateEventThread.GetUpdateEventThread(OnUpdate);
- m_Initialized = false;
- this.SetStyle(ControlStyles.UserPaint, true);
- }
- public bool Initialized
- {
- get { return m_Initialized; }
- private set { m_Initialized = value; }
- }
- public void Initialize()
- {
- m_DrawArea = new Rectangle(0, 0, this.Width, this.Height);
- if (Initialized)
- {
- m_DrawBufferBitmap.Dispose();
- }
- m_DrawBufferBitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);
- m_DrawBufferBitmap.MakeTransparent();
- m_BufferedGraphicsContext = BufferedGraphicsManager.Current;
- m_BufferedGraphics = m_BufferedGraphicsContext.Allocate(Graphics.FromImage(m_DrawBufferBitmap), m_DrawArea);
- Initialized = true;
- }
- public Graphics GetGraphics()
- {
- return m_BufferedGraphics.Graphics;
- }
- public void RenderToScreen()
- {
- lock (this)
- {
- var gfx = this.CreateGraphics();
- //gfx.Clear(Color.Transparent);
- //gfx.DrawImage(m_DrawBufferBitmap, m_DrawArea);
- m_BufferedGraphics.Render(gfx);
- m_BufferedGraphics.Graphics.Clear(Color.Transparent);
- }
- }
- ~GFXControl()
- {
- updateEventThread.RemoveEvent(OnUpdate);
- updateEventThread = null;
- }
- public void OnCreate(bool init)
- {
- if (init)
- {
- updateEventThread.AddEvent(OnUpdate);
- }
- else
- {
- updateEventThread.RemoveEvent(OnUpdate);
- }
- }
- public virtual void OnUpdate(object sender, UpdateEventArgs e)
- {
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- }
- private void InitializeComponent()
- {
- this.SuspendLayout();
- //
- // GFXControl
- //
- this.Move += new System.EventHandler(this.GFXControl_Move);
- this.Resize += new System.EventHandler(this.GFXControl_Resize);
- this.ResumeLayout(false);
- }
- private void GFXControl_Resize(object sender, EventArgs e)
- {
- Initialize();
- }
- private void GFXControl_Move(object sender, EventArgs e)
- {
- Initialize();
- }
- }
- }
- //--------------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using GFXLibrary;
- using System.Drawing;
- using System.Windows.Forms;
- using System.ComponentModel;
- namespace GFXLibrary_Test
- {
- public class FPSControl : GFXControl, IUpdatable
- {
- public FPSControl() : base()
- {
- this.SetStyle(ControlStyles.UserPaint, true);
- this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- this.BackColor = Color.Transparent;
- }
- public override void OnUpdate(object sender, UpdateEventArgs e)
- {
- if (this.Initialized)
- {
- lock (this)
- {
- //Console.WriteLine("Drawing to buffer!");
- var gfx = this.GetGraphics();
- gfx.DrawString(GetFPS(), SystemFonts.DefaultFont, new SolidBrush(Color.FromArgb(255,255,255,255)), 0, 0);
- this.RenderToScreen();
- }
- }
- }
- long startTicks = 0;
- public string GetFPS()
- {
- long now = DateTime.Now.Ticks;
- TimeSpan ts = new TimeSpan(now - startTicks);
- startTicks = now;
- return "FPS: " + Math.Floor(1000 / ts.TotalMilliseconds);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement