karlib.odin 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package karlib
  2. // Opens a window and initializes some internal state. The internal state will use `allocator` for
  3. // all dynamically allocated memory. The return value can be ignored unless you need to later call
  4. // `set_state`.
  5. init: proc(window_width: int, window_height: int, window_title: string,
  6. allocator := context.allocator, loc := #caller_location) -> ^State : _init
  7. // Closes the window and cleans up the internal state.
  8. shutdown: proc() : _shutdown
  9. // Clear the backbuffer with supplied color.
  10. clear: proc(color: Color) : _clear
  11. // Call at start or end of frame to process all events that have arrived to the window.
  12. //
  13. // WARNING: Not calling this will make your program impossible to interact with.
  14. process_events: proc() : _process_events
  15. // Present the backbuffer. Call at end of frame to make everything you've drawn appear on the screen.
  16. present: proc(do_flush := true) : _present
  17. window_should_close : proc() -> bool : _window_should_close
  18. // Can be used to restore the internal state using the pointer returned by `init`. Useful after
  19. // reloading the library (for example, when doing code hot reload).
  20. set_internal_state: proc(ks: ^State) : _set_internal_state
  21. set_window_size: proc(width: int, height: int) : _set_window_size
  22. set_window_position: proc(x: int, y: int) : _set_window_position
  23. get_screen_width: proc() -> int : _get_screen_width
  24. get_screen_height: proc() -> int : _get_screen_height
  25. load_texture_from_file: proc(filename: string) -> Texture : _load_texture
  26. // load_texture_from_bytes or buffer or something ()
  27. destroy_texture: proc(tex: Texture) : _destroy_texture
  28. set_camera: proc(camera: Maybe(Camera)) : _set_camera
  29. set_scissor_rect: proc(scissor_rect: Maybe(Rect)) : _set_scissor_rect
  30. draw_texture: proc(tex: Texture, pos: Vec2, tint := WHITE) : _draw_texture
  31. draw_texture_rect: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) : _draw_texture_rect
  32. draw_texture_ex: proc(tex: Texture, src: Rect, dest: Rect, origin: Vec2, rotation: f32, tint := WHITE) : _draw_texture_ex
  33. draw_rect: proc(rect: Rect, color: Color) : _draw_rectangle
  34. draw_rect_outline: proc(rect: Rect, thickness: f32, color: Color) : _draw_rectangle_outline
  35. draw_circle: proc(center: Vec2, radius: f32, color: Color) : _draw_circle
  36. draw_line: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) : _draw_line
  37. // WARNING: Not proper text rendering yet... No font support etc
  38. draw_text: proc(text: string, pos: Vec2, font_size: f32, color: Color) : _draw_text
  39. screen_to_world: proc(pos: Vec2, camera: Camera) -> Vec2 : _screen_to_world
  40. key_went_down: proc(key: Keyboard_Key) -> bool : _key_pressed
  41. key_went_up: proc(key: Keyboard_Key) -> bool : _key_released
  42. key_is_held: proc(key: Keyboard_Key) -> bool : _key_held
  43. mouse_button_went_down: proc(button: Mouse_Button) -> bool : _mouse_button_pressed
  44. mouse_button_went_up: proc(button: Mouse_Button) -> bool : _mouse_button_released
  45. mouse_button_is_held: proc(button: Mouse_Button) -> bool : _mouse_button_held
  46. get_mouse_wheel_delta: proc() -> f32 : _mouse_wheel_delta
  47. get_mouse_position: proc() -> Vec2 : _mouse_position
  48. Color :: [4]u8
  49. Vec2 :: [2]f32
  50. Vec2i :: [2]int
  51. Rect :: struct {
  52. x, y: f32,
  53. w, h: f32,
  54. }
  55. Texture_Handle :: distinct int
  56. Texture :: struct {
  57. id: Texture_Handle,
  58. width: int,
  59. height: int,
  60. }
  61. Camera :: struct {
  62. target: Vec2,
  63. origin: Vec2,
  64. rotation: f32,
  65. zoom: f32,
  66. }
  67. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  68. // options presented here.
  69. Mouse_Button :: enum {
  70. Left,
  71. Right,
  72. Middle,
  73. Max = 255,
  74. }
  75. // TODO: These are just copied from raylib, we probably want a list of our own "default colors"
  76. WHITE :: Color { 255, 255, 255, 255 }
  77. BLACK :: Color { 0, 0, 0, 255 }
  78. GRAY :: Color{ 130, 130, 130, 255 }
  79. RED :: Color { 230, 41, 55, 255 }
  80. YELLOW :: Color { 253, 249, 0, 255 }
  81. BLUE :: Color { 0, 121, 241, 255 }
  82. MAGENTA :: Color { 255, 0, 255, 255 }
  83. DARKGRAY :: Color{ 80, 80, 80, 255 }
  84. GREEN :: Color{ 0, 228, 48, 255 }
  85. // Based on Raylib / GLFW
  86. Keyboard_Key :: enum {
  87. None = 0,
  88. // Alphanumeric keys
  89. Apostrophe = 39,
  90. Comma = 44,
  91. Minus = 45,
  92. Period = 46,
  93. Slash = 47,
  94. Zero = 48,
  95. One = 49,
  96. Two = 50,
  97. Three = 51,
  98. Four = 52,
  99. Five = 53,
  100. Six = 54,
  101. Seven = 55,
  102. Eight = 56,
  103. Nine = 57,
  104. Semicolon = 59,
  105. Equal = 61,
  106. A = 65,
  107. B = 66,
  108. C = 67,
  109. D = 68,
  110. E = 69,
  111. F = 70,
  112. G = 71,
  113. H = 72,
  114. I = 73,
  115. J = 74,
  116. K = 75,
  117. L = 76,
  118. M = 77,
  119. N = 78,
  120. O = 79,
  121. P = 80,
  122. Q = 81,
  123. R = 82,
  124. S = 83,
  125. T = 84,
  126. U = 85,
  127. V = 86,
  128. W = 87,
  129. X = 88,
  130. Y = 89,
  131. Z = 90,
  132. Left_Bracket = 91,
  133. Backslash = 92,
  134. Right_Bracket = 93,
  135. Grave = 96,
  136. // Function keys
  137. Space = 32,
  138. Escape = 256,
  139. Enter = 257,
  140. Tab = 258,
  141. Backspace = 259,
  142. Insert = 260,
  143. Delete = 261,
  144. Right = 262,
  145. Left = 263,
  146. Down = 264,
  147. Up = 265,
  148. Page_Up = 266,
  149. Page_Down = 267,
  150. Home = 268,
  151. End = 269,
  152. Caps_Lock = 280,
  153. Scroll_Lock = 281,
  154. Num_Lock = 282,
  155. Print_Screen = 283,
  156. Pause = 284,
  157. F1 = 290,
  158. F2 = 291,
  159. F3 = 292,
  160. F4 = 293,
  161. F5 = 294,
  162. F6 = 295,
  163. F7 = 296,
  164. F8 = 297,
  165. F9 = 298,
  166. F10 = 299,
  167. F11 = 300,
  168. F12 = 301,
  169. Left_Shift = 340,
  170. Left_Control = 341,
  171. Left_Alt = 342,
  172. Left_Super = 343,
  173. Right_Shift = 344,
  174. Right_Control = 345,
  175. Right_Alt = 346,
  176. Right_Super = 347,
  177. Menu = 348,
  178. // Keypad keys
  179. KP_0 = 320,
  180. KP_1 = 321,
  181. KP_2 = 322,
  182. KP_3 = 323,
  183. KP_4 = 324,
  184. KP_5 = 325,
  185. KP_6 = 326,
  186. KP_7 = 327,
  187. KP_8 = 328,
  188. KP_9 = 329,
  189. KP_Decimal = 330,
  190. KP_Divide = 331,
  191. KP_Multiply = 332,
  192. KP_Subtract = 333,
  193. KP_Add = 334,
  194. KP_Enter = 335,
  195. KP_Equal = 336,
  196. }