karl2d.doc.odin 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. // This file is purely documentational. It is generated from the contents of 'karl2d.odin'.
  2. #+build ignore
  3. package karl2d
  4. //-----------------------------------------------//
  5. // SETUP, WINDOW MANAGEMENT AND FRAME MANAGEMENT //
  6. //-----------------------------------------------//
  7. // Opens a window and initializes some internal state. The internal state will use `allocator` for
  8. // all dynamically allocated memory. The return value can be ignored unless you need to later call
  9. // `set_internal_state`.
  10. init :: proc(window_width: int, window_height: int, window_title: string,
  11. window_creation_flags := Window_Flags {},
  12. allocator := context.allocator, loc := #caller_location) -> ^State
  13. // Returns true if the program wants to shut down. This happens when for example pressing the close
  14. // button on the window. The application can decide if it wants to shut down or if it wants to show
  15. // some kind of confirmation dialogue and shut down later.
  16. //
  17. // Commonly used for creating the "main loop" of a game.
  18. shutdown_wanted :: proc() -> bool
  19. // Closes the window and cleans up the internal state.
  20. shutdown :: proc()
  21. // Clear the backbuffer with supplied color.
  22. clear :: proc(color: Color)
  23. // Present the backbuffer. Call at end of frame to make everything you've drawn appear on the screen.
  24. present :: proc()
  25. // Call at start or end of frame to process all events that have arrived to the window.
  26. //
  27. // WARNING: Not calling this will make your program impossible to interact with.
  28. process_events :: proc()
  29. get_screen_width :: proc() -> int
  30. get_screen_height :: proc() -> int
  31. set_window_position :: proc(x: int, y: int)
  32. set_window_size :: proc(width: int, height: int)
  33. // Fetch the scale of the window. This usually comes from some DPI scaling setting in the OS.
  34. // 1 means 100% scale, 1.5 means 150% etc.
  35. get_window_scale :: proc() -> f32
  36. set_window_flags :: proc(flags: Window_Flags)
  37. // Flushes the current batch. This sends off everything to the GPU that has been queued in the
  38. // current batch. Normally, you do not need to do this manually. It is done automatically when these
  39. // procedures run:
  40. //
  41. // - present
  42. // - set_camera
  43. // - set_shader
  44. // - set_shader_constant
  45. // - set_scissor_rect
  46. // - draw_texture_* IF previous draw did not use the same texture (1)
  47. // - draw_rect_*, draw_circle_*, draw_line IF previous draw did not use the shapes drawing texture (2)
  48. //
  49. // (1) When drawing textures, the current texture is fed into the active shader. Everything within
  50. // the same batch must use the same texture. So drawing with a new texture will draw the current
  51. // batch. You can combine several textures into an atlas to get bigger batches.
  52. //
  53. // (2) In order to use the same shader for shapes drawing and textured drawing, the shapes drawing
  54. // uses a blank, white texture. For the same reasons as (1), drawing something else than shapes
  55. // before drawing a shape will break up the batches. TODO: Add possibility to customize shape
  56. // drawing texture so that you can put it into an atlas.
  57. //
  58. // The batch has maximum size of VERTEX_BUFFER_MAX bytes. The shader dictates how big a vertex is
  59. // so the maximum number of vertices that can be drawn in each batch is
  60. // VERTEX_BUFFER_MAX / shader.vertex_size
  61. draw_current_batch :: proc()
  62. //-------//
  63. // INPUT //
  64. //-------//
  65. // Returns true if a keyboard key went down between the current and the previous frame. Set when
  66. // 'process_events' runs (probably once per frame).
  67. key_went_down :: proc(key: Keyboard_Key) -> bool
  68. // Returns true if a keyboard key went up (was released) between the current and the previous frame.
  69. // Set when 'process_events' runs (probably once per frame).
  70. key_went_up :: proc(key: Keyboard_Key) -> bool
  71. // Returns true if a keyboard is currently being held down. Set when 'process_events' runs (probably
  72. // once per frame).
  73. key_is_held :: proc(key: Keyboard_Key) -> bool
  74. mouse_button_went_down :: proc(button: Mouse_Button) -> bool
  75. mouse_button_went_up :: proc(button: Mouse_Button) -> bool
  76. mouse_button_is_held :: proc(button: Mouse_Button) -> bool
  77. get_mouse_wheel_delta :: proc() -> f32
  78. get_mouse_position :: proc() -> Vec2
  79. gamepad_button_went_down :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool
  80. gamepad_button_went_up :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool
  81. gamepad_button_is_held :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool
  82. get_gamepad_axis :: proc(gamepad: Gamepad_Index, axis: Gamepad_Axis) -> f32
  83. // Set the left and right vibration motor speed. The range of left and right is 0 to 1. Note that on
  84. // most gamepads, the left motor is "low frequency" and the right motor is "high frequency". They do
  85. // not vibrate with the same speed.
  86. set_gamepad_vibration :: proc(gamepad: Gamepad_Index, left: f32, right: f32)
  87. //---------//
  88. // DRAWING //
  89. //---------//
  90. draw_rect :: proc(r: Rect, c: Color)
  91. draw_rect_vec :: proc(pos: Vec2, size: Vec2, c: Color)
  92. draw_rect_ex :: proc(r: Rect, origin: Vec2, rot: f32, c: Color)
  93. draw_rect_outline :: proc(r: Rect, thickness: f32, color: Color)
  94. draw_circle :: proc(center: Vec2, radius: f32, color: Color, segments := 16)
  95. draw_circle_outline :: proc(center: Vec2, radius: f32, thickness: f32, color: Color, segments := 16)
  96. draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color)
  97. draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE)
  98. draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE)
  99. draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rotation: f32, tint := WHITE)
  100. vec3 :: proc(v2: Vec2, z: f32) -> Vec3
  101. get_next_depth :: proc() -> f32
  102. measure_text :: proc(text: string, font_size: f32) -> Vec2
  103. draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color)
  104. draw_text_ex :: proc(font: Font_Handle, text: string, pos: Vec2, font_size: f32, color: Color)
  105. //--------------------//
  106. // TEXTURE MANAGEMENT //
  107. //--------------------//
  108. load_texture_from_file :: proc(filename: string) -> Texture
  109. // TODO should we have an error here or rely on check the handle of the texture?
  110. load_texture_from_bytes :: proc(bytes: []u8, width: int, height: int, format: Pixel_Format) -> Texture
  111. // Get a rectangle that spans the whole texture. Coordinates will be (x, y) = (0, 0) and size
  112. // (w, h) = (texture_width, texture_height)
  113. get_texture_rect :: proc(t: Texture) -> Rect
  114. // Update a texture with new pixels. `bytes` is the new pixel data. `rect` is the rectangle in
  115. // `tex` where the new pixels should end up.
  116. update_texture :: proc(tex: Texture, bytes: []u8, rect: Rect) -> bool
  117. destroy_texture :: proc(tex: Texture)
  118. //-------//
  119. // FONTS //
  120. //-------//
  121. load_font_from_file :: proc(filename: string) -> Font_Handle
  122. load_font_from_bytes :: proc(data: []u8) -> Font_Handle
  123. destroy_font :: proc(font: Font_Handle)
  124. get_default_font :: proc() -> Font_Handle
  125. //---------//
  126. // SHADERS //
  127. //---------//
  128. load_shader :: proc(vertex_shader_source: string, fragment_shader_source: string, layout_formats: []Pixel_Format = {}) -> Shader
  129. destroy_shader :: proc(shader: Shader)
  130. get_default_shader :: proc() -> Shader
  131. set_shader :: proc(shader: Maybe(Shader))
  132. set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: any)
  133. override_shader_input :: proc(shader: Shader, input: int, val: any)
  134. pixel_format_size :: proc(f: Pixel_Format) -> int
  135. //-------------------------------//
  136. // CAMERA AND COORDINATE SYSTEMS //
  137. //-------------------------------//
  138. set_camera :: proc(camera: Maybe(Camera))
  139. screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2
  140. world_to_screen :: proc(pos: Vec2, camera: Camera) -> Vec2
  141. get_camera_view_matrix :: proc(c: Camera) -> Mat4
  142. get_camera_world_matrix :: proc(c: Camera) -> Mat4
  143. //------//
  144. // MISC //
  145. //------//
  146. set_scissor_rect :: proc(scissor_rect: Maybe(Rect))
  147. // Restore the internal state using the pointer returned by `init`. Useful after reloading the
  148. // library (for example, when doing code hot reload).
  149. set_internal_state :: proc(state: ^State)
  150. //---------------------//
  151. // TYPES AND CONSTANTS //
  152. //---------------------//
  153. Vec2 :: [2]f32
  154. Vec3 :: [3]f32
  155. Vec4 :: [4]f32
  156. Mat4 :: matrix[4,4]f32
  157. // A two dimensional vector of integer numeric type.
  158. Vec2i :: [2]int
  159. // A rectangle that sits at position (x, y) and has size (w, h).
  160. Rect :: struct {
  161. x, y: f32,
  162. w, h: f32,
  163. }
  164. // An RGBA (Red, Green, Blue, Alpha) color. Each channel can have a value between 0 and 255.
  165. Color :: [4]u8
  166. WHITE :: Color { 255, 255, 255, 255 }
  167. BLACK :: Color { 0, 0, 0, 255 }
  168. GRAY :: Color { 127, 127, 127, 255 }
  169. RED :: Color { 198, 40, 90, 255 }
  170. GREEN :: Color { 30, 240, 30, 255 }
  171. BLANK :: Color { 0, 0, 0, 0 }
  172. BLUE :: Color { 30, 116, 240, 255 }
  173. // These are from Raylib. They are here so you can easily port a Raylib program to Karl2D.
  174. RL_LIGHTGRAY :: Color { 200, 200, 200, 255 }
  175. RL_GRAY :: Color { 130, 130, 130, 255 }
  176. RL_DARKGRAY :: Color { 80, 80, 80, 255 }
  177. RL_YELLOW :: Color { 253, 249, 0, 255 }
  178. RL_GOLD :: Color { 255, 203, 0, 255 }
  179. RL_ORANGE :: Color { 255, 161, 0, 255 }
  180. RL_PINK :: Color { 255, 109, 194, 255 }
  181. RL_RED :: Color { 230, 41, 55, 255 }
  182. RL_MAROON :: Color { 190, 33, 55, 255 }
  183. RL_GREEN :: Color { 0, 228, 48, 255 }
  184. RL_LIME :: Color { 0, 158, 47, 255 }
  185. RL_DARKGREEN :: Color { 0, 117, 44, 255 }
  186. RL_SKYBLUE :: Color { 102, 191, 255, 255 }
  187. RL_BLUE :: Color { 0, 121, 241, 255 }
  188. RL_DARKBLUE :: Color { 0, 82, 172, 255 }
  189. RL_PURPLE :: Color { 200, 122, 255, 255 }
  190. RL_VIOLET :: Color { 135, 60, 190, 255 }
  191. RL_DARKPURPLE :: Color { 112, 31, 126, 255 }
  192. RL_BEIGE :: Color { 211, 176, 131, 255 }
  193. RL_BROWN :: Color { 127, 106, 79, 255 }
  194. RL_DARKBROWN :: Color { 76, 63, 47, 255 }
  195. RL_WHITE :: WHITE
  196. RL_BLACK :: BLACK
  197. RL_BLANK :: BLANK
  198. RL_MAGENTA :: Color { 255, 0, 255, 255 }
  199. RL_RAYWHITE :: Color { 245, 245, 245, 255 }
  200. Texture :: struct {
  201. handle: Texture_Handle,
  202. width: int,
  203. height: int,
  204. }
  205. Camera :: struct {
  206. target: Vec2,
  207. offset: Vec2,
  208. rotation: f32,
  209. zoom: f32,
  210. }
  211. Window_Flag :: enum {
  212. Resizable,
  213. }
  214. Window_Flags :: bit_set[Window_Flag]
  215. Shader_Handle :: distinct Handle
  216. SHADER_NONE :: Shader_Handle {}
  217. Shader_Constant_Location :: struct {
  218. offset: int,
  219. size: int,
  220. }
  221. Shader :: struct {
  222. handle: Shader_Handle,
  223. // We store the CPU-side value of all constants in a single buffer to have less allocations.
  224. // The 'constants' array says where in this buffer each constant is, and 'constant_lookup'
  225. // maps a name to a constant location.
  226. constants_data: []u8,
  227. constants: []Shader_Constant_Location,
  228. constant_lookup: map[string]Shader_Constant_Location,
  229. // Maps built in constant types such as "model view projection matrix" to a location.
  230. constant_builtin_locations: [Shader_Builtin_Constant]Maybe(Shader_Constant_Location),
  231. inputs: []Shader_Input,
  232. input_overrides: []Shader_Input_Value_Override,
  233. default_input_offsets: [Shader_Default_Inputs]int,
  234. vertex_size: int,
  235. }
  236. SHADER_INPUT_VALUE_MAX_SIZE :: 256
  237. Shader_Input_Value_Override :: struct {
  238. val: [SHADER_INPUT_VALUE_MAX_SIZE]u8,
  239. used: int,
  240. }
  241. Shader_Input_Type :: enum {
  242. F32,
  243. Vec2,
  244. Vec3,
  245. Vec4,
  246. }
  247. Shader_Builtin_Constant :: enum {
  248. MVP,
  249. }
  250. Shader_Default_Inputs :: enum {
  251. Unknown,
  252. Position,
  253. UV,
  254. Color,
  255. }
  256. Shader_Input :: struct {
  257. name: string,
  258. register: int,
  259. type: Shader_Input_Type,
  260. format: Pixel_Format,
  261. }
  262. Pixel_Format :: enum {
  263. Unknown,
  264. RGBA_32_Float,
  265. RGB_32_Float,
  266. RG_32_Float,
  267. R_32_Float,
  268. RGBA_8_Norm,
  269. RG_8_Norm,
  270. R_8_Norm,
  271. R_8_UInt,
  272. }
  273. Font :: struct {
  274. atlas: Texture,
  275. // internal
  276. fontstash_handle: int,
  277. }
  278. Handle :: hm.Handle
  279. Texture_Handle :: distinct Handle
  280. Font_Handle :: distinct int
  281. FONT_NONE :: Font_Handle(0)
  282. TEXTURE_NONE :: Texture_Handle {}
  283. // This keeps track of the internal state of the library. Usually, you do not need to poke at it.
  284. // It is created and kept as a global variable when 'init' is called. However, 'init' also returns
  285. // the pointer to it, so you can later use 'set_internal_state' to restore it (after for example hot
  286. // reload).
  287. State :: struct {
  288. allocator: runtime.Allocator,
  289. frame_arena: runtime.Arena,
  290. frame_allocator: runtime.Allocator,
  291. custom_context: runtime.Context,
  292. win: Window_Interface,
  293. window_state: rawptr,
  294. rb: Render_Backend_Interface,
  295. rb_state: rawptr,
  296. fs: fs.FontContext,
  297. shutdown_wanted: bool,
  298. mouse_position: Vec2,
  299. mouse_delta: Vec2,
  300. mouse_wheel_delta: f32,
  301. key_went_down: #sparse [Keyboard_Key]bool,
  302. key_went_up: #sparse [Keyboard_Key]bool,
  303. key_is_held: #sparse [Keyboard_Key]bool,
  304. mouse_button_went_down: #sparse [Mouse_Button]bool,
  305. mouse_button_went_up: #sparse [Mouse_Button]bool,
  306. mouse_button_is_held: #sparse [Mouse_Button]bool,
  307. gamepad_button_went_down: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  308. gamepad_button_went_up: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  309. gamepad_button_is_held: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  310. window: Window_Handle,
  311. width: int,
  312. height: int,
  313. default_font: Font_Handle,
  314. fonts: [dynamic]Font,
  315. shape_drawing_texture: Texture_Handle,
  316. batch_font: Font_Handle,
  317. batch_camera: Maybe(Camera),
  318. batch_shader: Shader,
  319. batch_scissor: Maybe(Rect),
  320. batch_texture: Texture_Handle,
  321. view_matrix: Mat4,
  322. proj_matrix: Mat4,
  323. depth: f32,
  324. depth_start: f32,
  325. depth_increment: f32,
  326. vertex_buffer_cpu: []u8,
  327. vertex_buffer_cpu_used: int,
  328. default_shader: Shader,
  329. }
  330. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  331. // options presented here.
  332. Mouse_Button :: enum {
  333. Left,
  334. Right,
  335. Middle,
  336. Max = 255,
  337. }
  338. // Based on Raylib / GLFW
  339. Keyboard_Key :: enum {
  340. None = 0,
  341. // Numeric keys (top row)
  342. N0 = 48,
  343. N1 = 49,
  344. N2 = 50,
  345. N3 = 51,
  346. N4 = 52,
  347. N5 = 53,
  348. N6 = 54,
  349. N7 = 55,
  350. N8 = 56,
  351. N9 = 57,
  352. // Letter keys
  353. A = 65,
  354. B = 66,
  355. C = 67,
  356. D = 68,
  357. E = 69,
  358. F = 70,
  359. G = 71,
  360. H = 72,
  361. I = 73,
  362. J = 74,
  363. K = 75,
  364. L = 76,
  365. M = 77,
  366. N = 78,
  367. O = 79,
  368. P = 80,
  369. Q = 81,
  370. R = 82,
  371. S = 83,
  372. T = 84,
  373. U = 85,
  374. V = 86,
  375. W = 87,
  376. X = 88,
  377. Y = 89,
  378. Z = 90,
  379. // Special characters
  380. Apostrophe = 39,
  381. Comma = 44,
  382. Minus = 45,
  383. Period = 46,
  384. Slash = 47,
  385. Semicolon = 59,
  386. Equal = 61,
  387. Left_Bracket = 91,
  388. Backslash = 92,
  389. Right_Bracket = 93,
  390. Grave_Accent = 96,
  391. // Function keys, modifiers, caret control etc
  392. Space = 32,
  393. Escape = 256,
  394. Enter = 257,
  395. Tab = 258,
  396. Backspace = 259,
  397. Insert = 260,
  398. Delete = 261,
  399. Right = 262,
  400. Left = 263,
  401. Down = 264,
  402. Up = 265,
  403. Page_Up = 266,
  404. Page_Down = 267,
  405. Home = 268,
  406. End = 269,
  407. Caps_Lock = 280,
  408. Scroll_Lock = 281,
  409. Num_Lock = 282,
  410. Print_Screen = 283,
  411. Pause = 284,
  412. F1 = 290,
  413. F2 = 291,
  414. F3 = 292,
  415. F4 = 293,
  416. F5 = 294,
  417. F6 = 295,
  418. F7 = 296,
  419. F8 = 297,
  420. F9 = 298,
  421. F10 = 299,
  422. F11 = 300,
  423. F12 = 301,
  424. Left_Shift = 340,
  425. Left_Control = 341,
  426. Left_Alt = 342,
  427. Left_Super = 343,
  428. Right_Shift = 344,
  429. Right_Control = 345,
  430. Right_Alt = 346,
  431. Right_Super = 347,
  432. Menu = 348,
  433. // Numpad keys
  434. NP_0 = 320,
  435. NP_1 = 321,
  436. NP_2 = 322,
  437. NP_3 = 323,
  438. NP_4 = 324,
  439. NP_5 = 325,
  440. NP_6 = 326,
  441. NP_7 = 327,
  442. NP_8 = 328,
  443. NP_9 = 329,
  444. NP_Decimal = 330,
  445. NP_Divide = 331,
  446. NP_Multiply = 332,
  447. NP_Subtract = 333,
  448. NP_Add = 334,
  449. NP_Enter = 335,
  450. NP_Equal = 336,
  451. }
  452. MAX_GAMEPADS :: 4
  453. // A value between 0 and MAX_GAMEPADS - 1
  454. Gamepad_Index :: int
  455. Gamepad_Axis :: enum {
  456. Left_Stick_X,
  457. Left_Stick_Y,
  458. Right_Stick_X,
  459. Right_Stick_Y,
  460. Left_Trigger,
  461. Right_Trigger,
  462. }
  463. Gamepad_Button :: enum {
  464. // DPAD buttons
  465. Left_Face_Up,
  466. Left_Face_Down,
  467. Left_Face_Left,
  468. Left_Face_Right,
  469. Right_Face_Up, // XBOX: Y, PS: Triangle
  470. Right_Face_Down, // XBOX: A, PS: X
  471. Right_Face_Left, // XBOX: X, PS: Square
  472. Right_Face_Right, // XBOX: B, PS: Circle
  473. Left_Shoulder,
  474. Left_Trigger,
  475. Right_Shoulder,
  476. Right_Trigger,
  477. Left_Stick_Press, // Clicking the left analogue stick
  478. Right_Stick_Press, // Clicking the right analogue stick
  479. Middle_Face_Left, // Select / back / options button
  480. Middle_Face_Middle, // PS button (not available on XBox)
  481. Middle_Face_Right, // Start
  482. }