Mercurial > games > semicongine
annotate src/platform/windows/win32.nim @ 466:1dd9e2393a9e
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 |
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 |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 template checkWin32Result*(call: untyped) = |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 let value = call |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 if value != 0: |
1dd9e2393a9e
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) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 |
1dd9e2393a9e
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.} = |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 case uMsg |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 of WM_DESTROY: |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 discard |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 else: |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 return DefWindowProc(hwnd, uMsg, wParam, lParam) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 proc createWindow*(title: string): NativeWindow = |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 result.hInstance = HINSTANCE(GetModuleHandle(nil)) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 var |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 windowClassName = T"EngineWindowClass" |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 windowName = T(title) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 windowClass = WNDCLASS( |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 lpfnWndProc: WindowHandler, |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 hInstance: result.hInstance, |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 lpszClassName: windowClassName, |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 ) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 RegisterClass(addr(windowClass)) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 result.hwnd = CreateWindowEx( |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 DWORD(0), |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 windowClassName, |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 windowName, |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 DWORD(WS_OVERLAPPEDWINDOW), |
1dd9e2393a9e
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, |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 HMENU(0), |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 HINSTANCE(0), |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 result.hInstance, |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 nil |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 ) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 discard ShowWindow(result.hwnd, 0) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 proc trash*(window: NativeWindow) = |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 PostQuitMessage(0) |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 proc size*(window: NativeWindow): (int, int) = |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 var rect: RECT |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 checkWin32Result GetWindowRect(window.hwnd, addr(rect)) |
1dd9e2393a9e
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)) |
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 pendingEvents*(window: NativeWindow): seq[Event] = |
1dd9e2393a9e
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 result |