karl2d.odin 7.5 KB

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