/
TSOGame.cs
327 lines (282 loc) · 13 KB
/
TSOGame.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
*/
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Threading;
using FSO.Common.Rendering.Framework;
using FSO.LotView;
using FSO.HIT;
using FSO.Client.Network;
using FSO.Client.UI;
using FSO.Client.GameContent;
using Ninject;
using FSO.Client.Regulators;
using FSO.Server.Protocol.Voltron.DataService;
using FSO.Common.DataService;
using FSO.Server.DataService.Providers.Client;
using FSO.Common.Domain;
using FSO.Common.Utils;
using FSO.Common;
using Microsoft.Xna.Framework.Audio;
using FSO.HIT.Model;
using FSO.UI.Model;
using FSO.Files.RC;
using FSO.Files.Formats.IFF;
using FSO.SimAntics;
//using System.Windows.Forms;
namespace FSO.Client
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class TSOGame : FSO.Common.Rendering.Framework.Game
{
public UILayer uiLayer;
public _3DLayer SceneMgr;
public TSOGame() : base()
{
GameFacade.Game = this;
//if (GameFacade.DirectX) TimedReferenceController.SetMode(CacheType.PERMANENT);
Content.RootDirectory = FSOEnvironment.GFXContentDir;
Graphics.SynchronizeWithVerticalRetrace = true;
FSOEnvironment.DPIScaleFactor = GlobalSettings.Default.DPIScaleFactor;
if (!FSOEnvironment.SoftwareDepth)
{
Graphics.PreferredBackBufferWidth = (int)(GlobalSettings.Default.GraphicsWidth * FSOEnvironment.DPIScaleFactor);
Graphics.PreferredBackBufferHeight = (int)(GlobalSettings.Default.GraphicsHeight * FSOEnvironment.DPIScaleFactor);
//Graphics.PreferMultiSampling = true;
Graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
TargetElapsedTime = new TimeSpan(10000000 / GlobalSettings.Default.TargetRefreshRate);
FSOEnvironment.RefreshRate = GlobalSettings.Default.TargetRefreshRate;
Graphics.HardwareModeSwitch = false;
Graphics.ApplyChanges();
}
this.Window.AllowUserResizing = true;
this.Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
try
{
GameThread.Game = Thread.CurrentThread;
Thread.CurrentThread.Name = "Game";
} catch
{
//fails on android
}
}
bool newChange = false;
void Window_ClientSizeChanged(object sender, EventArgs e)
{
if (newChange || !GlobalSettings.Default.Windowed) return;
if (Window.ClientBounds.Width == 0 || Window.ClientBounds.Height == 0) return;
newChange = true;
var width = Math.Max(1, Window.ClientBounds.Width);
var height = Math.Max(1, Window.ClientBounds.Height);
Graphics.PreferredBackBufferWidth = width;
Graphics.PreferredBackBufferHeight = height;
Graphics.ApplyChanges();
GlobalSettings.Default.GraphicsWidth = width;
GlobalSettings.Default.GraphicsHeight = height;
newChange = false;
if (uiLayer?.CurrentUIScreen == null) return;
uiLayer.SpriteBatch.ResizeBuffer(GlobalSettings.Default.GraphicsWidth, GlobalSettings.Default.GraphicsHeight);
GlobalSettings.Default.GraphicsWidth = (int)(width / FSOEnvironment.DPIScaleFactor);
GlobalSettings.Default.GraphicsHeight = (int)(height / FSOEnvironment.DPIScaleFactor);
uiLayer.CurrentUIScreen.GameResized();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
System.Net.ServicePointManager.DefaultConnectionLimit = 32;
var kernel = new StandardKernel(
new RegulatorsModule(),
new NetworkModule(),
new CacheModule()
);
FSOFacade.Kernel = kernel;
var settings = GlobalSettings.Default;
if (FSOEnvironment.SoftwareDepth)
{
settings.GraphicsWidth = (int)(GraphicsDevice.Viewport.Width / FSOEnvironment.DPIScaleFactor);
settings.GraphicsHeight = (int)(GraphicsDevice.Viewport.Height / FSOEnvironment.DPIScaleFactor);
}
//manage settings
if (settings.LightingMode == -1)
{
if (settings.Lighting)
{
if (settings.Shadows3D)
settings.LightingMode = 2;
else
settings.LightingMode = 1;
}
else
settings.LightingMode = 0;
settings.Save();
}
FeatureLevelTest.UpdateFeatureLevel(GraphicsDevice);
if (!FSOEnvironment.MSAASupport)
settings.AntiAlias = false;
LotView.WorldConfig.Current = new LotView.WorldConfig()
{
LightingMode = settings.LightingMode,
SmoothZoom = settings.SmoothZoom,
SurroundingLots = settings.SurroundingLotMode,
AA = settings.AntiAlias,
};
if (!FSOEnvironment.TexCompressSupport) settings.TexCompression = 0;
else if ((settings.TexCompression & 2) == 0)
{
settings.TexCompression = 1;
}
FSOEnvironment.TexCompress = (!IffFile.RETAIN_CHUNK_DATA) && (settings.TexCompression & 1) > 0;
//end settings management
OperatingSystem os = Environment.OSVersion;
PlatformID pid = os.Platform;
GameFacade.Linux = (pid == PlatformID.MacOSX || pid == PlatformID.Unix);
FSO.Content.Content.TS1Hybrid = GlobalSettings.Default.TS1HybridEnable;
FSO.Content.Content.TS1HybridBasePath = GlobalSettings.Default.TS1HybridPath;
FSO.Content.Content.InitBasic(GlobalSettings.Default.StartupPath, GraphicsDevice);
//VMContext.InitVMConfig();
base.Initialize();
GameFacade.GameThread = Thread.CurrentThread;
SceneMgr = new _3DLayer();
SceneMgr.Initialize(GraphicsDevice);
FSOFacade.Controller = kernel.Get<GameController>();
FSOFacade.Hints = new UI.Hints.UIHintManager();
GameFacade.Screens = uiLayer;
GameFacade.Scenes = SceneMgr;
GameFacade.GraphicsDevice = GraphicsDevice;
GameFacade.GraphicsDeviceManager = Graphics;
GameFacade.Emojis = new Common.Rendering.Emoji.EmojiProvider(GraphicsDevice);
CurLoader.BmpLoaderFunc = Files.ImageLoader.FromStream;
GameFacade.Cursor = new CursorManager(GraphicsDevice);
if (!GameFacade.Linux) GameFacade.Cursor.Init(FSO.Content.Content.Get().GetPath(""), false);
/** Init any computed values **/
GameFacade.Init();
//init audio now
HITVM.Init();
var hit = HITVM.Get();
hit.SetMasterVolume(HITVolumeGroup.FX, GlobalSettings.Default.FXVolume / 10f);
hit.SetMasterVolume(HITVolumeGroup.MUSIC, GlobalSettings.Default.MusicVolume / 10f);
hit.SetMasterVolume(HITVolumeGroup.VOX, GlobalSettings.Default.VoxVolume / 10f);
hit.SetMasterVolume(HITVolumeGroup.AMBIENCE, GlobalSettings.Default.AmbienceVolume / 10f);
GameFacade.Strings = new ContentStrings();
FSOFacade.Controller.StartLoading();
GraphicsDevice.RasterizerState = new RasterizerState() { CullMode = CullMode.None };
try {
var audioTest = new SoundEffect(new byte[2], 44100, AudioChannels.Mono); //initialises XAudio.
audioTest.CreateInstance().Play();
} catch (Exception e)
{
FSOProgram.ShowDialog("Failed to initialize audio: \r\n\r\n" + e.StackTrace);
}
this.IsMouseVisible = true;
this.IsFixedTimeStep = true;
WorldContent.Init(this.Services, Content.RootDirectory);
DGRP3DMesh.InitRCWorkers();
if (!(FSOEnvironment.SoftwareKeyboard && FSOEnvironment.SoftwareDepth)) AddTextInput();
base.Screen.Layers.Add(SceneMgr);
base.Screen.Layers.Add(uiLayer);
GameFacade.LastUpdateState = base.Screen.State;
//Bind ninject objects
kernel.Bind<FSO.Content.Content>().ToConstant(FSO.Content.Content.Get());
kernel.Load(new ClientDomainModule());
//Have to be eager with this, it sets a singleton instance on itself to avoid packets having
//to be created using Ninject for performance reasons
kernel.Get<cTSOSerializer>();
var ds = kernel.Get<DataService>();
ds.AddProvider(new ClientAvatarProvider());
this.Window.Title = "FreeSO";
DiscordRpcEngine.Init();
if (!GlobalSettings.Default.Windowed && !GameFacade.GraphicsDeviceManager.IsFullScreen)
{
GameFacade.GraphicsDeviceManager.ToggleFullScreen();
}
}
/// <summary>
/// Run this instance with GameRunBehavior forced as Synchronous.
/// </summary>
public new void Run()
{
Run(GameRunBehavior.Synchronous);
}
/// <summary>
/// Only used on desktop targets. Use extensive reflection to AVOID linking on iOS!
/// </summary>
void AddTextInput()
{
this.Window.GetType().GetEvent("TextInput")?.AddEventHandler(this.Window, (EventHandler<TextInputEventArgs>)GameScreen.TextInput);
}
void RegainFocus(object sender, EventArgs e)
{
GameFacade.Focus = true;
}
void LostFocus(object sender, EventArgs e)
{
GameFacade.Focus = false;
}
protected override void OnExiting(object sender, EventArgs args)
{
base.OnExiting(sender, args);
GameThread.Killed = true;
GameThread.OnKilled.Set();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
Effect vitaboyEffect = null;
try
{
GameFacade.MainFont = new FSO.Client.UI.Framework.Font();
GameFacade.MainFont.AddSize(10, Content.Load<SpriteFont>("Fonts/FreeSO_10px"));
GameFacade.MainFont.AddSize(12, Content.Load<SpriteFont>("Fonts/FreeSO_12px"));
GameFacade.MainFont.AddSize(14, Content.Load<SpriteFont>("Fonts/FreeSO_14px"));
GameFacade.MainFont.AddSize(16, Content.Load<SpriteFont>("Fonts/FreeSO_16px"));
GameFacade.EdithFont = new FSO.Client.UI.Framework.Font();
GameFacade.EdithFont.AddSize(12, Content.Load<SpriteFont>("Fonts/Trebuchet_12px"));
GameFacade.EdithFont.AddSize(14, Content.Load<SpriteFont>("Fonts/Trebuchet_14px"));
vitaboyEffect = Content.Load<Effect>((FSOEnvironment.GLVer == 2)?"Effects/VitaboyiOS":"Effects/Vitaboy");
uiLayer = new UILayer(this, Content.Load<SpriteFont>("Fonts/FreeSO_12px"), Content.Load<SpriteFont>("Fonts/FreeSO_16px"));
}
catch (Exception e)
{
FSOProgram.ShowDialog("Content could not be loaded. Make sure that the FreeSO content has been compiled! (ContentSrc/TSOClientContent.mgcb) \r\n\r\n"+e.ToString());
Exit();
Environment.Exit(0);
}
FSO.Vitaboy.Avatar.setVitaboyEffect(vitaboyEffect);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
GameThread.UpdateExecuting = true;
DiscordRpcEngine.Update();
if (HITVM.Get() != null) HITVM.Get().Tick();
base.Update(gameTime);
GameThread.UpdateExecuting = false;
}
}
}