karl2d.doc.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. allocator := context.allocator, loc := #caller_location) -> ^State
  12. // Returns true if the program wants to shut down. This happens when for example pressing the close
  13. // button on the window. The application can decide if it wants to shut down or if it wants to show
  14. // some kind of confirmation dialogue and shut down later.
  15. //
  16. // Commonly used for creating the "main loop" of a game.
  17. shutdown_wanted :: proc() -> bool
  18. // Closes the window and cleans up the internal state.
  19. shutdown :: proc()
  20. // Clear the backbuffer with supplied color.
  21. clear :: proc(color: Color)
  22. // Present the backbuffer. Call at end of frame to make everything you've drawn appear on the screen.
  23. present :: proc()
  24. // Call at start or end of frame to process all events that have arrived to the window.
  25. //
  26. // WARNING: Not calling this will make your program impossible to interact with.
  27. process_events :: proc()
  28. get_screen_width :: proc() -> int
  29. get_screen_height :: proc() -> int
  30. set_window_position :: proc(x: int, y: int)
  31. set_window_size :: proc(width: int, height: int)
  32. // Flushes the current batch. This sends off everything to the GPU that has been queued in the
  33. // current batch. Normally, you do not need to do this manually. It is done automatically when these
  34. // procedures run:
  35. // present
  36. // set_camera
  37. // set_shader
  38. //
  39. // TODO: complete this list and motivate why it needs to happen on those procs (or do that in the
  40. // docs for those procs).
  41. draw_current_batch :: proc()
  42. //-------//
  43. // INPUT //
  44. //-------//
  45. // Returns true if a keyboard key went down between the current and the previous frame. Set when
  46. // 'process_events' runs (probably once per frame).
  47. key_went_down :: proc(key: Keyboard_Key) -> bool
  48. // Returns true if a keyboard key went up (was released) between the current and the previous frame.
  49. // Set when 'process_events' runs (probably once per frame).
  50. key_went_up :: proc(key: Keyboard_Key) -> bool
  51. // Returns true if a keyboard is currently being held down. Set when 'process_events' runs (probably
  52. // once per frame).
  53. key_is_held :: proc(key: Keyboard_Key) -> bool
  54. mouse_button_went_down :: proc(button: Mouse_Button) -> bool
  55. mouse_button_went_up :: proc(button: Mouse_Button) -> bool
  56. mouse_button_is_held :: proc(button: Mouse_Button) -> bool
  57. get_mouse_wheel_delta :: proc() -> f32
  58. get_mouse_position :: proc() -> Vec2
  59. //---------//
  60. // DRAWING //
  61. //---------//
  62. draw_rect :: proc(r: Rect, c: Color)
  63. draw_rect_ex :: proc(r: Rect, origin: Vec2, rot: f32, c: Color)
  64. draw_rect_outline :: proc(r: Rect, thickness: f32, color: Color)
  65. draw_circle :: proc(center: Vec2, radius: f32, color: Color, segments := 16)
  66. draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color)
  67. draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE)
  68. draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE)
  69. draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rotation: f32, tint := WHITE)
  70. draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color)
  71. //--------------------//
  72. // TEXTURE MANAGEMENT //
  73. //--------------------//
  74. load_texture_from_file :: proc(filename: string) -> Texture
  75. destroy_texture :: proc(tex: Texture)
  76. //---------//
  77. // SHADERS //
  78. //---------//
  79. load_shader :: proc(shader_source: string, layout_formats: []Shader_Input_Format = {}) -> Shader
  80. destroy_shader :: proc(shader: Shader)
  81. get_default_shader :: proc() -> Shader
  82. set_shader :: proc(shader: Maybe(Shader))
  83. set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: any)
  84. override_shader_input :: proc(shader: Shader, input: int, val: any)
  85. shader_input_format_size :: proc(f: Shader_Input_Format) -> int
  86. //-------------------------------//
  87. // CAMERA AND COORDINATE SYSTEMS //
  88. //-------------------------------//
  89. set_camera :: proc(camera: Maybe(Camera))
  90. screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2
  91. //------//
  92. // MISC //
  93. //------//
  94. set_scissor_rect :: proc(scissor_rect: Maybe(Rect))
  95. // Restore the internal state using the pointer returned by `init`. Useful after reloading the
  96. // library (for example, when doing code hot reload).
  97. set_internal_state :: proc(state: ^State)
  98. //---------------------//
  99. // TYPES AND CONSTANTS //
  100. //---------------------//
  101. // A RGBA (Red, Greeen, Blue, Alpha) color. Each channel can have a value between 0 and 255.
  102. Color :: [4]u8
  103. // A two dimensinal vector.
  104. Vec2 :: [2]f32
  105. // A three dimensinal vector.
  106. Vec3 :: [3]f32
  107. // A 4x4 column-major matrix.
  108. Mat4 :: matrix[4,4]f32
  109. // A two dimensional vector of integer numeric type.
  110. Vec2i :: [2]int
  111. // A rectangle that sits at position (x, y) and has size (w, h).
  112. Rect :: struct {
  113. x, y: f32,
  114. w, h: f32,
  115. }
  116. Texture :: struct {
  117. handle: Texture_Handle,
  118. width: int,
  119. height: int,
  120. }
  121. Camera :: struct {
  122. target: Vec2,
  123. origin: Vec2,
  124. rotation: f32,
  125. zoom: f32,
  126. }
  127. Shader_Handle :: distinct Handle
  128. SHADER_NONE :: Shader_Handle {}
  129. Shader :: struct {
  130. handle: Shader_Handle,
  131. constant_buffers: []Shader_Constant_Buffer,
  132. constant_lookup: map[string]Shader_Constant_Location,
  133. constant_builtin_locations: [Shader_Builtin_Constant]Maybe(Shader_Constant_Location),
  134. inputs: []Shader_Input,
  135. input_overrides: []Shader_Input_Value_Override,
  136. default_input_offsets: [Shader_Default_Inputs]int,
  137. vertex_size: int,
  138. }
  139. Shader_Constant_Buffer :: struct {
  140. cpu_data: []u8,
  141. }
  142. SHADER_INPUT_VALUE_MAX_SIZE :: 256
  143. Shader_Input_Value_Override :: struct {
  144. val: [SHADER_INPUT_VALUE_MAX_SIZE]u8,
  145. used: int,
  146. }
  147. Shader_Input_Type :: enum {
  148. F32,
  149. Vec2,
  150. Vec3,
  151. Vec4,
  152. }
  153. Shader_Builtin_Constant :: enum {
  154. MVP,
  155. }
  156. Shader_Default_Inputs :: enum {
  157. Unknown,
  158. Position,
  159. UV,
  160. Color,
  161. }
  162. Shader_Input :: struct {
  163. name: string,
  164. register: int,
  165. type: Shader_Input_Type,
  166. format: Shader_Input_Format,
  167. }
  168. Shader_Constant_Location :: struct {
  169. buffer_idx: u32,
  170. offset: u32,
  171. }
  172. Shader_Input_Format :: enum {
  173. Unknown,
  174. RGBA32_Float,
  175. RGBA8_Norm,
  176. RGBA8_Norm_SRGB,
  177. RGB32_Float,
  178. RG32_Float,
  179. R32_Float,
  180. }
  181. Handle :: hm.Handle
  182. Texture_Handle :: distinct Handle
  183. TEXTURE_NONE :: Texture_Handle {}
  184. // This keeps track of the internal state of the library. Usually, you do not need to poke at it.
  185. // It is created and kept as a global variable when 'init' is called. However, 'init' also returns
  186. // the pointer to it, so you can later use 'set_internal_state' to restore it (after for example hot
  187. // reload).
  188. State :: struct {
  189. allocator: runtime.Allocator,
  190. custom_context: runtime.Context,
  191. win: Window_Interface,
  192. window_state: rawptr,
  193. rb: Rendering_Backend_Interface,
  194. rb_state: rawptr,
  195. shutdown_wanted: bool,
  196. mouse_position: Vec2,
  197. mouse_delta: Vec2,
  198. mouse_wheel_delta: f32,
  199. keys_went_down: #sparse [Keyboard_Key]bool,
  200. keys_went_up: #sparse [Keyboard_Key]bool,
  201. keys_is_held: #sparse [Keyboard_Key]bool,
  202. window: Window_Handle,
  203. width: int,
  204. height: int,
  205. shape_drawing_texture: Texture_Handle,
  206. batch_camera: Maybe(Camera),
  207. batch_shader: Maybe(Shader),
  208. batch_texture: Texture_Handle,
  209. view_matrix: Mat4,
  210. proj_matrix: Mat4,
  211. vertex_buffer_cpu: []u8,
  212. vertex_buffer_cpu_used: int,
  213. default_shader: Shader,
  214. }
  215. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  216. // options presented here.
  217. Mouse_Button :: enum {
  218. Left,
  219. Right,
  220. Middle,
  221. Max = 255,
  222. }
  223. WHITE :: Color { 255, 255, 255, 255 }
  224. BLACK :: Color { 0, 0, 0, 255 }
  225. BLANK :: Color { 0, 0, 0, 0}
  226. // These are from Raylib. They are here so you can easily port a Raylib program to Karl2D.
  227. RL_LIGHTGRAY :: Color { 200, 200, 200, 255 }
  228. RL_GRAY :: Color { 130, 130, 130, 255 }
  229. RL_DARKGRAY :: Color { 80, 80, 80, 255 }
  230. RL_YELLOW :: Color { 253, 249, 0, 255 }
  231. RL_GOLD :: Color { 255, 203, 0, 255 }
  232. RL_ORANGE :: Color { 255, 161, 0, 255 }
  233. RL_PINK :: Color { 255, 109, 194, 255 }
  234. RL_RED :: Color { 230, 41, 55, 255 }
  235. RL_MAROON :: Color { 190, 33, 55, 255 }
  236. RL_GREEN :: Color { 0, 228, 48, 255 }
  237. RL_LIME :: Color { 0, 158, 47, 255 }
  238. RL_DARKGREEN :: Color { 0, 117, 44, 255 }
  239. RL_SKYBLUE :: Color { 102, 191, 255, 255 }
  240. RL_BLUE :: Color { 0, 121, 241, 255 }
  241. RL_DARKBLUE :: Color { 0, 82, 172, 255 }
  242. RL_PURPLE :: Color { 200, 122, 255, 255 }
  243. RL_VIOLET :: Color { 135, 60, 190, 255 }
  244. RL_DARKPURPLE :: Color { 112, 31, 126, 255 }
  245. RL_BEIGE :: Color { 211, 176, 131, 255 }
  246. RL_BROWN :: Color { 127, 106, 79, 255 }
  247. RL_DARKBROWN :: Color { 76, 63, 47, 255 }
  248. RL_WHITE :: WHITE
  249. RL_BLACK :: BLACK
  250. RL_BLANK :: BLANK
  251. RL_MAGENTA :: Color { 255, 0, 255, 255 }
  252. RL_RAYWHITE :: Color { 245, 245, 245, 255 }
  253. // Based on Raylib / GLFW
  254. Keyboard_Key :: enum {
  255. None = 0,
  256. // Alphanumeric keys
  257. Apostrophe = 39,
  258. Comma = 44,
  259. Minus = 45,
  260. Period = 46,
  261. Slash = 47,
  262. Zero = 48,
  263. One = 49,
  264. Two = 50,
  265. Three = 51,
  266. Four = 52,
  267. Five = 53,
  268. Six = 54,
  269. Seven = 55,
  270. Eight = 56,
  271. Nine = 57,
  272. Semicolon = 59,
  273. Equal = 61,
  274. A = 65,
  275. B = 66,
  276. C = 67,
  277. D = 68,
  278. E = 69,
  279. F = 70,
  280. G = 71,
  281. H = 72,
  282. I = 73,
  283. J = 74,
  284. K = 75,
  285. L = 76,
  286. M = 77,
  287. N = 78,
  288. O = 79,
  289. P = 80,
  290. Q = 81,
  291. R = 82,
  292. S = 83,
  293. T = 84,
  294. U = 85,
  295. V = 86,
  296. W = 87,
  297. X = 88,
  298. Y = 89,
  299. Z = 90,
  300. Left_Bracket = 91,
  301. Backslash = 92,
  302. Right_Bracket = 93,
  303. Grave = 96,
  304. // Function keys
  305. Space = 32,
  306. Escape = 256,
  307. Enter = 257,
  308. Tab = 258,
  309. Backspace = 259,
  310. Insert = 260,
  311. Delete = 261,
  312. Right = 262,
  313. Left = 263,
  314. Down = 264,
  315. Up = 265,
  316. Page_Up = 266,
  317. Page_Down = 267,
  318. Home = 268,
  319. End = 269,
  320. Caps_Lock = 280,
  321. Scroll_Lock = 281,
  322. Num_Lock = 282,
  323. Print_Screen = 283,
  324. Pause = 284,
  325. F1 = 290,
  326. F2 = 291,
  327. F3 = 292,
  328. F4 = 293,
  329. F5 = 294,
  330. F6 = 295,
  331. F7 = 296,
  332. F8 = 297,
  333. F9 = 298,
  334. F10 = 299,
  335. F11 = 300,
  336. F12 = 301,
  337. Left_Shift = 340,
  338. Left_Control = 341,
  339. Left_Alt = 342,
  340. Left_Super = 343,
  341. Right_Shift = 344,
  342. Right_Control = 345,
  343. Right_Alt = 346,
  344. Right_Super = 347,
  345. Menu = 348,
  346. // Keypad keys
  347. KP_0 = 320,
  348. KP_1 = 321,
  349. KP_2 = 322,
  350. KP_3 = 323,
  351. KP_4 = 324,
  352. KP_5 = 325,
  353. KP_6 = 326,
  354. KP_7 = 327,
  355. KP_8 = 328,
  356. KP_9 = 329,
  357. KP_Decimal = 330,
  358. KP_Divide = 331,
  359. KP_Multiply = 332,
  360. KP_Subtract = 333,
  361. KP_Add = 334,
  362. KP_Enter = 335,
  363. KP_Equal = 336,
  364. }