-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cs
108 lines (88 loc) · 2.79 KB
/
Game.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
namespace Storm;
using System.Drawing.Drawing2D;
using System.Diagnostics;
public abstract partial class Game
{
private Canvas window;
private Thread gameLoop;
private Stopwatch stopwatch;
private bool isGameActive;
private string _windowTitle;
public int windowWidth { get; }
public int windowHeight { get; }
public string windowTitle { get { return _windowTitle; } set { window.Text = value; _windowTitle = value; } }
public Game(
int windowWidth = 800, int windowHeight = 600, string windowTitle = "Game", string iconPath = "icon.ico", int fps = 30)
{
window = new()
{
ClientSize = new(windowWidth, windowHeight),
Text = windowTitle,
Icon = new(iconPath),
};
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
this._windowTitle = windowTitle;
FPS = fps;
window.Paint += Renderer;
stopwatch = new();
isGameActive = true;
gameLoop = new Thread(GameLoop);
window.FormClosed += (sender, e) => Exit();
}
public void Run()
{
window.Show();
gameLoop.Start();
stopwatch.Start();
Application.Run(window);
}
private void GameLoop()
{
OnLoad();
while (isGameActive)
{
try
{
window.Invalidate();
double dt = stopwatch.Elapsed.TotalSeconds;
stopwatch.Restart();
try { OnUpdate(dt); } catch {}
foreach (GameObject obj in gameObjectsUpdate)
{
try { obj.OnUpdate (dt); } catch {}
obj.UpdateComponents(dt);
}
Thread.Sleep(1000/FPS);
}
catch
{
Exit();
break;
}
}
}
private void Renderer(object? sender, PaintEventArgs e)
{
Graphics graph = e.Graphics;
graph.Clear(screenColor);
graph.InterpolationMode = InterpolationMode.NearestNeighbor;
for (int i = 0; i < gameObjectsDraw.Count; i++)
{
// The reason we do a try block when calling OnDraw is because stuff that is
// drawn might be null when drawing which will result in a crash, so we do
// this just in case it might happen
if (gameObjectsDraw[i].visible) try { gameObjectsDraw[i]?.OnDraw(graph); } catch {}
}
// Same thing here
try { OnDraw(graph); } catch {}
}
public abstract void OnLoad();
public abstract void OnUpdate(double deltaTime);
public abstract void OnDraw(Graphics graphics);
public void Exit()
{
isGameActive = false;
Application.Exit();
}
}