Mercurial > games > semicongine
annotate semiconginev2/rendering/platform/windows.nim @ 1256:bfb75c934f4e
add: window focus handling, improve window api a bit
author | sam <sam@basx.dev> |
---|---|
date | Sun, 28 Jul 2024 17:33:41 +0700 |
parents | b0f4c8ccd49a |
children |
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)) |
1256
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
99 of WM_SETFOCUS: |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
100 currentEvents.add(Event(eventType: GotFocus)) |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
101 of WM_KILLFOCUS: |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
102 currentEvents.add(Event(eventType: LostFocus)) |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
103 of WM_SETCURSOR: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
104 if LOWORD(lParam) == HTCLIENT: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
105 SetCursor(currentCursor) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
106 return 1 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
107 else: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
108 return DefWindowProc(hwnd, uMsg, wParam, lParam) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
109 else: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
110 return DefWindowProc(hwnd, uMsg, wParam, lParam) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
111 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
112 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
113 proc CreateWindow*(title: string): NativeWindow = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
114 when DEBUG: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
115 AllocConsole() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
116 discard stdin.reopen("conIN$", fmRead) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
117 discard stdout.reopen("conOUT$", fmWrite) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
118 discard stderr.reopen("conOUT$", fmWrite) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
119 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
120 result.hInstance = HINSTANCE(GetModuleHandle(nil)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
121 var |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
122 windowClassName = T"EngineWindowClass" |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
123 windowName = T(title) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
124 windowClass = WNDCLASSEX( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
125 cbSize: UINT(WNDCLASSEX.sizeof), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
126 lpfnWndProc: WindowHandler, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
127 hInstance: result.hInstance, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
128 lpszClassName: windowClassName, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
129 hcursor: currentCursor, |
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 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
132 if(RegisterClassEx(addr(windowClass)) == 0): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
133 raise newException(Exception, "Unable to register window class") |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
134 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
135 result.hwnd = CreateWindowEx( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
136 DWORD(0), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
137 windowClassName, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
138 windowName, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
139 DWORD(WS_OVERLAPPEDWINDOW), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
140 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
141 HMENU(0), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
142 HINSTANCE(0), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
143 result.hInstance, |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
144 nil |
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 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
147 result.g_wpPrev.length = UINT(sizeof(WINDOWPLACEMENT)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
148 discard result.hwnd.ShowWindow(SW_SHOW) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
149 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
150 proc SetTitle*(window: NativeWindow, title: string) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
151 window.hwnd.SetWindowText(T(title)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
152 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
153 # inspired by the one and only, Raymond Chen |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
154 # https://devblogs.microsoft.com/oldnewthing/20100412-00/?p=14353 |
1256
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
155 proc SetFullscreen*(window: var NativeWindow, enable: bool) = |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
156 let dwStyle: DWORD = GetWindowLong(window.hwnd, GWL_STYLE) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
157 if enable: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
158 var mi = MONITORINFO(cbSize: DWORD(sizeof(MONITORINFO))) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 else: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
163 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
|
164 SetWindowPlacement(window.hwnd, addr window.g_wpPrev) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
165 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
|
166 |
1256
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
167 proc ShowSystemCursor*(window: NativeWindow, value: bool) = |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
168 if value == true: |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
169 currentCursor = defaultCursor |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
170 SetCursor(currentCursor) |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
171 else: |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
172 currentCursor = invisibleCursor |
bfb75c934f4e
add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents:
1254
diff
changeset
|
173 SetCursor(currentCursor) |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
174 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
175 proc Destroy*(window: NativeWindow) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
176 discard |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
177 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
178 proc Size*(window: NativeWindow): (int, int) = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
179 var rect: RECT |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
180 CheckWin32Result GetWindowRect(window.hwnd, addr(rect)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
181 (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
|
182 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
183 proc PendingEvents*(window: NativeWindow): seq[Event] = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
184 # empty queue |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
185 currentEvents = newSeq[Event]() |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
186 var msg: MSG |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
187 # fill queue |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
188 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
|
189 TranslateMessage(addr(msg)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
190 DispatchMessage(addr(msg)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
191 return currentEvents |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
192 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
193 proc GetMousePosition*(window: NativeWindow): Option[Vec2f] = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
194 var p: POINT |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
195 let res = GetCursorPos(addr(p)) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
196 if res: |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
197 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
|
198 return none(Vec2f) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
199 |
1254 | 200 proc SetMousePosition*(window: NativeWindow, x, y: int) = |
201 CheckWin32Result SetCursorPos(x, y) | |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
202 |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
203 proc CreateNativeSurface*(instance: VkInstance, window: NativeWindow): VkSurfaceKHR = |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
204 assert instance.Valid |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
205 var surfaceCreateInfo = VkWin32SurfaceCreateInfoKHR( |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
206 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
|
207 hinstance: cast[HINSTANCE](window.hinstance), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
208 hwnd: cast[HWND](window.hwnd), |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
209 ) |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
diff
changeset
|
210 checkVkResult vkCreateWin32SurfaceKHR(instance, addr(surfaceCreateInfo), nil, addr(result)) |