karl2d.odin 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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:strings"
  9. import "core:reflect"
  10. import "core:image"
  11. import "core:image/bmp"
  12. import "core:image/png"
  13. import "core:image/tga"
  14. import hm "handle_map"
  15. //-----------------------------------------------//
  16. // SETUP, WINDOW MANAGEMENT AND FRAME MANAGEMENT //
  17. //-----------------------------------------------//
  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_internal_state`.
  21. init :: proc(window_width: int, window_height: int, window_title: string,
  22. window_creation_flags := Window_Flags {},
  23. allocator := context.allocator, loc := #caller_location) -> ^State {
  24. assert(s == nil, "Don't call 'init' twice.")
  25. s = new(State, allocator, loc)
  26. s.frame_allocator = runtime.arena_allocator(&s.frame_arena)
  27. frame_allocator = s.frame_allocator
  28. s.allocator = allocator
  29. s.custom_context = context
  30. s.width = window_width
  31. s.height = window_height
  32. s.win = WINDOW_INTERFACE_WIN32
  33. win = s.win
  34. window_state_alloc_error: runtime.Allocator_Error
  35. s.window_state, window_state_alloc_error = mem.alloc(win.state_size())
  36. log.assertf(window_state_alloc_error == nil, "Failed allocating memory for window state: %v", window_state_alloc_error)
  37. win.init(s.window_state, window_width, window_height, window_title, window_creation_flags, allocator)
  38. s.window = win.window_handle()
  39. s.rb = RENDER_BACKEND_INTERFACE_D3D11
  40. rb = s.rb
  41. rb_alloc_error: runtime.Allocator_Error
  42. s.rb_state, rb_alloc_error = mem.alloc(rb.state_size())
  43. log.assertf(rb_alloc_error == nil, "Failed allocating memory for rendering backend: %v", rb_alloc_error)
  44. s.proj_matrix = make_default_projection(window_width, window_height)
  45. s.view_matrix = 1
  46. rb.init(s.rb_state, s.window, window_width, window_height, allocator)
  47. s.vertex_buffer_cpu = make([]u8, VERTEX_BUFFER_MAX, allocator, loc)
  48. white_rect: [16*16*4]u8
  49. slice.fill(white_rect[:], 255)
  50. s.shape_drawing_texture = rb.load_texture(white_rect[:], 16, 16)
  51. s.default_shader = load_shader(string(DEFAULT_SHADER_SOURCE))
  52. s.batch_shader = s.default_shader
  53. return s
  54. }
  55. // Returns true if the program wants to shut down. This happens when for example pressing the close
  56. // button on the window. The application can decide if it wants to shut down or if it wants to show
  57. // some kind of confirmation dialogue and shut down later.
  58. //
  59. // Commonly used for creating the "main loop" of a game.
  60. shutdown_wanted :: proc() -> bool {
  61. return s.shutdown_wanted
  62. }
  63. // Closes the window and cleans up the internal state.
  64. shutdown :: proc() {
  65. assert(s != nil, "You've called 'shutdown' without calling 'init' first")
  66. rb.destroy_texture(s.shape_drawing_texture)
  67. destroy_shader(s.default_shader)
  68. rb.shutdown()
  69. delete(s.vertex_buffer_cpu, s.allocator)
  70. win.shutdown()
  71. a := s.allocator
  72. free(s.window_state, a)
  73. free(s.rb_state, a)
  74. free(s, a)
  75. s = nil
  76. }
  77. // Clear the backbuffer with supplied color.
  78. clear :: proc(color: Color) {
  79. rb.clear(color)
  80. }
  81. // Present the backbuffer. Call at end of frame to make everything you've drawn appear on the screen.
  82. present :: proc() {
  83. draw_current_batch()
  84. rb.present()
  85. free_all(s.frame_allocator)
  86. }
  87. // Call at start or end of frame to process all events that have arrived to the window.
  88. //
  89. // WARNING: Not calling this will make your program impossible to interact with.
  90. process_events :: proc() {
  91. s.key_went_up = {}
  92. s.key_went_down = {}
  93. s.mouse_button_went_up = {}
  94. s.mouse_button_went_down = {}
  95. s.mouse_delta = {}
  96. s.mouse_wheel_delta = 0
  97. win.process_events()
  98. events := win.get_events()
  99. for &event in events {
  100. switch &e in event {
  101. case Window_Event_Close_Wanted:
  102. s.shutdown_wanted = true
  103. case Window_Event_Key_Went_Down:
  104. s.key_went_down[e.key] = true
  105. s.key_is_held[e.key] = true
  106. case Window_Event_Key_Went_Up:
  107. s.key_went_up[e.key] = true
  108. s.key_is_held[e.key] = false
  109. case Window_Event_Mouse_Button_Went_Down:
  110. s.mouse_button_went_down[e.button] = true
  111. s.mouse_button_is_held[e.button] = true
  112. case Window_Event_Mouse_Button_Went_Up:
  113. s.mouse_button_went_up[e.button] = true
  114. s.mouse_button_is_held[e.button] = false
  115. case Window_Event_Mouse_Move:
  116. prev_pos := s.mouse_position
  117. s.mouse_position = e.position
  118. s.mouse_delta = prev_pos - s.mouse_position
  119. case Window_Event_Mouse_Wheel:
  120. s.mouse_wheel_delta = e.delta
  121. case Window_Event_Gamepad_Button_Went_Down:
  122. if e.gamepad < MAX_GAMEPADS {
  123. s.gamepad_button_went_down[e.gamepad][e.button] = true
  124. s.gamepad_button_is_held[e.gamepad][e.button] = true
  125. }
  126. case Window_Event_Gamepad_Button_Went_Up:
  127. if e.gamepad < MAX_GAMEPADS {
  128. s.gamepad_button_went_up[e.gamepad][e.button] = true
  129. s.gamepad_button_is_held[e.gamepad][e.button] = false
  130. }
  131. case Window_Event_Resize:
  132. s.width = e.width
  133. s.height = e.height
  134. rb.resize_swapchain(s.width, s.height)
  135. s.proj_matrix = make_default_projection(s.width, s.height)
  136. }
  137. }
  138. win.clear_events()
  139. }
  140. get_screen_width :: proc() -> int {
  141. return s.width
  142. }
  143. get_screen_height :: proc() -> int {
  144. return s.height
  145. }
  146. set_window_position :: proc(x: int, y: int) {
  147. win.set_position(x, y)
  148. }
  149. set_window_size :: proc(width: int, height: int) {
  150. // TODO not sure if we should resize swapchain here. On windows the WM_SIZE event fires and
  151. // it all works out. But perhaps not on all platforms?
  152. win.set_size(width, height)
  153. }
  154. // Fetch the scale of the window. This usually comes from some DPI scaling setting in the OS.
  155. // 1 means 100% scale, 1.5 means 150% etc.
  156. get_window_scale :: proc() -> f32 {
  157. return win.get_window_scale()
  158. }
  159. set_window_flags :: proc(flags: Window_Flags) {
  160. win.set_flags(flags)
  161. }
  162. // Flushes the current batch. This sends off everything to the GPU that has been queued in the
  163. // current batch. Normally, you do not need to do this manually. It is done automatically when these
  164. // procedures run:
  165. //
  166. // - present
  167. // - set_camera
  168. // - set_shader
  169. // - set_shader_constant
  170. // - set_scissor_rect
  171. // - draw_texture_* IF previous draw did not use the same texture (1)
  172. // - draw_rect_*, draw_circle_*, draw_line IF previous draw did not use the shapes drawing texture (2)
  173. //
  174. // (1) When drawing textures, the current texture is fed into the active shader. Everything within
  175. // the same batch must use the same texture. So drawing with a new texture will draw the current
  176. // batch. You can combine several textures into an atlas to get bigger batches.
  177. //
  178. // (2) In order to use the same shader for shapes drawing and textured drawing, the shapes drawing
  179. // uses a blank, white texture. For the same reasons as (1), drawing something else than shapes
  180. // before drawing a shape will break up the batches. TODO: Add possibility to customize shape
  181. // drawing texture so that you can put it into an atlas.
  182. //
  183. // The batch has maximum size of VERTEX_BUFFER_MAX bytes. The shader dictates how big a vertex is
  184. // so the maximum number of vertices that can be drawn in each batch is
  185. // VERTEX_BUFFER_MAX / shader.vertex_size
  186. draw_current_batch :: proc() {
  187. rb.draw(s.batch_shader, s.batch_texture, s.proj_matrix * s.view_matrix, s.batch_scissor, s.vertex_buffer_cpu[:s.vertex_buffer_cpu_used])
  188. s.vertex_buffer_cpu_used = 0
  189. }
  190. //-------//
  191. // INPUT //
  192. //-------//
  193. // Returns true if a keyboard key went down between the current and the previous frame. Set when
  194. // 'process_events' runs (probably once per frame).
  195. key_went_down :: proc(key: Keyboard_Key) -> bool {
  196. return s.key_went_down[key]
  197. }
  198. // Returns true if a keyboard key went up (was released) between the current and the previous frame.
  199. // Set when 'process_events' runs (probably once per frame).
  200. key_went_up :: proc(key: Keyboard_Key) -> bool {
  201. return s.key_went_up[key]
  202. }
  203. // Returns true if a keyboard is currently being held down. Set when 'process_events' runs (probably
  204. // once per frame).
  205. key_is_held :: proc(key: Keyboard_Key) -> bool {
  206. return s.key_is_held[key]
  207. }
  208. mouse_button_went_down :: proc(button: Mouse_Button) -> bool {
  209. return s.mouse_button_went_down[button]
  210. }
  211. mouse_button_went_up :: proc(button: Mouse_Button) -> bool {
  212. return s.mouse_button_went_up[button]
  213. }
  214. mouse_button_is_held :: proc(button: Mouse_Button) -> bool {
  215. return s.mouse_button_is_held[button]
  216. }
  217. get_mouse_wheel_delta :: proc() -> f32 {
  218. return s.mouse_wheel_delta
  219. }
  220. get_mouse_position :: proc() -> Vec2 {
  221. return s.mouse_position
  222. }
  223. gamepad_button_went_down :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool {
  224. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  225. return false
  226. }
  227. return s.gamepad_button_went_down[gamepad][button]
  228. }
  229. gamepad_button_went_up :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool {
  230. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  231. return false
  232. }
  233. return s.gamepad_button_went_up[gamepad][button]
  234. }
  235. gamepad_button_is_held :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool {
  236. if gamepad < 0 || gamepad >= MAX_GAMEPADS {
  237. return false
  238. }
  239. return s.gamepad_button_is_held[gamepad][button]
  240. }
  241. get_gamepad_axis :: proc(gamepad: Gamepad_Index, axis: Gamepad_Axis) -> f32 {
  242. return win.get_gamepad_axis(gamepad, axis)
  243. }
  244. // Set the left and right vibration motor speed. The range of left and right is 0 to 1. Note that on
  245. // most gamepads, the left motor is "low frequency" and the right motor is "high frequency". They do
  246. // not vibrate with the same speed.
  247. set_gamepad_vibration :: proc(gamepad: Gamepad_Index, left: f32, right: f32) {
  248. win.set_gamepad_vibration(gamepad, left, right)
  249. }
  250. //---------//
  251. // DRAWING //
  252. //---------//
  253. draw_rect :: proc(r: Rect, c: Color) {
  254. if s.vertex_buffer_cpu_used + s.batch_shader.vertex_size * 6 > len(s.vertex_buffer_cpu) {
  255. draw_current_batch()
  256. }
  257. if s.batch_texture != s.shape_drawing_texture {
  258. draw_current_batch()
  259. }
  260. s.batch_texture = s.shape_drawing_texture
  261. batch_vertex({r.x, r.y}, {0, 0}, c)
  262. batch_vertex({r.x + r.w, r.y}, {1, 0}, c)
  263. batch_vertex({r.x + r.w, r.y + r.h}, {1, 1}, c)
  264. batch_vertex({r.x, r.y}, {0, 0}, c)
  265. batch_vertex({r.x + r.w, r.y + r.h}, {1, 1}, c)
  266. batch_vertex({r.x, r.y + r.h}, {0, 1}, c)
  267. }
  268. draw_rect_vec :: proc(pos: Vec2, size: Vec2, c: Color) {
  269. draw_rect({pos.x, pos.y, size.x, size.y}, c)
  270. }
  271. draw_rect_ex :: proc(r: Rect, origin: Vec2, rot: f32, c: Color) {
  272. if s.vertex_buffer_cpu_used + s.batch_shader.vertex_size * 6 > len(s.vertex_buffer_cpu) {
  273. draw_current_batch()
  274. }
  275. if s.batch_texture != s.shape_drawing_texture {
  276. draw_current_batch()
  277. }
  278. s.batch_texture = s.shape_drawing_texture
  279. tl, tr, bl, br: Vec2
  280. // Rotation adapted from Raylib's "DrawTexturePro"
  281. if rot == 0 {
  282. x := r.x - origin.x
  283. y := r.y - origin.y
  284. tl = { x, y }
  285. tr = { x + r.w, y }
  286. bl = { x, y + r.h }
  287. br = { x + r.w, y + r.h }
  288. } else {
  289. sin_rot := math.sin(rot * math.RAD_PER_DEG)
  290. cos_rot := math.cos(rot * math.RAD_PER_DEG)
  291. x := r.x
  292. y := r.y
  293. dx := -origin.x
  294. dy := -origin.y
  295. tl = {
  296. x + dx * cos_rot - dy * sin_rot,
  297. y + dx * sin_rot + dy * cos_rot,
  298. }
  299. tr = {
  300. x + (dx + r.w) * cos_rot - dy * sin_rot,
  301. y + (dx + r.w) * sin_rot + dy * cos_rot,
  302. }
  303. bl = {
  304. x + dx * cos_rot - (dy + r.h) * sin_rot,
  305. y + dx * sin_rot + (dy + r.h) * cos_rot,
  306. }
  307. br = {
  308. x + (dx + r.w) * cos_rot - (dy + r.h) * sin_rot,
  309. y + (dx + r.w) * sin_rot + (dy + r.h) * cos_rot,
  310. }
  311. }
  312. batch_vertex(tl, {0, 0}, c)
  313. batch_vertex(tr, {1, 0}, c)
  314. batch_vertex(br, {1, 1}, c)
  315. batch_vertex(tl, {0, 0}, c)
  316. batch_vertex(br, {1, 1}, c)
  317. batch_vertex(bl, {0, 1}, c)
  318. }
  319. draw_rect_outline :: proc(r: Rect, thickness: f32, color: Color) {
  320. t := thickness
  321. // Based on DrawRectangleLinesEx from Raylib
  322. top := Rect {
  323. r.x,
  324. r.y,
  325. r.w,
  326. t,
  327. }
  328. bottom := Rect {
  329. r.x,
  330. r.y + r.h - t,
  331. r.w,
  332. t,
  333. }
  334. left := Rect {
  335. r.x,
  336. r.y + t,
  337. t,
  338. r.h - t * 2,
  339. }
  340. right := Rect {
  341. r.x + r.w - t,
  342. r.y + t,
  343. t,
  344. r.h - t * 2,
  345. }
  346. draw_rect(top, color)
  347. draw_rect(bottom, color)
  348. draw_rect(left, color)
  349. draw_rect(right, color)
  350. }
  351. draw_circle :: proc(center: Vec2, radius: f32, color: Color, segments := 16) {
  352. if s.vertex_buffer_cpu_used + s.batch_shader.vertex_size * 3 * segments > len(s.vertex_buffer_cpu) {
  353. draw_current_batch()
  354. }
  355. if s.batch_texture != s.shape_drawing_texture {
  356. draw_current_batch()
  357. }
  358. s.batch_texture = s.shape_drawing_texture
  359. prev := center + {radius, 0}
  360. for s in 1..=segments {
  361. sr := (f32(s)/f32(segments)) * 2*math.PI
  362. rot := linalg.matrix2_rotate(sr)
  363. p := center + rot * Vec2{radius, 0}
  364. batch_vertex(prev, {0, 0}, color)
  365. batch_vertex(p, {1, 0}, color)
  366. batch_vertex(center, {1, 1}, color)
  367. prev = p
  368. }
  369. }
  370. draw_circle_outline :: proc(center: Vec2, radius: f32, thickness: f32, color: Color, segments := 16) {
  371. prev := center + {radius, 0}
  372. for s in 1..=segments {
  373. sr := (f32(s)/f32(segments)) * 2*math.PI
  374. rot := linalg.matrix2_rotate(sr)
  375. p := center + rot * Vec2{radius, 0}
  376. draw_line(prev, p, thickness, color)
  377. prev = p
  378. }
  379. }
  380. draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) {
  381. p := Vec2{start.x, start.y + thickness*0.5}
  382. s := Vec2{linalg.length(end - start), thickness}
  383. origin := Vec2 {0, thickness*0.5}
  384. r := Rect {p.x, p.y, s.x, s.y}
  385. rot := math.atan2(end.y - start.y, end.x - start.x)
  386. draw_rect_ex(r, origin, rot * math.DEG_PER_RAD, color)
  387. }
  388. draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE) {
  389. draw_texture_ex(
  390. tex,
  391. {0, 0, f32(tex.width), f32(tex.height)},
  392. {pos.x, pos.y, f32(tex.width), f32(tex.height)},
  393. {},
  394. 0,
  395. tint,
  396. )
  397. }
  398. draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) {
  399. draw_texture_ex(
  400. tex,
  401. rect,
  402. {pos.x, pos.y, rect.w, rect.h},
  403. {},
  404. 0,
  405. tint,
  406. )
  407. }
  408. draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rotation: f32, tint := WHITE) {
  409. if tex.width == 0 || tex.height == 0 {
  410. return
  411. }
  412. if s.vertex_buffer_cpu_used + s.batch_shader.vertex_size * 6 > len(s.vertex_buffer_cpu) {
  413. draw_current_batch()
  414. }
  415. if s.batch_texture != tex.handle {
  416. draw_current_batch()
  417. }
  418. s.batch_texture = tex.handle
  419. flip_x, flip_y: bool
  420. src := src
  421. dst := dst
  422. if src.w < 0 {
  423. flip_x = true
  424. src.w = -src.w
  425. }
  426. if src.h < 0 {
  427. flip_y = true
  428. src.h = -src.h
  429. }
  430. if dst.w < 0 {
  431. dst.w *= -1
  432. }
  433. if dst.h < 0 {
  434. dst.h *= -1
  435. }
  436. tl, tr, bl, br: Vec2
  437. // Rotation adapted from Raylib's "DrawTexturePro"
  438. if rotation == 0 {
  439. x := dst.x - origin.x
  440. y := dst.y - origin.y
  441. tl = { x, y }
  442. tr = { x + dst.w, y }
  443. bl = { x, y + dst.h }
  444. br = { x + dst.w, y + dst.h }
  445. } else {
  446. sin_rot := math.sin(rotation * math.RAD_PER_DEG)
  447. cos_rot := math.cos(rotation * math.RAD_PER_DEG)
  448. x := dst.x
  449. y := dst.y
  450. dx := -origin.x
  451. dy := -origin.y
  452. tl = {
  453. x + dx * cos_rot - dy * sin_rot,
  454. y + dx * sin_rot + dy * cos_rot,
  455. }
  456. tr = {
  457. x + (dx + dst.w) * cos_rot - dy * sin_rot,
  458. y + (dx + dst.w) * sin_rot + dy * cos_rot,
  459. }
  460. bl = {
  461. x + dx * cos_rot - (dy + dst.h) * sin_rot,
  462. y + dx * sin_rot + (dy + dst.h) * cos_rot,
  463. }
  464. br = {
  465. x + (dx + dst.w) * cos_rot - (dy + dst.h) * sin_rot,
  466. y + (dx + dst.w) * sin_rot + (dy + dst.h) * cos_rot,
  467. }
  468. }
  469. ts := Vec2{f32(tex.width), f32(tex.height)}
  470. up := Vec2{src.x, src.y} / ts
  471. us := Vec2{src.w, src.h} / ts
  472. c := tint
  473. uv0 := up
  474. uv1 := up + {us.x, 0}
  475. uv2 := up + us
  476. uv3 := up
  477. uv4 := up + us
  478. uv5 := up + {0, us.y}
  479. if flip_x {
  480. uv0.x += us.x
  481. uv1.x -= us.x
  482. uv2.x -= us.x
  483. uv3.x += us.x
  484. uv4.x -= us.x
  485. uv5.x += us.x
  486. }
  487. if flip_y {
  488. uv0.y += us.y
  489. uv1.y += us.y
  490. uv2.y -= us.y
  491. uv3.y += us.y
  492. uv4.y -= us.y
  493. uv5.y -= us.y
  494. }
  495. batch_vertex(tl, uv0, c)
  496. batch_vertex(tr, uv1, c)
  497. batch_vertex(br, uv2, c)
  498. batch_vertex(tl, uv3, c)
  499. batch_vertex(br, uv4, c)
  500. batch_vertex(bl, uv5, c)
  501. }
  502. draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
  503. }
  504. //--------------------//
  505. // TEXTURE MANAGEMENT //
  506. //--------------------//
  507. load_texture_from_file :: proc(filename: string) -> Texture {
  508. img, img_err := image.load_from_file(filename, options = {.alpha_add_if_missing}, allocator = s.frame_allocator)
  509. if img_err != nil {
  510. log.errorf("Error loading texture %v: %v", filename, img_err)
  511. return {}
  512. }
  513. backend_tex := rb.load_texture(img.pixels.buf[:], img.width, img.height)
  514. return {
  515. handle = backend_tex,
  516. width = img.width,
  517. height = img.height,
  518. }
  519. }
  520. destroy_texture :: proc(tex: Texture) {
  521. rb.destroy_texture(tex.handle)
  522. }
  523. //---------//
  524. // SHADERS //
  525. //---------//
  526. load_shader :: proc(shader_source: string, layout_formats: []Pixel_Format = {}) -> Shader {
  527. handle, desc := rb.load_shader(shader_source, s.frame_allocator, layout_formats)
  528. if handle == SHADER_NONE {
  529. log.error("Failed loading shader")
  530. return {}
  531. }
  532. shd := Shader {
  533. handle = handle,
  534. constant_buffers = make([]Shader_Constant_Buffer, len(desc.constant_buffers), s.allocator),
  535. constant_lookup = make(map[string]Shader_Constant_Location, s.allocator),
  536. inputs = slice.clone(desc.inputs, s.allocator),
  537. input_overrides = make([]Shader_Input_Value_Override, len(desc.inputs), s.allocator),
  538. }
  539. for &input in shd.inputs {
  540. input.name = strings.clone(input.name, s.allocator)
  541. }
  542. for cb_idx in 0..<len(desc.constant_buffers) {
  543. cb_desc := &desc.constant_buffers[cb_idx]
  544. shd.constant_buffers[cb_idx] = {
  545. cpu_data = make([]u8, desc.constant_buffers[cb_idx].size, s.allocator),
  546. }
  547. for &v in cb_desc.variables {
  548. if v.name == "" {
  549. continue
  550. }
  551. shd.constant_lookup[strings.clone(v.name, s.allocator)] = v.loc
  552. switch v.name {
  553. case "mvp":
  554. shd.constant_builtin_locations[.MVP] = v.loc
  555. }
  556. }
  557. }
  558. for &d in shd.default_input_offsets {
  559. d = -1
  560. }
  561. input_offset: int
  562. for &input in shd.inputs {
  563. default_format := get_shader_input_default_type(input.name, input.type)
  564. if default_format != .Unknown {
  565. shd.default_input_offsets[default_format] = input_offset
  566. }
  567. input_offset += pixel_format_size(input.format)
  568. }
  569. shd.vertex_size = input_offset
  570. return shd
  571. }
  572. destroy_shader :: proc(shader: Shader) {
  573. rb.destroy_shader(shader.handle)
  574. for c in shader.constant_buffers {
  575. delete(c.cpu_data)
  576. }
  577. delete(shader.constant_buffers)
  578. for k, _ in shader.constant_lookup {
  579. delete(k)
  580. }
  581. delete(shader.constant_lookup)
  582. for i in shader.inputs {
  583. delete(i.name)
  584. }
  585. delete(shader.inputs)
  586. delete(shader.input_overrides)
  587. }
  588. get_default_shader :: proc() -> Shader {
  589. return s.default_shader
  590. }
  591. set_shader :: proc(shader: Maybe(Shader)) {
  592. if shd, shd_ok := shader.?; shd_ok {
  593. if shd.handle == s.batch_shader.handle {
  594. return
  595. }
  596. } else {
  597. if s.batch_shader.handle == s.default_shader.handle {
  598. return
  599. }
  600. }
  601. draw_current_batch()
  602. s.batch_shader = shader.? or_else s.default_shader
  603. }
  604. set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: any) {
  605. draw_current_batch()
  606. if int(loc.buffer_idx) >= len(shd.constant_buffers) {
  607. log.warnf("Constant buffer idx %v is out of bounds", loc.buffer_idx)
  608. return
  609. }
  610. sz := reflect.size_of_typeid(val.id)
  611. b := &shd.constant_buffers[loc.buffer_idx]
  612. if int(loc.offset) + sz > len(b.cpu_data) {
  613. 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))
  614. return
  615. }
  616. mem.copy(&b.cpu_data[loc.offset], val.data, sz)
  617. }
  618. override_shader_input :: proc(shader: Shader, input: int, val: any) {
  619. sz := reflect.size_of_typeid(val.id)
  620. assert(sz < SHADER_INPUT_VALUE_MAX_SIZE)
  621. if input >= len(shader.input_overrides) {
  622. log.errorf("Input override out of range. Wanted to override input %v, but shader only has %v inputs", input, len(shader.input_overrides))
  623. return
  624. }
  625. o := &shader.input_overrides[input]
  626. o.val = {}
  627. if sz > 0 {
  628. mem.copy(raw_data(&o.val), val.data, sz)
  629. }
  630. o.used = sz
  631. }
  632. pixel_format_size :: proc(f: Pixel_Format) -> int {
  633. switch f {
  634. case .Unknown: return 0
  635. case .RGBA_32_Float: return 32
  636. case .RGB_32_Float: return 12
  637. case .RG_32_Float: return 8
  638. case .R_32_Float: return 4
  639. case .RGBA_8_Norm: return 4
  640. case .RG_8_Norm: return 2
  641. case .R_8_Norm: return 1
  642. }
  643. return 0
  644. }
  645. //-------------------------------//
  646. // CAMERA AND COORDINATE SYSTEMS //
  647. //-------------------------------//
  648. set_camera :: proc(camera: Maybe(Camera)) {
  649. if camera == s.batch_camera {
  650. return
  651. }
  652. draw_current_batch()
  653. s.batch_camera = camera
  654. s.proj_matrix = make_default_projection(s.width, s.height)
  655. if c, c_ok := camera.?; c_ok {
  656. s.view_matrix = get_camera_view_matrix(c)
  657. } else {
  658. s.view_matrix = 1
  659. }
  660. }
  661. screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  662. return (get_camera_world_matrix(camera) * Vec4 { pos.x, pos.y, 0, 1 }).xy
  663. }
  664. world_to_screen :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  665. return (get_camera_view_matrix(camera) * Vec4 { pos.x, pos.y, 0, 1 }).xy
  666. }
  667. get_camera_view_matrix :: proc(c: Camera) -> Mat4 {
  668. inv_target_translate := linalg.matrix4_translate(vec3_from_vec2(-c.target))
  669. inv_rot := linalg.matrix4_rotate_f32(c.rotation * math.RAD_PER_DEG, {0, 0, 1})
  670. inv_scale := linalg.matrix4_scale(Vec3{c.zoom, c.zoom, 1})
  671. inv_offset_translate := linalg.matrix4_translate(vec3_from_vec2(c.offset))
  672. // A view matrix is essentially the world transform matrix of the camera, but inverted. We
  673. // bring everything in the world "in front of the camera".
  674. //
  675. // Instead of constructing the camera matrix and doing a matrix inverse, here we just do the
  676. // maths in "backwards order". I.e. a camera transform matrix would be:
  677. //
  678. // target_translate * rot * scale * offset_translate
  679. return inv_offset_translate * inv_scale * inv_rot * inv_target_translate
  680. }
  681. get_camera_world_matrix :: proc(c: Camera) -> Mat4 {
  682. offset_translate := linalg.matrix4_translate(vec3_from_vec2(-c.offset))
  683. rot := linalg.matrix4_rotate_f32(-c.rotation * math.RAD_PER_DEG, {0, 0, 1})
  684. scale := linalg.matrix4_scale(Vec3{1/c.zoom, 1/c.zoom, 1})
  685. target_translate := linalg.matrix4_translate(vec3_from_vec2(c.target))
  686. return target_translate * rot * scale * offset_translate
  687. }
  688. //------//
  689. // MISC //
  690. //------//
  691. set_scissor_rect :: proc(scissor_rect: Maybe(Rect)) {
  692. draw_current_batch()
  693. s.batch_scissor = scissor_rect
  694. }
  695. // Restore the internal state using the pointer returned by `init`. Useful after reloading the
  696. // library (for example, when doing code hot reload).
  697. set_internal_state :: proc(state: ^State) {
  698. s = state
  699. rb = s.rb
  700. win = s.win
  701. rb.set_internal_state(s.rb_state)
  702. win.set_internal_state(s.window_state)
  703. }
  704. //---------------------//
  705. // TYPES AND CONSTANTS //
  706. //---------------------//
  707. Vec2 :: [2]f32
  708. Vec3 :: [3]f32
  709. Vec4 :: [4]f32
  710. Mat4 :: matrix[4,4]f32
  711. // A two dimensional vector of integer numeric type.
  712. Vec2i :: [2]int
  713. // A rectangle that sits at position (x, y) and has size (w, h).
  714. Rect :: struct {
  715. x, y: f32,
  716. w, h: f32,
  717. }
  718. // An RGBA (Red, Green, Blue, Alpha) color. Each channel can have a value between 0 and 255.
  719. Color :: [4]u8
  720. WHITE :: Color { 255, 255, 255, 255 }
  721. BLACK :: Color { 0, 0, 0, 255 }
  722. GRAY :: Color { 127, 127, 127, 255 }
  723. RED :: Color { 198, 80, 90, 255 }
  724. BLANK :: Color { 0, 0, 0, 0 }
  725. BLUE :: Color { 30, 116, 240, 255 }
  726. // These are from Raylib. They are here so you can easily port a Raylib program to Karl2D.
  727. RL_LIGHTGRAY :: Color { 200, 200, 200, 255 }
  728. RL_GRAY :: Color { 130, 130, 130, 255 }
  729. RL_DARKGRAY :: Color { 80, 80, 80, 255 }
  730. RL_YELLOW :: Color { 253, 249, 0, 255 }
  731. RL_GOLD :: Color { 255, 203, 0, 255 }
  732. RL_ORANGE :: Color { 255, 161, 0, 255 }
  733. RL_PINK :: Color { 255, 109, 194, 255 }
  734. RL_RED :: Color { 230, 41, 55, 255 }
  735. RL_MAROON :: Color { 190, 33, 55, 255 }
  736. RL_GREEN :: Color { 0, 228, 48, 255 }
  737. RL_LIME :: Color { 0, 158, 47, 255 }
  738. RL_DARKGREEN :: Color { 0, 117, 44, 255 }
  739. RL_SKYBLUE :: Color { 102, 191, 255, 255 }
  740. RL_BLUE :: Color { 0, 121, 241, 255 }
  741. RL_DARKBLUE :: Color { 0, 82, 172, 255 }
  742. RL_PURPLE :: Color { 200, 122, 255, 255 }
  743. RL_VIOLET :: Color { 135, 60, 190, 255 }
  744. RL_DARKPURPLE :: Color { 112, 31, 126, 255 }
  745. RL_BEIGE :: Color { 211, 176, 131, 255 }
  746. RL_BROWN :: Color { 127, 106, 79, 255 }
  747. RL_DARKBROWN :: Color { 76, 63, 47, 255 }
  748. RL_WHITE :: WHITE
  749. RL_BLACK :: BLACK
  750. RL_BLANK :: BLANK
  751. RL_MAGENTA :: Color { 255, 0, 255, 255 }
  752. RL_RAYWHITE :: Color { 245, 245, 245, 255 }
  753. Texture :: struct {
  754. handle: Texture_Handle,
  755. width: int,
  756. height: int,
  757. }
  758. Camera :: struct {
  759. target: Vec2,
  760. offset: Vec2,
  761. rotation: f32,
  762. zoom: f32,
  763. }
  764. Window_Flag :: enum {
  765. Resizable,
  766. }
  767. Window_Flags :: bit_set[Window_Flag]
  768. Shader_Handle :: distinct Handle
  769. SHADER_NONE :: Shader_Handle {}
  770. Shader :: struct {
  771. handle: Shader_Handle,
  772. constant_buffers: []Shader_Constant_Buffer,
  773. constant_lookup: map[string]Shader_Constant_Location,
  774. constant_builtin_locations: [Shader_Builtin_Constant]Maybe(Shader_Constant_Location),
  775. inputs: []Shader_Input,
  776. input_overrides: []Shader_Input_Value_Override,
  777. default_input_offsets: [Shader_Default_Inputs]int,
  778. vertex_size: int,
  779. }
  780. Shader_Constant_Buffer :: struct {
  781. cpu_data: []u8,
  782. }
  783. SHADER_INPUT_VALUE_MAX_SIZE :: 256
  784. Shader_Input_Value_Override :: struct {
  785. val: [SHADER_INPUT_VALUE_MAX_SIZE]u8,
  786. used: int,
  787. }
  788. Shader_Input_Type :: enum {
  789. F32,
  790. Vec2,
  791. Vec3,
  792. Vec4,
  793. }
  794. Shader_Builtin_Constant :: enum {
  795. MVP,
  796. }
  797. Shader_Default_Inputs :: enum {
  798. Unknown,
  799. Position,
  800. UV,
  801. Color,
  802. }
  803. Shader_Input :: struct {
  804. name: string,
  805. register: int,
  806. type: Shader_Input_Type,
  807. format: Pixel_Format,
  808. }
  809. Shader_Constant_Location :: struct {
  810. buffer_idx: u32,
  811. offset: u32,
  812. }
  813. Pixel_Format :: enum {
  814. Unknown,
  815. RGBA_32_Float,
  816. RGB_32_Float,
  817. RG_32_Float,
  818. R_32_Float,
  819. RGBA_8_Norm,
  820. RG_8_Norm,
  821. R_8_Norm,
  822. }
  823. Handle :: hm.Handle
  824. Texture_Handle :: distinct Handle
  825. TEXTURE_NONE :: Texture_Handle {}
  826. // This keeps track of the internal state of the library. Usually, you do not need to poke at it.
  827. // It is created and kept as a global variable when 'init' is called. However, 'init' also returns
  828. // the pointer to it, so you can later use 'set_internal_state' to restore it (after for example hot
  829. // reload).
  830. State :: struct {
  831. allocator: runtime.Allocator,
  832. frame_arena: runtime.Arena,
  833. frame_allocator: runtime.Allocator,
  834. custom_context: runtime.Context,
  835. win: Window_Interface,
  836. window_state: rawptr,
  837. rb: Render_Backend_Interface,
  838. rb_state: rawptr,
  839. shutdown_wanted: bool,
  840. mouse_position: Vec2,
  841. mouse_delta: Vec2,
  842. mouse_wheel_delta: f32,
  843. key_went_down: #sparse [Keyboard_Key]bool,
  844. key_went_up: #sparse [Keyboard_Key]bool,
  845. key_is_held: #sparse [Keyboard_Key]bool,
  846. mouse_button_went_down: #sparse [Mouse_Button]bool,
  847. mouse_button_went_up: #sparse [Mouse_Button]bool,
  848. mouse_button_is_held: #sparse [Mouse_Button]bool,
  849. gamepad_button_went_down: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  850. gamepad_button_went_up: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  851. gamepad_button_is_held: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  852. window: Window_Handle,
  853. width: int,
  854. height: int,
  855. shape_drawing_texture: Texture_Handle,
  856. batch_camera: Maybe(Camera),
  857. batch_shader: Shader,
  858. batch_scissor: Maybe(Rect),
  859. batch_texture: Texture_Handle,
  860. view_matrix: Mat4,
  861. proj_matrix: Mat4,
  862. vertex_buffer_cpu: []u8,
  863. vertex_buffer_cpu_used: int,
  864. default_shader: Shader,
  865. }
  866. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  867. // options presented here.
  868. Mouse_Button :: enum {
  869. Left,
  870. Right,
  871. Middle,
  872. Max = 255,
  873. }
  874. // Based on Raylib / GLFW
  875. Keyboard_Key :: enum {
  876. None = 0,
  877. // Numeric keys (top row)
  878. N0 = 48,
  879. N1 = 49,
  880. N2 = 50,
  881. N3 = 51,
  882. N4 = 52,
  883. N5 = 53,
  884. N6 = 54,
  885. N7 = 55,
  886. N8 = 56,
  887. N9 = 57,
  888. // Letter keys
  889. A = 65,
  890. B = 66,
  891. C = 67,
  892. D = 68,
  893. E = 69,
  894. F = 70,
  895. G = 71,
  896. H = 72,
  897. I = 73,
  898. J = 74,
  899. K = 75,
  900. L = 76,
  901. M = 77,
  902. N = 78,
  903. O = 79,
  904. P = 80,
  905. Q = 81,
  906. R = 82,
  907. S = 83,
  908. T = 84,
  909. U = 85,
  910. V = 86,
  911. W = 87,
  912. X = 88,
  913. Y = 89,
  914. Z = 90,
  915. // Special characters
  916. Apostrophe = 39,
  917. Comma = 44,
  918. Minus = 45,
  919. Period = 46,
  920. Slash = 47,
  921. Semicolon = 59,
  922. Equal = 61,
  923. Left_Bracket = 91,
  924. Backslash = 92,
  925. Right_Bracket = 93,
  926. Grave_Accent = 96,
  927. // Function keys, modifiers, caret control etc
  928. Space = 32,
  929. Escape = 256,
  930. Enter = 257,
  931. Tab = 258,
  932. Backspace = 259,
  933. Insert = 260,
  934. Delete = 261,
  935. Right = 262,
  936. Left = 263,
  937. Down = 264,
  938. Up = 265,
  939. Page_Up = 266,
  940. Page_Down = 267,
  941. Home = 268,
  942. End = 269,
  943. Caps_Lock = 280,
  944. Scroll_Lock = 281,
  945. Num_Lock = 282,
  946. Print_Screen = 283,
  947. Pause = 284,
  948. F1 = 290,
  949. F2 = 291,
  950. F3 = 292,
  951. F4 = 293,
  952. F5 = 294,
  953. F6 = 295,
  954. F7 = 296,
  955. F8 = 297,
  956. F9 = 298,
  957. F10 = 299,
  958. F11 = 300,
  959. F12 = 301,
  960. Left_Shift = 340,
  961. Left_Control = 341,
  962. Left_Alt = 342,
  963. Left_Super = 343,
  964. Right_Shift = 344,
  965. Right_Control = 345,
  966. Right_Alt = 346,
  967. Right_Super = 347,
  968. Menu = 348,
  969. // Numpad keys
  970. NP_0 = 320,
  971. NP_1 = 321,
  972. NP_2 = 322,
  973. NP_3 = 323,
  974. NP_4 = 324,
  975. NP_5 = 325,
  976. NP_6 = 326,
  977. NP_7 = 327,
  978. NP_8 = 328,
  979. NP_9 = 329,
  980. NP_Decimal = 330,
  981. NP_Divide = 331,
  982. NP_Multiply = 332,
  983. NP_Subtract = 333,
  984. NP_Add = 334,
  985. NP_Enter = 335,
  986. NP_Equal = 336,
  987. }
  988. MAX_GAMEPADS :: 4
  989. // A value between 0 and MAX_GAMEPADS - 1
  990. Gamepad_Index :: int
  991. Gamepad_Axis :: enum {
  992. Left_Stick_X,
  993. Left_Stick_Y,
  994. Right_Stick_X,
  995. Right_Stick_Y,
  996. Left_Trigger,
  997. Right_Trigger,
  998. }
  999. Gamepad_Button :: enum {
  1000. // DPAD buttons
  1001. Left_Face_Up,
  1002. Left_Face_Down,
  1003. Left_Face_Left,
  1004. Left_Face_Right,
  1005. Right_Face_Up, // XBOX: Y, PS: Triangle
  1006. Right_Face_Down, // XBOX: A, PS: X
  1007. Right_Face_Left, // XBOX: X, PS: Square
  1008. Right_Face_Right, // XBOX: B, PS: Circle
  1009. Left_Shoulder,
  1010. Left_Trigger,
  1011. Right_Shoulder,
  1012. Right_Trigger,
  1013. Left_Stick_Press, // Clicking the left analogue stick
  1014. Right_Stick_Press, // Clicking the right analogue stick
  1015. Middle_Face_Left, // Select / back / options button
  1016. Middle_Face_Middle, // PS button (not available on XBox)
  1017. Middle_Face_Right, // Start
  1018. }
  1019. // Used by API builder. Everything after this constant will not be in karl2d.doc.odin
  1020. API_END :: true
  1021. batch_vertex :: proc(v: Vec2, uv: Vec2, color: Color) {
  1022. v := v
  1023. if s.vertex_buffer_cpu_used == len(s.vertex_buffer_cpu) {
  1024. draw_current_batch()
  1025. }
  1026. shd := s.batch_shader
  1027. base_offset := s.vertex_buffer_cpu_used
  1028. pos_offset := shd.default_input_offsets[.Position]
  1029. uv_offset := shd.default_input_offsets[.UV]
  1030. color_offset := shd.default_input_offsets[.Color]
  1031. mem.set(&s.vertex_buffer_cpu[base_offset], 0, shd.vertex_size)
  1032. if pos_offset != -1 {
  1033. (^Vec2)(&s.vertex_buffer_cpu[base_offset + pos_offset])^ = {v.x, v.y}
  1034. }
  1035. if uv_offset != -1 {
  1036. (^Vec2)(&s.vertex_buffer_cpu[base_offset + uv_offset])^ = uv
  1037. }
  1038. if color_offset != -1 {
  1039. (^Color)(&s.vertex_buffer_cpu[base_offset + color_offset])^ = color
  1040. }
  1041. override_offset: int
  1042. for &o, idx in shd.input_overrides {
  1043. input := &shd.inputs[idx]
  1044. sz := pixel_format_size(input.format)
  1045. if o.used != 0 {
  1046. mem.copy(&s.vertex_buffer_cpu[base_offset + override_offset], raw_data(&o.val), o.used)
  1047. }
  1048. override_offset += sz
  1049. }
  1050. s.vertex_buffer_cpu_used += shd.vertex_size
  1051. }
  1052. VERTEX_BUFFER_MAX :: 1000000
  1053. DEFAULT_SHADER_SOURCE :: #load("shader.hlsl")
  1054. @(private="file")
  1055. s: ^State
  1056. frame_allocator: runtime.Allocator
  1057. win: Window_Interface
  1058. rb: Render_Backend_Interface
  1059. get_shader_input_default_type :: proc(name: string, type: Shader_Input_Type) -> Shader_Default_Inputs {
  1060. if name == "POS" && type == .Vec2 {
  1061. return .Position
  1062. } else if name == "UV" && type == .Vec2 {
  1063. return .UV
  1064. } else if name == "COL" && type == .Vec4 {
  1065. return .Color
  1066. }
  1067. return .Unknown
  1068. }
  1069. get_shader_input_format :: proc(name: string, type: Shader_Input_Type) -> Pixel_Format {
  1070. default_type := get_shader_input_default_type(name, type)
  1071. if default_type != .Unknown {
  1072. switch default_type {
  1073. case .Position: return .RG_32_Float
  1074. case .UV: return .RG_32_Float
  1075. case .Color: return .RGBA_8_Norm
  1076. case .Unknown: unreachable()
  1077. }
  1078. }
  1079. switch type {
  1080. case .F32: return .R_32_Float
  1081. case .Vec2: return .RG_32_Float
  1082. case .Vec3: return .RGB_32_Float
  1083. case .Vec4: return .RGBA_32_Float
  1084. }
  1085. return .Unknown
  1086. }
  1087. vec3_from_vec2 :: proc(v: Vec2) -> Vec3 {
  1088. return {
  1089. v.x, v.y, 0,
  1090. }
  1091. }
  1092. frame_cstring :: proc(str: string, loc := #caller_location) -> cstring {
  1093. return strings.clone_to_cstring(str, s.frame_allocator, loc)
  1094. }
  1095. make_default_projection :: proc(w, h: int) -> matrix[4,4]f32 {
  1096. return linalg.matrix_ortho3d_f32(0, f32(w), f32(h), 0, 0.001, 2)
  1097. }
  1098. _ :: bmp
  1099. _ :: png
  1100. _ :: tga