comparison src/platform/windows/win32.nim @ 10:0660ba9d1930

did: make it work on windows
author sam <sam@basx.dev>
date Sat, 24 Dec 2022 22:32:46 +0700
parents 4ed9cb098315
children
comparison
equal deleted inserted replaced
8:1134f41a49e9 10:0660ba9d1930
5 type 5 type
6 NativeWindow* = object 6 NativeWindow* = object
7 hinstance*: HINSTANCE 7 hinstance*: HINSTANCE
8 hwnd*: HWND 8 hwnd*: HWND
9 9
10 var currentEvents: seq[Event]
11
10 template checkWin32Result*(call: untyped) = 12 template checkWin32Result*(call: untyped) =
11 let value = call 13 let value = call
12 if value != 0: 14 if value != 0:
13 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value) 15 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
14 16
15 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} = 17 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
16 case uMsg 18 case uMsg
17 of WM_DESTROY: 19 of WM_DESTROY:
18 discard 20 currentEvents.add(Event(eventType: events.EventType.Quit))
19 else: 21 else:
20 return DefWindowProc(hwnd, uMsg, wParam, lParam) 22 return DefWindowProc(hwnd, uMsg, wParam, lParam)
21 23
22 24
23 proc createWindow*(title: string): NativeWindow = 25 proc createWindow*(title: string): NativeWindow =
24 result.hInstance = HINSTANCE(GetModuleHandle(nil)) 26 result.hInstance = HINSTANCE(GetModuleHandle(nil))
25 var 27 var
26 windowClassName = T"EngineWindowClass" 28 windowClassName = T"EngineWindowClass"
27 windowName = T(title) 29 windowName = T(title)
28 windowClass = WNDCLASS( 30 windowClass = WNDCLASSEX(
31 cbSize: UINT(WNDCLASSEX.sizeof),
29 lpfnWndProc: WindowHandler, 32 lpfnWndProc: WindowHandler,
30 hInstance: result.hInstance, 33 hInstance: result.hInstance,
31 lpszClassName: windowClassName, 34 lpszClassName: windowClassName,
32 ) 35 )
33 RegisterClass(addr(windowClass)) 36
37 if(RegisterClassEx(addr(windowClass)) == 0):
38 raise newException(Exception, "Unable to register window class")
34 39
35 result.hwnd = CreateWindowEx( 40 result.hwnd = CreateWindowEx(
36 DWORD(0), 41 DWORD(0),
37 windowClassName, 42 windowClassName,
38 windowName, 43 windowName,
42 HINSTANCE(0), 47 HINSTANCE(0),
43 result.hInstance, 48 result.hInstance,
44 nil 49 nil
45 ) 50 )
46 51
47 discard ShowWindow(result.hwnd, 0) 52 discard ShowWindow(result.hwnd, 1)
48 53
49 proc trash*(window: NativeWindow) = 54 proc trash*(window: NativeWindow) =
50 PostQuitMessage(0) 55 discard
51 56
52 proc size*(window: NativeWindow): (int, int) = 57 proc size*(window: NativeWindow): (int, int) =
53 var rect: RECT 58 var rect: RECT
54 checkWin32Result GetWindowRect(window.hwnd, addr(rect)) 59 checkWin32Result GetWindowRect(window.hwnd, addr(rect))
55 (int(rect.right - rect.left), int(rect.bottom - rect.top)) 60 (int(rect.right - rect.left), int(rect.bottom - rect.top))
56 61
57 proc pendingEvents*(window: NativeWindow): seq[Event] = 62 proc pendingEvents*(window: NativeWindow): seq[Event] =
58 result 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