karl2d.odin 32 KB

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