annotate src/platform/windows/win32.nim @ 474:d4750f2e0760

add: vector/matrix modules, tests, nim config script
author Sam <sam@basx.dev>
date Wed, 28 Dec 2022 11:42:21 +0700
parents 9eeaeb35eb1c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
1 import winim
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
2
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
3 import ../../events
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
4
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
5 type
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
6 NativeWindow* = object
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
7 hinstance*: HINSTANCE
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
8 hwnd*: HWND
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
9
471
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
10 var currentEvents: seq[Event]
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
11
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
12 template checkWin32Result*(call: untyped) =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
13 let value = call
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
14 if value != 0:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
15 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
16
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
17 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
18 case uMsg
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
19 of WM_DESTROY:
471
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
20 currentEvents.add(Event(eventType: events.EventType.Quit))
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
21 else:
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
22 return DefWindowProc(hwnd, uMsg, wParam, lParam)
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
23
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
24
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
25 proc createWindow*(title: string): NativeWindow =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
26 result.hInstance = HINSTANCE(GetModuleHandle(nil))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
27 var
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
28 windowClassName = T"EngineWindowClass"
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
29 windowName = T(title)
471
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
30 windowClass = WNDCLASSEX(
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
31 cbSize: UINT(WNDCLASSEX.sizeof),
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
32 lpfnWndProc: WindowHandler,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
33 hInstance: result.hInstance,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
34 lpszClassName: windowClassName,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
35 )
471
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
36
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
37 if(RegisterClassEx(addr(windowClass)) == 0):
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
38 raise newException(Exception, "Unable to register window class")
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
39
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
40 result.hwnd = CreateWindowEx(
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
41 DWORD(0),
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
42 windowClassName,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
43 windowName,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
44 DWORD(WS_OVERLAPPEDWINDOW),
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
45 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
46 HMENU(0),
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
47 HINSTANCE(0),
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
48 result.hInstance,
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
49 nil
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
50 )
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
51
471
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
52 discard ShowWindow(result.hwnd, 1)
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
53
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
54 proc trash*(window: NativeWindow) =
471
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
55 discard
466
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
56
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
57 proc size*(window: NativeWindow): (int, int) =
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
58 var rect: RECT
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
59 checkWin32Result GetWindowRect(window.hwnd, addr(rect))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
60 (int(rect.right - rect.left), int(rect.bottom - rect.top))
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
61
1dd9e2393a9e add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
62 proc pendingEvents*(window: NativeWindow): seq[Event] =
471
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
63 currentEvents = newSeq[Event]()
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
64 var msg: MSG
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
65 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE):
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
66 DispatchMessage(addr(msg))
9eeaeb35eb1c did: make it work on windows
sam <sam@basx.dev>
parents: 466
diff changeset
67 return currentEvents