-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathSimitoneGame.cs
More file actions
323 lines (274 loc) · 13 KB
/
Copy pathSimitoneGame.cs
File metadata and controls
323 lines (274 loc) · 13 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
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
/*
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.UI;
using FSO.Client.GameContent;
using FSO.Common.Utils;
using FSO.Common;
using Microsoft.Xna.Framework.Audio;
using FSO.HIT.Model;
using FSO.Client;
using FSO.Files;
using FSO.SimAntics;
using MSDFData;
using FSO.LotView.Model;
using Simitone.Client.UI.Panels;
using FSO.Files.Formats.IFF.Chunks;
namespace Simitone.Client
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class SimitoneGame : FSO.Common.Rendering.Framework.Game
{
//** if the window is resized below this value, UI elements can attempt to resize to available space below 0px -- resulting in immediate crash
// we should ensure a minimum width and height of at least the smallest supported resolution by the UI.
const int MIN_WIDTH = 800, MIN_HEIGHT = 600;
public UILayer uiLayer;
public _3DLayer SceneMgr;
private bool HasUpdated;
/// <summary>
/// The current language setting of this Simitone game
/// </summary>
public STRLangCode CurrentLanguage { get; private set; } = STRLangCode.EnglishUS;
public SimitoneGame() : base()
{
GameFacade.Game = this;
GameThread.Game = Thread.CurrentThread;
if (GameFacade.DirectX) TimedReferenceController.SetMode(CacheType.PERMANENT);
Content.RootDirectory = FSOEnvironment.GFXContentDir;
TargetElapsedTime = new TimeSpan(10000000 / GlobalSettings.Default.TargetRefreshRate);
FSOEnvironment.RefreshRate = GlobalSettings.Default.TargetRefreshRate;
FSOEnvironment.TexCompress = false;
UILotControl.ShowSimanticsExceptions = !FSOEnvironment.Args.Contains("nosimantics-exc");
if (!FSOEnvironment.SoftwareKeyboard)
{
Graphics.SynchronizeWithVerticalRetrace = true;
Graphics.PreferredBackBufferWidth = GlobalSettings.Default.GraphicsWidth;
Graphics.PreferredBackBufferHeight = GlobalSettings.Default.GraphicsHeight;
Graphics.HardwareModeSwitch = false;
Graphics.ApplyChanges();
}
this.Window.AllowUserResizing = true;
this.Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
Thread.CurrentThread.Name = "Game";
}
bool newChange = false;
void Window_ClientSizeChanged(object sender, EventArgs e)
{
if (newChange || !GlobalSettings.Default.Windowed || FSOEnvironment.SoftwareKeyboard) return;
if (Window.ClientBounds.Width == 0 || Window.ClientBounds.Height == 0) return;
newChange = true;
var width = Math.Max(MIN_WIDTH, Window.ClientBounds.Width);
var height = Math.Max(MIN_HEIGHT, 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);
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()
{
var settings = GlobalSettings.Default;
if (FSOEnvironment.DPIScaleFactor != 1 || FSOEnvironment.SoftwareDepth)
{
settings.GraphicsWidth = (int)(GraphicsDevice.Viewport.Width / FSOEnvironment.DPIScaleFactor);
settings.GraphicsHeight = (int)(GraphicsDevice.Viewport.Height / FSOEnvironment.DPIScaleFactor);
}
var initialMode = (GlobalGraphicsMode)settings.GlobalGraphicsMode;
if (FSOEnvironment.Enable3D)
{
if (initialMode == GlobalGraphicsMode.Full2D) initialMode = GlobalGraphicsMode.Full3D;
}
else
{
initialMode = GlobalGraphicsMode.Full2D;
}
GraphicsModeControl.ChangeMode(initialMode);
GraphicsModeControl.ModeChanged += SaveGraphicsModePreference;
FSO.LotView.WorldConfig.Current = new FSO.LotView.WorldConfig()
{
LightingMode = settings.LightingMode,
SmoothZoom = settings.SmoothZoom,
SurroundingLots = settings.SurroundingLotMode,
AA = settings.AntiAlias,
Directional = settings.DirectionalLight3D,
Complex = settings.ComplexShaders,
EnableTransitions = settings.EnableTransitions
};
OperatingSystem os = Environment.OSVersion;
PlatformID pid = os.Platform;
GameFacade.Linux = (pid == PlatformID.MacOSX || pid == PlatformID.Unix);
FSO.Content.Content.Target = FSO.Content.FSOEngineMode.TS1;
FSO.Content.Content.TS1HybridBasePath = GlobalSettings.Default.TS1HybridPath;
if (FSOEnvironment.Enable3D) FSO.Files.RC.DGRP3DMesh.InitRCWorkers();
//FSO.Content.Content.Init(GlobalSettings.Default.StartupPath, GraphicsDevice);
FSO.SimAntics.VMAvatar.MissingIconProvider = Simitone.Client.UI.Model.UIIconCache.GetObject;
base.Initialize();
GameFacade.GameThread = Thread.CurrentThread;
SceneMgr = new _3DLayer();
SceneMgr.Initialize(GraphicsDevice);
GameFacade.Scenes = SceneMgr;
GameFacade.GraphicsDevice = GraphicsDevice;
GameFacade.GraphicsDeviceManager = Graphics;
GameFacade.Cursor = new CursorManager(GraphicsDevice);
if (!GameFacade.Linux)
{
CurLoader.BmpLoaderFunc = ImageLoader.BaseFunction;
GameFacade.Cursor.Init(GlobalSettings.Default.TS1HybridPath, true);
}
/** 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);
ChangeLanguage((STRLangCode)settings.LanguageCode);
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)
{
//MessageBox.Show("Failed to initialize audio: \r\n\r\n" + e.StackTrace);
}
this.IsFixedTimeStep = true;
WorldContent.Init(this.Services, Content.RootDirectory);
base.Screen.Layers.Add(SceneMgr);
base.Screen.Layers.Add(uiLayer);
GameFacade.LastUpdateState = base.Screen.State;
if (!GlobalSettings.Default.Windowed && !GameFacade.GraphicsDeviceManager.IsFullScreen)
{
GameFacade.GraphicsDeviceManager.ToggleFullScreen();
}
}
internal void ChangeLanguage(STRLangCode NewLanguage)
{
//check if this language is already loaded.
if (GameFacade.Strings != null && CurrentLanguage == NewLanguage) return;
CurrentLanguage = NewLanguage;
ContentStrings.TS1 = true;
GameFacade.Strings = new ContentStrings(CurrentLanguage);
}
private void SaveGraphicsModePreference(GlobalGraphicsMode obj)
{
GlobalSettings.Default.GlobalGraphicsMode = (int)obj;
GlobalSettings.Default.Save();
}
/// <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;
}
/// <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(12, Content.Load<SpriteFont>("Fonts/Mobile_15px"));
//GameFacade.MainFont.AddSize(15, Content.Load<SpriteFont>("Fonts/Mobile_20px"));
//GameFacade.MainFont.AddSize(19, Content.Load<SpriteFont>("Fonts/Mobile_25px"));
//GameFacade.MainFont.AddSize(37, Content.Load<SpriteFont>("Fonts/Mobile_50px"));
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"));
GameFacade.VectorFont = new FSO.UI.Framework.MSDFFont(Content.Load<FieldFont>("../Fonts/mobile"));
GameFacade.EdithVectorFont = new FSO.UI.Framework.MSDFFont(Content.Load<FieldFont>("../Fonts/trebuchet"));
GameFacade.EdithVectorFont.VectorScale = 0.366f;
GameFacade.EdithVectorFont.Height = 15;
GameFacade.EdithVectorFont.YOff = 11;
FSO.UI.Framework.MSDFFont.MSDFEffect = Content.Load<Effect>("Effects/MSDFFont");
vitaboyEffect = Content.Load<Effect>("Effects/Vitaboy"+((FSOEnvironment.SoftwareDepth)?"iOS":""));
uiLayer = new UILayer(this);
}
catch (Exception e)
{
//MessageBox.Windows.Forms.MessageBox.Show("Content could not be loaded. Make sure that the FreeSO content has been compiled! (ContentSrc/TSOClientContent.mgcb)");
Console.WriteLine(e.ToString());
Exit();
}
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
}
protected override void OnExiting(object sender, ExitingEventArgs args)
{
base.OnExiting(sender, args);
GameThread.SetKilled();
args.Cancel = !(GameFacade.Screens.CurrentUIScreen?.CloseAttempt() ?? true);
}
/// <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)
{
if (!HasUpdated)
{
this.IsMouseVisible = true;
if (!FSOEnvironment.SoftwareKeyboard) AddTextInput();
this.Window.Title = "Simitone";
HasUpdated = true;
GameFacade.Screens = uiLayer;
GameController.EnterLoading();
}
GameThread.UpdateExecuting = true;
if (HITVM.Get() != null) HITVM.Get().Tick();
base.Update(gameTime);
GameThread.UpdateExecuting = false;
}
}
}