karl2d.odin 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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() : _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. draw_current_batch: proc() : _draw_current_batch
  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_from_file
  31. load_texture_from_memory: proc(data: []u8, width: int, height: int) -> Texture : _load_texture_from_memory
  32. // load_texture_from_bytes or buffer or something ()
  33. destroy_texture: proc(tex: Texture) : _destroy_texture
  34. set_camera: proc(camera: Maybe(Camera)) : _set_camera
  35. set_scissor_rect: proc(scissor_rect: Maybe(Rect)) : _set_scissor_rect
  36. set_shader: proc(shader: Shader_Handle) : _set_shader
  37. draw_texture: proc(tex: Texture, pos: Vec2, tint := WHITE) : _draw_texture
  38. draw_texture_rect: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) : _draw_texture_rect
  39. draw_texture_ex: proc(tex: Texture, src: Rect, dest: Rect, origin: Vec2, rotation: f32, tint := WHITE) : _draw_texture_ex
  40. draw_rect: proc(rect: Rect, color: Color) : _draw_rectangle
  41. draw_rect_outline: proc(rect: Rect, thickness: f32, color: Color) : _draw_rectangle_outline
  42. draw_circle: proc(center: Vec2, radius: f32, color: Color) : _draw_circle
  43. draw_line: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) : _draw_line
  44. load_shader: proc(shader_source: string) -> Shader_Handle : _load_shader
  45. destroy_shader: proc(shader: Shader_Handle) : _destroy_shader
  46. get_shader_constant_location: proc(shader: Shader_Handle, name: string) -> Shader_Constant_Location : _get_shader_constant_location
  47. set_shader_constant :: _set_shader_constant
  48. set_shader_constant_mat4: proc(shader: Shader_Handle, loc: Shader_Constant_Location, val: matrix[4,4]f32) : _set_shader_constant_mat4
  49. set_shader_constant_f32: proc(shader: Shader_Handle, loc: Shader_Constant_Location, val: f32) : _set_shader_constant_f32
  50. set_shader_constant_vec2: proc(shader: Shader_Handle, loc: Shader_Constant_Location, val: Vec2) : _set_shader_constant_vec2
  51. // WARNING: Not proper text rendering yet... No font support etc
  52. draw_text: proc(text: string, pos: Vec2, font_size: f32, color: Color) : _draw_text
  53. screen_to_world: proc(pos: Vec2, camera: Camera) -> Vec2 : _screen_to_world
  54. key_went_down: proc(key: Keyboard_Key) -> bool : _key_went_down
  55. key_went_up: proc(key: Keyboard_Key) -> bool : _key_went_up
  56. key_is_held: proc(key: Keyboard_Key) -> bool : _key_is_held
  57. mouse_button_went_down: proc(button: Mouse_Button) -> bool : _mouse_button_pressed
  58. mouse_button_went_up: proc(button: Mouse_Button) -> bool : _mouse_button_released
  59. mouse_button_is_held: proc(button: Mouse_Button) -> bool : _mouse_button_held
  60. get_mouse_wheel_delta: proc() -> f32 : _mouse_wheel_delta
  61. get_mouse_position: proc() -> Vec2 : _mouse_position
  62. Color :: [4]u8
  63. Vec2 :: [2]f32
  64. Vec2i :: [2]int
  65. Rect :: struct {
  66. x, y: f32,
  67. w, h: f32,
  68. }
  69. Texture :: struct {
  70. id: Texture_Handle,
  71. width: int,
  72. height: int,
  73. }
  74. Camera :: struct {
  75. target: Vec2,
  76. origin: Vec2,
  77. rotation: f32,
  78. zoom: f32,
  79. }
  80. // Support for up to 255 mouse buttons. Cast an int to type `Mouse_Button` to use things outside the
  81. // options presented here.
  82. Mouse_Button :: enum {
  83. Left,
  84. Right,
  85. Middle,
  86. Max = 255,
  87. }
  88. // TODO: These are just copied from raylib, we probably want a list of our own "default colors"
  89. WHITE :: Color { 255, 255, 255, 255 }
  90. BLACK :: Color { 0, 0, 0, 255 }
  91. GRAY :: Color{ 130, 130, 130, 255 }
  92. RED :: Color { 230, 41, 55, 255 }
  93. YELLOW :: Color { 253, 249, 0, 255 }
  94. BLUE :: Color { 0, 121, 241, 255 }
  95. MAGENTA :: Color { 255, 0, 255, 255 }
  96. DARKGRAY :: Color{ 80, 80, 80, 255 }
  97. GREEN :: Color{ 0, 228, 48, 255 }
  98. Shader_Handle :: distinct Handle
  99. SHADER_NONE :: Shader_Handle {}
  100. // Based on Raylib / GLFW
  101. Keyboard_Key :: enum {
  102. None = 0,
  103. // Alphanumeric keys
  104. Apostrophe = 39,
  105. Comma = 44,
  106. Minus = 45,
  107. Period = 46,
  108. Slash = 47,
  109. Zero = 48,
  110. One = 49,
  111. Two = 50,
  112. Three = 51,
  113. Four = 52,
  114. Five = 53,
  115. Six = 54,
  116. Seven = 55,
  117. Eight = 56,
  118. Nine = 57,
  119. Semicolon = 59,
  120. Equal = 61,
  121. A = 65,
  122. B = 66,
  123. C = 67,
  124. D = 68,
  125. E = 69,
  126. F = 70,
  127. G = 71,
  128. H = 72,
  129. I = 73,
  130. J = 74,
  131. K = 75,
  132. L = 76,
  133. M = 77,
  134. N = 78,
  135. O = 79,
  136. P = 80,
  137. Q = 81,
  138. R = 82,
  139. S = 83,
  140. T = 84,
  141. U = 85,
  142. V = 86,
  143. W = 87,
  144. X = 88,
  145. Y = 89,
  146. Z = 90,
  147. Left_Bracket = 91,
  148. Backslash = 92,
  149. Right_Bracket = 93,
  150. Grave = 96,
  151. // Function keys
  152. Space = 32,
  153. Escape = 256,
  154. Enter = 257,
  155. Tab = 258,
  156. Backspace = 259,
  157. Insert = 260,
  158. Delete = 261,
  159. Right = 262,
  160. Left = 263,
  161. Down = 264,
  162. Up = 265,
  163. Page_Up = 266,
  164. Page_Down = 267,
  165. Home = 268,
  166. End = 269,
  167. Caps_Lock = 280,
  168. Scroll_Lock = 281,
  169. Num_Lock = 282,
  170. Print_Screen = 283,
  171. Pause = 284,
  172. F1 = 290,
  173. F2 = 291,
  174. F3 = 292,
  175. F4 = 293,
  176. F5 = 294,
  177. F6 = 295,
  178. F7 = 296,
  179. F8 = 297,
  180. F9 = 298,
  181. F10 = 299,
  182. F11 = 300,
  183. F12 = 301,
  184. Left_Shift = 340,
  185. Left_Control = 341,
  186. Left_Alt = 342,
  187. Left_Super = 343,
  188. Right_Shift = 344,
  189. Right_Control = 345,
  190. Right_Alt = 346,
  191. Right_Super = 347,
  192. Menu = 348,
  193. // Keypad keys
  194. KP_0 = 320,
  195. KP_1 = 321,
  196. KP_2 = 322,
  197. KP_3 = 323,
  198. KP_4 = 324,
  199. KP_5 = 325,
  200. KP_6 = 326,
  201. KP_7 = 327,
  202. KP_8 = 328,
  203. KP_9 = 329,
  204. KP_Decimal = 330,
  205. KP_Divide = 331,
  206. KP_Multiply = 332,
  207. KP_Subtract = 333,
  208. KP_Add = 334,
  209. KP_Enter = 335,
  210. KP_Equal = 336,
  211. }