yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Anders LeinoAdd backtraces to examples (#5973)5b9931456

master
5.2 KiB255 linesraw
1// window.h
2#pragma once
3
4#include "core/slang-basic.h"
5#include "core/slang-func-ptr.h"
6#include "platform-api.h"
7#include "slang-com-ptr.h"
8
9namespace platform
10{
11
12enum class KeyCode : uint32_t
13{
14    None = 0,
15    Left = 0x25,
16    Up = 0x26,
17    Down = 0x28,
18    Right = 0x27,
19    Escape = 0x1B,
20    Return = 0x0D,
21    Space = 0x20,
22    Shift = 0x10,
23    Ctrl = 0x11,
24    Alt = 0x12,
25    Backspace = 0x08,
26    Delete = 0x2E,
27    Home = 0x24,
28    End = 0x23,
29    PageUp = 0x21,
30    PageDown = 0x22,
31    Insert = 0x2D,
32    Tab = 0x09,
33    A = 0x41,
34    B = 0x42,
35    C = 0x43,
36    D = 0x44,
37    E = 0x45,
38    F = 0x46,
39    G = 0x47,
40    H = 0x48,
41    I = 0x49,
42    J = 0x4A,
43    K = 0x4B,
44    L = 0x4C,
45    M = 0x4D,
46    N = 0x4E,
47    O = 0x4F,
48    P = 0x50,
49    Q = 0x51,
50    R = 0x52,
51    S = 0x53,
52    T = 0x54,
53    U = 0x55,
54    V = 0x56,
55    W = 0x57,
56    X = 0x58,
57    Y = 0x59,
58    Z = 0x5A,
59    Semicolon = 0xBA,
60    Comma = 0xBC,
61    Dot = 0xBE,
62    Slash = 0xBF,
63    Quote = 0xDE,
64    LBracket = 0xDB,
65    RBracket = 0xDD,
66    Backslash = 0xDC,
67    Minus = 0xBD,
68    Plus = 0xBB,
69    Tilde = 0xC0,
70    Key0 = 0x30,
71    Key1 = 0x31,
72    Key2 = 0x32,
73    Key3 = 0x33,
74    Key4 = 0x34,
75    Key5 = 0x35,
76    Key6 = 0x36,
77    Key7 = 0x37,
78    Key8 = 0x38,
79    Key9 = 0x39,
80    F1 = 0x70,
81    F2 = 0x71,
82    F3 = 0x72,
83    F4 = 0x73,
84    F5 = 0x74,
85    F6 = 0x75,
86    F7 = 0x76,
87    F8 = 0x77,
88    F9 = 0x78,
89    F10 = 0x79,
90    F11 = 0x7A,
91    F12 = 0x7B,
92};
93
94struct WindowHandle
95{
96    enum class Type
97    {
98        Unknown,
99        Win32Handle,
100        NSWindowHandle,
101        XLibHandle,
102    };
103    Type type;
104    intptr_t handleValues[2];
105    static WindowHandle fromHwnd(void* hwnd)
106    {
107        WindowHandle handle = {};
108        handle.type = WindowHandle::Type::Win32Handle;
109        handle.handleValues[0] = (intptr_t)(hwnd);
110        return handle;
111    }
112    static WindowHandle fromNSWindow(void* nswindow)
113    {
114        WindowHandle handle = {};
115        handle.type = WindowHandle::Type::NSWindowHandle;
116        handle.handleValues[0] = (intptr_t)(nswindow);
117        return handle;
118    }
119    static WindowHandle fromXWindow(void* xdisplay, uint32_t xwindow)
120    {
121        WindowHandle handle = {};
122        handle.type = WindowHandle::Type::XLibHandle;
123        handle.handleValues[0] = (intptr_t)(xdisplay);
124        handle.handleValues[1] = xwindow;
125        return handle;
126    }
127    template<typename T>
128    T convert()
129    {
130        T result;
131        result.type = (decltype(result.type))type;
132        result.handleValues[0] = handleValues[0];
133        result.handleValues[1] = handleValues[1];
134        return result;
135    }
136};
137
138struct ButtonState
139{
140    enum Enum
141    {
142        None = 0,
143        LeftButton = 1,
144        RightButton = 2,
145        MiddleButton = 4,
146        Shift = 8,
147        Control = 16,
148        Alt = 32
149    };
150};
151
152struct KeyEventArgs
153{
154    KeyCode key;
155    wchar_t keyChar; // For KeyPress event
156    ButtonState::Enum buttons;
157    bool cancelEvent;
158};
159
160struct MouseEventArgs
161{
162    int x, y;
163    int delta;
164    ButtonState::Enum buttons;
165};
166
167struct Rect
168{
169    int x, y;
170    int width, height;
171};
172
173enum class WindowStyle
174{
175    Default,
176    FixedSize,
177};
178
179struct WindowDesc
180{
181    char const* title = nullptr;
182    int width = 0;
183    int height = 0;
184    WindowStyle style = WindowStyle::Default;
185};
186
187class Window : public Slang::RefObject
188{
189public:
190    struct Events
191    {
192        Slang::Action<> mainLoop;
193        Slang::Action<> sizeChanged;
194        Slang::Action<> focus;
195        Slang::Action<> lostFocus;
196        Slang::Action<KeyEventArgs&> keyDown;
197        Slang::Action<KeyEventArgs&> keyUp;
198        Slang::Action<KeyEventArgs&> keyPress;
199        Slang::Action<MouseEventArgs> mouseMove;
200        Slang::Action<MouseEventArgs> mouseUp;
201        Slang::Action<MouseEventArgs> mouseDown;
202        Slang::Action<MouseEventArgs> mouseWheel;
203    };
204
205    Events events;
206
207    virtual void setClientSize(uint32_t width, uint32_t height) = 0;
208    virtual Rect getClientRect() = 0;
209    virtual void centerScreen() = 0;
210    virtual void close() = 0;
211    virtual bool getFocused() = 0;
212    virtual bool getVisible() = 0;
213    virtual WindowHandle getNativeHandle() = 0;
214    virtual void setText(Slang::String text) = 0;
215    virtual void show() = 0;
216    virtual void hide() = 0;
217    virtual int getCurrentDpi() = 0;
218};
219
220class Application
221{
222public:
223    static SLANG_PLATFORM_API Window* createWindow(const WindowDesc& desc);
224    static SLANG_PLATFORM_API void init();
225    static SLANG_PLATFORM_API void run(Window* mainWindow, bool waitForEvents = false);
226    static SLANG_PLATFORM_API void quit();
227    static SLANG_PLATFORM_API void doEvents();
228    static SLANG_PLATFORM_API void dispose();
229};
230
231} // namespace platform
232
233#ifdef _WIN32
234
235#ifdef _MSC_VER
236#ifdef _DEBUG
237#define GFX_DUMP_LEAK _CrtDumpMemoryLeaks();
238#endif
239#endif
240
241#endif
242
243#ifndef GFX_DUMP_LEAK
244#define GFX_DUMP_LEAK
245#endif
246
247#define PLATFORM_UI_MAIN(APPLICATION_ENTRY)      \
248    int exampleMain(int argc, char** argv)       \
249    {                                            \
250        platform::Application::init();           \
251        auto rs = APPLICATION_ENTRY(argc, argv); \
252        platform::Application::dispose();        \
253        GFX_DUMP_LEAK                            \
254        return rs;                               \
255    }