karl2d.odin 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. package karl2d
  2. import "base:runtime"
  3. import "core:mem"
  4. import "core:log"
  5. import "core:math"
  6. import "core:math/linalg"
  7. import "core:slice"
  8. import "core:image"
  9. import "core:image/bmp"
  10. import "core:image/png"
  11. import "core:image/tga"
  12. import hm "handle_map"
  13. _ :: bmp
  14. _ :: png
  15. _ :: tga
  16. Handle :: hm.Handle
  17. Texture_Handle :: distinct Handle
  18. // Opens a window and initializes some internal state. The internal state will use `allocator` for
  19. // all dynamically allocated memory. The return value can be ignored unless you need to later call
  20. // `set_state`.
  21. init :: proc(window_width: int, window_height: int, window_title: string,
  22. allocator := context.allocator, loc := #caller_location) -> ^State {
  23. s = new(State, allocator, loc)
  24. s.allocator = allocator
  25. s.custom_context = context
  26. s.width = window_width
  27. s.height = window_height
  28. s.win = WINDOW_INTERFACE_WIN32
  29. win = s.win
  30. window_state_alloc_error: runtime.Allocator_Error
  31. s.window_state, window_state_alloc_error = mem.alloc(win.state_size())
  32. log.assertf(window_state_alloc_error == nil, "Failed allocating memory for window state: %v", window_state_alloc_error)
  33. win.init(s.window_state, window_width, window_height, window_title, allocator)
  34. s.window = win.window_handle()
  35. s.rb = BACKEND_D3D11
  36. rb = s.rb
  37. rb_alloc_error: runtime.Allocator_Error
  38. s.rb_state, rb_alloc_error = mem.alloc(rb.state_size())
  39. log.assertf(rb_alloc_error == nil, "Failed allocating memory for rendering backend: %v", rb_alloc_error)
  40. s.proj_matrix = make_default_projection(window_width, window_height)
  41. s.view_matrix = 1
  42. rb.init(s.rb_state, s.window, window_width, window_height, allocator)
  43. s.vertex_buffer_cpu = make([]u8, VERTEX_BUFFER_MAX, allocator, loc)
  44. white_rect: [16*16*4]u8
  45. slice.fill(white_rect[:], 255)
  46. s.shape_drawing_texture = rb.load_texture(white_rect[:], 16, 16)
  47. s.default_shader = rb.load_shader(string(DEFAULT_SHADER_SOURCE), {
  48. .RG32_Float,
  49. .RG32_Float,
  50. .RGBA8_Norm,
  51. })
  52. return s
  53. }
  54. DEFAULT_SHADER_SOURCE :: #load("shader.hlsl")
  55. // Closes the window and cleans up the internal state.
  56. shutdown :: proc() {
  57. rb.destroy_texture(s.shape_drawing_texture)
  58. destroy_shader(s.default_shader)
  59. rb.shutdown()
  60. delete(s.vertex_buffer_cpu, s.allocator)
  61. win.shutdown()
  62. a := s.allocator
  63. free(s.window_state, a)
  64. free(s.rb_state, a)
  65. free(s, a)
  66. s = nil
  67. }
  68. // Clear the backbuffer with supplied color.
  69. clear :: proc(color: Color) {
  70. rb.clear(color)
  71. }
  72. // Present the backbuffer. Call at end of frame to make everything you've drawn appear on the screen.
  73. present :: proc() {
  74. draw_current_batch()
  75. rb.present()
  76. }
  77. // Call at start or end of frame to process all events that have arrived to the window.
  78. //
  79. // WARNING: Not calling this will make your program impossible to interact with.
  80. process_events :: proc() {
  81. s.keys_went_up = {}
  82. s.keys_went_down = {}
  83. s.mouse_delta = {}
  84. s.mouse_wheel_delta = 0
  85. win.process_events()
  86. events := win.get_events()
  87. for &event in events {
  88. switch &e in event {
  89. case Window_Event_Close_Wanted:
  90. s.shutdown_wanted = true
  91. case Window_Event_Key_Went_Down:
  92. s.keys_went_down[e.key] = true
  93. s.keys_is_held[e.key] = true
  94. case Window_Event_Key_Went_Up:
  95. s.keys_is_held[e.key] = false
  96. s.keys_went_up[e.key] = true
  97. case Window_Event_Mouse_Move:
  98. prev_pos := s.mouse_position
  99. s.mouse_position = e.position
  100. s.mouse_delta = prev_pos - s.mouse_position
  101. case Window_Event_Mouse_Wheel:
  102. s.mouse_wheel_delta = e.delta
  103. }
  104. }
  105. win.clear_events()
  106. }
  107. /* Flushes the current batch. This sends off everything to the GPU that has been queued in the
  108. current batch. Normally, you do not need to do this manually. It is done automatically when these
  109. procedures run:
  110. present
  111. set_camera
  112. set_shader
  113. TODO: complete this list and motivate why it needs to happen on those procs (or do that in the
  114. docs for those procs).
  115. */
  116. draw_current_batch :: proc() {
  117. shader := s.batch_shader.? or_else s.default_shader
  118. rb.draw(shader, s.batch_texture, s.proj_matrix * s.view_matrix, s.vertex_buffer_cpu[:s.vertex_buffer_cpu_used])
  119. s.vertex_buffer_cpu_used = 0
  120. }
  121. // Can be used to restore the internal state using the pointer returned by `init`. Useful after
  122. // reloading the library (for example, when doing code hot reload).
  123. set_internal_state :: proc(state: ^State) {
  124. s = state
  125. rb.set_internal_state(s.rb_state)
  126. }
  127. get_screen_width :: proc() -> int {
  128. return rb.get_swapchain_width()
  129. }
  130. get_screen_height :: proc() -> int {
  131. return rb.get_swapchain_height()
  132. }
  133. key_went_down :: proc(key: Keyboard_Key) -> bool {
  134. return s.keys_went_down[key]
  135. }
  136. key_went_up :: proc(key: Keyboard_Key) -> bool {
  137. return s.keys_went_up[key]
  138. }
  139. key_is_held :: proc(key: Keyboard_Key) -> bool {
  140. return s.keys_is_held[key]
  141. }
  142. shutdown_wanted :: proc() -> bool {
  143. return s.shutdown_wanted
  144. }
  145. set_window_position :: proc(x: int, y: int) {
  146. win.set_position(x, y)
  147. }
  148. set_window_size :: proc(width: int, height: int) {
  149. panic("Not implemented")
  150. }
  151. set_camera :: proc(camera: Maybe(Camera)) {
  152. if camera == s.batch_camera {
  153. return
  154. }
  155. draw_current_batch()
  156. s.batch_camera = camera
  157. s.proj_matrix = make_default_projection(s.width, s.height)
  158. if c, c_ok := camera.?; c_ok {
  159. origin_trans := linalg.matrix4_translate(vec3_from_vec2(-c.origin))
  160. translate := linalg.matrix4_translate(vec3_from_vec2(c.target))
  161. scale := linalg.matrix4_scale(Vec3{c.zoom, c.zoom, 1})
  162. rot := linalg.matrix4_rotate_f32(c.rotation * math.RAD_PER_DEG, {0, 0, 1})
  163. camera_matrix := translate * rot *scale* origin_trans
  164. s.view_matrix = linalg.inverse(camera_matrix)
  165. } else {
  166. s.view_matrix = 1
  167. }
  168. }
  169. load_texture_from_file :: proc(filename: string) -> Texture {
  170. img, img_err := image.load_from_file(filename, options = {.alpha_add_if_missing}, allocator = context.temp_allocator)
  171. if img_err != nil {
  172. log.errorf("Error loading texture %v: %v", filename, img_err)
  173. return {}
  174. }
  175. backend_tex := rb.load_texture(img.pixels.buf[:], img.width, img.height)
  176. return {
  177. handle = backend_tex,
  178. width = img.width,
  179. height = img.height,
  180. }
  181. }
  182. destroy_texture :: proc(tex: Texture) {
  183. rb.destroy_texture(tex.handle)
  184. }
  185. draw_rect :: proc(r: Rect, c: Color) {
  186. if s.batch_texture != TEXTURE_NONE && s.batch_texture != s.shape_drawing_texture {
  187. draw_current_batch()
  188. }
  189. s.batch_texture = s.shape_drawing_texture
  190. _batch_vertex({r.x, r.y}, {0, 0}, c)
  191. _batch_vertex({r.x + r.w, r.y}, {1, 0}, c)
  192. _batch_vertex({r.x + r.w, r.y + r.h}, {1, 1}, c)
  193. _batch_vertex({r.x, r.y}, {0, 0}, c)
  194. _batch_vertex({r.x + r.w, r.y + r.h}, {1, 1}, c)
  195. _batch_vertex({r.x, r.y + r.h}, {0, 1}, c)
  196. }
  197. draw_rect_ex :: proc(r: Rect, origin: Vec2, rot: f32, c: Color) {
  198. if s.batch_texture != TEXTURE_NONE && s.batch_texture != s.shape_drawing_texture {
  199. draw_current_batch()
  200. }
  201. s.batch_texture = s.shape_drawing_texture
  202. tl, tr, bl, br: Vec2
  203. // Rotation adapted from Raylib's "DrawTexturePro"
  204. if rot == 0 {
  205. x := r.x - origin.x
  206. y := r.y - origin.y
  207. tl = { x, y }
  208. tr = { x + r.w, y }
  209. bl = { x, y + r.h }
  210. br = { x + r.w, y + r.h }
  211. } else {
  212. sin_rot := math.sin(rot * math.RAD_PER_DEG)
  213. cos_rot := math.cos(rot * math.RAD_PER_DEG)
  214. x := r.x
  215. y := r.y
  216. dx := -origin.x
  217. dy := -origin.y
  218. tl = {
  219. x + dx * cos_rot - dy * sin_rot,
  220. y + dx * sin_rot + dy * cos_rot,
  221. }
  222. tr = {
  223. x + (dx + r.w) * cos_rot - dy * sin_rot,
  224. y + (dx + r.w) * sin_rot + dy * cos_rot,
  225. }
  226. bl = {
  227. x + dx * cos_rot - (dy + r.h) * sin_rot,
  228. y + dx * sin_rot + (dy + r.h) * cos_rot,
  229. }
  230. br = {
  231. x + (dx + r.w) * cos_rot - (dy + r.h) * sin_rot,
  232. y + (dx + r.w) * sin_rot + (dy + r.h) * cos_rot,
  233. }
  234. }
  235. _batch_vertex(tl, {0, 0}, c)
  236. _batch_vertex(tr, {1, 0}, c)
  237. _batch_vertex(br, {1, 1}, c)
  238. _batch_vertex(tl, {0, 0}, c)
  239. _batch_vertex(br, {1, 1}, c)
  240. _batch_vertex(bl, {0, 1}, c)
  241. }
  242. draw_rect_outline :: proc(r: Rect, thickness: f32, color: Color) {
  243. t := thickness
  244. // Based on DrawRectangleLinesEx from Raylib
  245. top := Rect {
  246. r.x,
  247. r.y,
  248. r.w,
  249. t,
  250. }
  251. bottom := Rect {
  252. r.x,
  253. r.y + r.h - t,
  254. r.w,
  255. t,
  256. }
  257. left := Rect {
  258. r.x,
  259. r.y + t,
  260. t,
  261. r.h - t * 2,
  262. }
  263. right := Rect {
  264. r.x + r.w - t,
  265. r.y + t,
  266. t,
  267. r.h - t * 2,
  268. }
  269. draw_rect(top, color)
  270. draw_rect(bottom, color)
  271. draw_rect(left, color)
  272. draw_rect(right, color)
  273. }
  274. draw_circle :: proc(center: Vec2, radius: f32, color: Color, segments := 16) {
  275. if s.batch_texture != TEXTURE_NONE && s.batch_texture != s.shape_drawing_texture {
  276. draw_current_batch()
  277. }
  278. s.batch_texture = s.shape_drawing_texture
  279. prev := center + {radius, 0}
  280. for s in 1..=segments {
  281. sr := (f32(s)/f32(segments)) * 2*math.PI
  282. rot := linalg.matrix2_rotate(sr)
  283. p := center + rot * Vec2{radius, 0}
  284. _batch_vertex(prev, {0, 0}, color)
  285. _batch_vertex(p, {1, 0}, color)
  286. _batch_vertex(center, {1, 1}, color)
  287. prev = p
  288. }
  289. }
  290. draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) {
  291. p := Vec2{start.x, start.y + thickness*0.5}
  292. s := Vec2{linalg.length(end - start), thickness}
  293. origin := Vec2 {0, thickness*0.5}
  294. r := Rect {p.x, p.y, s.x, s.y}
  295. rot := math.atan2(end.y - start.y, end.x - start.x)
  296. draw_rect_ex(r, origin, rot * math.DEG_PER_RAD, color)
  297. }
  298. draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE) {
  299. draw_texture_ex(
  300. tex,
  301. {0, 0, f32(tex.width), f32(tex.height)},
  302. {pos.x, pos.y, f32(tex.width), f32(tex.height)},
  303. {},
  304. 0,
  305. tint,
  306. )
  307. }
  308. draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) {
  309. draw_texture_ex(
  310. tex,
  311. rect,
  312. {pos.x, pos.y, rect.w, rect.h},
  313. {},
  314. 0,
  315. tint,
  316. )
  317. }
  318. draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rotation: f32, tint := WHITE) {
  319. if tex.width == 0 || tex.height == 0 {
  320. return
  321. }
  322. if s.batch_texture != TEXTURE_NONE && s.batch_texture != tex.handle {
  323. draw_current_batch()
  324. }
  325. s.batch_texture = tex.handle
  326. tl, tr, bl, br: Vec2
  327. // Rotation adapted from Raylib's "DrawTexturePro"
  328. if rotation == 0 {
  329. x := dst.x - origin.x
  330. y := dst.y - origin.y
  331. tl = { x, y }
  332. tr = { x + dst.w, y }
  333. bl = { x, y + dst.h }
  334. br = { x + dst.w, y + dst.h }
  335. } else {
  336. sin_rot := math.sin(rotation * math.RAD_PER_DEG)
  337. cos_rot := math.cos(rotation * math.RAD_PER_DEG)
  338. x := dst.x
  339. y := dst.y
  340. dx := -origin.x
  341. dy := -origin.y
  342. tl = {
  343. x + dx * cos_rot - dy * sin_rot,
  344. y + dx * sin_rot + dy * cos_rot,
  345. }
  346. tr = {
  347. x + (dx + dst.w) * cos_rot - dy * sin_rot,
  348. y + (dx + dst.w) * sin_rot + dy * cos_rot,
  349. }
  350. bl = {
  351. x + dx * cos_rot - (dy + dst.h) * sin_rot,
  352. y + dx * sin_rot + (dy + dst.h) * cos_rot,
  353. }
  354. br = {
  355. x + (dx + dst.w) * cos_rot - (dy + dst.h) * sin_rot,
  356. y + (dx + dst.w) * sin_rot + (dy + dst.h) * cos_rot,
  357. }
  358. }
  359. ts := Vec2{f32(tex.width), f32(tex.height)}
  360. up := Vec2{src.x, src.y} / ts
  361. us := Vec2{src.w, src.h} / ts
  362. c := tint
  363. _batch_vertex(tl, up, c)
  364. _batch_vertex(tr, up + {us.x, 0}, c)
  365. _batch_vertex(br, up + us, c)
  366. _batch_vertex(tl, up, c)
  367. _batch_vertex(br, up + us, c)
  368. _batch_vertex(bl, up + {0, us.y}, c)
  369. }
  370. load_shader :: proc(shader_source: string, layout_formats: []Shader_Input_Format = {}) -> Shader {
  371. return rb.load_shader(shader_source, layout_formats)
  372. }
  373. destroy_shader :: proc(shader: Shader) {
  374. rb.destroy_shader(shader)
  375. }
  376. set_shader :: proc(shader: Maybe(Shader)) {
  377. if maybe_handle_equal(shader, s.batch_shader) {
  378. return
  379. }
  380. draw_current_batch()
  381. s.batch_shader = shader
  382. }
  383. maybe_handle_equal :: proc(m1: Maybe($T), m2: Maybe(T)) -> bool {
  384. if m1 == nil && m2 == nil {
  385. return true
  386. }
  387. m1v, m1v_ok := m1.?
  388. m2v, m2v_ok := m2.?
  389. if !m1v_ok || !m2v_ok {
  390. return false
  391. }
  392. return m1v.handle == m2v.handle
  393. }
  394. set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: $T) {
  395. draw_current_batch()
  396. if int(loc.buffer_idx) >= len(shd.constant_buffers) {
  397. log.warnf("Constant buffer idx %v is out of bounds", loc.buffer_idx)
  398. return
  399. }
  400. b := &shd.constant_buffers[loc.buffer_idx]
  401. if int(loc.offset) + size_of(val) > len(b.cpu_data) {
  402. log.warnf("Constant buffer idx %v is trying to be written out of bounds by at offset %v with %v bytes", loc.buffer_idx, loc.offset, size_of(val))
  403. return
  404. }
  405. dst := (^T)(&b.cpu_data[loc.offset])
  406. dst^ = val
  407. }
  408. set_shader_constant_mat4 :: proc(shader: Shader, loc: Shader_Constant_Location, val: matrix[4,4]f32) {
  409. set_shader_constant(shader, loc, val)
  410. }
  411. set_shader_constant_f32 :: proc(shader: Shader, loc: Shader_Constant_Location, val: f32) {
  412. set_shader_constant(shader, loc, val)
  413. }
  414. set_shader_constant_vec2 :: proc(shader: Shader, loc: Shader_Constant_Location, val: Vec2) {
  415. set_shader_constant(shader, loc, val)
  416. }
  417. get_default_shader :: proc() -> Shader {
  418. return s.default_shader
  419. }
  420. set_scissor_rect :: proc(scissor_rect: Maybe(Rect)) {
  421. panic("not implemented")
  422. }
  423. screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  424. panic("not implemented")
  425. }
  426. draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
  427. }
  428. mouse_button_went_down :: proc(button: Mouse_Button) -> bool {
  429. panic("not implemented")
  430. }
  431. mouse_button_went_up :: proc(button: Mouse_Button) -> bool {
  432. panic("not implemented")
  433. }
  434. mouse_button_is_held :: proc(button: Mouse_Button) -> bool {
  435. panic("not implemented")
  436. }
  437. get_mouse_wheel_delta :: proc() -> f32 {
  438. return s.mouse_wheel_delta
  439. }
  440. get_mouse_position :: proc() -> Vec2 {
  441. return s.mouse_position
  442. }
  443. _batch_vertex :: proc(v: Vec2, uv: Vec2, color: Color) {
  444. v := v
  445. if s.vertex_buffer_cpu_used == len(s.vertex_buffer_cpu) {
  446. panic("Must dispatch here")
  447. }
  448. shd := s.batch_shader.? or_else s.default_shader
  449. base_offset := s.vertex_buffer_cpu_used
  450. pos_offset := shd.default_input_offsets[.Position]
  451. uv_offset := shd.default_input_offsets[.UV]
  452. color_offset := shd.default_input_offsets[.Color]
  453. mem.set(&s.vertex_buffer_cpu[base_offset], 0, shd.vertex_size)
  454. if pos_offset != -1 {
  455. (^Vec2)(&s.vertex_buffer_cpu[base_offset + pos_offset])^ = v
  456. }
  457. if uv_offset != -1 {
  458. (^Vec2)(&s.vertex_buffer_cpu[base_offset + uv_offset])^ = uv
  459. }
  460. if color_offset != -1 {
  461. (^Color)(&s.vertex_buffer_cpu[base_offset + color_offset])^ = color
  462. }
  463. override_offset: int
  464. for &o, idx in shd.input_overrides {
  465. input := &shd.inputs[idx]
  466. sz := shader_input_format_size(input.format)
  467. if o.used != 0 {
  468. mem.copy(&s.vertex_buffer_cpu[base_offset + override_offset], raw_data(&o.val), o.used)
  469. }
  470. override_offset += sz
  471. }
  472. s.vertex_buffer_cpu_used += shd.vertex_size
  473. }
  474. State :: struct {
  475. allocator: runtime.Allocator,
  476. custom_context: runtime.Context,
  477. win: Window_Interface,
  478. window_state: rawptr,
  479. rb: Rendering_Backend_Interface,
  480. rb_state: rawptr,
  481. shutdown_wanted: bool,
  482. mouse_position: Vec2,
  483. mouse_delta: Vec2,
  484. mouse_wheel_delta: f32,
  485. keys_went_down: #sparse [Keyboard_Key]bool,
  486. keys_went_up: #sparse [Keyboard_Key]bool,
  487. keys_is_held: #sparse [Keyboard_Key]bool,
  488. window: Window_Handle,
  489. width: int,
  490. height: int,
  491. shape_drawing_texture: Texture_Handle,
  492. batch_camera: Maybe(Camera),
  493. batch_shader: Maybe(Shader),
  494. batch_texture: Texture_Handle,
  495. view_matrix: Mat4,
  496. proj_matrix: Mat4,
  497. vertex_buffer_cpu: []u8,
  498. vertex_buffer_cpu_used: int,
  499. default_shader: Shader,
  500. }
  501. @(private="file")
  502. s: ^State
  503. win: Window_Interface
  504. rb: Rendering_Backend_Interface
  505. Shader_Input_Format :: enum {
  506. Unknown,
  507. RGBA32_Float,
  508. RGBA8_Norm,
  509. RGBA8_Norm_SRGB,
  510. RG32_Float,
  511. R32_Float,
  512. }
  513. Color :: [4]u8
  514. Vec2 :: [2]f32
  515. Vec3 :: [3]f32
  516. Mat4 :: matrix[4,4]f32
  517. Vec2i :: [2]int
  518. Rect :: struct {
  519. x, y: f32,
  520. w, h: f32,
  521. }
  522. Texture :: struct {
  523. handle: Texture_Handle,
  524. width: int,
  525. height: int,
  526. }
  527. Shader :: struct {
  528. handle: Shader_Handle,
  529. constant_buffers: []Shader_Constant_Buffer,
  530. constant_lookup: map[string]Shader_Constant_Location,
  531. constant_builtin_locations: [Shader_Builtin_Constant]Maybe(Shader_Constant_Location),
  532. inputs: []Shader_Input,
  533. input_overrides: []Shader_Input_Value_Override,
  534. default_input_offsets: [Shader_Default_Inputs]int,
  535. vertex_size: int,
  536. }
  537. Camera :: struct {
  538. target: Vec2,
  539. origin: Vec2,
  540. rotation: f32,
  541. zoom: f32,
  542. }
  543. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  544. // options presented here.
  545. Mouse_Button :: enum {
  546. Left,
  547. Right,
  548. Middle,
  549. Max = 255,
  550. }
  551. // TODO: These are just copied from raylib, we probably want a list of our own "default colors"
  552. WHITE :: Color { 255, 255, 255, 255 }
  553. BLACK :: Color { 0, 0, 0, 255 }
  554. GRAY :: Color{ 130, 130, 130, 255 }
  555. RED :: Color { 230, 41, 55, 255 }
  556. YELLOW :: Color { 253, 249, 0, 255 }
  557. BLUE :: Color { 0, 121, 241, 255 }
  558. MAGENTA :: Color { 255, 0, 255, 255 }
  559. DARKGRAY :: Color{ 80, 80, 80, 255 }
  560. GREEN :: Color{ 0, 228, 48, 255 }
  561. Shader_Handle :: distinct Handle
  562. SHADER_NONE :: Shader_Handle {}
  563. // Based on Raylib / GLFW
  564. Keyboard_Key :: enum {
  565. None = 0,
  566. // Alphanumeric keys
  567. Apostrophe = 39,
  568. Comma = 44,
  569. Minus = 45,
  570. Period = 46,
  571. Slash = 47,
  572. Zero = 48,
  573. One = 49,
  574. Two = 50,
  575. Three = 51,
  576. Four = 52,
  577. Five = 53,
  578. Six = 54,
  579. Seven = 55,
  580. Eight = 56,
  581. Nine = 57,
  582. Semicolon = 59,
  583. Equal = 61,
  584. A = 65,
  585. B = 66,
  586. C = 67,
  587. D = 68,
  588. E = 69,
  589. F = 70,
  590. G = 71,
  591. H = 72,
  592. I = 73,
  593. J = 74,
  594. K = 75,
  595. L = 76,
  596. M = 77,
  597. N = 78,
  598. O = 79,
  599. P = 80,
  600. Q = 81,
  601. R = 82,
  602. S = 83,
  603. T = 84,
  604. U = 85,
  605. V = 86,
  606. W = 87,
  607. X = 88,
  608. Y = 89,
  609. Z = 90,
  610. Left_Bracket = 91,
  611. Backslash = 92,
  612. Right_Bracket = 93,
  613. Grave = 96,
  614. // Function keys
  615. Space = 32,
  616. Escape = 256,
  617. Enter = 257,
  618. Tab = 258,
  619. Backspace = 259,
  620. Insert = 260,
  621. Delete = 261,
  622. Right = 262,
  623. Left = 263,
  624. Down = 264,
  625. Up = 265,
  626. Page_Up = 266,
  627. Page_Down = 267,
  628. Home = 268,
  629. End = 269,
  630. Caps_Lock = 280,
  631. Scroll_Lock = 281,
  632. Num_Lock = 282,
  633. Print_Screen = 283,
  634. Pause = 284,
  635. F1 = 290,
  636. F2 = 291,
  637. F3 = 292,
  638. F4 = 293,
  639. F5 = 294,
  640. F6 = 295,
  641. F7 = 296,
  642. F8 = 297,
  643. F9 = 298,
  644. F10 = 299,
  645. F11 = 300,
  646. F12 = 301,
  647. Left_Shift = 340,
  648. Left_Control = 341,
  649. Left_Alt = 342,
  650. Left_Super = 343,
  651. Right_Shift = 344,
  652. Right_Control = 345,
  653. Right_Alt = 346,
  654. Right_Super = 347,
  655. Menu = 348,
  656. // Keypad keys
  657. KP_0 = 320,
  658. KP_1 = 321,
  659. KP_2 = 322,
  660. KP_3 = 323,
  661. KP_4 = 324,
  662. KP_5 = 325,
  663. KP_6 = 326,
  664. KP_7 = 327,
  665. KP_8 = 328,
  666. KP_9 = 329,
  667. KP_Decimal = 330,
  668. KP_Divide = 331,
  669. KP_Multiply = 332,
  670. KP_Subtract = 333,
  671. KP_Add = 334,
  672. KP_Enter = 335,
  673. KP_Equal = 336,
  674. }