karl2d.odin 7.9 KB

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