window_win32.odin 4.0 KB

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