window_win32.odin 4.4 KB

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