karl2d.doc.odin 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. //
  11. // `screen_width` and `screen_height` refer to the the resolution of the drawable area of the
  12. // window. The window might be slightly larger due borders and headers.
  13. init :: proc(screen_width: int, screen_height: int, window_title: string,
  14. window_creation_flags := Window_Flags {},
  15. allocator := context.allocator, loc := #caller_location) -> ^State
  16. // Returns true the user has pressed the close button on the window, or used a key stroke such as
  17. // ALT+F4 on Windows. The application can decide if it wants to shut down or if it wants to show
  18. // some kind of confirmation dialogue.
  19. //
  20. // Commonly used for creating the "main loop" of a game: `for !k2.shutdown_wanted {}`
  21. shutdown_wanted :: proc() -> bool
  22. // Closes the window and cleans up Karl2D's internal state.
  23. shutdown :: proc()
  24. // Clear the "screen" with the supplied color. By default this will clear your window. But if you
  25. // have set a Render Texture using the `set_render_texture` procedure, then that Render Texture will
  26. // be cleared instead.
  27. clear :: proc(color: Color)
  28. // Call at the start of each frame. This procedure does two main things:
  29. // - Fetches how long the previous frame took and how long since the program started. These values
  30. // can be fetched using `get_frame_time()` and `get_time()`
  31. // - Clears Karl2D's internal "frame_allocator" -- that's the allocator the library uses for
  32. // dynamic memory that has a lifetime of a single frame.
  33. new_frame :: proc()
  34. // "Flips the backbuffer": Call at end of frame to make everything you've drawn appear on the screen.
  35. //
  36. // When you draw using for example `draw_texture`, then that stuff is drawn to an invisible texture
  37. // called a "backbuffer". This makes sure that we don't see half-drawn frames. So when you are happy
  38. // with a frame and want to show it to the player, call this procedure.
  39. //
  40. // WebGL note: WebGL does the backbuffer flipping automatically. But you should still call this to
  41. // make sure that all rendering has been sent off to the GPU (it calls `draw_current_batch()`).
  42. present :: proc()
  43. // Call at start or end of frame to process all events that have arrived to the window. This
  44. // includes keyboard, mouse, gamepad and window events.
  45. //
  46. // WARNING: Not calling this will make your program impossible to interact with.
  47. process_events :: proc()
  48. // Returns how many seconds the previous frame took. Often a tiny number such as 0.016 s.
  49. //
  50. // You must call `new_frame()` at the start of your frame in order for the frame_time to be updated.
  51. get_frame_time :: proc() -> f32
  52. // Returns how many seconds has elapsed since the game started.
  53. //
  54. // You must call `new_frame()` at the start of your frame for this value to get updated.
  55. get_time :: proc() -> f64
  56. // Gets the width of the drawing area within the window. The returned number is not scaled by any
  57. // monitor DPI scaling. You do that manually using the number returned by `get_window_scale()`.
  58. get_screen_width :: proc() -> int
  59. // Gets the height of the drawing area within the window. The returned number is not scaled by any
  60. // monitor DPI scaling. You do that manually using the number returned by `get_window_scale()`.
  61. get_screen_height :: proc() -> int
  62. // Moves the window.
  63. //
  64. // WebGL note: This moves the canvas within the window, which may not be what you want.
  65. set_window_position :: proc(x: int, y: int)
  66. // Resize the window to a new size. If the window has the flag Resizable set, then the backbuffer
  67. // will also be resized.
  68. set_window_size :: proc(width: int, height: int)
  69. // Fetch the scale of the window. This usually comes from some DPI scaling setting in the OS.
  70. // 1 means 100% scale, 1.5 means 150% etc.
  71. get_window_scale :: proc() -> f32
  72. // These are the same kind of flags that you can send to `init`.
  73. set_window_flags :: proc(flags: Window_Flags)
  74. // Flushes the current batch. This sends off everything to the GPU that has been queued in the
  75. // current batch. Normally, you do not need to do this manually. It is done automatically when these
  76. // procedures run:
  77. //
  78. // - present
  79. // - set_camera
  80. // - set_shader
  81. // - set_shader_constant
  82. // - set_scissor_rect
  83. // - set_blend_mode
  84. // - set_render_texture
  85. // - clear
  86. // - draw_texture_* IF previous draw did not use the same texture (1)
  87. // - draw_rect_*, draw_circle_*, draw_line IF previous draw did not use the shapes drawing texture (2)
  88. //
  89. // (1) When drawing textures, the current texture is fed into the active shader. Everything within
  90. // the same batch must use the same texture. So drawing with a new texture forces the current to
  91. // be drawn. You can combine several textures into an atlas to get bigger batches.
  92. //
  93. // (2) In order to use the same shader for shapes drawing and textured drawing, the shapes drawing
  94. // uses a blank, white texture. For the same reasons as (1), drawing something else than shapes
  95. // before drawing a shape will break up the batches. In a future update I'll add so that you can
  96. // set your own shapes drawing texture, making it possible to combine it with a bigger atlas.
  97. //
  98. // The batch has maximum size of VERTEX_BUFFER_MAX bytes. The shader dictates how big a vertex is
  99. // so the maximum number of vertices that can be drawn in each batch is
  100. // VERTEX_BUFFER_MAX / shader.vertex_size
  101. draw_current_batch :: proc()
  102. //-------//
  103. // INPUT //
  104. //-------//
  105. // Returns true if a keyboard key went down between the current and the previous frame. Set when
  106. // 'process_events' runs.
  107. key_went_down :: proc(key: Keyboard_Key) -> bool
  108. // Returns true if a keyboard key went up (was released) between the current and the previous frame.
  109. // Set when 'process_events' runs.
  110. key_went_up :: proc(key: Keyboard_Key) -> bool
  111. // Returns true if a keyboard is currently being held down. Set when 'process_events' runs.
  112. key_is_held :: proc(key: Keyboard_Key) -> bool
  113. // Returns true if a mouse button went down between the current and the previous frame. Specify
  114. // which mouse button using the `button` parameter.
  115. //
  116. // Set when 'process_events' runs.
  117. mouse_button_went_down :: proc(button: Mouse_Button) -> bool
  118. // Returns true if a mouse button went up (was released) between the current and the previous frame.
  119. // Specify which mouse button using the `button` parameter.
  120. //
  121. // Set when 'process_events' runs.
  122. mouse_button_went_up :: proc(button: Mouse_Button) -> bool
  123. // Returns true if a mouse button is currently being held down. Specify which mouse button using the
  124. // `button` parameter. Set when 'process_events' runs.
  125. mouse_button_is_held :: proc(button: Mouse_Button) -> bool
  126. // Returns how many clicks the mouse wheel has scrolled between the previous and current frame.
  127. get_mouse_wheel_delta :: proc() -> f32
  128. // Returns the mouse position, measured from the top-left corner of the window.
  129. get_mouse_position :: proc() -> Vec2
  130. // Returns how many pixels the mouse moved between the previous and the current frame.
  131. get_mouse_delta :: proc() -> Vec2
  132. // Returns true if a gamepad with the supplied index is connected. The parameter should be a value
  133. // between 0 and MAX_GAMEPADS.
  134. is_gamepad_active :: proc(gamepad: Gamepad_Index) -> bool
  135. // Returns true if a gamepad button went down between the previous and the current frame.
  136. gamepad_button_went_down :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool
  137. // Returns true if a gamepad button went up (was released) between the previous and the current
  138. // frame.
  139. gamepad_button_went_up :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool
  140. // Returns true if a gamepad button is currently held down.
  141. //
  142. // The "trigger buttons" on some gamepads also have an analogue "axis value" associated with them.
  143. // Fetch that value using `get_gamepad_axis()`.
  144. gamepad_button_is_held :: proc(gamepad: Gamepad_Index, button: Gamepad_Button) -> bool
  145. // Returns the value of analogue gamepad axes such as the thumbsticks and trigger buttons. The value
  146. // is in the range -1 to 1 for sticks and 0 to 1 for trigger buttons.
  147. get_gamepad_axis :: proc(gamepad: Gamepad_Index, axis: Gamepad_Axis) -> f32
  148. // Set the left and right vibration motor speed. The range of left and right is 0 to 1. Note that on
  149. // most gamepads, the left motor is "low frequency" and the right motor is "high frequency". They do
  150. // not vibrate with the same speed.
  151. set_gamepad_vibration :: proc(gamepad: Gamepad_Index, left: f32, right: f32)
  152. //---------//
  153. // DRAWING //
  154. //---------//
  155. // Draw a colored rectangle. The rectangles have their (x, y) position in the top-left corner of the
  156. // rectangle.
  157. draw_rect :: proc(r: Rect, c: Color)
  158. // Creates a rectangle from a position and a size and draws it.
  159. draw_rect_vec :: proc(pos: Vec2, size: Vec2, c: Color)
  160. // Draw a rectangle with a custom origin and rotation.
  161. //
  162. // The origin says which point the rotation rotates around. If the origin is `(0, 0)`, then the
  163. // rectangle rotates around the top-left corner of the rectangle. If it is `(rect.w/2, rect.h/2)`
  164. // then the rectangle rotates around its center.
  165. draw_rect_ex :: proc(r: Rect, origin: Vec2, rot: f32, c: Color)
  166. // Draw the outline of a rectangle with a specific thickness. The outline is drawn using four
  167. // rectangles.
  168. draw_rect_outline :: proc(r: Rect, thickness: f32, color: Color)
  169. // Draw a circle with a certain center and radius. Note the `segments` parameter: This circle is not
  170. // perfect! It is drawn using a number of "cake segments".
  171. draw_circle :: proc(center: Vec2, radius: f32, color: Color, segments := 16)
  172. // Like `draw_circle` but only draws the outer edge of the circle.
  173. draw_circle_outline :: proc(center: Vec2, radius: f32, thickness: f32, color: Color, segments := 16)
  174. // Draws a line from `start` to `end` of a certain thickness.
  175. draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color)
  176. // Draw a texture at a specific position. The texture will be drawn with its top-left corner at
  177. // position `pos`.
  178. //
  179. // Load textures using `load_texture_from_file` or `load_texture_from_bytes`.
  180. draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE)
  181. // Draw a section of a texture at a specific position. `rect` is a rectangle measured in pixels. It
  182. // tells the procedure which part of the texture to display. The texture will be drawn with its
  183. // top-left corner at position `pos`.
  184. draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE)
  185. // Draw a texture by taking a section of the texture specified by `src` and draw it into the area of
  186. // the screen specified by `dst`. You can also rotate the texture around an origin point of your
  187. // choice.
  188. //
  189. // Tip: Use `k2.get_texture_rect(tex)` for `src` if you want to draw the whole texture.
  190. draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rotation: f32, tint := WHITE)
  191. // Tells you how much space some text of a certain size will use on the screen. The font used is the
  192. // default font. The return value contains the width and height of the text.
  193. measure_text :: proc(text: string, font_size: f32) -> Vec2
  194. // Tells you how much space some text of a certain size will use on the screen, using a custom font.
  195. // The return value contains the width and height of the text.
  196. measure_text_ex :: proc(font_handle: Font, text: string, font_size: f32) -> Vec2
  197. // Draw text at a position with a size. This uses the default font. `pos` will be equal to the
  198. // top-left position of the text.
  199. draw_text :: proc(text: string, pos: Vec2, font_size: f32, color := BLACK)
  200. // Draw text at a position with a size, using a custom font. `pos` will be equal to the top-left
  201. // position of the text.
  202. draw_text_ex :: proc(font_handle: Font, text: string, pos: Vec2, font_size: f32, color := BLACK)
  203. //--------------------//
  204. // TEXTURE MANAGEMENT //
  205. //--------------------//
  206. // Create an empty texture.
  207. create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture
  208. // Load a texture from disk and upload it to the GPU so you can draw it to the screen.
  209. // Supports PNG, BMP, TGA and baseline PNG. Note that progressive PNG files are not supported!
  210. //
  211. // The `options` parameter can be used to specify things things such as premultiplication of alpha.
  212. load_texture_from_file :: proc(filename: string, options: Load_Texture_Options = {}) -> Texture
  213. // Load a texture from a byte slice and upload it to the GPU so you can draw it to the screen.
  214. // Supports PNG, BMP, TGA and baseline PNG. Note that progressive PNG files are not supported!
  215. //
  216. // The `options` parameter can be used to specify things things such as premultiplication of alpha.
  217. load_texture_from_bytes :: proc(bytes: []u8, options: Load_Texture_Options = {}) -> Texture
  218. // Load raw texture data. You need to specify the data, size and format of the texture yourself.
  219. // This assumes that there is no header in the data. If your data has a header (you read the data
  220. // from a file on disk), then please use `load_texture_from_bytes` instead.
  221. load_texture_from_bytes_raw :: proc(bytes: []u8, width: int, height: int, format: Pixel_Format) -> Texture
  222. // Get a rectangle that spans the whole texture. Coordinates will be (x, y) = (0, 0) and size
  223. // (w, h) = (texture_width, texture_height)
  224. get_texture_rect :: proc(t: Texture) -> Rect
  225. // Update a texture with new pixels. `bytes` is the new pixel data. `rect` is the rectangle in
  226. // `tex` where the new pixels should end up.
  227. update_texture :: proc(tex: Texture, bytes: []u8, rect: Rect) -> bool
  228. // Destroy a texture, freeing up any memory it has used on the GPU.
  229. destroy_texture :: proc(tex: Texture)
  230. // Controls how a texture should be filtered. You can choose "point" or "linear" filtering. Which
  231. // means "pixly" or "smooth". This filter will be used for up and down-scaling as well as for
  232. // mipmap sampling. Use `set_texture_filter_ex` if you need to control these settings separately.
  233. set_texture_filter :: proc(t: Texture, filter: Texture_Filter)
  234. // Controls how a texture should be filtered. `scale_down_filter` and `scale_up_filter` controls how
  235. // the texture is filtered when we render the texture at a smaller or larger size.
  236. // `mip_filter` controls how the texture is filtered when it is sampled using _mipmapping_.
  237. //
  238. // TODO: Add mipmapping generation controls for texture and refer to it from here.
  239. set_texture_filter_ex :: proc(
  240. t: Texture,
  241. scale_down_filter: Texture_Filter,
  242. scale_up_filter: Texture_Filter,
  243. mip_filter: Texture_Filter,
  244. )
  245. //-----------------//
  246. // RENDER TEXTURES //
  247. //-----------------//
  248. // Create a texture that you can render into. Meaning that you can draw into it instead of drawing
  249. // onto the screen. Use `set_render_texture` to enable this Render Texture for drawing.
  250. create_render_texture :: proc(width: int, height: int) -> Render_Texture
  251. // Destroy a Render_Texture previously created using `create_render_texture`.
  252. destroy_render_texture :: proc(render_texture: Render_Texture)
  253. // Make all rendering go into a texture instead of onto the screen. Create the render texture using
  254. // `create_render_texture`. Pass `nil` to resume drawing onto the screen.
  255. set_render_texture :: proc(render_texture: Maybe(Render_Texture))
  256. //-------//
  257. // FONTS //
  258. //-------//
  259. // Loads a font from disk and returns a handle that represents it.
  260. load_font_from_file :: proc(filename: string) -> Font
  261. // Loads a font from a block of memory and returns a handle that represents it.
  262. load_font_from_bytes :: proc(data: []u8) -> Font
  263. // Destroy a font previously loaded using `load_font_from_file` or `load_font_from_bytes`.
  264. destroy_font :: proc(font: Font)
  265. // Returns the built-in font of Karl2D (the font is known as "roboto")
  266. get_default_font :: proc() -> Font
  267. //---------//
  268. // SHADERS //
  269. //---------//
  270. // Load a shader from a vertex and fragment shader file. If the vertex and fragment shaders live in
  271. // the same file, then pass it twice.
  272. //
  273. // `layout_formats` can in many cases be left default initialized. It is used to specify the format
  274. // of the vertex shader inputs. By formats this means the format that you pass on the CPU side.
  275. load_shader_from_file :: proc(
  276. vertex_filename: string,
  277. fragment_filename: string,
  278. layout_formats: []Pixel_Format = {}
  279. ) -> Shader
  280. // Load a vertex and fragment shader from a block of memory. See `load_shader_from_file` for what
  281. // `layout_formats` means.
  282. load_shader_from_bytes :: proc(
  283. vertex_shader_bytes: []byte,
  284. fragment_shader_bytes: []byte,
  285. layout_formats: []Pixel_Format = {},
  286. ) -> Shader
  287. // Destroy a shader previously loaded using `load_shader_from_file` or `load_shader_from_bytes`
  288. destroy_shader :: proc(shader: Shader)
  289. // Fetches the shader that Karl2D uses by default.
  290. get_default_shader :: proc() -> Shader
  291. // The supplied shader will be used for subsequent drawing. Return to the default shader by calling
  292. // `set_shader(nil)`.
  293. set_shader :: proc(shader: Maybe(Shader))
  294. // Set the value of a constant (also known as uniform in OpenGL). Look up shader constant locations
  295. // (the kind of value needed for `loc`) by running `loc := shader.constant_lookup["constant_name"]`.
  296. set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: any)
  297. // Sets the value of a shader input (also known as a shader attribute). There are three default
  298. // shader inputs known as position, texcoord and color. If you have shader with additional inputs,
  299. // then you can use this procedure to set their values. This is a way to feed per-object data into
  300. // your shader.
  301. //
  302. // `input` should be the index of the input and `val` should be a value of the correct size.
  303. //
  304. // You can modify which type that is expected for `val` by passing a custom `layout_formats` when
  305. // you load the shader.
  306. override_shader_input :: proc(shader: Shader, input: int, val: any)
  307. // Returns the number of bytes that a pixel in a texture uses.
  308. pixel_format_size :: proc(f: Pixel_Format) -> int
  309. //-------------------------------//
  310. // CAMERA AND COORDINATE SYSTEMS //
  311. //-------------------------------//
  312. // Make Karl2D use a camera. Return to the "default camera" by passing `nil`. All drawing operations
  313. // will use this camera until you again change it.
  314. set_camera :: proc(camera: Maybe(Camera))
  315. // Transform a point `pos` that lives on the screen to a point in the world. This can be useful for
  316. // bringing (for example) mouse positions (k2.get_mouse_position()) into world-space.
  317. screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2
  318. // Transform a point `pos` that lices in the world to a point on the screen. This can be useful when
  319. // you need to take a position in the world and compare it to a screen-space point.
  320. world_to_screen :: proc(pos: Vec2, camera: Camera) -> Vec2
  321. // Get the matrix that `screen_to_world` and `world_to_screen` uses to do their transformations.
  322. //
  323. // A view matrix is essentially the world transform matrix of the camera, but inverted. In other
  324. // words, instead of bringing the camera in front of things in the world, we bring everything in the
  325. // world "in front of the camera".
  326. //
  327. // Instead of constructing the camera matrix and doing a matrix inverse, here we just do the
  328. // maths in "backwards order". I.e. a camera transform matrix would be:
  329. //
  330. // target_translate * rot * scale * offset_translate
  331. //
  332. // but we do
  333. //
  334. // inv_offset_translate * inv_scale * inv_rot * inv_target_translate
  335. //
  336. // This is faster, since matrix inverses are expensive.
  337. get_camera_view_matrix :: proc(c: Camera) -> Mat4
  338. // Get the matrix that brings something in front of the camera.
  339. get_camera_world_matrix :: proc(c: Camera) -> Mat4
  340. //------//
  341. // MISC //
  342. //------//
  343. // Choose how the alpha channel is used when mixing half-transparent color with what is already
  344. // drawn. The default is the .Alpha mode, but you also have the option of using .Premultiply_Alpha.
  345. set_blend_mode :: proc(mode: Blend_Mode)
  346. // Make everything outside of the screen-space rectangle `scissor_rect` not render. Disable the
  347. // scissor rectangle by running `set_scissor_rect(nil)`.
  348. set_scissor_rect :: proc(scissor_rect: Maybe(Rect))
  349. // Restore the internal state using the pointer returned by `init`. Useful after reloading the
  350. // library (for example, when doing code hot reload).
  351. set_internal_state :: proc(state: ^State)
  352. //---------------------//
  353. // TYPES AND CONSTANTS //
  354. //---------------------//
  355. Vec2 :: [2]f32
  356. Vec3 :: [3]f32
  357. Vec4 :: [4]f32
  358. Mat4 :: matrix[4,4]f32
  359. // A rectangle that sits at position (x, y) and has size (w, h).
  360. Rect :: struct {
  361. x, y: f32,
  362. w, h: f32,
  363. }
  364. // An RGBA (Red, Green, Blue, Alpha) color. Each channel can have a value between 0 and 255.
  365. Color :: [4]u8
  366. // See the folder examples/palette for a demo that shows all colors
  367. BLACK :: Color { 0, 0, 0, 255 }
  368. WHITE :: Color { 255, 255, 255, 255 }
  369. BLANK :: Color { 0, 0, 0, 0 }
  370. GRAY :: Color { 183, 183, 183, 255 }
  371. DARK_GRAY :: Color { 66, 66, 66, 255}
  372. BLUE :: Color { 25, 198, 236, 255 }
  373. DARK_BLUE :: Color { 7, 47, 88, 255 }
  374. LIGHT_BLUE :: Color { 200, 230, 255, 255 }
  375. GREEN :: Color { 16, 130, 11, 255 }
  376. DARK_GREEN :: Color { 6, 53, 34, 255}
  377. LIGHT_GREEN :: Color { 175, 246, 184, 255 }
  378. ORANGE :: Color { 255, 114, 0, 255 }
  379. RED :: Color { 239, 53, 53, 255 }
  380. DARK_RED :: Color { 127, 10, 10, 255 }
  381. LIGHT_RED :: Color { 248, 183, 183, 255 }
  382. BROWN :: Color { 115, 78, 74, 255 }
  383. DARK_BROWN :: Color { 50, 36, 32, 255 }
  384. LIGHT_BROWN :: Color { 146, 119, 119, 255 }
  385. PURPLE :: Color { 155, 31, 232, 255 }
  386. LIGHT_PURPLE :: Color { 217, 172, 248, 255 }
  387. MAGENTA :: Color { 209, 17, 209, 255 }
  388. YELLOW :: Color { 250, 250, 129, 255 }
  389. LIGHT_YELLOW :: Color { 253, 250, 222, 255 }
  390. // These are from Raylib. They are here so you can easily port a Raylib program to Karl2D.
  391. RL_LIGHTGRAY :: Color { 200, 200, 200, 255 }
  392. RL_GRAY :: Color { 130, 130, 130, 255 }
  393. RL_DARKGRAY :: Color { 80, 80, 80, 255 }
  394. RL_YELLOW :: Color { 253, 249, 0, 255 }
  395. RL_GOLD :: Color { 255, 203, 0, 255 }
  396. RL_ORANGE :: Color { 255, 161, 0, 255 }
  397. RL_PINK :: Color { 255, 109, 194, 255 }
  398. RL_RED :: Color { 230, 41, 55, 255 }
  399. RL_MAROON :: Color { 190, 33, 55, 255 }
  400. RL_GREEN :: Color { 0, 228, 48, 255 }
  401. RL_LIME :: Color { 0, 158, 47, 255 }
  402. RL_DARKGREEN :: Color { 0, 117, 44, 255 }
  403. RL_SKYBLUE :: Color { 102, 191, 255, 255 }
  404. RL_BLUE :: Color { 0, 121, 241, 255 }
  405. RL_DARKBLUE :: Color { 0, 82, 172, 255 }
  406. RL_PURPLE :: Color { 200, 122, 255, 255 }
  407. RL_VIOLET :: Color { 135, 60, 190, 255 }
  408. RL_DARKPURPLE :: Color { 112, 31, 126, 255 }
  409. RL_BEIGE :: Color { 211, 176, 131, 255 }
  410. RL_BROWN :: Color { 127, 106, 79, 255 }
  411. RL_DARKBROWN :: Color { 76, 63, 47, 255 }
  412. RL_WHITE :: WHITE
  413. RL_BLACK :: BLACK
  414. RL_BLANK :: BLANK
  415. RL_MAGENTA :: Color { 255, 0, 255, 255 }
  416. RL_RAYWHITE :: Color { 245, 245, 245, 255 }
  417. color_alpha :: proc(c: Color, a: u8) -> Color
  418. Texture :: struct {
  419. // The render-backend specific texture identifier.
  420. handle: Texture_Handle,
  421. // The horizontal size of the texture, measured in pixels.
  422. width: int,
  423. // The vertical size of the texture, measure in pixels.
  424. height: int,
  425. }
  426. Load_Texture_Option :: enum {
  427. // Will multiply the alpha value of the each pixel into the its RGB values. Useful if you want
  428. // to use `set_blend_mode(.Premultiplied_Alpha)`
  429. Premultiply_Alpha,
  430. }
  431. Load_Texture_Options :: bit_set[Load_Texture_Option]
  432. Blend_Mode :: enum {
  433. Alpha,
  434. // Requires the alpha-channel to be multiplied into texture RGB channels. You can automatically
  435. // do this using the `Premultiply_Alpha` option when loading a texture.
  436. Premultiplied_Alpha,
  437. }
  438. // A render texture is a texture that you can draw into, instead of drawing to the screen. Create
  439. // one using `create_render_texture`.
  440. Render_Texture :: struct {
  441. // The texture that the things will be drawn into. You can use this as a normal texture, for
  442. // example, you can pass it to `draw_texture`.
  443. texture: Texture,
  444. // The render backend's internal identifier. It describes how to use the texture as something
  445. // the render backend can draw into.
  446. render_target: Render_Target_Handle,
  447. }
  448. Texture_Filter :: enum {
  449. Point, // Similar to "nearest neighbor". Pixly texture scaling.
  450. Linear, // Smoothed texture scaling.
  451. }
  452. Camera :: struct {
  453. // Where the camera looks.
  454. target: Vec2,
  455. // By default `target` will be the position of the upper-left corner of the camera. Use this
  456. // offset to change that. If you set the offset to half the size of the camera view, then the
  457. // target position will end up in the middle of the scren.
  458. offset: Vec2,
  459. // Rotate the camera (unit: degrees)
  460. rotation: f32,
  461. // Zoom the camera. A bigger value means "more zoom".
  462. //
  463. // To make a certain amount of pixels always occupy the height of the camera, set the zoom to:
  464. //
  465. // k2.get_screen_height()/wanted_pixel_height
  466. zoom: f32,
  467. }
  468. Window_Flag :: enum {
  469. // Make the window possible to resize. This will make the backbuffer automatically resize as
  470. // well.
  471. Resizable,
  472. }
  473. Window_Flags :: bit_set[Window_Flag]
  474. Shader_Handle :: distinct Handle
  475. SHADER_NONE :: Shader_Handle {}
  476. Shader_Constant_Location :: struct {
  477. offset: int,
  478. size: int,
  479. }
  480. Shader :: struct {
  481. // The render backend's internal identifier.
  482. handle: Shader_Handle,
  483. // We store the CPU-side value of all constants in a single buffer to have less allocations.
  484. // The 'constants' array says where in this buffer each constant is, and 'constant_lookup'
  485. // maps a name to a constant location.
  486. constants_data: []u8,
  487. constants: []Shader_Constant_Location,
  488. // Look up named constants. If you have a constant (uniform) in the shader called "bob", then
  489. // you can find its location by running `shader.constant_lookup["bob"]`. You can then use that
  490. // location in combination with `set_shader_constant`
  491. constant_lookup: map[string]Shader_Constant_Location,
  492. // Maps built in constant types such as "model view projection matrix" to a location.
  493. constant_builtin_locations: [Shader_Builtin_Constant]Maybe(Shader_Constant_Location),
  494. texture_bindpoints: []Texture_Handle,
  495. // Used to lookup bindpoints of textures. You can then set the texture by overriding
  496. // `shader.texture_bindpoints[shader.texture_lookup["some_tex"]] = some_texture.handle`
  497. texture_lookup: map[string]int,
  498. default_texture_index: Maybe(int),
  499. inputs: []Shader_Input,
  500. // Overrides the value of a specific vertex input.
  501. //
  502. // It's recommended you use `override_shader_input` to modify these overrides.
  503. input_overrides: []Shader_Input_Value_Override,
  504. default_input_offsets: [Shader_Default_Inputs]int,
  505. // How many bytes a vertex uses gives the input of the shader.
  506. vertex_size: int,
  507. }
  508. SHADER_INPUT_VALUE_MAX_SIZE :: 256
  509. Shader_Input_Value_Override :: struct {
  510. val: [SHADER_INPUT_VALUE_MAX_SIZE]u8,
  511. used: int,
  512. }
  513. Shader_Input_Type :: enum {
  514. F32,
  515. Vec2,
  516. Vec3,
  517. Vec4,
  518. }
  519. Shader_Builtin_Constant :: enum {
  520. MVP,
  521. }
  522. Shader_Default_Inputs :: enum {
  523. Unknown,
  524. Position,
  525. UV,
  526. Color,
  527. }
  528. Shader_Input :: struct {
  529. name: string,
  530. register: int,
  531. type: Shader_Input_Type,
  532. format: Pixel_Format,
  533. }
  534. Pixel_Format :: enum {
  535. Unknown,
  536. RGBA_32_Float,
  537. RGB_32_Float,
  538. RG_32_Float,
  539. R_32_Float,
  540. RGBA_8_Norm,
  541. RG_8_Norm,
  542. R_8_Norm,
  543. R_8_UInt,
  544. }
  545. Font_Data :: struct {
  546. atlas: Texture,
  547. // internal
  548. fontstash_handle: int,
  549. }
  550. Handle :: hm.Handle
  551. Texture_Handle :: distinct Handle
  552. Render_Target_Handle :: distinct Handle
  553. Font :: distinct int
  554. FONT_NONE :: Font {}
  555. TEXTURE_NONE :: Texture_Handle {}
  556. RENDER_TARGET_NONE :: Render_Target_Handle {}
  557. // This keeps track of the internal state of the library. Usually, you do not need to poke at it.
  558. // It is created and kept as a global variable when 'init' is called. However, 'init' also returns
  559. // the pointer to it, so you can later use 'set_internal_state' to restore it (after for example hot
  560. // reload).
  561. State :: struct {
  562. allocator: runtime.Allocator,
  563. frame_arena: runtime.Arena,
  564. frame_allocator: runtime.Allocator,
  565. win: Window_Interface,
  566. window_state: rawptr,
  567. rb: Render_Backend_Interface,
  568. rb_state: rawptr,
  569. fs: fs.FontContext,
  570. shutdown_wanted: bool,
  571. mouse_position: Vec2,
  572. mouse_delta: Vec2,
  573. mouse_wheel_delta: f32,
  574. key_went_down: #sparse [Keyboard_Key]bool,
  575. key_went_up: #sparse [Keyboard_Key]bool,
  576. key_is_held: #sparse [Keyboard_Key]bool,
  577. mouse_button_went_down: #sparse [Mouse_Button]bool,
  578. mouse_button_went_up: #sparse [Mouse_Button]bool,
  579. mouse_button_is_held: #sparse [Mouse_Button]bool,
  580. gamepad_button_went_down: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  581. gamepad_button_went_up: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  582. gamepad_button_is_held: [MAX_GAMEPADS]#sparse [Gamepad_Button]bool,
  583. window: Window_Handle,
  584. default_font: Font,
  585. fonts: [dynamic]Font_Data,
  586. shape_drawing_texture: Texture_Handle,
  587. batch_font: Font,
  588. batch_camera: Maybe(Camera),
  589. batch_shader: Shader,
  590. batch_scissor: Maybe(Rect),
  591. batch_texture: Texture_Handle,
  592. batch_render_target: Render_Target_Handle,
  593. batch_blend_mode: Blend_Mode,
  594. view_matrix: Mat4,
  595. proj_matrix: Mat4,
  596. depth: f32,
  597. depth_start: f32,
  598. depth_increment: f32,
  599. vertex_buffer_cpu: []u8,
  600. vertex_buffer_cpu_used: int,
  601. default_shader: Shader,
  602. // Time when the first call to `new_frame` happened
  603. start_time: time.Time,
  604. prev_frame_time: time.Time,
  605. // "dt"
  606. frame_time: f32,
  607. time: f64,
  608. }
  609. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  610. // options presented here.
  611. Mouse_Button :: enum {
  612. Left,
  613. Right,
  614. Middle,
  615. Max = 255,
  616. }
  617. // Based on Raylib / GLFW
  618. Keyboard_Key :: enum {
  619. None = 0,
  620. // Numeric keys (top row)
  621. N0 = 48,
  622. N1 = 49,
  623. N2 = 50,
  624. N3 = 51,
  625. N4 = 52,
  626. N5 = 53,
  627. N6 = 54,
  628. N7 = 55,
  629. N8 = 56,
  630. N9 = 57,
  631. // Letter keys
  632. A = 65,
  633. B = 66,
  634. C = 67,
  635. D = 68,
  636. E = 69,
  637. F = 70,
  638. G = 71,
  639. H = 72,
  640. I = 73,
  641. J = 74,
  642. K = 75,
  643. L = 76,
  644. M = 77,
  645. N = 78,
  646. O = 79,
  647. P = 80,
  648. Q = 81,
  649. R = 82,
  650. S = 83,
  651. T = 84,
  652. U = 85,
  653. V = 86,
  654. W = 87,
  655. X = 88,
  656. Y = 89,
  657. Z = 90,
  658. // Special characters
  659. Apostrophe = 39,
  660. Comma = 44,
  661. Minus = 45,
  662. Period = 46,
  663. Slash = 47,
  664. Semicolon = 59,
  665. Equal = 61,
  666. Left_Bracket = 91,
  667. Backslash = 92,
  668. Right_Bracket = 93,
  669. Backtick = 96,
  670. // Function keys, modifiers, caret control etc
  671. Space = 32,
  672. Escape = 256,
  673. Enter = 257,
  674. Tab = 258,
  675. Backspace = 259,
  676. Insert = 260,
  677. Delete = 261,
  678. Right = 262,
  679. Left = 263,
  680. Down = 264,
  681. Up = 265,
  682. Page_Up = 266,
  683. Page_Down = 267,
  684. Home = 268,
  685. End = 269,
  686. Caps_Lock = 280,
  687. Scroll_Lock = 281,
  688. Num_Lock = 282,
  689. Print_Screen = 283,
  690. Pause = 284,
  691. F1 = 290,
  692. F2 = 291,
  693. F3 = 292,
  694. F4 = 293,
  695. F5 = 294,
  696. F6 = 295,
  697. F7 = 296,
  698. F8 = 297,
  699. F9 = 298,
  700. F10 = 299,
  701. F11 = 300,
  702. F12 = 301,
  703. Left_Shift = 340,
  704. Left_Control = 341,
  705. Left_Alt = 342,
  706. Left_Super = 343,
  707. Right_Shift = 344,
  708. Right_Control = 345,
  709. Right_Alt = 346,
  710. Right_Super = 347,
  711. Menu = 348,
  712. // Numpad keys
  713. NP_0 = 320,
  714. NP_1 = 321,
  715. NP_2 = 322,
  716. NP_3 = 323,
  717. NP_4 = 324,
  718. NP_5 = 325,
  719. NP_6 = 326,
  720. NP_7 = 327,
  721. NP_8 = 328,
  722. NP_9 = 329,
  723. NP_Decimal = 330,
  724. NP_Divide = 331,
  725. NP_Multiply = 332,
  726. NP_Subtract = 333,
  727. NP_Add = 334,
  728. NP_Enter = 335,
  729. NP_Equal = 336,
  730. }
  731. MAX_GAMEPADS :: 4
  732. // A value between 0 and MAX_GAMEPADS - 1
  733. Gamepad_Index :: int
  734. Gamepad_Axis :: enum {
  735. Left_Stick_X,
  736. Left_Stick_Y,
  737. Right_Stick_X,
  738. Right_Stick_Y,
  739. Left_Trigger,
  740. Right_Trigger,
  741. }
  742. Gamepad_Button :: enum {
  743. // DPAD buttons
  744. Left_Face_Up,
  745. Left_Face_Down,
  746. Left_Face_Left,
  747. Left_Face_Right,
  748. Right_Face_Up, // XBOX: Y, PS: Triangle
  749. Right_Face_Down, // XBOX: A, PS: X
  750. Right_Face_Left, // XBOX: X, PS: Square
  751. Right_Face_Right, // XBOX: B, PS: Circle
  752. Left_Shoulder,
  753. Left_Trigger,
  754. Right_Shoulder,
  755. Right_Trigger,
  756. Left_Stick_Press, // Clicking the left analogue stick
  757. Right_Stick_Press, // Clicking the right analogue stick
  758. Middle_Face_Left, // Select / back / options button
  759. Middle_Face_Middle, // PS button (not available on XBox)
  760. Middle_Face_Right, // Start
  761. }