karl2d.odin 6.8 KB

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