Mercurial > games > semicongine
comparison src/zamikongine/platform/windows/win32.nim @ 480:14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
| author | Sam <sam@basx.dev> |
|---|---|
| date | Mon, 09 Jan 2023 11:04:19 +0700 |
| parents | |
| children | 85f6b1b29c98 |
comparison
equal
deleted
inserted
replaced
| 479:16842d15319a | 480:14e5151f68d1 |
|---|---|
| 1 import winim | |
| 2 | |
| 3 import ../../events | |
| 4 | |
| 5 type | |
| 6 NativeWindow* = object | |
| 7 hinstance*: HINSTANCE | |
| 8 hwnd*: HWND | |
| 9 | |
| 10 var currentEvents: seq[Event] | |
| 11 | |
| 12 template checkWin32Result*(call: untyped) = | |
| 13 let value = call | |
| 14 if value != 0: | |
| 15 raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value) | |
| 16 | |
| 17 proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} = | |
| 18 case uMsg | |
| 19 of WM_DESTROY: | |
| 20 currentEvents.add(Event(eventType: events.EventType.Quit)) | |
| 21 else: | |
| 22 return DefWindowProc(hwnd, uMsg, wParam, lParam) | |
| 23 | |
| 24 | |
| 25 proc createWindow*(title: string): NativeWindow = | |
| 26 result.hInstance = HINSTANCE(GetModuleHandle(nil)) | |
| 27 var | |
| 28 windowClassName = T"EngineWindowClass" | |
| 29 windowName = T(title) | |
| 30 windowClass = WNDCLASSEX( | |
| 31 cbSize: UINT(WNDCLASSEX.sizeof), | |
| 32 lpfnWndProc: WindowHandler, | |
| 33 hInstance: result.hInstance, | |
| 34 lpszClassName: windowClassName, | |
| 35 ) | |
| 36 | |
| 37 if(RegisterClassEx(addr(windowClass)) == 0): | |
| 38 raise newException(Exception, "Unable to register window class") | |
| 39 | |
| 40 result.hwnd = CreateWindowEx( | |
| 41 DWORD(0), | |
| 42 windowClassName, | |
| 43 windowName, | |
| 44 DWORD(WS_OVERLAPPEDWINDOW), | |
| 45 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, | |
| 46 HMENU(0), | |
| 47 HINSTANCE(0), | |
| 48 result.hInstance, | |
| 49 nil | |
| 50 ) | |
| 51 | |
| 52 discard ShowWindow(result.hwnd, 1) | |
| 53 | |
| 54 proc trash*(window: NativeWindow) = | |
| 55 discard | |
| 56 | |
| 57 proc size*(window: NativeWindow): (int, int) = | |
| 58 var rect: RECT | |
| 59 checkWin32Result GetWindowRect(window.hwnd, addr(rect)) | |
| 60 (int(rect.right - rect.left), int(rect.bottom - rect.top)) | |
| 61 | |
| 62 proc pendingEvents*(window: NativeWindow): seq[Event] = | |
| 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 |
