-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame1.cs
106 lines (80 loc) · 3.45 KB
/
Game1.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
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RayKeys.UI;
using RayKeys.Misc;
using RayKeys.Options;
using RayKeys.Render;
namespace RayKeys {
public class Game1 : Game {
public static Game1 Game;
public static GraphicsDeviceManager Graphics;
public static SpriteBatch SpriteBatch;
public static float Scaling;
public static Rectangle RenderRectangle { get; private set; }
private FPSCounter fpsCounter = new FPSCounter();
private RenderTarget2D renderTarget;
public delegate void UpdateEventD(float delta);
public static event UpdateEventD UpdateEvent;
public static event UpdateEventD StaticUpdateEvent; // an update event that will never clear
public static event UpdateEventD DrawEvent;
public Game1(LogLevel logLevel) {
Game = this;
Graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
Logger.Init(logLevel);
}
protected override void Initialize() {
Scaling = Graphics.PreferredBackBufferHeight / 1080.0f;
renderTarget = new RenderTarget2D(GraphicsDevice, 1920, 1080, false, GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
StaticUpdateEvent += RKeyboard.Update;
StaticUpdateEvent += RMouse.Update;
base.Initialize();
}
protected override void LoadContent() {
SpriteBatch = new SpriteBatch(GraphicsDevice);
RRender.Initialise();
PrepareLoadScene();
LoadScene(new LoadingScene());
}
protected override void Update(GameTime gameTime) {
float delta = (float) gameTime.ElapsedGameTime.TotalSeconds;
fpsCounter.Update(gameTime);
StaticUpdateEvent?.Invoke(delta);
UpdateEvent?.Invoke(delta);
base.Update(gameTime);
}
public void PrepareLoadScene() {
UpdateEvent = null;
DrawEvent = null;
}
public void LoadScene(Scene scene) {
RRender.CameraPos = Vector2.Zero;
}
public void RedoRenderPos() {
Point thing = new Point((int) (1920 * Scaling), (int) (1080 * Scaling));
RenderRectangle = new Rectangle((Graphics.PreferredBackBufferWidth - thing.X) / 2,
(Graphics.PreferredBackBufferHeight - thing.Y) / 2, thing.X, thing.Y);
}
protected override void Draw(GameTime gameTime) {
RRender.Draw();
GraphicsDevice.Clear(Color.Black);
GraphicsDevice.SetRenderTarget(renderTarget);
SpriteBatch.Begin(SpriteSortMode.BackToFront);
DrawEvent?.Invoke((float) gameTime.ElapsedGameTime.TotalSeconds);
fpsCounter.DrawFps();
SpriteBatch.End();
SpriteBatch.Begin();
GraphicsDevice.SetRenderTarget(null);
SpriteBatch.Draw(renderTarget, RenderRectangle, new Rectangle(0, 0, 1920, 1080), Color.White);
SpriteBatch.End();
base.Draw(gameTime);
}
protected override void OnExiting(object sender, EventArgs args) {
base.OnExiting(sender, args);
}
}
}