karl2d.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package karl2d
  2. import win32 "core:sys/windows"
  3. import "base:runtime"
  4. import "core:mem"
  5. import "core:log"
  6. Rendering_Backend :: struct {
  7. state_size: proc() -> int,
  8. init: proc(state: rawptr, window_handle: uintptr, swapchain_width, swapchain_height: int,
  9. allocator := context.allocator, loc := #caller_location),
  10. set_internal_state: proc(state: rawptr),
  11. }
  12. State :: struct {
  13. allocator: runtime.Allocator,
  14. custom_context: runtime.Context,
  15. rb: Rendering_Backend,
  16. rb_state: rawptr,
  17. keys_went_down: #sparse [Keyboard_Key]bool,
  18. keys_went_up: #sparse [Keyboard_Key]bool,
  19. keys_is_held: #sparse [Keyboard_Key]bool,
  20. window: win32.HWND,
  21. width: int,
  22. height: int,
  23. run: bool,
  24. }
  25. VK_MAP := [255]Keyboard_Key {
  26. win32.VK_A = .A,
  27. win32.VK_B = .B,
  28. win32.VK_C = .C,
  29. win32.VK_D = .D,
  30. win32.VK_E = .E,
  31. win32.VK_F = .F,
  32. win32.VK_G = .G,
  33. win32.VK_H = .H,
  34. win32.VK_I = .I,
  35. win32.VK_J = .J,
  36. win32.VK_K = .K,
  37. win32.VK_L = .L,
  38. win32.VK_M = .M,
  39. win32.VK_N = .N,
  40. win32.VK_O = .O,
  41. win32.VK_P = .P,
  42. win32.VK_Q = .Q,
  43. win32.VK_R = .R,
  44. win32.VK_S = .S,
  45. win32.VK_T = .T,
  46. win32.VK_U = .U,
  47. win32.VK_V = .V,
  48. win32.VK_W = .W,
  49. win32.VK_X = .X,
  50. win32.VK_Y = .Y,
  51. win32.VK_Z = .Z,
  52. win32.VK_LEFT = .Left,
  53. win32.VK_RIGHT = .Right,
  54. win32.VK_UP = .Up,
  55. win32.VK_DOWN = .Down,
  56. }
  57. // Opens a window and initializes some internal state. The internal state will use `allocator` for
  58. // all dynamically allocated memory. The return value can be ignored unless you need to later call
  59. // `set_state`.
  60. init :: proc(window_width: int, window_height: int, window_title: string,
  61. allocator := context.allocator, loc := #caller_location) -> ^State {
  62. win32.SetProcessDPIAware()
  63. s = new(State, allocator, loc)
  64. s.allocator = allocator
  65. s.custom_context = context
  66. CLASS_NAME :: "karl2d"
  67. instance := win32.HINSTANCE(win32.GetModuleHandleW(nil))
  68. s.run = true
  69. s.width = window_width
  70. s.height = window_height
  71. cls := win32.WNDCLASSW {
  72. lpfnWndProc = window_proc,
  73. lpszClassName = CLASS_NAME,
  74. hInstance = instance,
  75. hCursor = win32.LoadCursorA(nil, win32.IDC_ARROW),
  76. }
  77. window_proc :: proc "stdcall" (hwnd: win32.HWND, msg: win32.UINT, wparam: win32.WPARAM, lparam: win32.LPARAM) -> win32.LRESULT {
  78. context = s.custom_context
  79. switch msg {
  80. case win32.WM_DESTROY:
  81. win32.PostQuitMessage(0)
  82. s.run = false
  83. case win32.WM_CLOSE:
  84. s.run = false
  85. case win32.WM_KEYDOWN:
  86. key := VK_MAP[wparam]
  87. s.keys_went_down[key] = true
  88. s.keys_is_held[key] = true
  89. case win32.WM_KEYUP:
  90. key := VK_MAP[wparam]
  91. s.keys_is_held[key] = false
  92. s.keys_went_up[key] = true
  93. }
  94. return win32.DefWindowProcW(hwnd, msg, wparam, lparam)
  95. }
  96. win32.RegisterClassW(&cls)
  97. r: win32.RECT
  98. r.right = i32(window_width)
  99. r.bottom = i32(window_height)
  100. style := win32.WS_OVERLAPPEDWINDOW | win32.WS_VISIBLE
  101. win32.AdjustWindowRect(&r, style, false)
  102. hwnd := win32.CreateWindowW(CLASS_NAME,
  103. win32.utf8_to_wstring(window_title),
  104. style,
  105. 100, 10, r.right - r.left, r.bottom - r.top,
  106. nil, nil, instance, nil)
  107. s.window = hwnd
  108. assert(hwnd != nil, "Failed creating window")
  109. s.rb = make_backend_d3d11()
  110. rb_alloc_error: runtime.Allocator_Error
  111. s.rb_state, rb_alloc_error = mem.alloc(s.rb.state_size())
  112. log.assertf(rb_alloc_error == nil, "Failed allocating memory for rendering backend: %v", rb_alloc_error)
  113. s.rb.init(s.rb_state, uintptr(hwnd), window_width, window_height, allocator, loc)
  114. return s
  115. }
  116. // Call at start or end of frame to process all events that have arrived to the window.
  117. //
  118. // WARNING: Not calling this will make your program impossible to interact with.
  119. process_events :: proc() {
  120. s.keys_went_up = {}
  121. s.keys_went_down = {}
  122. msg: win32.MSG
  123. for win32.PeekMessageW(&msg, nil, 0, 0, win32.PM_REMOVE) {
  124. win32.TranslateMessage(&msg)
  125. win32.DispatchMessageW(&msg)
  126. }
  127. }
  128. get_screen_width :: proc() -> int {
  129. return s.width
  130. }
  131. get_screen_height :: proc() -> int {
  132. return s.height
  133. }
  134. key_went_down :: proc(key: Keyboard_Key) -> bool {
  135. return s.keys_went_down[key]
  136. }
  137. key_went_up :: proc(key: Keyboard_Key) -> bool {
  138. return s.keys_went_up[key]
  139. }
  140. key_is_held :: proc(key: Keyboard_Key) -> bool {
  141. return s.keys_is_held[key]
  142. }
  143. window_should_close :: proc() -> bool {
  144. return !s.run
  145. }
  146. set_window_position :: proc(x: int, y: int) {
  147. // TODO: Does x, y respect monitor DPI?
  148. win32.SetWindowPos(
  149. s.window,
  150. {},
  151. i32(x),
  152. i32(y),
  153. 0,
  154. 0,
  155. win32.SWP_NOACTIVATE | win32.SWP_NOZORDER | win32.SWP_NOSIZE,
  156. )
  157. }
  158. @(private="file")
  159. s: ^State
  160. // Closes the window and cleans up the internal state.
  161. shutdown: proc() : _shutdown
  162. // Clear the backbuffer with supplied color.
  163. clear: proc(color: Color) : _clear
  164. // Present the backbuffer. Call at end of frame to make everything you've drawn appear on the screen.
  165. present: proc() : _present
  166. // Flushes the current batch. This sends off everything to the GPU that has been queued in the
  167. // current batch. Done automatically by `present` (possible to disable). Also done by `set_camera`
  168. // and `set_scissor_rect` since the batch depends on those options.
  169. draw_current_batch: proc() : _draw_current_batch
  170. // Returns true if the user has tried to close the window.
  171. // Can be used to restore the internal state using the pointer returned by `init`. Useful after
  172. // reloading the library (for example, when doing code hot reload).
  173. set_internal_state :: proc(state: ^State) {
  174. s = state
  175. s.rb.set_internal_state(s.rb_state)
  176. }
  177. set_window_size: proc(width: int, height: int) : _set_window_size
  178. get_default_shader: proc() -> Shader_Handle : _get_default_shader
  179. load_texture_from_file: proc(filename: string) -> Texture : _load_texture_from_file
  180. load_texture_from_memory: proc(data: []u8, width: int, height: int) -> Texture : _load_texture_from_memory
  181. // load_texture_from_bytes or buffer or something ()
  182. destroy_texture: proc(tex: Texture) : _destroy_texture
  183. set_camera: proc(camera: Maybe(Camera)) : _set_camera
  184. set_scissor_rect: proc(scissor_rect: Maybe(Rect)) : _set_scissor_rect
  185. set_shader: proc(shader: Shader_Handle) : _set_shader
  186. //set_vertex_value :: _set_vertex_value
  187. draw_texture: proc(tex: Texture, pos: Vec2, tint := WHITE) : _draw_texture
  188. draw_texture_rect: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) : _draw_texture_rect
  189. draw_texture_ex: proc(tex: Texture, src: Rect, dest: Rect, origin: Vec2, rotation: f32, tint := WHITE) : _draw_texture_ex
  190. draw_rect: proc(rect: Rect, color: Color) : _draw_rectangle
  191. draw_rect_outline: proc(rect: Rect, thickness: f32, color: Color) : _draw_rectangle_outline
  192. draw_circle: proc(center: Vec2, radius: f32, color: Color) : _draw_circle
  193. draw_line: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) : _draw_line
  194. load_shader: proc(shader_source: string, layout_formats: []Shader_Input_Format = {}) -> Shader_Handle : _load_shader
  195. destroy_shader: proc(shader: Shader_Handle) : _destroy_shader
  196. get_shader_constant_location: proc(shader: Shader_Handle, name: string) -> Shader_Constant_Location : _get_shader_constant_location
  197. set_shader_constant :: _set_shader_constant
  198. set_shader_constant_mat4: proc(shader: Shader_Handle, loc: Shader_Constant_Location, val: matrix[4,4]f32) : _set_shader_constant_mat4
  199. set_shader_constant_f32: proc(shader: Shader_Handle, loc: Shader_Constant_Location, val: f32) : _set_shader_constant_f32
  200. set_shader_constant_vec2: proc(shader: Shader_Handle, loc: Shader_Constant_Location, val: Vec2) : _set_shader_constant_vec2
  201. Shader_Input_Format :: enum {
  202. Unknown,
  203. RGBA32_Float,
  204. RGBA8_Norm,
  205. RGBA8_Norm_SRGB,
  206. RG32_Float,
  207. R32_Float,
  208. }
  209. // WARNING: Not proper text rendering yet... No font support etc
  210. draw_text: proc(text: string, pos: Vec2, font_size: f32, color: Color) : _draw_text
  211. screen_to_world: proc(pos: Vec2, camera: Camera) -> Vec2 : _screen_to_world
  212. mouse_button_went_down: proc(button: Mouse_Button) -> bool : _mouse_button_pressed
  213. mouse_button_went_up: proc(button: Mouse_Button) -> bool : _mouse_button_released
  214. mouse_button_is_held: proc(button: Mouse_Button) -> bool : _mouse_button_held
  215. get_mouse_wheel_delta: proc() -> f32 : _mouse_wheel_delta
  216. get_mouse_position: proc() -> Vec2 : _mouse_position
  217. Color :: [4]u8
  218. Vec2 :: [2]f32
  219. Vec2i :: [2]int
  220. Rect :: struct {
  221. x, y: f32,
  222. w, h: f32,
  223. }
  224. Texture :: struct {
  225. id: Texture_Handle,
  226. width: int,
  227. height: int,
  228. }
  229. Camera :: struct {
  230. target: Vec2,
  231. origin: Vec2,
  232. rotation: f32,
  233. zoom: f32,
  234. }
  235. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  236. // options presented here.
  237. Mouse_Button :: enum {
  238. Left,
  239. Right,
  240. Middle,
  241. Max = 255,
  242. }
  243. // TODO: These are just copied from raylib, we probably want a list of our own "default colors"
  244. WHITE :: Color { 255, 255, 255, 255 }
  245. BLACK :: Color { 0, 0, 0, 255 }
  246. GRAY :: Color{ 130, 130, 130, 255 }
  247. RED :: Color { 230, 41, 55, 255 }
  248. YELLOW :: Color { 253, 249, 0, 255 }
  249. BLUE :: Color { 0, 121, 241, 255 }
  250. MAGENTA :: Color { 255, 0, 255, 255 }
  251. DARKGRAY :: Color{ 80, 80, 80, 255 }
  252. GREEN :: Color{ 0, 228, 48, 255 }
  253. Shader_Handle :: distinct Handle
  254. SHADER_NONE :: Shader_Handle {}
  255. // Based on Raylib / GLFW
  256. Keyboard_Key :: enum {
  257. None = 0,
  258. // Alphanumeric keys
  259. Apostrophe = 39,
  260. Comma = 44,
  261. Minus = 45,
  262. Period = 46,
  263. Slash = 47,
  264. Zero = 48,
  265. One = 49,
  266. Two = 50,
  267. Three = 51,
  268. Four = 52,
  269. Five = 53,
  270. Six = 54,
  271. Seven = 55,
  272. Eight = 56,
  273. Nine = 57,
  274. Semicolon = 59,
  275. Equal = 61,
  276. A = 65,
  277. B = 66,
  278. C = 67,
  279. D = 68,
  280. E = 69,
  281. F = 70,
  282. G = 71,
  283. H = 72,
  284. I = 73,
  285. J = 74,
  286. K = 75,
  287. L = 76,
  288. M = 77,
  289. N = 78,
  290. O = 79,
  291. P = 80,
  292. Q = 81,
  293. R = 82,
  294. S = 83,
  295. T = 84,
  296. U = 85,
  297. V = 86,
  298. W = 87,
  299. X = 88,
  300. Y = 89,
  301. Z = 90,
  302. Left_Bracket = 91,
  303. Backslash = 92,
  304. Right_Bracket = 93,
  305. Grave = 96,
  306. // Function keys
  307. Space = 32,
  308. Escape = 256,
  309. Enter = 257,
  310. Tab = 258,
  311. Backspace = 259,
  312. Insert = 260,
  313. Delete = 261,
  314. Right = 262,
  315. Left = 263,
  316. Down = 264,
  317. Up = 265,
  318. Page_Up = 266,
  319. Page_Down = 267,
  320. Home = 268,
  321. End = 269,
  322. Caps_Lock = 280,
  323. Scroll_Lock = 281,
  324. Num_Lock = 282,
  325. Print_Screen = 283,
  326. Pause = 284,
  327. F1 = 290,
  328. F2 = 291,
  329. F3 = 292,
  330. F4 = 293,
  331. F5 = 294,
  332. F6 = 295,
  333. F7 = 296,
  334. F8 = 297,
  335. F9 = 298,
  336. F10 = 299,
  337. F11 = 300,
  338. F12 = 301,
  339. Left_Shift = 340,
  340. Left_Control = 341,
  341. Left_Alt = 342,
  342. Left_Super = 343,
  343. Right_Shift = 344,
  344. Right_Control = 345,
  345. Right_Alt = 346,
  346. Right_Super = 347,
  347. Menu = 348,
  348. // Keypad keys
  349. KP_0 = 320,
  350. KP_1 = 321,
  351. KP_2 = 322,
  352. KP_3 = 323,
  353. KP_4 = 324,
  354. KP_5 = 325,
  355. KP_6 = 326,
  356. KP_7 = 327,
  357. KP_8 = 328,
  358. KP_9 = 329,
  359. KP_Decimal = 330,
  360. KP_Divide = 331,
  361. KP_Multiply = 332,
  362. KP_Subtract = 333,
  363. KP_Add = 334,
  364. KP_Enter = 335,
  365. KP_Equal = 336,
  366. }