annotate src/platform/windows/win32.nim @ 5:4ed9cb098315

add: structure code for crossplatform, add some input handling + bugfixes
author Sam <sam@basx.dev>
date Thu, 22 Dec 2022 00:06:40 +0700
parents
children 0660ba9d1930
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
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
3 import ../../events
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
4
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
5 type
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
6 NativeWindow* = object
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
7 hinstance*: HINSTANCE
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
8 hwnd*: HWND
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
9
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
10 template checkWin32Result*(call: untyped) =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
11 let value = call
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
12 if value != 0:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
13 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
14
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
15 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
16 case uMsg
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
17 of WM_DESTROY:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
18 discard
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
19 else:
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
20 return DefWindowProc(hwnd, uMsg, wParam, lParam)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
21
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
22
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
23 proc createWindow*(title: string): NativeWindow =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
24 result.hInstance = HINSTANCE(GetModuleHandle(nil))
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
25 var
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
26 windowClassName = T"EngineWindowClass"
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
27 windowName = T(title)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
28 windowClass = WNDCLASS(
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
29 lpfnWndProc: WindowHandler,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
30 hInstance: result.hInstance,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
31 lpszClassName: windowClassName,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
32 )
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
33 RegisterClass(addr(windowClass))
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
34
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
35 result.hwnd = CreateWindowEx(
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
36 DWORD(0),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
37 windowClassName,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
38 windowName,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
39 DWORD(WS_OVERLAPPEDWINDOW),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
40 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
41 HMENU(0),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
42 HINSTANCE(0),
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
43 result.hInstance,
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
44 nil
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
45 )
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
46
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
47 discard ShowWindow(result.hwnd, 0)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
48
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
49 proc trash*(window: NativeWindow) =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
50 PostQuitMessage(0)
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
51
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
52 proc size*(window: NativeWindow): (int, int) =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
53 var rect: RECT
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
54 checkWin32Result GetWindowRect(window.hwnd, addr(rect))
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
55 (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
56
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
57 proc pendingEvents*(window: NativeWindow): seq[Event] =
4ed9cb098315 add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff changeset
58 result