annotate src/zamikongine/platform/windows/win32.nim @ 39:0cb294c5d2fd

fix: hello cube, add: run_all command
author Sam <sam@basx.dev>
date Wed, 18 Jan 2023 13:49:12 +0700
parents c3c963e7c1a6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
1 import winim
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
2
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
3 import ./virtualkey_map
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
4 import ../../events
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
6 type
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
7 NativeWindow* = object
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
8 hinstance*: HINSTANCE
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
9 hwnd*: HWND
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
10
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
11 # sorry, have to use module-global variable to capture windows events
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
12 var currentEvents: seq[Event]
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
13
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
14 template checkWin32Result*(call: untyped) =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
15 let value = call
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
16 if value != 0:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
17 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
18
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
19
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
20 proc MapLeftRightKeys(key: INT, lparam: LPARAM): INT =
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
21 case key
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
22 of VK_SHIFT:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
23 MapVirtualKey(UINT((lParam and 0x00ff0000) shr 16), MAPVK_VSC_TO_VK_EX)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
24 of VK_CONTROL:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
25 if (lParam and 0x01000000) == 0: VK_LCONTROL else: VK_RCONTROL
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
26 of VK_MENU:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
27 if (lParam and 0x01000000) == 0: VK_LMENU else: VK_RMENU
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
28 else:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
29 key
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
30
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
31 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
32 case uMsg
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
33 of WM_DESTROY:
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
34 currentEvents.add(Event(eventType: events.EventType.Quit))
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
35 of WM_KEYDOWN, WM_SYSKEYDOWN:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
36 let key = MapLeftRightKeys(INT(wParam), lParam)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
37 currentEvents.add(Event(eventType: KeyPressed, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN)))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
38 of WM_KEYUP, WM_SYSKEYUP:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
39 let key = MapLeftRightKeys(INT(wParam), lParam)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
40 currentEvents.add(Event(eventType: KeyReleased, key: KeyTypeMap.getOrDefault(key, Key.UNKNOWN)))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
41 of WM_LBUTTONDOWN:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
42 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse1))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
43 of WM_LBUTTONUP:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
44 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse1))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
45 of WM_MBUTTONDOWN:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
46 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse2))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
47 of WM_MBUTTONUP:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
48 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse2))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
49 of WM_RBUTTONDOWN:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
50 currentEvents.add(Event(eventType: MousePressed, button: MouseButton.Mouse3))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
51 of WM_RBUTTONUP:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
52 currentEvents.add(Event(eventType: MouseReleased, button: MouseButton.Mouse3))
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
53 of WM_MOUSEMOVE:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
54 currentEvents.add(Event(eventType: events.MouseMoved, x: GET_X_LPARAM(lParam), y: GET_Y_LPARAM(lParam)))
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
55 else:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
56 return DefWindowProc(hwnd, uMsg, wParam, lParam)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
57
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
58
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
59 proc createWindow*(title: string): NativeWindow =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
60 result.hInstance = HINSTANCE(GetModuleHandle(nil))
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
61 var
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
62 windowClassName = T"EngineWindowClass"
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
63 windowName = T(title)
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
64 windowClass = WNDCLASSEX(
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
65 cbSize: UINT(WNDCLASSEX.sizeof),
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
66 lpfnWndProc: WindowHandler,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
67 hInstance: result.hInstance,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
68 lpszClassName: windowClassName,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
69 )
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
70
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
71 if(RegisterClassEx(addr(windowClass)) == 0):
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
72 raise newException(Exception, "Unable to register window class")
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
73
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
74 result.hwnd = CreateWindowEx(
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
75 DWORD(0),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
76 windowClassName,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
77 windowName,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
78 DWORD(WS_OVERLAPPEDWINDOW),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
79 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
80 HMENU(0),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
81 HINSTANCE(0),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
82 result.hInstance,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
83 nil
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
84 )
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
85
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
86 discard ShowWindow(result.hwnd, SW_SHOW)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
87 discard ShowCursor(false)
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
88
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
89 proc trash*(window: NativeWindow) =
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
90 discard
5
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
91
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
92 proc size*(window: NativeWindow): (int, int) =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
93 var rect: RECT
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
94 checkWin32Result GetWindowRect(window.hwnd, addr(rect))
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
95 (int(rect.right - rect.left), int(rect.bottom - rect.top))
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
96
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
97 proc pendingEvents*(window: NativeWindow): seq[Event] =
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
98 # empty queue
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
99 currentEvents = newSeq[Event]()
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
100 var msg: MSG
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 34
diff changeset
101 # fill queue
10
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
102 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE):
0660ba9d1930 did: make it work on windows
sam <sam@basx.dev>
parents: 5
diff changeset
103 DispatchMessage(addr(msg))
34
a3d46f434616 fix: newline
Sam <sam@basx.dev>
parents: 19
diff changeset
104 return currentEvents