-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cs
138 lines (105 loc) · 3.28 KB
/
Client.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
using Silk.NET.Input;
using Silk.NET.Windowing;
namespace drawing;
public unsafe partial class Client
{
public Client()
{
// Create a Silk.NET window
var options = WindowOptions.Default;
options.API = new GraphicsAPI(ContextAPI.OpenGL, new APIVersion(3, 3));
options.Position = new(200, 200);
options.PreferredDepthBufferBits = 32;
window = Window.Create(options);
// Callback when the window is created
window.Load += () =>
{
// Create an OpenGL Context
Gl = window.CreateOpenGL();
OnDidCreateOpenGLContext();
// Precalculate input stuff
inputContext = window.CreateInput();
keyboard = inputContext.Keyboards[0];
keyboard.KeyDown += OnKeyDown;
keyboard.KeyUp += OnKeyUp;
};
window.Render += (_) => Render();
window.Size = new(1920, 1080);
window.FramesPerSecond = 144;
window.UpdatesPerSecond = 144;
window.VSync = false;
// Initialise OpenGL and the input context
window.Initialize();
}
// Silk
IWindow window;
IKeyboard keyboard;
IInputContext inputContext;
Bitmap bitmap;
Texture texture;
public void Run()
{
// Run forever
window.Run();
}
void OnDidCreateOpenGLContext()
{
var major = Gl.GetInteger(GetPName.MajorVersion);
var minor = Gl.GetInteger(GetPName.MinorVersion);
var version = major * 10 + minor;
Console.WriteLine($"OpenGL Version: {version}");
}
public void OnKeyDown(IKeyboard keyboard, Key key, int something)
{
OnKeyEvent(new KeyEvent()
{
IsPress = true,
KeyCode = key,
});
}
public void OnKeyUp(IKeyboard keyboard, Key key, int something)
{
OnKeyEvent(new KeyEvent()
{
IsPress = false,
KeyCode = key,
});
}
void Render()
{
// Prepare OpenGL
PreRenderSetup();
// Ensure bitmaps and textures are the same size as the window
if (bitmap == null || bitmap.width != window.Size.X || bitmap.height != window.Size.Y)
{
bitmap?.Dispose();
texture?.Dispose();
bitmap = new(window.Size.X, window.Size.Y);
texture = new(window.Size.X, window.Size.Y);
}
// Render to the bitmap
bitmap.Prepare();
Draw();
bitmap.Flush();
// Copy bitmap pixels to a texture
texture.SetData(bitmap);
// Render the texture
BitmapShader.UseProgram();
BitmapShader.tex.Set(1);
Gl.ActiveTexture(TextureUnit.Texture1);
texture.Bind();
GLHelper.DrawWindowBuffer();
}
public void PreRenderSetup()
{
// Prepare rendering
Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
Gl.Disable(EnableCap.DepthTest);
Gl.Disable(EnableCap.Blend);
Gl.Disable(EnableCap.StencilTest);
Gl.Disable(EnableCap.CullFace);
// No need to clear colour here as the shader renders to the whole window every frame
// Set the viewport to the window size
Gl.Viewport(0, 0, (uint)window.Size.X, (uint)window.Size.Y);
}
}