-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinFormsGame.cs
More file actions
81 lines (71 loc) · 2.57 KB
/
WinFormsGame.cs
File metadata and controls
81 lines (71 loc) · 2.57 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
using System;
using System.Runtime.InteropServices;
using engenious.Graphics;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Platform;
using OpenTK.Platform.X11;
namespace engenious.WinForms
{
/// <inheritdoc />
public class WinFormsGame : Game<GameControl>
{
private bool _initialized = false;
private void InitializeOnce(GameControl control)
{
if (_initialized)
return;
lock (this)
{
if (_initialized)
return;
_initialized = true;
control.WindowInfo = CreateWindow(control);
InitializeControl(control);
}
}
private IWindowInfo CreateWindow(GameControl control)
{
GraphicsContext.ShareContexts = true;
IWindowInfo baseWindow;
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
baseWindow = WinHelper.CreateWindowInfo(GraphicsMode.Default, control);
break;
case PlatformID.MacOSX:
baseWindow = CocoaHelper.CreateWindowInfo(GraphicsMode.Default, control);
break;
case PlatformID.Unix:
baseWindow = X11Helper.CreateWindowInfo(GraphicsMode.Default, control);
break;
default:
throw new NotSupportedException(Environment.OSVersion.Platform.ToString());
}
var options = new ToolkitOptions();
options.Backend = PlatformBackend.PreferNative;
OpenTK.Toolkit.Init(options);
var context = new GraphicsContext(GraphicsMode.Default, baseWindow, 0, 0, ContextFlags);
ConstructContext(baseWindow, context);
CreateSharedContext(baseWindow);
_context.MakeCurrent(baseWindow);
_context.LoadAll();
return baseWindow;
}
/// <inheritdoc />
public WinFormsGame(GameControl control)
{
control.HandleCreated += ControlOnHandleCreated;
if (control.IsHandleCreated)
InitializeOnce(control);
}
private void ControlOnHandleCreated(object sender, EventArgs e)
{
InitializeOnce((GameControl)sender);
((GameControl)sender).HandleCreated -= ControlOnHandleCreated;
}
}
}