-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceRender.cs
88 lines (71 loc) · 2.32 KB
/
ServiceRender.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
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using System.Threading.Tasks;
using System;
using System.Timers;
namespace render
{
static class ServiceRender
{
public static double DeltaTime { get; private set; }
public static Scene scene;
public static Vector2 ViewSize
{
get => viewSize;
set
{
viewSize = value;
shouldResize = true;
}
}
public delegate void UpdateDelegate();
public static event UpdateDelegate OnUpdate;
private static Render render = new Render();
private static Image image;
private static WriteableBitmap bitmap;
private static Int32Rect rect;
private static Vector2 viewSize;
private static Stopwatch stopwatch = new Stopwatch();
private static DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Render);
private static bool shouldResize;
public static void Start(Image image, Vector2 viewSize)
{
ServiceRender.image = image;
ViewSize = viewSize;
//image.Source = bitmap;
stopwatch.Start();
timer.Interval = new System.TimeSpan(0, 0, 0, 0, 1);
timer.Tick += (object sender, System.EventArgs e) => Process();
timer.Start();
}
public static void Process()
{
timer.Stop();
OnUpdate?.Invoke();
RunRender();
DeltaTime = stopwatch.Elapsed.TotalSeconds;
stopwatch.Restart();
timer.Start();
}
private static void Resize()
{
scene.camera.Size = ViewSize;
bitmap = new WriteableBitmap((int)ViewSize.X, (int)ViewSize.Y, 96, 96, PixelFormats.Bgra32, null);
rect = new Int32Rect(0, 0, (int)ViewSize.X, (int)ViewSize.Y);
image.Source = bitmap;
shouldResize = false;
}
private static void RunRender()
{
if (shouldResize)
{
Resize();
}
bitmap.WritePixels(rect, render.GetImage(scene), 4 * (int)ViewSize.X, 0);
}
}
}