-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyGame.cs
237 lines (208 loc) · 5.68 KB
/
MyGame.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
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
using SkiaSharp;
using Silk.NET.Windowing;
using Silk.NET.OpenGL;
using Silk.NET.Input;
class MyGame
{
private int SizeX;
private int SizeY;
private IWindow window;
private SKSurface surface;
private SKCanvas canvas;
private GL gl;
private SKPaint paint;
private List<Key> pressedKeys;
private bool mouse1Pressed;
private CircleOfLife circleOfLife;
private System.Numerics.Vector2 mousePosition;
private List<Cell> wereChanged;
private float simulationSpeed;
private bool simulationRunning;
private float simulationTimer;
private string title;
public MyGame(int width, int height, string title)
{
var options = WindowOptions.Default;
this.title = title;
SizeX = width;
SizeY = height;
options.Title = title;
options.Size = new Silk.NET.Maths.Vector2D<int>(width, height);
options.ShouldSwapAutomatically = false;
window = Window.Create(options);
window.FramesPerSecond = 60;
window.Update += OnUpdate;
window.Render += OnRender;
window.Load += OnLoad;
window.Closing += onClosing;
}
private void OnUpdate(double arg1)
{
window.Title = title + " Simulation Speed:" + (simulationSpeed * 100).ToString("0.00") + "%";
window.Title += simulationRunning ? " RUNNING" : " PAUSED";
if (simulationRunning)
{
if (simulationTimer < 0)
{
circleOfLife.SimulateLive();
simulationTimer += 1;
}
simulationTimer -= (float)arg1 * simulationSpeed;
}
else
{
simulationTimer = 1;
if (mouse1Pressed)
{
int x = (int)((mousePosition.X / Cell.Size) - ((mousePosition.X / Cell.Size) % 1f));
int y = (int)((mousePosition.Y / Cell.Size) - ((mousePosition.Y / Cell.Size) % 1f));
circleOfLife.Field[x, y].Change();
if (!wereChanged.Contains(circleOfLife.Field[x, y]))
{
wereChanged.Add(circleOfLife.Field[x, y]);
}
}
}
}
private void onFileDrop(string[] arg1)
{
if (arg1.Count() == 0) return;
circleOfLife.LoadFromFile(arg1[0]);
}
private void OnRender(double arg1)
{
drawCells();
swapBuffers();
}
private void OnLoad()
{
//create and bind input context
var input = window.CreateInput();
window.FileDrop += onFileDrop;
foreach (var keyboard in input.Keyboards)
{
keyboard.KeyDown += OnKeyDown;
keyboard.KeyUp += OnKeyUp;
}
foreach (var mouse in input.Mice)
{
mouse.MouseDown += OnMouseDown;
mouse.MouseUp += OnMouseUp;
mouse.MouseMove += OnMouseMove;
mouse.Scroll += OnMouseScroll;
}
//create and configure OpenGL context
gl = window.CreateOpenGL();
gl.ClearColor(1f, 1f, 1f, 1f);
//create SkiaSharp context
var grinterface = GRGlInterface.CreateOpenGl(name =>
{
if (window.GLContext.TryGetProcAddress(name, out nint fn)) return fn;
return (nint)0;
});
var skiabackendcontext = GRContext.CreateGl(grinterface);
var format = SKColorType.Rgba8888;
var backendrendertarget = new GRBackendRenderTarget(window.Size.X, window.Size.Y, window.Samples ?? 1, window.PreferredStencilBufferBits ?? 16, new GRGlFramebufferInfo(
0, format.ToGlSizedFormat()
));
//var info = new SKImageInfo(window.Size.X, window.Size.Y);
//surface = SKSurface.Create(info);
surface = SKSurface.Create(skiabackendcontext, backendrendertarget, format);
canvas = surface.Canvas;
var typeface = SKTypeface.CreateDefault();
paint = new SKPaint(new SKFont(typeface));
//add gameObjects
//add ground
createObjects();
}
public void Run()
{
window.Run();
}
private void drawCells()
{
var background = new SKPaint() { Color = SKColors.DimGray };
var alive = new SKPaint() { Color = Cell.AliveColor };
var dead = new SKPaint() { Color = Cell.DeadColor };
for (int x = 0; x < circleOfLife.SizeX; x++)
{
for (int y = 0; y < circleOfLife.SizeY; y++)
{
canvas.DrawRect(x * Cell.Size, y * Cell.Size, Cell.Size, Cell.Size, background);
canvas.DrawRect(x * Cell.Size + 1, y * Cell.Size + 1, Cell.Size - 2, Cell.Size - 2, circleOfLife.Field[x, y].IsAlive ? alive : dead);
}
}
}
private void OnKeyDown(IKeyboard arg1, Key arg2, int arg3)
{
if (arg2 == Key.Space) simulationRunning = !simulationRunning;
if (arg2 == Key.Escape) window.Close();
if (simulationRunning) return;
if (arg2 == Key.Backspace) circleOfLife.killAllLive();
if (arg2 == Key.R) circleOfLife.generateRandom(25);
if (arg2 == Key.Right) circleOfLife.SimulateLive();
if (arg1.IsKeyPressed(Key.ControlLeft) && arg2 == Key.S)
{
circleOfLife.SaveToFile();
}
}
private void OnKeyUp(IKeyboard arg1, Key arg2, int arg3)
{
}
private void OnMouseMove(IMouse arg1, System.Numerics.Vector2 arg2)
{
mousePosition = arg1.Position;
}
private void OnMouseDown(IMouse arg1, MouseButton arg2)
{
if (MouseButton.Left == arg2) mouse1Pressed = true;
}
private void OnMouseUp(IMouse arg1, MouseButton arg2)
{
if (MouseButton.Left == arg2)
{
mouse1Pressed = false;
setAllChangedFalse();
}
}
private void setAllChangedFalse()
{
/*for (int x = 0; x < circleOfLife.SizeX; x++){
for (int y = 0; y < circleOfLife.SizeY; y++){
circleOfLife.Field[x, y].Reset();
}
}*/
wereChanged.ForEach(delegate (Cell cell) { cell.Reset(); });
wereChanged.Clear();
}
private void OnMouseScroll(IMouse arg1, ScrollWheel arg2)
{
if (arg2.Y < 0)
{
simulationSpeed -= 0.05f;
}
else if (arg2.Y > 0)
{
simulationSpeed += 0.05f;
}
}
private void swapBuffers()
{
canvas.Flush();
window.SwapBuffers();
canvas.Clear(SKColors.DarkSlateGray); // set the background color here
}
private void createObjects()
{
//create all variables
pressedKeys = new List<Key>();
circleOfLife = new CircleOfLife(SizeX / Cell.Size, SizeY / Cell.Size);
mouse1Pressed = false;
wereChanged = new List<Cell>();
simulationSpeed = 1f;
simulationRunning = false;
}
void onClosing()
{
}
}