-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
153 lines (135 loc) · 5.05 KB
/
Copy pathGame.cs
File metadata and controls
153 lines (135 loc) · 5.05 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
namespace Engine
{
using System;
using System.IO;
using Veldrid;
/// <summary>
/// The game base class.
/// </summary>
public abstract class Game
{
/// <summary>
/// Initializes a new instance of the <see cref="Game"/> class.
/// </summary>
/// <param name="window">The window.</param>
/// <exception cref="ArgumentException">The <paramref name="window"/> cannot be <c>null</c>.</exception>
protected Game(IGameWindow window)
{
this.Window = window ?? throw new ArgumentNullException(nameof(window), $"The {nameof(window)} cannot be null.");
this.Window.Resized += () => this.OnWindowResized(this.Window.Width, this.Window.Height);
this.Window.GraphicsDeviceCreated += this.OnGraphicsDeviceCreated;
this.Window.GraphicsDeviceDestroyed += this.OnDeviceDestroyed;
this.Window.Rendering += this.Update;
this.Window.Rendering += this.Draw;
this.Window.KeyPressed += this.OnKeyDown;
}
/// <summary>
/// Gets the window.
/// </summary>
public IGameWindow Window { get; }
/// <summary>
/// Gets the graphics device.
/// </summary>
public GraphicsDevice GraphicsDevice { get; private set; }
/// <summary>
/// Gets the resource factory.
/// </summary>
public ResourceFactory ResourceFactory { get; private set; }
/// <summary>
/// Gets the swapchain.
/// </summary>
public Swapchain Swapchain { get; private set; }
/// <summary>
/// Opens the embedded asset stream.
/// </summary>
/// <param name="name">The resource name.</param>
/// <returns>The <see cref="Stream"/>.</returns>
public Stream OpenEmbeddedAssetStream(string name) => this.GetType().Assembly.GetManifestResourceStream(name);
/// <summary>
/// Reads the embedded asset into a byte array.
/// </summary>
/// <param name="name">The resource name.</param>
/// <returns>The byte array.</returns>
public byte[] ReadEmbeddedAssetBytes(string name)
{
using (var stream = this.OpenEmbeddedAssetStream(name))
{
var bytes = new byte[stream.Length];
using (var memory = new MemoryStream(bytes))
{
stream.CopyTo(memory);
return bytes;
}
}
}
/// <summary>
/// Loads the game resources.
/// </summary>
/// <param name="factory">The resource factory.</param>
protected abstract void CreateResources(ResourceFactory factory);
/// <summary>
/// Frees all allocated resources.
/// </summary>
protected abstract void FreeResources();
/// <summary>
/// Updates the game state before rendering.
/// </summary>
/// <param name="deltaSeconds">The elapsed seconds since the last frame.</param>
protected virtual void Update(float deltaSeconds)
{
// Override in the derived class.
}
/// <summary>
/// Renders the game.
/// </summary>
/// <param name="deltaSeconds">The elapsed seconds since the last frame.</param>
protected abstract void Draw(float deltaSeconds);
/// <summary>
/// Called when the window has been resized.
/// </summary>
/// <param name="width">The new pixel width.</param>
/// <param name="height">The new pixel height.</param>
protected virtual void OnWindowResized(uint width, uint height)
{
// Override in a derived class.
}
/// <summary>
/// Called when a key has been pressed down.
/// </summary>
/// <param name="state">The key state info.</param>
protected virtual void OnKeyDown(KeyEvent state)
{
// Override in a derived class.
}
/// <summary>
/// Closes the application.
/// </summary>
protected void Exit()
{
this.Window?.Close();
}
/// <summary>
/// Called when the graphics device has been created.
/// </summary>
/// <param name="device">The graphics device.</param>
/// <param name="factory">The resource factory.</param>
/// <param name="swapchain">The swapchain.</param>
private void OnGraphicsDeviceCreated(GraphicsDevice device, ResourceFactory factory, Swapchain swapchain)
{
this.GraphicsDevice = device;
this.ResourceFactory = factory;
this.Swapchain = swapchain;
this.CreateResources(factory);
}
/// <summary>
/// Called when the device has been destroyed.
/// </summary>
private void OnDeviceDestroyed()
{
this.FreeResources();
this.GraphicsDevice = null;
this.ResourceFactory = null;
this.Swapchain = null;
}
}
}