Browse code

WIP: Add GLBackendWxWidgets

Robert Cranston authored on 20/10/2021 21:11:33
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,183 @@
1
+/// Guards
2
+
3
+#ifdef  GLBACKEND_WXWIDGETS
4
+#ifndef GLBACKEND_WXWIDGETS_HPP_
5
+#define GLBACKEND_WXWIDGETS_HPP_
6
+
7
+
8
+/// Includes
9
+
10
+#include <array>
11
+#include <string>
12
+
13
+#include <glbackend.hpp>
14
+
15
+// #include <wx/wx.h>
16
+#include <wx/evtloop.h>
17
+#include <wx/glcanvas.h>
18
+#include <wx/app.h>
19
+#include <wx/gdicmn.h>
20
+#include <wx/gtk/app.h>
21
+#include <wx/gtk/evtloop.h>
22
+#include <wx/longlong.h>
23
+#include <wx/time.h>
24
+#include <wx/unix/glx11.h>
25
+#include "wx/cursor.h"
26
+class wxAppConsole;
27
+class wxFrame;
28
+
29
+
30
+/// Class definition
31
+
32
+class GLBackendWxWidgets final : public GLBackend
33
+{
34
+
35
+public:
36
+
37
+    //// Special member functions
38
+
39
+    explicit GLBackendWxWidgets(
40
+        std::string const & title,
41
+        std::array<int, 2>  size        = {0, 0},
42
+        std::array<int, 2>  version     = {0, 0},
43
+        int                 samples     = 0,
44
+        bool                fullscreen  = false,
45
+        bool                transparent = false
46
+    );
47
+    ~GLBackendWxWidgets();
48
+    GLBackendWxWidgets(GLBackendWxWidgets &&) noexcept;
49
+    GLBackendWxWidgets(GLBackendWxWidgets const &) = delete;
50
+    GLBackendWxWidgets & operator=(GLBackendWxWidgets &&) = delete;
51
+    GLBackendWxWidgets & operator=(GLBackendWxWidgets const &) = delete;
52
+
53
+    //// Context
54
+
55
+    void current() override;
56
+
57
+    //// Render loop
58
+
59
+    void swap() override;
60
+
61
+    void events() override;
62
+
63
+    bool running() const override;
64
+    bool running(bool running) override;
65
+
66
+    float time() const override;
67
+    float time(float time) override;
68
+
69
+    //// Input and output
70
+
71
+    void lock(bool lock) override;
72
+
73
+    bool key   (std::string const & key)    const override;
74
+    bool button(int                 button) const override;
75
+
76
+    //// Debug
77
+
78
+    std::string debug_info() const override;
79
+
80
+protected:
81
+
82
+    //// Special member functions
83
+
84
+    void destroy_();
85
+
86
+protected:
87
+
88
+    //// Context
89
+
90
+    wxAppConsole * app_;
91
+    wxFrame      * frame_;
92
+    wxGLCanvas   * canvas_;
93
+    wxGLContext  * context_;
94
+
95
+    //// Render loop
96
+
97
+    wxGUIEventLoop event_loop_;
98
+    float          time_;
99
+
100
+    //// Input and output
101
+
102
+    bool locked_;
103
+
104
+};
105
+
106
+
107
+/// Inline definitions
108
+
109
+inline void GLBackendWxWidgets::current()
110
+{
111
+    context_->SetCurrent(*canvas_);
112
+}
113
+
114
+inline void GLBackendWxWidgets::events()
115
+{
116
+    // wxWidgets/include/wx/evtloop.h
117
+    // wxWidgets/src/common/evtloopcmn.cpp
118
+    wxEventLoopActivator event_loop_activator(&event_loop_);
119
+    while (event_loop_.Pending())
120
+        event_loop_.Dispatch();
121
+    event_loop_.ProcessIdle();
122
+    if (wxTheApp)
123
+        wxTheApp->ProcessPendingEvents();
124
+}
125
+
126
+inline void GLBackendWxWidgets::swap()
127
+{
128
+    canvas_->SwapBuffers();
129
+}
130
+
131
+inline bool GLBackendWxWidgets::running() const
132
+{
133
+    return canvas_->IsShown();
134
+}
135
+
136
+inline bool GLBackendWxWidgets::running(bool running)
137
+{
138
+    canvas_->Show(running);
139
+    return running;
140
+}
141
+
142
+inline float GLBackendWxWidgets::time() const
143
+{
144
+    return (float)(wxGetUTCTimeUSec().ToDouble() / 1000000.0) - time_;
145
+}
146
+
147
+inline float GLBackendWxWidgets::time(float time)
148
+{
149
+    time_ = (float)(wxGetUTCTimeUSec().ToDouble() / 1000000.0) - time;
150
+    return time;
151
+}
152
+
153
+inline void GLBackendWxWidgets::lock(bool lock)
154
+{
155
+    canvas_->SetCursor(lock ? wxCursor(wxCURSOR_BLANK) : wxNullCursor);
156
+    if (lock && !locked_)
157
+        canvas_->CaptureMouse();
158
+    else if (!lock && locked_)
159
+        canvas_->ReleaseMouse();
160
+    locked_ = lock;
161
+    // TODO(rcrnstn): wxWidgets `WarpPointer` needed?
162
+    // canvas_->WarpPointer(
163
+    //     size_[0] / 2,
164
+    //     size_[1] / 2
165
+    // );
166
+}
167
+
168
+inline bool GLBackendWxWidgets::key(std::string const & key) const
169
+{
170
+    (void)key;
171
+    return false;
172
+}
173
+inline bool GLBackendWxWidgets::button(int button) const
174
+{
175
+    (void)button;
176
+    return false;
177
+}
178
+
179
+
180
+/// Guards
181
+
182
+#endif
183
+#endif