karl2d.doc.odin 15 KB

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