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