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