-
Notifications
You must be signed in to change notification settings - Fork 2
/
Game.cs
178 lines (151 loc) · 6.43 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
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
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
namespace OpenTkNoesisGui
{
public class Game : GameWindow
{
private readonly float[] _vertices =
{
-0.5f, -0.5f, 0.0f, // Bottom-left vertex
0.5f, -0.5f, 0.0f, // Bottom-right vertex
0.0f, 0.5f, 0.0f // Top vertex
};
private static Noesis.View _view = null;
private int _vertexBufferObject;
private int _vertexArrayObject;
private Shader _shader;
private double totalTime;
public Game(int width, int height, string title)
: base(width, height, GraphicsMode.Default, title)
{
NoesisInit();
}
protected override void OnLoad(EventArgs e)
{
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
_vertexBufferObject = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject);
GL.BufferData(BufferTarget.ArrayBuffer, _vertices.Length * sizeof(float), _vertices, BufferUsageHint.StaticDraw);
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
_vertexArrayObject = GL.GenVertexArray();
GL.BindVertexArray(_vertexArrayObject);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject);
base.OnLoad(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
totalTime += e.Time;
// UI pre-render
// Update view (layout, animations, ...)
_view.Update(totalTime / 1000.0);
// Offscreen rendering phase populates textures needed by the on-screen rendering
_view.Renderer.UpdateRenderTree();
_view.Renderer.RenderOffscreen();
GL.Clear(ClearBufferMask.ColorBufferBit);
_shader.Use();
GL.BindVertexArray(_vertexArrayObject);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
_view.Renderer.Render();
SwapBuffers();
base.OnRenderFrame(e);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
var input = Keyboard.GetState();
if (input.IsKeyDown(Key.Escape))
{
Exit();
}
base.OnUpdateFrame(e);
}
protected override void OnResize(EventArgs e)
{
GL.Viewport(0, 0, Width, Height);
_view.SetSize(Width, Height);
base.OnResize(e);
}
protected override void OnUnload(EventArgs e)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(0);
GL.UseProgram(0);
GL.DeleteBuffer(_vertexBufferObject);
GL.DeleteVertexArray(_vertexArrayObject);
GL.DeleteProgram(_shader.Handle);
base.OnUnload(e);
}
protected override void OnMouseMove(MouseMoveEventArgs e)
{
var mouseState = Mouse.GetState();
_view.MouseMove(mouseState.X, mouseState.Y);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
if (e.Button == MouseButton.Left)
{
_view.MouseButtonDown(e.Mouse.X, e.Mouse.Y, Noesis.MouseButton.Left);
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
if (e.Button == MouseButton.Left)
{
_view.MouseButtonUp(e.Mouse.X, e.Mouse.Y, Noesis.MouseButton.Left);
}
}
/// Noesis GUI \o/
static void NoesisInit()
{
Noesis.Log.SetLogCallback((level, channel, message) =>
{
if (channel == "")
{
// [TRACE] [DEBUG] [INFO] [WARNING] [ERROR]
string[] prefixes = new string[] { "T", "D", "I", "W", "E" };
string prefix = (int)level < prefixes.Length ? prefixes[(int)level] : " ";
Console.WriteLine("[NOESIS/" + prefix + "] " + message);
}
});
// Noesis initialization. This must be the first step before using any NoesisGUI functionality
Noesis.GUI.Init();
// For simplicity purposes we are not using resource providers in this sample. ParseXaml() is
// enough if there is no extra XAML dependencies
Noesis.Grid xaml = (Noesis.Grid)Noesis.GUI.ParseXaml(@"
<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<Grid.Background>
<LinearGradientBrush StartPoint=""0,0"" EndPoint=""0,1"">
<GradientStop Offset=""0"" Color=""#FF123F61""/>
<GradientStop Offset=""0.6"" Color=""#FF0E4B79""/>
<GradientStop Offset=""0.7"" Color=""#FF106097""/>
</LinearGradientBrush>
</Grid.Background>
<Viewbox>
<StackPanel Margin=""50"">
<Button Content=""Hello World!"" Margin=""0,30,0,0""/>
<Rectangle Height=""5"" Margin=""-10,20,-10,0"">
<Rectangle.Fill>
<RadialGradientBrush>
<GradientStop Offset=""0"" Color=""#40000000""/>
<GradientStop Offset=""1"" Color=""#00000000""/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</Viewbox>
</Grid>");
// View creation to render and interact with the user interface
// We transfer the ownership to a global pointer instead of a Ptr<> because there is no way
// in GLUT to do shutdown and we don't want the Ptr<> to be released at global time
_view = Noesis.GUI.CreateView(xaml);
_view.SetIsPPAAEnabled(true);
// Renderer initialization with an OpenGL device
_view.Renderer.Init(new Noesis.RenderDeviceGL());
}
}
}