-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
301 lines (261 loc) · 8.07 KB
/
Game.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
global using System;
global using System.Collections.Concurrent;
global using System.Collections.Generic;
global using System.Collections.ObjectModel;
global using System.Diagnostics;
global using System.Globalization;
global using System.IO;
global using System.IO.Compression;
global using System.Linq;
global using System.Numerics;
global using System.Reflection;
global using System.Runtime.CompilerServices;
global using System.Runtime.InteropServices;
global using System.Runtime.Serialization;
global using System.Text;
global using System.Threading;
global using System.Windows.Forms;
global using Fasterflect;
global using Newtonsoft.Json;
global using Newtonsoft.Json.Serialization;
global using SFML.Audio;
global using SFML.Graphics;
global using SFML.Graphics.Glsl;
global using SFML.System;
global using SFML.Window;
global using SMPL.GUI;
global using SMPL.Prefabs;
global using SMPL.Tools;
global using Color = SFML.Graphics.Color;
global using Console = SMPL.Tools.Console;
global using Time = SMPL.Tools.Time;
using static SMPL.Thing;
namespace SMPL
{
public static class Game
{
public enum WindowState { Windowed, Borderless, Fullscreen }
public static Settings Settings => settings;
public static RenderWindow Window => window;
public static Vector2 MousePosition
{
get { var p = Mouse.GetPosition(Window); return new(p.X, p.Y); }
set { Mouse.SetPosition(new((int)value.X, (int)value.Y), Window); }
}
public static void Start(string sceneName)
{
if(string.IsNullOrWhiteSpace(sceneName))
return;
Scene.CurrentScene = new(sceneName);
Run();
}
public static void Load(string scenePath)
{
Run(scenePath);
}
public static void UpdateEngine(RenderTarget mainCamera)
{
Time.Update();
Pseudo3DInstance.Update();
Draw(mainCamera);
Scene.UpdateCurrentScene();
AudioInstance.Update();
}
public static void FinishRendering(RenderWindow window, RenderTarget mainCamera)
{
CameraInstance.DrawMainCameraToWindow(window, window == mainCamera);
}
public static void Stop()
{
Settings.Save();
Event.GameStop();
Window.Close();
}
public static void OpenWebPage(string url)
{
try { Process.Start(url); }
catch
{
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
Process.Start("xdg-open", url);
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Process.Start("open", url);
else
Console.LogError(1, $"Could not load URL '{url}'.");
}
}
public static void CenterWindow()
{
var sz = Settings.ScreenResolution;
Window.Size = new((uint)(sz.X / 1.5f), (uint)(sz.Y / 1.5f));
Window.Position = new Vector2i((int)(sz.X / 2f), (int)(sz.Y / 2f)) - new Vector2i((int)(Window.Size.X / 2), (int)(Window.Size.Y / 2));
}
#region Backend
private static RenderWindow window;
internal static readonly Texture defaultTexture;
internal static Settings settings = new();
internal static Styles currWindowStyle;
static Game()
{
const uint SIZE = 8;
var img = new Image(SIZE, SIZE);
for(uint y = 0; y < SIZE; y++)
for(uint x = 0; x < SIZE; x++)
{
var col = x % 2 == 1 ?
(y % 2 == 0 ? Color.Magenta : Color.Black) :
(y % 2 == 0 ? Color.Black : Color.Magenta);
img.SetPixel(x, y, col);
}
defaultTexture = new Texture(img);
img.Dispose();
}
private static void Main() { }
private static void Run(string loadScene = null)
{
if(Window != null)
return;
Settings.Load();
InitWindow(Settings.WindowState, Settings.Resolution);
if(string.IsNullOrWhiteSpace(loadScene) == false)
Scene.Load(loadScene);
if(Thing.Exists(Scene.MAIN_CAMERA_UID) == false)
Thing.CreateCamera(Scene.MAIN_CAMERA_UID, Settings.ScreenResolution);
while(Window.IsOpen)
{
Window.DispatchEvents();
Window.Clear();
UpdateEngine(Scene.MainCamera.RenderTexture);
FinishRendering(Window, Scene.MainCamera.RenderTexture);
}
}
private static void OnClose(object sender, EventArgs e) => Stop();
private static void Draw(RenderTarget mainCamera)
{
var visuals = VisualInstance.visuals;
var cameras = CameraInstance.cameras;
var shadowMappedCams = new List<CameraInstance>();
var minOrderZ = visuals.FirstOrDefault().Key;
var maxOrderZ = visuals.LastOrDefault().Key;
ClearAllCamerasAndGenerateHitboxMap();
UpdateShadowMapAndDrawUserCameras();
DisplayUserCameras();
DrawMainCamera();
// displaying the main camera/drawing it to the window is a call from outside
// since it might be a window, like in the SMPLSceneEditor
void ClearAllCamerasAndGenerateHitboxMap()
{
for(int i = 0; i < cameras.Count; i++)
{
var cam = cameras[i];
cam.RenderTexture.Clear(cam.BackgroundColor);
cam.shadowMapVerts.Clear();
LightInstance.UpdateGlobalArrays(mainCamera);
LightInstance.GenerateHitboxMap();
}
}
void UpdateShadowMapAndDrawUserCameras()
{
foreach(var kvp in visuals)
for(int i = 0; i < kvp.Value.Count; i++)
{
var visual = kvp.Value[i];
var camUIDs = GetUIDsByTag(visual.CameraTag);
if(visual.Effect == Effect.Lights)
LightInstance.UpdateUniforms(visual);
for(int j = 0; j < camUIDs.Count; j++)
{
var cam = ThingInstance.Get_<CameraInstance>(camUIDs[j]);
if(visual.Effect == Effect.Lights)
{
//LightInstance.UpdateGlobalArrays(cam.RenderTexture);
LightInstance.CalculateShadowVerts(cam.shadowMapVerts);
LightInstance.UpdateUniforms(visual);
}
TryDrawShadowMap(kvp.Key, cam, true, false, false); // try before draw
if(cam != null && cam.IsDisabled == false)
visual.Draw(cam.RenderTexture);
TryDrawShadowMap(kvp.Key, cam, false, true, true); // try after draw
}
}
}
void DisplayUserCameras()
{
for(int i = 0; i < cameras.Count; i++)
{
var cam = cameras[i];
if(cam.UID != Scene.MAIN_CAMERA_UID)
cam.RenderTexture.Display();
}
}
void DrawMainCamera()
{
LightInstance.UpdateGlobalArrays(Scene.MainCamera.RenderTexture);
LightInstance.CalculateShadowVerts(Scene.MainCamera.shadowMapVerts);
foreach(var kvp in visuals)
{
TryDrawShadowMap(kvp.Key, Scene.MainCamera, true, false, false); // try before draw
for(int i = 0; i < kvp.Value.Count; i++)
kvp.Value[i].Draw(mainCamera);
TryDrawShadowMap(kvp.Key, Scene.MainCamera, false, true, true); // try after draw
}
}
void TryDrawShadowMap(int currentOrderZ, CameraInstance cam, bool beforeTest, bool inbetweenTest, bool afterTest)
{
if(shadowMappedCams.Contains(cam))
return;
if((beforeTest && ShadowOrderZ < minOrderZ) ||
(inbetweenTest && currentOrderZ == ShadowOrderZ) ||
(afterTest && ShadowOrderZ > maxOrderZ))
{
shadowMappedCams.Add(cam);
cam.DrawShadowMap();
}
}
}
internal static void InitWindow(WindowState windowState, Vector2 resolution)
{
if(Window != null)
{
Window.Closed -= OnClose;
Window.Dispose();
}
currWindowStyle = windowState.ToWindowStyles();
window = new(new((uint)resolution.X, (uint)resolution.Y), "SMPL Game", currWindowStyle) { Position = new() };
Window.Clear();
Window.Display();
Window.Closed += OnClose;
Window.SetFramerateLimit(120);
Window.SetVerticalSyncEnabled(Settings.IsVSyncEnabled);
var view = Window.GetView();
view.Center = new();
Window.SetView(view);
if(windowState == WindowState.Windowed)
CenterWindow();
}
internal static WindowState ToWindowStates(this Styles style)
{
return style switch
{
Styles.Fullscreen => WindowState.Fullscreen,
Styles.None => WindowState.Borderless,
_ => WindowState.Windowed,
};
}
internal static Styles ToWindowStyles(this WindowState windowStates)
{
return windowStates switch
{
WindowState.Borderless => Styles.None,
WindowState.Fullscreen => Styles.Fullscreen,
_ => Styles.Default,
};
}
#endregion
}
}