comparison 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
comparison
equal deleted inserted replaced
465:2fcb9268072b 466:1dd9e2393a9e
1 import winim
2
3 import ../../events
4
5 type
6 NativeWindow* = object
7 hinstance*: HINSTANCE
8 hwnd*: HWND
9
10 template checkWin32Result*(call: untyped) =
11 let value = call
12 if value != 0:
13 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
14
15 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
16 case uMsg
17 of WM_DESTROY:
18 discard
19 else:
20 return DefWindowProc(hwnd, uMsg, wParam, lParam)
21
22
23 proc createWindow*(title: string): NativeWindow =
24 result.hInstance = HINSTANCE(GetModuleHandle(nil))
25 var
26 windowClassName = T"EngineWindowClass"
27 windowName = T(title)
28 windowClass = WNDCLASS(
29 lpfnWndProc: WindowHandler,
30 hInstance: result.hInstance,
31 lpszClassName: windowClassName,
32 )
33 RegisterClass(addr(windowClass))
34
35 result.hwnd = CreateWindowEx(
36 DWORD(0),
37 windowClassName,
38 windowName,
39 DWORD(WS_OVERLAPPEDWINDOW),
40 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
41 HMENU(0),
42 HINSTANCE(0),
43 result.hInstance,
44 nil
45 )
46
47 discard ShowWindow(result.hwnd, 0)
48
49 proc trash*(window: NativeWindow) =
50 PostQuitMessage(0)
51
52 proc size*(window: NativeWindow): (int, int) =
53 var rect: RECT
54 checkWin32Result GetWindowRect(window.hwnd, addr(rect))
55 (int(rect.right - rect.left), int(rect.bottom - rect.top))
56
57 proc pendingEvents*(window: NativeWindow): seq[Event] =
58 result