|
| 1 | +using System; |
| 2 | + |
| 3 | +using GameOverlay.Graphics; |
| 4 | +using GameOverlay.Graphics.Primitives; |
| 5 | +using GameOverlay.Utilities; |
| 6 | +using GameOverlay.Windows; |
| 7 | + |
| 8 | +namespace GameOverlayExample |
| 9 | +{ |
| 10 | + public class Example |
| 11 | + { |
| 12 | + private OverlayWindow _window; |
| 13 | + private D2DDevice _device; |
| 14 | + private FrameTimer _frameTimer; |
| 15 | + |
| 16 | + private bool _initializeGraphicObjects; |
| 17 | + |
| 18 | + private D2DColor _backgroundColor; |
| 19 | + |
| 20 | + private D2DFont _font; |
| 21 | + |
| 22 | + private D2DSolidColorBrush _blackBrush; |
| 23 | + |
| 24 | + private D2DSolidColorBrush _redBrush; |
| 25 | + private D2DSolidColorBrush _greenBrush; |
| 26 | + private D2DSolidColorBrush _blueBrush; |
| 27 | + |
| 28 | + private D2DImage _image; |
| 29 | + |
| 30 | + public Example() |
| 31 | + { |
| 32 | + SetupInstance(); |
| 33 | + } |
| 34 | + |
| 35 | + ~Example() |
| 36 | + { |
| 37 | + DestroyInstance(); |
| 38 | + } |
| 39 | + |
| 40 | + private void SetupInstance() |
| 41 | + { |
| 42 | + _window = new OverlayWindow(new OverlayOptions() |
| 43 | + { |
| 44 | + BypassTopmost = false, |
| 45 | + Height = 600, |
| 46 | + Width = 800, |
| 47 | + WindowTitle = "GameOverlayExample", |
| 48 | + X = 0, |
| 49 | + Y = 0 |
| 50 | + }); |
| 51 | + |
| 52 | + _device = new D2DDevice(new DeviceOptions() |
| 53 | + { |
| 54 | + AntiAliasing = true, |
| 55 | + Hwnd = _window.WindowHandle, |
| 56 | + MeasureFps = true, |
| 57 | + MultiThreaded = false, |
| 58 | + VSync = false |
| 59 | + }); |
| 60 | + |
| 61 | + _frameTimer = new FrameTimer(_device, 60); |
| 62 | + |
| 63 | + _window.OnWindowBoundsChanged += _window_OnWindowBoundsChanged; |
| 64 | + |
| 65 | + _initializeGraphicObjects = true; |
| 66 | + |
| 67 | + _frameTimer.OnFrameStarting += _frameTimer_OnFrameStarting; |
| 68 | + _frameTimer.OnFrame += _frameTimer_OnFrame; |
| 69 | + |
| 70 | + _frameTimer.Start(); |
| 71 | + } |
| 72 | + |
| 73 | + private void DestroyInstance() |
| 74 | + { |
| 75 | + _frameTimer.Stop(); |
| 76 | + |
| 77 | + _frameTimer.Dispose(); |
| 78 | + _device.Dispose(); |
| 79 | + _window.Dispose(); |
| 80 | + |
| 81 | + _window = null; |
| 82 | + _device = null; |
| 83 | + _frameTimer = null; |
| 84 | + } |
| 85 | + |
| 86 | + private void _window_OnWindowBoundsChanged(int x, int y, int width, int height) |
| 87 | + { |
| 88 | + if (_device == null) return; |
| 89 | + if (!_device.IsInitialized) return; |
| 90 | + |
| 91 | + _device.Resize(width, height); |
| 92 | + } |
| 93 | + |
| 94 | + private void _frameTimer_OnFrameStarting(FrameTimer timer, D2DDevice device) |
| 95 | + { |
| 96 | + if (!_initializeGraphicObjects) return; |
| 97 | + |
| 98 | + if (!device.IsInitialized) return; |
| 99 | + if (device.IsDrawing) return; |
| 100 | + |
| 101 | + _backgroundColor = new D2DColor(0x33, 0x33, 0x33, 0xEF); |
| 102 | + |
| 103 | + _font = _device.CreateFont(new FontOptions() |
| 104 | + { |
| 105 | + Bold = false, |
| 106 | + FontFamilyName = "Arial", |
| 107 | + FontSize = 16, |
| 108 | + Italic = false, |
| 109 | + WordWrapping = true |
| 110 | + }); |
| 111 | + |
| 112 | + _blackBrush = device.CreateSolidColorBrush(0x0, 0x0, 0x0, 0xFF); |
| 113 | + |
| 114 | + _redBrush = device.CreateSolidColorBrush(0xFF, 0x0, 0x0, 0xFF); |
| 115 | + _greenBrush = device.CreateSolidColorBrush(0x0, 0xFF, 0x0, 0xFF); |
| 116 | + _blueBrush = device.CreateSolidColorBrush(0x0, 0x0, 0xFF, 0xFF); |
| 117 | + |
| 118 | + _image = device.LoadImage(Properties.Resources.placeholder_image_bytes); |
| 119 | + } |
| 120 | + |
| 121 | + private void _frameTimer_OnFrame(FrameTimer timer, D2DDevice device) |
| 122 | + { |
| 123 | + if (!device.IsDrawing) |
| 124 | + { |
| 125 | + _initializeGraphicObjects = true; |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // clear the scene / fill it with our background |
| 130 | + |
| 131 | + device.ClearScene(_backgroundColor); |
| 132 | + |
| 133 | + // text |
| 134 | + |
| 135 | + device.DrawTextWithBackground("FPS: " + device.FramesPerSecond, 10, 10, _font, _redBrush, _blackBrush); |
| 136 | + |
| 137 | + // primitives |
| 138 | + |
| 139 | + device.DrawCircle(100, 100, 50, 2.0f, _redBrush); |
| 140 | + device.DrawDashedCircle(250, 100, 50, 2.0f, _greenBrush); |
| 141 | + |
| 142 | + device.DrawRectangle(Rectangle.Create(350, 50, 100, 100), 2.0f, _blueBrush); |
| 143 | + device.DrawRoundedRectangle(RoundedRectangle.Create(500, 50, 100, 100, 6.0f), 2.0f, _redBrush); |
| 144 | + |
| 145 | + device.DrawTriangle(650, 150, 750, 150, 700, 50, _greenBrush, 2.0f); |
| 146 | + |
| 147 | + // lines |
| 148 | + |
| 149 | + device.DrawLine(50, 175, 750, 175, 2.0f, _blueBrush); |
| 150 | + device.DrawDashedLine(50, 200, 750, 200, 2.0f, _redBrush); |
| 151 | + |
| 152 | + // outlines & filled |
| 153 | + |
| 154 | + device.OutlineCircle(100, 275, 50, 4.0f, _redBrush, _blackBrush); |
| 155 | + device.FillCircle(250, 275, 50, _greenBrush); |
| 156 | + |
| 157 | + device.OutlineRectangle(Rectangle.Create(350, 225, 100, 100), 4.0f, _blueBrush, _blackBrush); |
| 158 | + device.FillRoundedRectangle(RoundedRectangle.Create(500, 225, 100, 100, 6.0f), _redBrush); |
| 159 | + |
| 160 | + device.FillTriangle(650, 325, 750, 325, 700, 225, _greenBrush); |
| 161 | + |
| 162 | + // images |
| 163 | + |
| 164 | + device.DrawImage(_image, 310, 375); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments