karl2d.doc.odin 10 KB

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