Mercurial > games > semicongine
annotate src/zamikongine/platform/windows/win32.nim @ 26:8b4bc545958d
did: update gitignore
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 11 Jan 2023 11:43:59 +0700 |
parents | b55d6ecde79d |
children | a3d46f434616 |
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 |
10 | 10 var currentEvents: seq[Event] |
11 | |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 template checkWin32Result*(call: untyped) = |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 let value = call |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 if value != 0: |
4ed9cb098315
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) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 |
4ed9cb098315
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.} = |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 case uMsg |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 of WM_DESTROY: |
10 | 20 currentEvents.add(Event(eventType: events.EventType.Quit)) |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 else: |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 return DefWindowProc(hwnd, uMsg, wParam, lParam) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 proc createWindow*(title: string): NativeWindow = |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 result.hInstance = HINSTANCE(GetModuleHandle(nil)) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 var |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 windowClassName = T"EngineWindowClass" |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 windowName = T(title) |
10 | 30 windowClass = WNDCLASSEX( |
31 cbSize: UINT(WNDCLASSEX.sizeof), | |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 lpfnWndProc: WindowHandler, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 hInstance: result.hInstance, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 lpszClassName: windowClassName, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 ) |
10 | 36 |
37 if(RegisterClassEx(addr(windowClass)) == 0): | |
38 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
|
39 |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 result.hwnd = CreateWindowEx( |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 DWORD(0), |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 windowClassName, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 windowName, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 DWORD(WS_OVERLAPPEDWINDOW), |
4ed9cb098315
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, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 HMENU(0), |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 HINSTANCE(0), |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 result.hInstance, |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 nil |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 ) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 |
10 | 52 discard ShowWindow(result.hwnd, 1) |
5
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 proc trash*(window: NativeWindow) = |
10 | 55 discard |
5
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 size*(window: NativeWindow): (int, int) = |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 var rect: RECT |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 checkWin32Result GetWindowRect(window.hwnd, addr(rect)) |
4ed9cb098315
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)) |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 |
4ed9cb098315
add: structure code for crossplatform, add some input handling + bugfixes
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 proc pendingEvents*(window: NativeWindow): seq[Event] = |
10 | 63 currentEvents = newSeq[Event]() |
64 var msg: MSG | |
65 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE): | |
66 DispatchMessage(addr(msg)) | |
67 return currentEvents |