-
Notifications
You must be signed in to change notification settings - Fork 9
/
Application.cs
178 lines (150 loc) · 4.99 KB
/
Application.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
// Copyright (c) Amer Koleci and contributors.
// Distributed under the MIT license. See the LICENSE file in the project root for more information.
using RayTracingTutorial05.Interop;
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace RayTracingTutorial05
{
public abstract partial class Application : IDisposable
{
private bool _paused;
private bool _exitRequested;
private Scene _graphicsDevice;
public Window MainWindow { get; private set; }
protected Application()
{
PlatformConstruct();
}
public void Dispose()
{
_graphicsDevice.Dispose();
}
public void Tick()
{
_graphicsDevice.DrawFrame(OnDraw);
}
public void Run()
{
PlatformRun();
}
protected virtual void OnActivated()
{
}
protected virtual void OnDeactivated()
{
}
protected virtual void OnDraw(int width, int height)
{
}
private void InitializeBeforeRun()
{
_graphicsDevice = new Scene(MainWindow);
}
public static readonly string WndClassName = "D3D12SampleRaytracerSharpWindow";
public readonly IntPtr HInstance = Kernel32.GetModuleHandle(null);
private WNDPROC _wndProc;
private void PlatformConstruct()
{
_wndProc = ProcessWindowMessage;
var wndClassEx = new WNDCLASSEX
{
Size = Unsafe.SizeOf<WNDCLASSEX>(),
Styles = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC,
WindowProc = _wndProc,
InstanceHandle = HInstance,
CursorHandle = User32.LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW),
BackgroundBrushHandle = IntPtr.Zero,
IconHandle = IntPtr.Zero,
ClassName = WndClassName,
};
var atom = User32.RegisterClassEx(ref wndClassEx);
if (atom == 0)
{
throw new InvalidOperationException(
$"Failed to register window class. Error: {Marshal.GetLastWin32Error()}"
);
}
// Create main window.
MainWindow = new Window("Vortice Tutorial 05 - Shader Table", 1280, 720);
}
private void PlatformRun()
{
InitializeBeforeRun();
while (!_exitRequested)
{
if (!_paused)
{
const uint PM_REMOVE = 1;
if (User32.PeekMessage(out var msg, IntPtr.Zero, 0, 0, PM_REMOVE))
{
User32.TranslateMessage(ref msg);
User32.DispatchMessage(ref msg);
if (msg.Value == (uint)WindowMessage.Quit)
{
_exitRequested = true;
break;
}
}
Tick();
}
else
{
var ret = User32.GetMessage(out var msg, IntPtr.Zero, 0, 0);
if (ret == 0)
{
_exitRequested = true;
break;
}
else if (ret == -1)
{
//Log.Error("[Win32] - Failed to get message");
_exitRequested = true;
break;
}
else
{
User32.TranslateMessage(ref msg);
User32.DispatchMessage(ref msg);
}
}
}
}
private IntPtr ProcessWindowMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == (uint)WindowMessage.ActivateApp)
{
_paused = IntPtrToInt32(wParam) == 0;
if (IntPtrToInt32(wParam) != 0)
{
OnActivated();
}
else
{
OnDeactivated();
}
return User32.DefWindowProc(hWnd, msg, wParam, lParam);
}
switch ((WindowMessage)msg)
{
case WindowMessage.Destroy:
User32.PostQuitMessage(0);
break;
}
return User32.DefWindowProc(hWnd, msg, wParam, lParam);
}
private static int SignedLOWORD(int n)
{
return (short)(n & 0xFFFF);
}
private static int SignedHIWORD(int n)
{
return (short)(n >> 16 & 0xFFFF);
}
private static int IntPtrToInt32(IntPtr intPtr)
{
return (int)intPtr.ToInt64();
}
}
}