karl2d.odin 33 KB

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