-
Notifications
You must be signed in to change notification settings - Fork 58
/
SilkDoom.cs
218 lines (180 loc) · 5.7 KB
/
SilkDoom.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
using System;
using System.Runtime.ExceptionServices;
using Silk.NET.Input;
using Silk.NET.Input.Glfw;
using Silk.NET.Maths;
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
using Silk.NET.Windowing.Glfw;
using DrippyAL;
namespace ManagedDoom.Silk
{
public partial class SilkDoom : IDisposable
{
private CommandLineArgs args;
private Config config;
private GameContent content;
private IWindow window;
private GL gl;
private SilkVideo video;
private AudioDevice audioDevice;
private SilkSound sound;
private SilkMusic music;
private SilkUserInput userInput;
private Doom doom;
private int fpsScale;
private int frameCount;
private Exception exception;
public SilkDoom(CommandLineArgs args)
{
try
{
this.args = args;
GlfwWindowing.RegisterPlatform();
GlfwInput.RegisterPlatform();
config = SilkConfigUtilities.GetConfig();
content = new GameContent(args);
config.video_screenwidth = Math.Clamp(config.video_screenwidth, 320, 3200);
config.video_screenheight = Math.Clamp(config.video_screenheight, 200, 2000);
var windowOptions = WindowOptions.Default;
windowOptions.Size = new Vector2D<int>(config.video_screenwidth, config.video_screenheight);
windowOptions.Title = ApplicationInfo.Title;
windowOptions.VSync = false;
windowOptions.WindowState = config.video_fullscreen ? WindowState.Fullscreen : WindowState.Normal;
window = Window.Create(windowOptions);
window.Load += OnLoad;
window.Update += OnUpdate;
window.Render += OnRender;
window.Resize += OnResize;
window.Closing += OnClose;
}
catch (Exception e)
{
Dispose();
ExceptionDispatchInfo.Throw(e);
}
}
private void Quit()
{
if (exception != null)
{
ExceptionDispatchInfo.Throw(exception);
}
}
private void OnLoad()
{
gl = window.CreateOpenGL();
gl.ClearColor(0.15F, 0.15F, 0.15F, 1F);
gl.Clear(ClearBufferMask.ColorBufferBit);
window.SwapBuffers();
video = new SilkVideo(config, content, window, gl);
if (!args.nosound.Present && !(args.nosfx.Present && args.nomusic.Present))
{
audioDevice = new AudioDevice();
if (!args.nosfx.Present)
{
sound = new SilkSound(config, content, audioDevice);
}
if (!args.nomusic.Present)
{
music = SilkConfigUtilities.GetMusicInstance(config, content, audioDevice);
}
}
userInput = new SilkUserInput(config, window, this, !args.nomouse.Present);
doom = new Doom(args, config, content, video, sound, music, userInput);
fpsScale = args.timedemo.Present ? 1 : config.video_fpsscale;
frameCount = -1;
}
private void OnUpdate(double obj)
{
try
{
frameCount++;
if (frameCount % fpsScale == 0)
{
if (doom.Update() == UpdateResult.Completed)
{
window.Close();
}
}
}
catch (Exception e)
{
exception = e;
}
if (exception != null)
{
window.Close();
}
}
private void OnRender(double obj)
{
try
{
var frameFrac = Fixed.FromInt(frameCount % fpsScale + 1) / fpsScale;
video.Render(doom, frameFrac);
}
catch (Exception e)
{
exception = e;
}
}
private void OnResize(Vector2D<int> obj)
{
video.Resize(obj.X, obj.Y);
}
private void OnClose()
{
if (userInput != null)
{
userInput.Dispose();
userInput = null;
}
if (music != null)
{
music.Dispose();
music = null;
}
if (sound != null)
{
sound.Dispose();
sound = null;
}
if (audioDevice != null)
{
audioDevice.Dispose();
audioDevice = null;
}
if (video != null)
{
video.Dispose();
video = null;
}
if (gl != null)
{
gl.Dispose();
gl = null;
}
config.Save(ConfigUtilities.GetConfigPath());
}
public void KeyDown(Key key)
{
doom.PostEvent(new DoomEvent(EventType.KeyDown, SilkUserInput.SilkToDoom(key)));
}
public void KeyUp(Key key)
{
doom.PostEvent(new DoomEvent(EventType.KeyUp, SilkUserInput.SilkToDoom(key)));
}
public void Dispose()
{
if (window != null)
{
window.Close();
window.Dispose();
window = null;
}
}
public string QuitMessage => doom.QuitMessage;
public Exception Exception => exception;
}
}