aboutsummaryrefslogtreecommitdiff
path: root/Unicity.Renderer/RenderWindow.cs
blob: 840d4fb7b1ed51fbca255c671226d6ee2c94ce9b (plain) (blame)
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
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using GLFW;

namespace Unicity.Renderer
{
    public class RenderWindow : IDisposable
    {
        const int FPS = 60;

        internal NativeWindow window = null;

        Stopwatch loopTimer = new Stopwatch();

        public event EventHandler Init;
        public event EventHandler Update;
        public event EventHandler Render;

        public int Width
        {
            get => window.ClientSize.Width;
            set => window.ClientSize = new Size(value, window.ClientSize.Height);
        }

        public int Height
        {
            get => window.ClientSize.Height;
            set => window.ClientSize = new Size(window.ClientSize.Width, value);
        }

        public string Title
        {
            get => window.Title;
            set => window.Title = value;
        }

        public RenderWindow(int width, int height, string title)
        {
            if (!File.Exists(Glfw.LIBRARY + ".dll"))
            {
                throw new WindowCreationFailedException("A required library file is missing and operation cannot continue.");
            }

            if (!Glfw.Init())
            {
                throw new WindowCreationFailedException("Failed to initialize GLFW.");
            }

            window = new NativeWindow(width, height, title);
            window.SizeChanged += Window_SizeChanged;
        }

        private void Window_SizeChanged(object sender, SizeChangeEventArgs e)
        {
            GraphicsRenderer.GL.Viewport(0, 0, Width, Height);
            UpdateWindow();
        }

        public void StartUpdateLoop()
        {
            if (loopTimer.IsRunning) return;

            loopTimer.Start();

            Init?.Invoke(this, EventArgs.Empty);

            while (!window.IsClosed)
            {
                Glfw.PollEvents();

                if (!window.IsClosing) UpdateWindow();
            }

            loopTimer.Stop();
        }

        private void UpdateWindow()
        {
            if (loopTimer.Elapsed.TotalMilliseconds >= 1000 / FPS)
            {
                loopTimer.Restart();

                Update?.Invoke(this, EventArgs.Empty);
                Render?.Invoke(this, EventArgs.Empty);

                window.SwapBuffers();
            }
        }

        bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            // Return of already disposed
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                // Free managed objects here
            }

            // Dispose of any unmanaged resources
            window?.Dispose();
            Glfw.Terminate();

            // Set disposed flag to true
            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}