1 /// Optional window package, mainly to run gfx-d examples
2 module gfx.window;
3 
4 import gfx.core.rc : AtomicRefCounted;
5 import gfx.graal : Backend, Instance;
6 
7 enum gfxWindowLogMask = 0x0800_0000;
8 package immutable gfxWindowLog = LogTag("GFX-WINDOW", gfxWindowLogMask);
9 
10 alias MouseHandler = void delegate(uint x, uint y);
11 alias KeyHandler = void delegate(uint key);
12 alias CloseHandler = bool delegate();
13 
14 interface Display : AtomicRefCounted
15 {
16     @property Instance instance();
17     @property Window[] windows();
18     Window createWindow();
19     void pollAndDispatch();
20 }
21 
22 interface Window
23 {
24     import gfx.graal.presentation : Surface;
25 
26     void show(uint width, uint height);
27     void close();
28 
29     @property Surface surface();
30     @property bool closeFlag() const;
31     @property void closeFlag(in bool flag);
32 
33     @property void onMouseMove(MouseHandler handler);
34     @property void onMouseOn(MouseHandler handler);
35     @property void onMouseOff(MouseHandler handler);
36     @property void onKeyOn(KeyHandler handler);
37     @property void onKeyOff(KeyHandler handler);
38     @property void onClose(CloseHandler handler);
39 }
40 
41 /// The backend load order is the order into which backend load attempts
42 /// will be performed.
43 /// This array provides a default value for createDisplay parameter
44 immutable Backend[] defaultBackendLoadOrder = [
45     Backend.vulkan,
46     Backend.gl3,
47 ];
48 
49 
50 /// Create a display for the running platform.
51 /// The display will load a backend instance during startup.
52 /// It will try the backends in the provided loadOrder
53 Display createDisplay(in Backend[] loadOrder=defaultBackendLoadOrder)
54 {
55     version(linux) {
56         enum useWayland = false;
57         static if (useWayland) {
58             import gfx.window.wayland : WaylandDisplay;
59             return new WaylandDisplay;
60         }
61         else {
62             import gfx.window.xcb : XcbDisplay;
63             return new XcbDisplay(loadOrder);
64         }
65     }
66     else version(Windows) {
67         import gfx.window.win32 : Win32Display;
68         return new Win32Display(loadOrder);
69     }
70     else {
71         pragma(msg, "Unsupported platform");
72         assert(false, "Unsupported platform");
73     }
74 }