window_win32.odin 4.5 KB

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