karl2d.odin 19 KB

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