-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cs
More file actions
138 lines (125 loc) · 4.54 KB
/
Game.cs
File metadata and controls
138 lines (125 loc) · 4.54 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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using OpenTK.Graphics.ES20;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common.Input;
using OpenTK.Windowing.Desktop;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using Image = OpenTK.Windowing.Common.Input.Image;
namespace engenious
{
/// <inheritdoc />
public abstract class Game : Game<Window>
{
private readonly GameSettings _settings;
private SixLabors.ImageSharp.Image[]? _icons;
private void CreateWindow()
{
var nativeWindowSettings = new NativeWindowSettings
{
StartVisible = !_settings.Offscreen,
NumberOfSamples = _settings.NumberOfSamples,
Flags = ContextFlags,
Size = new Vector2i(1024, 768)
};
var baseWindow = new OpenTkWindowWrapper(new GameWindow(GameWindowSettings.Default, nativeWindowSettings));
//GraphicsContext.ShareContexts = true;
Window = new Window(baseWindow);
ConstructContext(Window, baseWindow.Context);
baseWindow.Context.MakeCurrent();
}
/// <summary>
/// Initializes a new instance of the <see cref="Game"/> class.
/// </summary>
/// <param name="settings">
/// The settings with which the rendering environment should be initialized.
/// <c>null</c> is equivalent to <see cref="GameSettings.Default"/>.
/// </param>
protected Game(GameSettings? settings = null)
{
_settings = settings ?? GameSettings.Default;
Window = null!;
CreateWindow();
InitializeControl();
}
/// <summary>
/// Gets a Window(rendering view) associated with this <see cref="Game"/>.
/// </summary>
public Window Window{ get; private set; }
/// <summary>
/// Gets or sets an <see cref="Icons"/> associated with the rendering view.
/// <remarks>This sets or gets the window icon, if the rendering view is a <see cref="Window"/>.</remarks>
/// </summary>
public SixLabors.ImageSharp.Image[]? Icons
{
get => _icons;
set
{
if (_icons != value)
{
if (_icons != null)
{
foreach (var i in _icons)
{
i.Dispose();
}
}
_icons = value;
ReloadIcons();
}
}
}
private void ReloadIcons()
{
if (_icons != null)
{
var img = new Image[_icons.Length];
for (int i = 0; i < _icons.Length; i++)
{
var icon = _icons[i];
var normal = icon.CloneAs<Rgba32>();
var data = new byte[icon.Width * icon.Height * sizeof(int)];
normal.CopyPixelDataTo(data);
img[i] = new Image(_icons[i].Width, _icons[i].Height, data);
}
Window.BaseWindow.Icon = new WindowIcon(img);
}
else
{
Window.BaseWindow.Icon = null;
}
}
/// <summary>
/// Gets or sets a title associated with the rendering view.
/// <remarks>This sets or gets the window title, if the rendering view is a <see cref="Window"/>.</remarks>
/// </summary>
public string Title{ get => Window.Title;
set => Window.Title = value;
}
/// <summary>
/// Runs the rendering view's message loop, as well as update and rendering loop.
/// </summary>
public void Run()
{
Window.BaseWindow.Run();
}
/// <summary>
/// Runs the rendering view's message loop, as well as update and rendering loop within given parameters.
/// </summary>
/// <param name="updatesPerSec">The frequency at which the update loop should run.</param>
/// <param name="framesPerSec">The frequency at which the rendering loop should run.</param>
public void Run(double updatesPerSec, double framesPerSec)
{
Window.BaseWindow.Run();
}
/// <summary>
/// Closes the rendering view.
/// </summary>
public void Exit()
{
Window.Close();
}
}
}