karl2d.doc.odin 14 KB

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