-
Notifications
You must be signed in to change notification settings - Fork 2
/
GraphicsDeviceControlPenumbra.cs
164 lines (150 loc) · 5.19 KB
/
GraphicsDeviceControlPenumbra.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#region File Description
//-----------------------------------------------------------------------------
// GraphicsDeviceControl.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
#pragma warning disable CS1591
namespace Penumbra
{
public abstract class GraphicsDeviceControlPenumbra : System.Windows.Forms.Control
{
private bool designMode
{
get
{
System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
bool res = process.ProcessName == "devenv";
process.Dispose();
return res;
}
}
private SwapChainRenderTarget _chain;
public GraphicsDeviceServicePenumbra _graphicsDeviceService;
public GraphicsDevice GraphicsDevice => _graphicsDeviceService.GraphicsDevice;
public ServiceContainerPenumbra Services { get; } = new ServiceContainerPenumbra();
protected override void OnCreateControl()
{
if (!designMode)
{
_graphicsDeviceService = GraphicsDeviceServicePenumbra.AddRef(Handle, ClientSize.Width, ClientSize.Height);
_chain = new SwapChainRenderTarget(_graphicsDeviceService.GraphicsDevice, Handle, ClientSize.Width,
ClientSize.Height);
Services.AddService<IGraphicsDeviceService>(_graphicsDeviceService);
Initialize();
}
base.OnCreateControl();
}
protected override void Dispose(bool disposing)
{
if (_graphicsDeviceService != null)
{
_graphicsDeviceService.Release(disposing);
_graphicsDeviceService = null;
}
base.Dispose(disposing);
}
protected override void OnPaint(PaintEventArgs e)
{
var beginDrawError = BeginDraw();
if (string.IsNullOrEmpty(beginDrawError))
{
Draw();
EndDraw();
}
else
{
PaintUsingSystemDrawing(e.Graphics, beginDrawError);
}
}
private string BeginDraw()
{
if (_graphicsDeviceService == null)
{
return Text + "\n\n" + GetType();
}
var deviceResetError = HandleDeviceReset();
if (!string.IsNullOrEmpty(deviceResetError))
{
return deviceResetError;
}
var viewport = new Viewport
{
X = 0,
Y = 0,
Width = ClientSize.Width,
Height = ClientSize.Height,
MinDepth = 0,
MaxDepth = 1
};
GraphicsDevice.Viewport = viewport;
_graphicsDeviceService.GraphicsDevice.SetRenderTarget(_chain);
return null;
}
private void EndDraw()
{
try
{
_chain.Present();
}
catch
{
// ignored
}
}
private string HandleDeviceReset()
{
var deviceNeedsReset = false;
switch (GraphicsDevice.GraphicsDeviceStatus)
{
case GraphicsDeviceStatus.Lost:
return "Graphics device lost";
case GraphicsDeviceStatus.NotReset:
deviceNeedsReset = true;
break;
case GraphicsDeviceStatus.Normal:
break;
default:
var pp = GraphicsDevice.PresentationParameters;
deviceNeedsReset = (ClientSize.Width > pp.BackBufferWidth) ||
(ClientSize.Height > pp.BackBufferHeight);
break;
}
if (!deviceNeedsReset) return null;
try
{
_graphicsDeviceService.ResetDevice(ClientSize.Width,
ClientSize.Height);
}
catch (Exception e)
{
return "Graphics device reset failed\n\n" + e;
}
return null;
}
protected virtual void PaintUsingSystemDrawing(System.Drawing.Graphics graphics, string text)
{
graphics.Clear(System.Drawing.Color.DimGray);
using (Brush brush = new SolidBrush(System.Drawing.Color.CornflowerBlue))
{
using (var format = new StringFormat())
{
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
graphics.DrawString(text, Font, brush, ClientRectangle, format);
}
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
public virtual void Initialize() { }
public virtual void Draw() { }
}
}