karl2d.doc.odin 14 KB

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