karl2d.doc.odin 12 KB

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