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