karl2d.doc.odin 32 KB

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