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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Newtonsoft.Json;
using ShiftOS.Engine;
using ShiftOS.Frontend.GraphicsSubsystem;
namespace ShiftOS.Frontend
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class ShiftOS : Game
{
GraphicsDeviceManager GraphicsDevice;
SpriteBatch spriteBatch;
private bool DisplayDebugInfo = false;
public ShiftOS()
{
GraphicsDevice = new GraphicsDeviceManager(this);
GraphicsDevice.PreferredBackBufferHeight = 1080;
GraphicsDevice.PreferredBackBufferWidth = 1920;
SkinEngine.SkinLoaded += () =>
{
UIManager.ResetSkinTextures(GraphicsDevice.GraphicsDevice);
UIManager.InvalidateAll();
};
UIManager.Viewport = new System.Drawing.Size(
GraphicsDevice.PreferredBackBufferWidth,
GraphicsDevice.PreferredBackBufferHeight
);
Content.RootDirectory = "Content";
//Make window borderless
Window.IsBorderless = false;
//Set the title
Window.Title = "ShiftOS";
//Fullscreen
GraphicsDevice.IsFullScreen = false;
}
private GUI.TextControl _titleLabel = null;
private Keys lastKey = Keys.None;
/// <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()
{
//Before we do ANYTHING, we've got to initiate the ShiftOS engine.
UIManager.GraphicsDevice = GraphicsDevice.GraphicsDevice;
//Let's get localization going.
Localization.RegisterProvider(new MonoGameLanguageProvider());
//First things first, let's initiate the window manager.
AppearanceManager.Initiate(new Desktop.WindowManager());
//Cool. Now the engine's window management system talks to us.
//Also initiate the desktop
Engine.Desktop.Init(new Desktop.Desktop());
//Now we can initiate the Infobox subsystem
Engine.Infobox.Init(new Infobox());
//Let's initiate the engine just for a ha.
TerminalBackend.TerminalRequested += () =>
{
AppearanceManager.SetupWindow(new Apps.Terminal());
};
//We'll use sandbox mode
SaveSystem.IsSandbox = true;
Engine.Infobox.Show("Test window", "This is a test window.");
SaveSystem.Begin(true);
base.Initialize();
}
private Texture2D MouseTexture = null;
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
this.spriteBatch = new SpriteBatch(base.GraphicsDevice);
UIManager.ResetSkinTextures(GraphicsDevice.GraphicsDevice);
// TODO: use this.Content to load your game content here
var bmp = Properties.Resources.cursor_9x_pointer;
var _lock = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
byte[] rgb = new byte[Math.Abs(_lock.Stride) * _lock.Height];
Marshal.Copy(_lock.Scan0, rgb, 0, rgb.Length);
bmp.UnlockBits(_lock);
MouseTexture = new Texture2D(GraphicsDevice.GraphicsDevice, bmp.Width, bmp.Height);
MouseTexture.SetData<byte>(rgb);
rgb = null;
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
MouseTexture = null;
// TODO: Unload any non ContentManager content here
}
private double kb_elapsedms = 0;
private MouseState LastMouseState;
/// <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 (UIManager.CrossThreadOperations.Count > 0)
{
var action = UIManager.CrossThreadOperations.Dequeue();
action?.Invoke();
}
//Let's get the mouse state
var mouseState = Mouse.GetState(this.Window);
LastMouseState = mouseState;
UIManager.ProcessMouseState(LastMouseState);
//So we have mouse input, and the UI layout system working...
//But an OS isn't useful without the keyboard!
//Let's see how keyboard input works.
//Hmmm... just like the mouse...
var keystate = Keyboard.GetState();
//Simple... just iterate through this list and generate some key events?
var keys = keystate.GetPressedKeys();
if (keys.Length > 0)
{
var key = keys.FirstOrDefault(x => x != Keys.LeftControl && x != Keys.RightControl && x != Keys.LeftShift && x != Keys.RightShift && x != Keys.LeftAlt && x != Keys.RightAlt);
if(lastKey != key)
{
kb_elapsedms = 0;
lastKey = key;
}
}
if (keystate.IsKeyDown(lastKey))
{
if (kb_elapsedms == 0 || kb_elapsedms >= 500)
{
if (lastKey == Keys.F11)
{
GraphicsDevice.IsFullScreen = !GraphicsDevice.IsFullScreen;
GraphicsDevice.ApplyChanges();
}
else
{
var shift = keystate.IsKeyDown(Keys.LeftShift) || keystate.IsKeyDown(Keys.RightShift);
var alt = keystate.IsKeyDown(Keys.LeftAlt) || keystate.IsKeyDown(Keys.RightAlt);
var control = keystate.IsKeyDown(Keys.LeftControl) || keystate.IsKeyDown(Keys.RightControl);
if (control && lastKey == Keys.D)
{
DisplayDebugInfo = !DisplayDebugInfo;
}
else if(control && lastKey == Keys.E)
{
UIManager.ExperimentalEffects = !UIManager.ExperimentalEffects;
}
else
{
var e = new KeyEvent(control, alt, shift, lastKey);
UIManager.ProcessKeyEvent(e);
}
}
}
kb_elapsedms += gameTime.ElapsedGameTime.TotalMilliseconds;
}
else
{
kb_elapsedms = 0;
}
//Cause layout update on all elements
UIManager.LayoutUpdate();
base.Update(gameTime);
}
private GUI.TextControl framerate = new GUI.TextControl();
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
UIManager.DrawControlsToTargets(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0);
var rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
rasterizerState.MultiSampleAntiAlias = true;
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied,
SamplerState.LinearClamp, DepthStencilState.Default,
rasterizerState);
//Draw the desktop BG.
UIManager.DrawBackgroundLayer(GraphicsDevice.GraphicsDevice, spriteBatch, 640, 480);
//The desktop is drawn, now we can draw the UI.
UIManager.DrawTArgets(spriteBatch);
//Draw a mouse cursor
var mousepos = Mouse.GetState(this.Window).Position;
spriteBatch.Draw(MouseTexture, new Rectangle(mousepos.X+1, mousepos.Y+1, MouseTexture.Width, MouseTexture.Height), Color.Black * 0.5f);
spriteBatch.Draw(MouseTexture, new Rectangle(mousepos.X, mousepos.Y, MouseTexture.Width, MouseTexture.Height), Color.White);
if (DisplayDebugInfo)
{
var gfxContext = new GraphicsContext(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0, GraphicsDevice.PreferredBackBufferWidth, GraphicsDevice.PreferredBackBufferHeight);
var color = Color.White;
double fps = 1 / gameTime.ElapsedGameTime.TotalSeconds;
if (fps <= 20)
color = Color.Red;
gfxContext.DrawString($@"ShiftOS 1.0 Beta 4
Copyright (c) 2017 Michael VanOverbeek, Rylan Arbour, RogueAI
This is an unstable build.
FPS: {(fps)}
An FPS below 20 can cause glitches in keyboard and mouse handling. It is advised that if you are getting these
framerates, press CTRL+E to disable fancy effects, close any apps you are not using, and try running in windowed mode
or in a lower resolution.
If all else fails, you can set a breakpoint somewhere in the ShiftOS.Update() or ShiftOS.Draw() methods in the game's source
code and use Visual Studio's debugger to step through the code to find bottlenecks.
If a method takes more than 30 milliseconds to complete, that is a sign that it is bottlenecking the game and may need to be
optimized.
Try using the SkinTextures cache when rendering skin elements, and try using the GraphicsContext.DrawString() method when drawing
text. In this build, we are aware that this method causes bottlenecking, we are working on a caching system for fonts so we do not need to
use the System.Drawing.Graphics class to draw text.
UI render target count: {UIManager.TextureCaches.Count}
Skin texture caches: {UIManager.SkinTextures.Count}
Open windows (excluding dialog boxes): {AppearanceManager.OpenForms.Count}
Experimental effects enabled: {UIManager.ExperimentalEffects}
Fullscreen: {GraphicsDevice.IsFullScreen}
Game resolution: {GraphicsDevice.PreferredBackBufferWidth}x{GraphicsDevice.PreferredBackBufferHeight}", 0, 0, color, new System.Drawing.Font("Lucida Console", 9F, System.Drawing.FontStyle.Bold));
}
spriteBatch.End();
base.Draw(gameTime);
}
}
[ShiftoriumProvider]
public class MonoGameShiftoriumProvider : IShiftoriumProvider
{
public List<ShiftoriumUpgrade> GetDefaults()
{
return JsonConvert.DeserializeObject<List<ShiftoriumUpgrade>>(Properties.Resources.Shiftorium);
}
}
}
|