diff src/zamikongine/platform/windows/win32.nim @ 19:b55d6ecde79d

did: introduce scene graph, meshs and generic vertex buffers
author Sam <sam@basx.dev>
date Mon, 09 Jan 2023 11:04:19 +0700
parents src/platform/windows/win32.nim@0660ba9d1930
children a3d46f434616
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/zamikongine/platform/windows/win32.nim	Mon Jan 09 11:04:19 2023 +0700
@@ -0,0 +1,67 @@
+import winim
+
+import ../../events
+
+type
+  NativeWindow* = object
+    hinstance*: HINSTANCE
+    hwnd*: HWND
+
+var currentEvents: seq[Event]
+
+template checkWin32Result*(call: untyped) =
+  let value = call
+  if value != 0:
+    raise newException(Exception, "Win32 error: " & astToStr(call) & " returned " & $value)
+
+proc WindowHandler(hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
+  case uMsg
+  of WM_DESTROY:
+    currentEvents.add(Event(eventType: events.EventType.Quit))
+  else:
+    return DefWindowProc(hwnd, uMsg, wParam, lParam)
+
+
+proc createWindow*(title: string): NativeWindow =
+  result.hInstance = HINSTANCE(GetModuleHandle(nil))
+  var
+    windowClassName = T"EngineWindowClass"
+    windowName = T(title)
+    windowClass = WNDCLASSEX(
+      cbSize: UINT(WNDCLASSEX.sizeof),
+      lpfnWndProc: WindowHandler,
+      hInstance: result.hInstance,
+      lpszClassName: windowClassName,
+    )
+  
+  if(RegisterClassEx(addr(windowClass)) == 0):
+    raise newException(Exception, "Unable to register window class")
+
+  result.hwnd = CreateWindowEx(
+      DWORD(0),
+      windowClassName,
+      windowName,
+      DWORD(WS_OVERLAPPEDWINDOW),
+      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+      HMENU(0),
+      HINSTANCE(0),
+      result.hInstance,
+      nil
+    )
+
+  discard ShowWindow(result.hwnd, 1)
+
+proc trash*(window: NativeWindow) =
+  discard
+
+proc size*(window: NativeWindow): (int, int) =
+  var rect: RECT
+  checkWin32Result GetWindowRect(window.hwnd, addr(rect))
+  (int(rect.right - rect.left), int(rect.bottom - rect.top))
+
+proc pendingEvents*(window: NativeWindow): seq[Event] =
+  currentEvents = newSeq[Event]()
+  var msg: MSG
+  while PeekMessage(addr(msg), window.hwnd, 0, 0, PM_REMOVE):
+    DispatchMessage(addr(msg))
+  return currentEvents
\ No newline at end of file