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. .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{1/c.zoom, 1/c.zoom, 1})
  162. rot := linalg.matrix4_rotate_f32(c.rotation * math.RAD_PER_DEG, {0, 0, 1})
  163. camera_matrix := translate * scale * rot * 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. flip_x, flip_y: bool
  326. src := src
  327. dst := dst
  328. if src.w < 0 {
  329. flip_x = true
  330. src.w = -src.w
  331. }
  332. if src.h < 0 {
  333. flip_y = true
  334. src.h = -src.h
  335. }
  336. if dst.w < 0 {
  337. dst.w *= -1
  338. }
  339. if dst.h < 0 {
  340. dst.h *= -1
  341. }
  342. s.batch_texture = tex.handle
  343. tl, tr, bl, br: Vec2
  344. // Rotation adapted from Raylib's "DrawTexturePro"
  345. if rotation == 0 {
  346. x := dst.x - origin.x
  347. y := dst.y - origin.y
  348. tl = { x, y }
  349. tr = { x + dst.w, y }
  350. bl = { x, y + dst.h }
  351. br = { x + dst.w, y + dst.h }
  352. } else {
  353. sin_rot := math.sin(rotation * math.RAD_PER_DEG)
  354. cos_rot := math.cos(rotation * math.RAD_PER_DEG)
  355. x := dst.x
  356. y := dst.y
  357. dx := -origin.x
  358. dy := -origin.y
  359. tl = {
  360. x + dx * cos_rot - dy * sin_rot,
  361. y + dx * sin_rot + dy * cos_rot,
  362. }
  363. tr = {
  364. x + (dx + dst.w) * cos_rot - dy * sin_rot,
  365. y + (dx + dst.w) * sin_rot + dy * cos_rot,
  366. }
  367. bl = {
  368. x + dx * cos_rot - (dy + dst.h) * sin_rot,
  369. y + dx * sin_rot + (dy + dst.h) * cos_rot,
  370. }
  371. br = {
  372. x + (dx + dst.w) * cos_rot - (dy + dst.h) * sin_rot,
  373. y + (dx + dst.w) * sin_rot + (dy + dst.h) * cos_rot,
  374. }
  375. }
  376. ts := Vec2{f32(tex.width), f32(tex.height)}
  377. up := Vec2{src.x, src.y} / ts
  378. us := Vec2{src.w, src.h} / ts
  379. c := tint
  380. uv0 := up
  381. uv1 := up + {us.x, 0}
  382. uv2 := up + us
  383. uv3 := up
  384. uv4 := up + us
  385. uv5 := up + {0, us.y}
  386. if flip_x {
  387. uv0.x += us.x
  388. uv1.x -= us.x
  389. uv2.x -= us.x
  390. uv3.x += us.x
  391. uv4.x -= us.x
  392. uv5.x += us.x
  393. }
  394. if flip_y {
  395. uv0.y += us.y
  396. uv1.y += us.y
  397. uv2.y -= us.y
  398. uv3.y += us.y
  399. uv4.y -= us.y
  400. uv5.y -= us.y
  401. }
  402. _batch_vertex(tl, uv0, c)
  403. _batch_vertex(tr, uv1, c)
  404. _batch_vertex(br, uv2, c)
  405. _batch_vertex(tl, uv3, c)
  406. _batch_vertex(br, uv4, c)
  407. _batch_vertex(bl, uv5, c)
  408. }
  409. load_shader :: proc(shader_source: string, layout_formats: []Shader_Input_Format = {}) -> Shader {
  410. return rb.load_shader(shader_source, layout_formats)
  411. }
  412. destroy_shader :: proc(shader: Shader) {
  413. rb.destroy_shader(shader)
  414. }
  415. set_shader :: proc(shader: Maybe(Shader)) {
  416. if maybe_handle_equal(shader, s.batch_shader) {
  417. return
  418. }
  419. draw_current_batch()
  420. s.batch_shader = shader
  421. }
  422. maybe_handle_equal :: proc(m1: Maybe($T), m2: Maybe(T)) -> bool {
  423. if m1 == nil && m2 == nil {
  424. return true
  425. }
  426. m1v, m1v_ok := m1.?
  427. m2v, m2v_ok := m2.?
  428. if !m1v_ok || !m2v_ok {
  429. return false
  430. }
  431. return m1v.handle == m2v.handle
  432. }
  433. set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: $T) {
  434. draw_current_batch()
  435. if int(loc.buffer_idx) >= len(shd.constant_buffers) {
  436. log.warnf("Constant buffer idx %v is out of bounds", loc.buffer_idx)
  437. return
  438. }
  439. b := &shd.constant_buffers[loc.buffer_idx]
  440. if int(loc.offset) + size_of(val) > len(b.cpu_data) {
  441. 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))
  442. return
  443. }
  444. dst := (^T)(&b.cpu_data[loc.offset])
  445. dst^ = val
  446. }
  447. set_shader_constant_mat4 :: proc(shader: Shader, loc: Shader_Constant_Location, val: matrix[4,4]f32) {
  448. set_shader_constant(shader, loc, val)
  449. }
  450. set_shader_constant_f32 :: proc(shader: Shader, loc: Shader_Constant_Location, val: f32) {
  451. set_shader_constant(shader, loc, val)
  452. }
  453. set_shader_constant_vec2 :: proc(shader: Shader, loc: Shader_Constant_Location, val: Vec2) {
  454. set_shader_constant(shader, loc, val)
  455. }
  456. get_default_shader :: proc() -> Shader {
  457. return s.default_shader
  458. }
  459. set_scissor_rect :: proc(scissor_rect: Maybe(Rect)) {
  460. panic("not implemented")
  461. }
  462. screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  463. panic("not implemented")
  464. }
  465. draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
  466. }
  467. mouse_button_went_down :: proc(button: Mouse_Button) -> bool {
  468. panic("not implemented")
  469. }
  470. mouse_button_went_up :: proc(button: Mouse_Button) -> bool {
  471. panic("not implemented")
  472. }
  473. mouse_button_is_held :: proc(button: Mouse_Button) -> bool {
  474. panic("not implemented")
  475. }
  476. get_mouse_wheel_delta :: proc() -> f32 {
  477. return s.mouse_wheel_delta
  478. }
  479. get_mouse_position :: proc() -> Vec2 {
  480. return s.mouse_position
  481. }
  482. _batch_vertex :: proc(v: Vec2, uv: Vec2, color: Color) {
  483. v := v
  484. if s.vertex_buffer_cpu_used == len(s.vertex_buffer_cpu) {
  485. panic("Must dispatch here")
  486. }
  487. shd := s.batch_shader.? or_else s.default_shader
  488. base_offset := s.vertex_buffer_cpu_used
  489. pos_offset := shd.default_input_offsets[.Position]
  490. uv_offset := shd.default_input_offsets[.UV]
  491. color_offset := shd.default_input_offsets[.Color]
  492. mem.set(&s.vertex_buffer_cpu[base_offset], 0, shd.vertex_size)
  493. if pos_offset != -1 {
  494. (^Vec2)(&s.vertex_buffer_cpu[base_offset + pos_offset])^ = v
  495. }
  496. if uv_offset != -1 {
  497. (^Vec2)(&s.vertex_buffer_cpu[base_offset + uv_offset])^ = uv
  498. }
  499. if color_offset != -1 {
  500. (^Color)(&s.vertex_buffer_cpu[base_offset + color_offset])^ = color
  501. }
  502. override_offset: int
  503. for &o, idx in shd.input_overrides {
  504. input := &shd.inputs[idx]
  505. sz := shader_input_format_size(input.format)
  506. if o.used != 0 {
  507. mem.copy(&s.vertex_buffer_cpu[base_offset + override_offset], raw_data(&o.val), o.used)
  508. }
  509. override_offset += sz
  510. }
  511. s.vertex_buffer_cpu_used += shd.vertex_size
  512. }
  513. State :: struct {
  514. allocator: runtime.Allocator,
  515. custom_context: runtime.Context,
  516. win: Window_Interface,
  517. window_state: rawptr,
  518. rb: Rendering_Backend_Interface,
  519. rb_state: rawptr,
  520. shutdown_wanted: bool,
  521. mouse_position: Vec2,
  522. mouse_delta: Vec2,
  523. mouse_wheel_delta: f32,
  524. keys_went_down: #sparse [Keyboard_Key]bool,
  525. keys_went_up: #sparse [Keyboard_Key]bool,
  526. keys_is_held: #sparse [Keyboard_Key]bool,
  527. window: Window_Handle,
  528. width: int,
  529. height: int,
  530. shape_drawing_texture: Texture_Handle,
  531. batch_camera: Maybe(Camera),
  532. batch_shader: Maybe(Shader),
  533. batch_texture: Texture_Handle,
  534. view_matrix: Mat4,
  535. proj_matrix: Mat4,
  536. vertex_buffer_cpu: []u8,
  537. vertex_buffer_cpu_used: int,
  538. default_shader: Shader,
  539. }
  540. @(private="file")
  541. s: ^State
  542. win: Window_Interface
  543. rb: Rendering_Backend_Interface
  544. Shader_Input_Format :: enum {
  545. Unknown,
  546. RGBA32_Float,
  547. RGBA8_Norm,
  548. RGBA8_Norm_SRGB,
  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. }