karl2d.doc.odin 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. measure_text :: proc(text: string, font_size: f32) -> Vec2
  101. draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color)
  102. draw_text_ex :: proc(font: Font_Handle, text: string, pos: Vec2, font_size: f32, color: Color)
  103. //--------------------//
  104. // TEXTURE MANAGEMENT //
  105. //--------------------//
  106. load_texture_from_file :: proc(filename: string) -> Texture
  107. // TODO should we have an error here or rely on check the handle of the texture?
  108. load_texture_from_bytes :: proc(bytes: []u8, width: int, height: int, format: Pixel_Format) -> Texture
  109. // Get a rectangle that spans the whole texture. Coordinates will be (x, y) = (0, 0) and size
  110. // (w, h) = (texture_width, texture_height)
  111. get_texture_rect :: proc(t: Texture) -> Rect
  112. // Update a texture with new pixels. `bytes` is the new pixel data. `rect` is the rectangle in
  113. // `tex` where the new pixels should end up.
  114. update_texture :: proc(tex: Texture, bytes: []u8, rect: Rect) -> bool
  115. destroy_texture :: proc(tex: Texture)
  116. //-------//
  117. // FONTS //
  118. //-------//
  119. load_font_from_file :: proc(filename: string) -> Font_Handle
  120. load_font_from_bytes :: proc(data: []u8) -> Font_Handle
  121. get_default_font :: proc() -> Font_Handle
  122. //---------//
  123. // SHADERS //
  124. //---------//
  125. load_shader :: proc(shader_source: string, layout_formats: []Pixel_Format = {}) -> Shader
  126. destroy_shader :: proc(shader: Shader)
  127. get_default_shader :: proc() -> Shader
  128. set_shader :: proc(shader: Maybe(Shader))
  129. set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: any)
  130. override_shader_input :: proc(shader: Shader, input: int, val: any)
  131. pixel_format_size :: proc(f: Pixel_Format) -> int
  132. //-------------------------------//
  133. // CAMERA AND COORDINATE SYSTEMS //
  134. //-------------------------------//
  135. set_camera :: proc(camera: Maybe(Camera))
  136. screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2
  137. world_to_screen :: proc(pos: Vec2, camera: Camera) -> Vec2
  138. get_camera_view_matrix :: proc(c: Camera) -> Mat4
  139. get_camera_world_matrix :: proc(c: Camera) -> Mat4
  140. //------//
  141. // MISC //
  142. //------//
  143. set_scissor_rect :: proc(scissor_rect: Maybe(Rect))
  144. // Restore the internal state using the pointer returned by `init`. Useful after reloading the
  145. // library (for example, when doing code hot reload).
  146. set_internal_state :: proc(state: ^State)
  147. //---------------------//
  148. // TYPES AND CONSTANTS //
  149. //---------------------//
  150. Vec2 :: [2]f32
  151. Vec3 :: [3]f32
  152. Vec4 :: [4]f32
  153. Mat4 :: matrix[4,4]f32
  154. // A two dimensional vector of integer numeric type.
  155. Vec2i :: [2]int
  156. // A rectangle that sits at position (x, y) and has size (w, h).
  157. Rect :: struct {
  158. x, y: f32,
  159. w, h: f32,
  160. }
  161. // An RGBA (Red, Green, Blue, Alpha) color. Each channel can have a value between 0 and 255.
  162. Color :: [4]u8
  163. WHITE :: Color { 255, 255, 255, 255 }
  164. BLACK :: Color { 0, 0, 0, 255 }
  165. GRAY :: Color { 127, 127, 127, 255 }
  166. RED :: Color { 198, 80, 90, 255 }
  167. BLANK :: Color { 0, 0, 0, 0 }
  168. BLUE :: Color { 30, 116, 240, 255 }
  169. // These are from Raylib. They are here so you can easily port a Raylib program to Karl2D.
  170. RL_LIGHTGRAY :: Color { 200, 200, 200, 255 }
  171. RL_GRAY :: Color { 130, 130, 130, 255 }
  172. RL_DARKGRAY :: Color { 80, 80, 80, 255 }
  173. RL_YELLOW :: Color { 253, 249, 0, 255 }
  174. RL_GOLD :: Color { 255, 203, 0, 255 }
  175. RL_ORANGE :: Color { 255, 161, 0, 255 }
  176. RL_PINK :: Color { 255, 109, 194, 255 }
  177. RL_RED :: Color { 230, 41, 55, 255 }
  178. RL_MAROON :: Color { 190, 33, 55, 255 }
  179. RL_GREEN :: Color { 0, 228, 48, 255 }
  180. RL_LIME :: Color { 0, 158, 47, 255 }
  181. RL_DARKGREEN :: Color { 0, 117, 44, 255 }
  182. RL_SKYBLUE :: Color { 102, 191, 255, 255 }
  183. RL_BLUE :: Color { 0, 121, 241, 255 }
  184. RL_DARKBLUE :: Color { 0, 82, 172, 255 }
  185. RL_PURPLE :: Color { 200, 122, 255, 255 }
  186. RL_VIOLET :: Color { 135, 60, 190, 255 }
  187. RL_DARKPURPLE :: Color { 112, 31, 126, 255 }
  188. RL_BEIGE :: Color { 211, 176, 131, 255 }
  189. RL_BROWN :: Color { 127, 106, 79, 255 }
  190. RL_DARKBROWN :: Color { 76, 63, 47, 255 }
  191. RL_WHITE :: WHITE
  192. RL_BLACK :: BLACK
  193. RL_BLANK :: BLANK
  194. RL_MAGENTA :: Color { 255, 0, 255, 255 }
  195. RL_RAYWHITE :: Color { 245, 245, 245, 255 }
  196. Texture :: struct {
  197. handle: Texture_Handle,
  198. width: int,
  199. height: int,
  200. }
  201. Camera :: struct {
  202. target: Vec2,
  203. offset: Vec2,
  204. rotation: f32,
  205. zoom: f32,
  206. }
  207. Window_Flag :: enum {
  208. Resizable,
  209. }
  210. Window_Flags :: bit_set[Window_Flag]
  211. Shader_Handle :: distinct Handle
  212. SHADER_NONE :: Shader_Handle {}
  213. Shader :: struct {
  214. handle: Shader_Handle,
  215. constant_buffers: []Shader_Constant_Buffer,
  216. constant_lookup: map[string]Shader_Constant_Location,
  217. constant_builtin_locations: [Shader_Builtin_Constant]Maybe(Shader_Constant_Location),
  218. inputs: []Shader_Input,
  219. input_overrides: []Shader_Input_Value_Override,
  220. default_input_offsets: [Shader_Default_Inputs]int,
  221. vertex_size: int,
  222. }
  223. Shader_Constant_Buffer :: struct {
  224. cpu_data: []u8,
  225. }
  226. SHADER_INPUT_VALUE_MAX_SIZE :: 256
  227. Shader_Input_Value_Override :: struct {
  228. val: [SHADER_INPUT_VALUE_MAX_SIZE]u8,
  229. used: int,
  230. }
  231. Shader_Input_Type :: enum {
  232. F32,
  233. Vec2,
  234. Vec3,
  235. Vec4,
  236. }
  237. Shader_Builtin_Constant :: enum {
  238. MVP,
  239. }
  240. Shader_Default_Inputs :: enum {
  241. Unknown,
  242. Position,
  243. UV,
  244. Color,
  245. }
  246. Shader_Input :: struct {
  247. name: string,
  248. register: int,
  249. type: Shader_Input_Type,
  250. format: Pixel_Format,
  251. }
  252. Shader_Constant_Location :: struct {
  253. buffer_idx: u32,
  254. offset: u32,
  255. }
  256. Pixel_Format :: enum {
  257. Unknown,
  258. RGBA_32_Float,
  259. RGB_32_Float,
  260. RG_32_Float,
  261. R_32_Float,
  262. RGBA_8_Norm,
  263. RG_8_Norm,
  264. R_8_Norm,
  265. R_8_UInt,
  266. }
  267. Font :: struct {
  268. atlas: Texture,
  269. // internal
  270. fontstash_handle: int,
  271. }
  272. Handle :: hm.Handle
  273. Texture_Handle :: distinct Handle
  274. Font_Handle :: distinct int
  275. FONT_NONE :: Font_Handle(0)
  276. TEXTURE_NONE :: Texture_Handle {}
  277. // This keeps track of the internal state of the library. Usually, you do not need to poke at it.
  278. // It is created and kept as a global variable when 'init' is called. However, 'init' also returns
  279. // the pointer to it, so you can later use 'set_internal_state' to restore it (after for example hot
  280. // reload).
  281. State :: struct {
  282. allocator: runtime.Allocator,
  283. frame_arena: runtime.Arena,
  284. frame_allocator: runtime.Allocator,
  285. custom_context: runtime.Context,
  286. win: Window_Interface,
  287. window_state: rawptr,
  288. rb: Render_Backend_Interface,
  289. rb_state: rawptr,
  290. fs: fs.FontContext,
  291. shutdown_wanted: bool,
  292. mouse_position: Vec2,
  293. mouse_delta: Vec2,
  294. mouse_wheel_delta: f32,
  295. key_went_down: #sparse [Keyboard_Key]bool,
  296. key_went_up: #sparse [Keyboard_Key]bool,
  297. key_is_held: #sparse [Keyboard_Key]bool,
  298. mouse_button_went_down: #sparse [Mouse_Button]bool,
  299. mouse_button_went_up: #sparse [Mouse_Button]bool,
  300. mouse_button_is_held: #sparse [Mouse_Button]bool,
  301. gamepad_button_went_down: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  302. gamepad_button_went_up: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  303. gamepad_button_is_held: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  304. window: Window_Handle,
  305. width: int,
  306. height: int,
  307. default_font: Font_Handle,
  308. fonts: [dynamic]Font,
  309. shape_drawing_texture: Texture_Handle,
  310. batch_font: Font_Handle,
  311. batch_camera: Maybe(Camera),
  312. batch_shader: Shader,
  313. batch_scissor: Maybe(Rect),
  314. batch_texture: Texture_Handle,
  315. view_matrix: Mat4,
  316. proj_matrix: Mat4,
  317. vertex_buffer_cpu: []u8,
  318. vertex_buffer_cpu_used: int,
  319. default_shader: Shader,
  320. }
  321. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  322. // options presented here.
  323. Mouse_Button :: enum {
  324. Left,
  325. Right,
  326. Middle,
  327. Max = 255,
  328. }
  329. // Based on Raylib / GLFW
  330. Keyboard_Key :: enum {
  331. None = 0,
  332. // Numeric keys (top row)
  333. N0 = 48,
  334. N1 = 49,
  335. N2 = 50,
  336. N3 = 51,
  337. N4 = 52,
  338. N5 = 53,
  339. N6 = 54,
  340. N7 = 55,
  341. N8 = 56,
  342. N9 = 57,
  343. // Letter keys
  344. A = 65,
  345. B = 66,
  346. C = 67,
  347. D = 68,
  348. E = 69,
  349. F = 70,
  350. G = 71,
  351. H = 72,
  352. I = 73,
  353. J = 74,
  354. K = 75,
  355. L = 76,
  356. M = 77,
  357. N = 78,
  358. O = 79,
  359. P = 80,
  360. Q = 81,
  361. R = 82,
  362. S = 83,
  363. T = 84,
  364. U = 85,
  365. V = 86,
  366. W = 87,
  367. X = 88,
  368. Y = 89,
  369. Z = 90,
  370. // Special characters
  371. Apostrophe = 39,
  372. Comma = 44,
  373. Minus = 45,
  374. Period = 46,
  375. Slash = 47,
  376. Semicolon = 59,
  377. Equal = 61,
  378. Left_Bracket = 91,
  379. Backslash = 92,
  380. Right_Bracket = 93,
  381. Grave_Accent = 96,
  382. // Function keys, modifiers, caret control etc
  383. Space = 32,
  384. Escape = 256,
  385. Enter = 257,
  386. Tab = 258,
  387. Backspace = 259,
  388. Insert = 260,
  389. Delete = 261,
  390. Right = 262,
  391. Left = 263,
  392. Down = 264,
  393. Up = 265,
  394. Page_Up = 266,
  395. Page_Down = 267,
  396. Home = 268,
  397. End = 269,
  398. Caps_Lock = 280,
  399. Scroll_Lock = 281,
  400. Num_Lock = 282,
  401. Print_Screen = 283,
  402. Pause = 284,
  403. F1 = 290,
  404. F2 = 291,
  405. F3 = 292,
  406. F4 = 293,
  407. F5 = 294,
  408. F6 = 295,
  409. F7 = 296,
  410. F8 = 297,
  411. F9 = 298,
  412. F10 = 299,
  413. F11 = 300,
  414. F12 = 301,
  415. Left_Shift = 340,
  416. Left_Control = 341,
  417. Left_Alt = 342,
  418. Left_Super = 343,
  419. Right_Shift = 344,
  420. Right_Control = 345,
  421. Right_Alt = 346,
  422. Right_Super = 347,
  423. Menu = 348,
  424. // Numpad keys
  425. NP_0 = 320,
  426. NP_1 = 321,
  427. NP_2 = 322,
  428. NP_3 = 323,
  429. NP_4 = 324,
  430. NP_5 = 325,
  431. NP_6 = 326,
  432. NP_7 = 327,
  433. NP_8 = 328,
  434. NP_9 = 329,
  435. NP_Decimal = 330,
  436. NP_Divide = 331,
  437. NP_Multiply = 332,
  438. NP_Subtract = 333,
  439. NP_Add = 334,
  440. NP_Enter = 335,
  441. NP_Equal = 336,
  442. }
  443. MAX_GAMEPADS :: 4
  444. // A value between 0 and MAX_GAMEPADS - 1
  445. Gamepad_Index :: int
  446. Gamepad_Axis :: enum {
  447. Left_Stick_X,
  448. Left_Stick_Y,
  449. Right_Stick_X,
  450. Right_Stick_Y,
  451. Left_Trigger,
  452. Right_Trigger,
  453. }
  454. Gamepad_Button :: enum {
  455. // DPAD buttons
  456. Left_Face_Up,
  457. Left_Face_Down,
  458. Left_Face_Left,
  459. Left_Face_Right,
  460. Right_Face_Up, // XBOX: Y, PS: Triangle
  461. Right_Face_Down, // XBOX: A, PS: X
  462. Right_Face_Left, // XBOX: X, PS: Square
  463. Right_Face_Right, // XBOX: B, PS: Circle
  464. Left_Shoulder,
  465. Left_Trigger,
  466. Right_Shoulder,
  467. Right_Trigger,
  468. Left_Stick_Press, // Clicking the left analogue stick
  469. Right_Stick_Press, // Clicking the right analogue stick
  470. Middle_Face_Left, // Select / back / options button
  471. Middle_Face_Middle, // PS button (not available on XBox)
  472. Middle_Face_Right, // Start
  473. }