annotate src/zamikongine/platform/windows/win32.nim @ 484:04605ca6dd8a

add: some comments for assets
author Sam <sam@basx.dev>
date Wed, 11 Jan 2023 00:43:43 +0700
parents 14e5151f68d1
children 85f6b1b29c98
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
1 import winim
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
2
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
3 import ../../events
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
4
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
5 type
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
6 NativeWindow* = object
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
7 hinstance*: HINSTANCE
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
8 hwnd*: HWND
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
9
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
10 var currentEvents: seq[Event]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
11
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
12 template checkWin32Result*(call: untyped) =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
13 let value = call
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
14 if value != 0:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
15 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
16
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
17 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
18 case uMsg
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
19 of WM_DESTROY:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
20 currentEvents.add(Event(eventType: events.EventType.Quit))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
21 else:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
22 return DefWindowProc(hwnd, uMsg, wParam, lParam)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
23
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
24
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
25 proc createWindow*(title: string): NativeWindow =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
26 result.hInstance = HINSTANCE(GetModuleHandle(nil))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
27 var
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
28 windowClassName = T"EngineWindowClass"
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
29 windowName = T(title)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
30 windowClass = WNDCLASSEX(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
31 cbSize: UINT(WNDCLASSEX.sizeof),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
32 lpfnWndProc: WindowHandler,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
33 hInstance: result.hInstance,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
34 lpszClassName: windowClassName,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
35 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
36
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
37 if(RegisterClassEx(addr(windowClass)) == 0):
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
38 raise newException(Exception, "Unable to register window class")
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
39
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
40 result.hwnd = CreateWindowEx(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
41 DWORD(0),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
42 windowClassName,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
43 windowName,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
44 DWORD(WS_OVERLAPPEDWINDOW),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
45 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
46 HMENU(0),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
47 HINSTANCE(0),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
48 result.hInstance,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
49 nil
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
50 )
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
51
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
52 discard ShowWindow(result.hwnd, 1)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
53
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
54 proc trash*(window: NativeWindow) =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
55 discard
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
56
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
57 proc size*(window: NativeWindow): (int, int) =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
58 var rect: RECT
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
59 checkWin32Result GetWindowRect(window.hwnd, addr(rect))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
60 (int(rect.right - rect.left), int(rect.bottom - rect.top))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
61
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
62 proc pendingEvents*(window: NativeWindow): seq[Event] =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
63 currentEvents = newSeq[Event]()
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
64 var msg: MSG
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
65 while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE):
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
66 DispatchMessage(addr(msg))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
67 return currentEvents