-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSCWindow.cs
More file actions
229 lines (183 loc) · 6.62 KB
/
SCWindow.cs
File metadata and controls
229 lines (183 loc) · 6.62 KB
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
using Silk.NET.Input;
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
using System.Numerics;
namespace OpenSC
{
using OpenSC.Game;
using OpenSC.Audio;
using OpenSC.Game.Proxy;
public class SCWindow : IDisposable
{
private GL _gl;
private IWindow _window;
private IInputContext _inputContext;
private IKeyboard _primaryKeyboard;
private IMouse _primaryMouse;
private IGamepad _primaryGamepad;
private Cursor _cursor;
private Camera _camera;
private Scene _scene;
private EngineContext _context;
private readonly StateManager _stateManager;
private readonly AudioManager _audioManager;
private readonly LuaProxy _proxy;
private string _execArg = String.Empty;
public SCWindow()
{
var size = new Vector2(1920, 1080);
SCEnvironment.ViewportSize = size;
var options = WindowOptions.Default;
options.VSync = true;
options.Size = new Silk.NET.Maths.Vector2D<int>((int)size.X, (int)size.Y);
options.Title = "OpenSC";
_window = Window.Create(options);
_window.Load += OnLoad;
_window.Update += OnUpdate;
_window.Render += OnRender;
_window.Closing += OnClosing;
_window.FramebufferResize += OnFramebufferResize;
_stateManager = new StateManager();
_audioManager = new AudioManager();
_proxy = new LuaProxy(_stateManager);
}
public void Run(string execArg)
{
_execArg = execArg;
_window.Run();
}
private void OnMouseMove(IMouse mouse, Vector2 position)
{
_camera.Rotate(position);
_cursor.MoveTo(position); // Free-mode only
}
private void OnClosing()
{
}
private void OnFramebufferResize(Silk.NET.Maths.Vector2D<int> newSize)
{
_gl.Viewport(newSize);
var viewport = new Vector2(_window.FramebufferSize.X, _window.FramebufferSize.Y);
SCEnvironment.ViewportSize = viewport;
_context.Viewport = viewport;
_camera.SetViewport(viewport);
_cursor.SetViewport(viewport);
}
private void KeyDown(IKeyboard arg1, Key arg2, int arg3)
{
if (arg2 == Key.Escape)
{
_window.Close();
}
if (arg2 == Key.Space)
{
_scene.Skip();
// _proxy.Exec("scChoiceGo(\"phone\")");
// _proxy.Exec("is_door_locked = 0");
// _proxy.ExecCoroutine("enterGarden");
// _proxy.ExecCoroutine("enterHouse");
// _proxy.Exec("scSwitch(SC_ROOM, \"house-lobby\")");
}
}
public void Dispose()
{
_window.Dispose();
}
private void InitializeInput()
{
_inputContext = _window.CreateInput();
_inputContext.ConnectionChanged += ConnectionChanged;
_primaryKeyboard = _inputContext.Keyboards.FirstOrDefault();
if (_primaryKeyboard != null)
{
_primaryKeyboard.KeyDown += KeyDown;
}
_primaryMouse = _inputContext.Mice.FirstOrDefault();
if (_primaryMouse != null)
{
_primaryMouse.Cursor.CursorMode = CursorMode.Raw;
_primaryMouse.MouseMove += OnMouseMove;
// FUCK THIS:
// _primaryMouse.Click += OnMouseClick;
}
_primaryGamepad = _inputContext.Gamepads.FirstOrDefault();
if (_primaryGamepad != null)
{
_primaryGamepad.ThumbstickMoved += ThumbstickMoved;
}
}
private void ConnectionChanged(IInputDevice device, bool connected)
{
if (connected && device is IGamepad gamepad)
{
_primaryGamepad = gamepad;
_primaryGamepad.ThumbstickMoved += ThumbstickMoved;
}
}
private void ThumbstickMoved(IGamepad gamepad, Thumbstick stick)
{
var pos = new Vector2(stick.X, stick.Y);
}
private void OnLoad()
{
_gl = GL.GetApi(_window);
_gl.Enable(EnableCap.DepthTest);
_gl.DepthFunc(DepthFunction.Lequal);
InitializeInput();
var viewport = new Vector2(_window.FramebufferSize.X, _window.FramebufferSize.Y);
_cursor = new Cursor(_gl, viewport);
_camera = new Camera(viewport);
_context = new EngineContext()
{
GL = _gl,
Cursor = _cursor,
Camera = _camera,
Proxy = _proxy,
AudioManager = _audioManager,
StateManager = _stateManager,
Viewport = viewport
};
_scene = new Scene(_context);
if (!String.IsNullOrEmpty(_execArg))
{
_proxy.ExecCoroutine(_execArg);
}
}
private void OnRender(double deltaTime)
{
_gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
float delta = (float) deltaTime;
_scene?.Draw(delta);
_cursor.Draw(delta);
}
private void OnUpdate(double deltaTime)
{
var delta = (float)deltaTime;
_scene?.Update(delta);
// We poll the mouse click state in the update loop because the event system
// decided to take a coffee break every time we needed a click.
MouseClickTrigger();
}
private bool _wasLeftMousePressed = false;
private bool _wasRightMousePressed = false;
public void MouseClickTrigger()
{
if (_primaryMouse != null)
{
bool isLeftMousePressed = _primaryMouse.IsButtonPressed(MouseButton.Left);
bool isRightMousePressed = _primaryMouse.IsButtonPressed(MouseButton.Right);
if (!_wasLeftMousePressed && isLeftMousePressed)
{
_scene.Click(MouseButton.Left);
}
if (!_wasRightMousePressed && isRightMousePressed)
{
_scene.Click(MouseButton.Right);
}
_wasLeftMousePressed = isLeftMousePressed;
_wasRightMousePressed = isRightMousePressed;
}
}
}
}