karlib.odin 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // Based on Raylib / GLFW
  84. Keyboard_Key :: enum {
  85. None = 0,
  86. // Alphanumeric keys
  87. Apostrophe = 39,
  88. Comma = 44,
  89. Minus = 45,
  90. Period = 46,
  91. Slash = 47,
  92. Zero = 48,
  93. One = 49,
  94. Two = 50,
  95. Three = 51,
  96. Four = 52,
  97. Five = 53,
  98. Six = 54,
  99. Seven = 55,
  100. Eight = 56,
  101. Nine = 57,
  102. Semicolon = 59,
  103. Equal = 61,
  104. A = 65,
  105. B = 66,
  106. C = 67,
  107. D = 68,
  108. E = 69,
  109. F = 70,
  110. G = 71,
  111. H = 72,
  112. I = 73,
  113. J = 74,
  114. K = 75,
  115. L = 76,
  116. M = 77,
  117. N = 78,
  118. O = 79,
  119. P = 80,
  120. Q = 81,
  121. R = 82,
  122. S = 83,
  123. T = 84,
  124. U = 85,
  125. V = 86,
  126. W = 87,
  127. X = 88,
  128. Y = 89,
  129. Z = 90,
  130. Left_Bracket = 91,
  131. Backslash = 92,
  132. Right_Bracket = 93,
  133. Grave = 96,
  134. // Function keys
  135. Space = 32,
  136. Escape = 256,
  137. Enter = 257,
  138. Tab = 258,
  139. Backspace = 259,
  140. Insert = 260,
  141. Delete = 261,
  142. Right = 262,
  143. Left = 263,
  144. Down = 264,
  145. Up = 265,
  146. Page_Up = 266,
  147. Page_Down = 267,
  148. Home = 268,
  149. End = 269,
  150. Caps_Lock = 280,
  151. Scroll_Lock = 281,
  152. Num_Lock = 282,
  153. Print_Screen = 283,
  154. Pause = 284,
  155. F1 = 290,
  156. F2 = 291,
  157. F3 = 292,
  158. F4 = 293,
  159. F5 = 294,
  160. F6 = 295,
  161. F7 = 296,
  162. F8 = 297,
  163. F9 = 298,
  164. F10 = 299,
  165. F11 = 300,
  166. F12 = 301,
  167. Left_Shift = 340,
  168. Left_Control = 341,
  169. Left_Alt = 342,
  170. Left_Super = 343,
  171. Right_Shift = 344,
  172. Right_Control = 345,
  173. Right_Alt = 346,
  174. Right_Super = 347,
  175. Menu = 348,
  176. // Keypad keys
  177. KP_0 = 320,
  178. KP_1 = 321,
  179. KP_2 = 322,
  180. KP_3 = 323,
  181. KP_4 = 324,
  182. KP_5 = 325,
  183. KP_6 = 326,
  184. KP_7 = 327,
  185. KP_8 = 328,
  186. KP_9 = 329,
  187. KP_Decimal = 330,
  188. KP_Divide = 331,
  189. KP_Multiply = 332,
  190. KP_Subtract = 333,
  191. KP_Add = 334,
  192. KP_Enter = 335,
  193. KP_Equal = 336,
  194. }