-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cs
248 lines (215 loc) · 8.34 KB
/
Client.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using KirosEngine3.Debug;
using KirosEngine3.Input;
using KirosEngine3.Math.Matrix;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace KirosEngine3
{
/// <summary>
/// Abstract class to be inherited by individual works using the engine
/// </summary>
public abstract class Client : GameWindow
{
/// <summary>
/// Key for the graphics mode variable
/// </summary>
public const string GRAPHICSMODE_KEY = "GRAPHICS_MODE";
/// <summary>
/// Value constant for the OpenGL graphics mode
/// </summary>
public const string GRAPHICSMODE_GL_VAL = "OPENGL";
/// <summary>
/// Value constant for the DirectX graphics mode
/// </summary>
public const string GRAPHICSMODE_DX_VAL = "DIRECTX";
/// <summary>
/// Flag to allow or disallow the output of OpenGL debug messages that are only notifications.
/// </summary>
private static bool _showGLDebugNotify = false;
/// <summary>
/// Flag for if the debug notify messages should be output.
/// </summary>
public static bool ShowDebugNotify => _showGLDebugNotify;
/// <summary>
/// Basic constructor.
/// </summary>
/// <param name="width">The width of the client window.</param>
/// <param name="height">The height of the client window.</param>
/// <param name="title">The title for the window.</param>
public Client(int width, int height, string title) : base(GameWindowSettings.Default, new NativeWindowSettings() { ClientSize = (width, height), Title = title }) { }
/// <summary>
/// OpenGL debug delegate method
/// </summary>
/// <param name="source">Source for the debug message</param>
/// <param name="type">The type of the message</param>
/// <param name="id">The message id</param>
/// <param name="severity">The severity of the message</param>
/// <param name="length">The length of the message</param>
/// <param name="pMessage">The pointer to the message</param>
/// <param name="pUser">The pointer to user callback data</param>
protected static void OnDebugMessage(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr pMessage, IntPtr pUser)
{
string message = Marshal.PtrToStringUTF8(pMessage, length);
if (severity == DebugSeverity.DebugSeverityNotification)
{
if (_showGLDebugNotify)
Report("[{0} type={1} id={2}] {3}", severity, type, id, message);
}
else
Report("[{0} type={1} id={2}] {3}", severity, type, id, message);
}
/// <summary>
/// OpenGL debug delegate instance
/// </summary>
private static DebugProc DebugDelegate = new DebugProc(OnDebugMessage);
/// <summary>
/// Enable the output of notification level OpenGL debug messages.
/// </summary>
public static void EnableGLDebugNotify()
{
_showGLDebugNotify = true;
}
/// <summary>
/// Disable the output of notification level OpenGL debug messages.
/// </summary>
public static void DisableGLDebugNotify()
{
_showGLDebugNotify = false;
}
/// <summary>
/// Write a report message to all relevant receivers
/// </summary>
/// <param name="msg">The message to write.</param>
public static void Report(string msg)
{
Console.WriteLine(msg);
Logger.WriteToLog(msg);
DebugConsole.WriteLine(msg);
}
/// <summary>
/// Write a report message to all relevant receivers
/// </summary>
/// <param name="msg">The message format to write.</param>
/// <param name="arg0">The arguments for the format.</param>
public static void Report(string msg, object? arg0)
{
Console.WriteLine(msg, arg0);
Logger.WriteToLog(msg, arg0);
DebugConsole.WriteLine(msg, arg0);
}
/// <summary>
/// Write a report message to all relevant receivers
/// </summary>
/// <param name="msg">The message format to write.</param>
/// <param name="args">The arguments for the format.</param>
public static void Report(string msg, params object?[] args)
{
Console.WriteLine(msg, args);
Logger.WriteToLog(msg, args);
DebugConsole.WriteLine(msg, args);
}
/// <summary>
/// Load any data needed for the initial screen
/// </summary>
protected override void OnLoad()
{
base.OnLoad();
//OpenGL debug messaging
GL.DebugMessageCallback(DebugDelegate, 0);
CommandManager.RegisterCommand("get", new Action<string, string>(GetValue));
}
/// <summary>
/// Update data before the draw of the next frame
/// </summary>
/// <param name="args">Frame event arguments</param>
protected override void OnUpdateFrame(FrameEventArgs args)
{
base.OnUpdateFrame(args);
if (!IsFocused) { return; }
//check keyboard state and notify subscribers
KeyboardEventManager.Update(KeyboardState, args.Time);
MouseEventManager.Update(MouseState, args.Time);
DebugConsole.Instance?.Update();
}
/// <summary>
/// Render the frame, making draw calls to any objects to be drawn
/// </summary>
/// <param name="args">Frame event arguments</param>
protected override void OnRenderFrame(FrameEventArgs args)
{
base.OnRenderFrame(args);
}
#region Cmd Methods
/// <summary>
/// Command method for getting the specified variable value.
/// </summary>
/// <param name="objName">The name of the object to get the property of.</param>
/// <param name="propertyName">The name of the variable/property to get.</param>
public void GetValue(string objName, string propertyName)
{
if (objName.Equals("client", StringComparison.InvariantCultureIgnoreCase))
{
object? prop = GetType().GetProperty(propertyName);
//object val = prop?.GetValue(this);
//todo: incomplete
Report("{0}: {1}", propertyName, prop);
}
else
{
//todo: pass to scene manager to check.
}
}
#endregion
/// <summary>
/// Handle window resize events
/// </summary>
/// <param name="e">Resize event arguments</param>
protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, e.Width, e.Height);
}
/// <summary>
/// Cleanup and unload items before closing the program
/// </summary>
protected override void OnUnload()
{
base.OnUnload();
DebugConsole.Instance?.Dispose();
}
}
/// <summary>
/// Container struct for the view matrices.
/// </summary>
public struct ViewMatrixes
{
/// <summary>
/// The Model matrix, commonly the identity matrix and modified inside each renderable.
/// </summary>
public Matrix4 Model { get; set; }
/// <summary>
/// The View matrix, constructed by the active camera object.
/// </summary>
public Matrix4 View { get; set; }
/// <summary>
/// The Projection matrix, constructed by the active camera object.
/// </summary>
public Matrix4 Projection { get; set; }
/// <summary>
/// The Orthographic matrix, constructed by the active camera object and used mainly for UI/HUD.
/// </summary>
public Matrix4 Orthographic { get; set; }
/// <summary>
/// The Orthographic matrix for UI elements.
/// </summary>
public Matrix4 UIOrtho { get; set; }
}
}