annotate semiconginev2/rendering/platform/windows.nim @ 1254:b0f4c8ccd49a

did: stuff to test gltf importer
author sam <sam@basx.dev>
date Sat, 27 Jul 2024 20:47:54 +0700
parents a3fa15c25026
children bfb75c934f4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1224
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
1 import ../../../thirdparty/winim/winim/inc/[windef, winuser, wincon, winbase]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
2 import ../../../thirdparty/winim/winim/[winstr, utils]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
3
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
4 const REQUIRED_PLATFORM_EXTENSIONS = @["VK_KHR_win32_surface"]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
5
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
6 const KeyTypeMap* = {
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
7 VK_ESCAPE: Key.Escape, VK_F1: F1, VK_F2: F2, VK_F3: F3, VK_F4: F4, VK_F5: F5,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
8 VK_F6: F6, VK_F7: F7, VK_F8: F8, VK_F9: F9, VK_F10: F10, VK_F11: F11,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
9 VK_F12: F12,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
10 VK_OEM_3: NumberRowExtra1, int('0'): `0`, int('1'): `1`, int('2'): `2`, int(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
11 '3'): `3`, int('4'): `4`, int('5'): `5`, int('6'): `6`, int('7'): `7`,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
12 int('8'): `8`, int('9'): `9`, VK_OEM_MINUS: NumberRowExtra2,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
13 VK_OEM_PLUS: NumberRowExtra3,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
14 int('A'): A, int('B'): B, int('C'): C, int('D'): D, int('E'): E, int('F'): F,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
15 int('G'): G, int('H'): H, int('I'): I, int('J'): J, int('K'): K, int(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
16 'L'): L, int('M'): M, int('N'): N, int('O'): O, int('P'): P, int('Q'): Q,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
17 int('R'): R, int('S'): S, int('T'): T, int('U'): U, int('V'): V, int(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
18 'W'): W, int('X'): X, int('Y'): Y, int('Z'): Z,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
19 VK_TAB: Tab, VK_CAPITAL: CapsLock, VK_LSHIFT: ShiftL, VK_SHIFT: ShiftL,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
20 VK_RSHIFT: ShiftR, VK_LCONTROL: CtrlL, VK_CONTROL: CtrlL,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
21 VK_RCONTROL: CtrlR, VK_LWIN: SuperL, VK_RWIN: SuperR, VK_LMENU: AltL,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
22 VK_RMENU: AltR, VK_SPACE: Space, VK_RETURN: Enter, VK_BACK: Backspace,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
23 VK_OEM_4: LetterRow1Extra1, VK_OEM_6: LetterRow1Extra2,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
24 VK_OEM_5: LetterRow2Extra3,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
25 VK_OEM_1: LetterRow2Extra1, VK_OEM_7: LetterRow2Extra2,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
26 VK_OEM_COMMA: LetterRow3Extra1, VK_OEM_PERIOD: LetterRow3Extra2,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
27 VK_OEM_2: LetterRow3Extra3,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
28 VK_UP: Up, VK_DOWN: Down, VK_LEFT: Left, VK_RIGHT: Right,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
29 VK_PRIOR: PageUp, VK_NEXT: PageDown, VK_HOME: Home, VK_END: End,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
30 VK_INSERT: Insert, VK_DELETE: Key.Delete,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
31 }.toTable
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
32
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
33 type
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
34 NativeWindow* = object
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
35 hinstance*: HINSTANCE
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
36 hwnd*: HWND
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
37 g_wpPrev: WINDOWPLACEMENT
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
38
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
39
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
40 # sorry, have to use module-global variable to capture windows events
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
41 var currentEvents: seq[Event]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
42
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
43 template CheckWin32Result*(call: untyped) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
44 let value = call
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
45 if value == 0:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
46 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
47
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
48 let
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
49 andCursorMask = [0xff]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
50 xorCursorMask = [0x00]
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
51 invisibleCursor = CreateCursor(0, 0, 0, 1, 1, pointer(addr andCursorMask), pointer(addr xorCursorMask))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
52 defaultCursor = LoadCursor(0, IDC_ARROW)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
53 var currentCursor = defaultCursor
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
54
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
55 proc MapLeftRightKeys(key: INT, lparam: LPARAM): INT =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
56 case key
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
57 of VK_SHIFT:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
58 MapVirtualKey(UINT((lParam and 0x00ff0000) shr 16), MAPVK_VSC_TO_VK_EX)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
59 of VK_CONTROL:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
60 if (lParam and 0x01000000) == 0: VK_LCONTROL else: VK_RCONTROL
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
61 of VK_MENU:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
62 if (lParam and 0x01000000) == 0: VK_LMENU else: VK_RMENU
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
63 else:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
64 key
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
65
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
66 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
67 case uMsg
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
68 of WM_DESTROY:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
69 currentEvents.add(Event(eventType: EventType.Quit))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
70 of WM_KEYDOWN, WM_SYSKEYDOWN:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
71 let key = MapLeftRightKeys(INT(wParam), lParam)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
72 currentEvents.add(Event(eventType: KeyPressed, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN)))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
73 of WM_KEYUP, WM_SYSKEYUP:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
74 let key = MapLeftRightKeys(INT(wParam), lParam)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
75 currentEvents.add(Event(eventType: KeyReleased, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN)))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
76 of WM_LBUTTONDOWN:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
77 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse1))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
78 of WM_LBUTTONUP:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
79 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse1))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
80 of WM_MBUTTONDOWN:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
81 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse2))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
82 of WM_MBUTTONUP:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
83 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse2))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
84 of WM_RBUTTONDOWN:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
85 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse3))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
86 of WM_RBUTTONUP:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
87 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse3))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
88 of WM_MOUSEMOVE:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
89 currentEvents.add(Event(eventType: MouseMoved, x: GET_X_LPARAM(lParam), y: GET_Y_LPARAM(lParam)))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
90 of WM_MOUSEWHEEL:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
91 currentEvents.add(Event(eventType: MouseWheel, amount: float32(GET_WHEEL_DELTA_WPARAM(wParam)) / WHEEL_DELTA))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
92 of WM_SIZING:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
93 currentEvents.add(Event(eventType: ResizedWindow))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
94 of WM_SIZE:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
95 if wParam == SIZE_MINIMIZED:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
96 currentEvents.add(Event(eventType: MinimizedWindow))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
97 elif wParam == SIZE_RESTORED:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
98 currentEvents.add(Event(eventType: RestoredWindow))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
99 of WM_SETCURSOR:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
100 if LOWORD(lParam) == HTCLIENT:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
101 SetCursor(currentCursor)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
102 return 1
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
103 else:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
104 return DefWindowProc(hwnd, uMsg, wParam, lParam)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
105 else:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
106 return DefWindowProc(hwnd, uMsg, wParam, lParam)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
107
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
108
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
109 proc CreateWindow*(title: string): NativeWindow =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
110 when DEBUG:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
111 AllocConsole()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
112 discard stdin.reopen("conIN$", fmRead)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
113 discard stdout.reopen("conOUT$", fmWrite)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
114 discard stderr.reopen("conOUT$", fmWrite)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
115
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
116 result.hInstance = HINSTANCE(GetModuleHandle(nil))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
117 var
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
118 windowClassName = T"EngineWindowClass"
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
119 windowName = T(title)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
120 windowClass = WNDCLASSEX(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
121 cbSize: UINT(WNDCLASSEX.sizeof),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
122 lpfnWndProc: WindowHandler,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
123 hInstance: result.hInstance,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
124 lpszClassName: windowClassName,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
125 hcursor: currentCursor,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
126 )
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
127
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
128 if(RegisterClassEx(addr(windowClass)) == 0):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
129 raise newException(Exception, "Unable to register window class")
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
130
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
131 result.hwnd = CreateWindowEx(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
132 DWORD(0),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
133 windowClassName,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
134 windowName,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
135 DWORD(WS_OVERLAPPEDWINDOW),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
136 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
137 HMENU(0),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
138 HINSTANCE(0),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
139 result.hInstance,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
140 nil
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
141 )
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
142
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
143 result.g_wpPrev.length = UINT(sizeof(WINDOWPLACEMENT))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
144 discard result.hwnd.ShowWindow(SW_SHOW)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
145
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
146 proc SetTitle*(window: NativeWindow, title: string) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
147 window.hwnd.SetWindowText(T(title))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
148
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
149 # inspired by the one and only, Raymond Chen
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
150 # https://devblogs.microsoft.com/oldnewthing/20100412-00/?p=14353
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
151 proc Fullscreen*(window: var NativeWindow, enable: bool) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
152 let dwStyle: DWORD = GetWindowLong(window.hwnd, GWL_STYLE)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
153 if enable:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
154 var mi = MONITORINFO(cbSize: DWORD(sizeof(MONITORINFO)))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
155 if GetWindowPlacement(window.hwnd, addr window.g_wpPrev) and GetMonitorInfo(MonitorFromWindow(window.hwnd, MONITOR_DEFAULTTOPRIMARY), addr mi):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
156 SetWindowLong(window.hwnd, GWL_STYLE, dwStyle and (not WS_OVERLAPPEDWINDOW))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
157 SetWindowPos(window.hwnd, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top, SWP_NOOWNERZORDER or SWP_FRAMECHANGED)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
158 else:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
159 SetWindowLong(window.hwnd, GWL_STYLE, dwStyle or WS_OVERLAPPEDWINDOW)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
160 SetWindowPlacement(window.hwnd, addr window.g_wpPrev)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
161 SetWindowPos(window.hwnd, HWND(0), 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER or SWP_NOOWNERZORDER or SWP_FRAMECHANGED)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
162
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
163 proc HideSystemCursor*(window: NativeWindow) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
164 currentCursor = invisibleCursor
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
165 SetCursor(currentCursor)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
166
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
167 proc ShowSystemCursor*(window: NativeWindow) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
168 currentCursor = defaultCursor
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
169 SetCursor(currentCursor)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
170
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
171 proc Destroy*(window: NativeWindow) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
172 discard
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
173
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
174 proc Size*(window: NativeWindow): (int, int) =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
175 var rect: RECT
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
176 CheckWin32Result GetWindowRect(window.hwnd, addr(rect))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
177 (int(rect.right - rect.left), int(rect.bottom - rect.top))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
178
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
179 proc PendingEvents*(window: NativeWindow): seq[Event] =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
180 # empty queue
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
181 currentEvents = newSeq[Event]()
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
182 var msg: MSG
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
183 # fill queue
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
184 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE):
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
185 TranslateMessage(addr(msg))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
186 DispatchMessage(addr(msg))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
187 return currentEvents
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
188
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
189 proc GetMousePosition*(window: NativeWindow): Option[Vec2f] =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
190 var p: POINT
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
191 let res = GetCursorPos(addr(p))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
192 if res:
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
193 return some(Vec2f([float32(p.x), float32(p.y)]))
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
194 return none(Vec2f)
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
195
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1224
diff changeset
196 proc SetMousePosition*(window: NativeWindow, x, y: int) =
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1224
diff changeset
197 CheckWin32Result SetCursorPos(x, y)
1224
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
198
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
199 proc CreateNativeSurface*(instance: VkInstance, window: NativeWindow): VkSurfaceKHR =
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
200 assert instance.Valid
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
201 var surfaceCreateInfo = VkWin32SurfaceCreateInfoKHR(
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
202 sType: VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
203 hinstance: cast[HINSTANCE](window.hinstance),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
204 hwnd: cast[HWND](window.hwnd),
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
205 )
a3fa15c25026 did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff changeset
206 checkVkResult vkCreateWin32SurfaceKHR(instance, addr(surfaceCreateInfo), nil, addr(result))