window_win32.odin 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #+build windows
  2. package karl2d
  3. import win32 "core:sys/windows"
  4. import "base:runtime"
  5. WINDOW_INTERFACE_WIN32 :: Window_Interface {
  6. state_size = win32_state_size,
  7. init = win32_init,
  8. shutdown = win32_shutdown,
  9. window_handle = win32_window_handle,
  10. process_events = win32_process_events,
  11. get_events = win32_get_events,
  12. clear_events = win32_clear_events,
  13. set_position = win32_set_position,
  14. }
  15. Win32_State :: struct {
  16. allocator: runtime.Allocator,
  17. custom_context: runtime.Context,
  18. hwnd: win32.HWND,
  19. window_should_close: bool,
  20. events: [dynamic]Window_Event,
  21. }
  22. win32_state_size :: proc() -> int {
  23. return size_of(Win32_State)
  24. }
  25. // TODO rename to "ws"
  26. @(private="file")
  27. s: ^Win32_State
  28. win32_init :: proc(window_state: rawptr, window_width: int, window_height: int, window_title: string, allocator := context.allocator) {
  29. assert(window_state != nil)
  30. s = (^Win32_State)(window_state)
  31. s.allocator = allocator
  32. s.events = make([dynamic]Window_Event, allocator)
  33. s.custom_context = context
  34. win32.SetProcessDPIAware()
  35. CLASS_NAME :: "karl2d"
  36. instance := win32.HINSTANCE(win32.GetModuleHandleW(nil))
  37. cls := win32.WNDCLASSW {
  38. lpfnWndProc = _win32_window_proc,
  39. lpszClassName = CLASS_NAME,
  40. hInstance = instance,
  41. hCursor = win32.LoadCursorA(nil, win32.IDC_ARROW),
  42. }
  43. win32.RegisterClassW(&cls)
  44. r: win32.RECT
  45. r.right = i32(window_width)
  46. r.bottom = i32(window_height)
  47. style := win32.WS_OVERLAPPEDWINDOW | win32.WS_VISIBLE
  48. win32.AdjustWindowRect(&r, style, false)
  49. hwnd := win32.CreateWindowW(CLASS_NAME,
  50. win32.utf8_to_wstring(window_title),
  51. style,
  52. 100, 10, r.right - r.left, r.bottom - r.top,
  53. nil, nil, instance, nil,
  54. )
  55. assert(hwnd != nil, "Failed creating window")
  56. s.hwnd = hwnd
  57. }
  58. win32_shutdown :: proc() {
  59. delete(s.events)
  60. win32.DestroyWindow(s.hwnd)
  61. }
  62. win32_set_position :: proc(x: int, y: int) {
  63. // TODO: Does x, y respect monitor DPI?
  64. win32.SetWindowPos(
  65. s.hwnd,
  66. {},
  67. i32(x),
  68. i32(y),
  69. 0,
  70. 0,
  71. win32.SWP_NOACTIVATE | win32.SWP_NOZORDER | win32.SWP_NOSIZE,
  72. )
  73. }
  74. win32_window_handle :: proc() -> Window_Handle {
  75. return Window_Handle(s.hwnd)
  76. }
  77. win32_process_events :: proc() {
  78. msg: win32.MSG
  79. for win32.PeekMessageW(&msg, nil, 0, 0, win32.PM_REMOVE) {
  80. win32.TranslateMessage(&msg)
  81. win32.DispatchMessageW(&msg)
  82. }
  83. }
  84. win32_get_events :: proc() -> []Window_Event {
  85. return s.events[:]
  86. }
  87. win32_clear_events :: proc() {
  88. runtime.clear(&s.events)
  89. }
  90. _win32_window_proc :: proc "stdcall" (hwnd: win32.HWND, msg: win32.UINT, wparam: win32.WPARAM, lparam: win32.LPARAM) -> win32.LRESULT {
  91. context = s.custom_context
  92. switch msg {
  93. case win32.WM_DESTROY:
  94. win32.PostQuitMessage(0)
  95. case win32.WM_CLOSE:
  96. append(&s.events, Window_Event_Close_Wanted{})
  97. case win32.WM_KEYDOWN:
  98. key := WIN32_VK_MAP[wparam]
  99. append(&s.events, Window_Event_Key_Went_Down {
  100. key = key,
  101. })
  102. return 0
  103. case win32.WM_KEYUP:
  104. key := WIN32_VK_MAP[wparam]
  105. append(&s.events, Window_Event_Key_Went_Up {
  106. key = key,
  107. })
  108. return 0
  109. case win32.WM_MOUSEMOVE:
  110. x := win32.GET_X_LPARAM(lparam)
  111. y := win32.GET_Y_LPARAM(lparam)
  112. append(&s.events, Window_Event_Mouse_Move {
  113. position = {f32(x), f32(y)},
  114. })
  115. return 0
  116. }
  117. return win32.DefWindowProcW(hwnd, msg, wparam, lparam)
  118. }
  119. WIN32_VK_MAP := [255]Keyboard_Key {
  120. win32.VK_A = .A,
  121. win32.VK_B = .B,
  122. win32.VK_C = .C,
  123. win32.VK_D = .D,
  124. win32.VK_E = .E,
  125. win32.VK_F = .F,
  126. win32.VK_G = .G,
  127. win32.VK_H = .H,
  128. win32.VK_I = .I,
  129. win32.VK_J = .J,
  130. win32.VK_K = .K,
  131. win32.VK_L = .L,
  132. win32.VK_M = .M,
  133. win32.VK_N = .N,
  134. win32.VK_O = .O,
  135. win32.VK_P = .P,
  136. win32.VK_Q = .Q,
  137. win32.VK_R = .R,
  138. win32.VK_S = .S,
  139. win32.VK_T = .T,
  140. win32.VK_U = .U,
  141. win32.VK_V = .V,
  142. win32.VK_W = .W,
  143. win32.VK_X = .X,
  144. win32.VK_Y = .Y,
  145. win32.VK_Z = .Z,
  146. win32.VK_LEFT = .Left,
  147. win32.VK_RIGHT = .Right,
  148. win32.VK_UP = .Up,
  149. win32.VK_DOWN = .Down,
  150. win32.VK_RETURN = .Enter,
  151. }