sdl2.odin 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package sdl2
  2. /*
  3. Simple DirectMedia Layer
  4. Copyright (C) 1997-2017 Sam Lantinga <[email protected]>
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. import "core:c"
  20. import "base:intrinsics"
  21. when ODIN_OS == .Windows {
  22. foreign import lib "SDL2.lib"
  23. } else {
  24. foreign import lib "system:SDL2"
  25. }
  26. version :: struct {
  27. major: u8, /**< major version */
  28. minor: u8, /**< minor version */
  29. patch: u8, /**< update version */
  30. }
  31. MAJOR_VERSION :: 2
  32. MINOR_VERSION :: 0
  33. PATCHLEVEL :: 16
  34. VERSION :: proc "contextless" (ver: ^version) {
  35. ver.major = MAJOR_VERSION
  36. ver.minor = MINOR_VERSION
  37. ver.patch = PATCHLEVEL
  38. }
  39. @(default_calling_convention="c", link_prefix="SDL_")
  40. foreign lib {
  41. GetVersion :: proc(ver: ^version) ---
  42. GetRevision :: proc() -> cstring ---
  43. }
  44. InitFlag :: enum u32 {
  45. TIMER = 0x00,
  46. AUDIO = 0x04,
  47. VIDEO = 0x05,
  48. JOYSTICK = 0x09,
  49. HAPTIC = 0x0c,
  50. GAMECONTROLLER = 0x0d,
  51. EVENTS = 0x0e,
  52. SENSOR = 0x0f,
  53. NOPARACHUTE = 0x14,
  54. }
  55. InitFlags :: bit_set[InitFlag; u32]
  56. INIT_TIMER :: InitFlags{.TIMER}
  57. INIT_AUDIO :: InitFlags{.AUDIO}
  58. INIT_VIDEO :: InitFlags{.VIDEO} /**< INIT_VIDEO implies INIT_EVENTS */
  59. INIT_JOYSTICK :: InitFlags{.JOYSTICK} /**< INIT_JOYSTICK implies INIT_EVENTS */
  60. INIT_HAPTIC :: InitFlags{.HAPTIC}
  61. INIT_GAMECONTROLLER :: InitFlags{.GAMECONTROLLER} /**< INIT_GAMECONTROLLER implies INIT_JOYSTICK */
  62. INIT_EVENTS :: InitFlags{.EVENTS}
  63. INIT_SENSOR :: InitFlags{.SENSOR}
  64. INIT_NOPARACHUTE :: InitFlags{.NOPARACHUTE} /**< compatibility; this flag is ignored. */
  65. INIT_EVERYTHING :: InitFlags{.TIMER, .AUDIO, .VIDEO, .EVENTS, .JOYSTICK, .HAPTIC, .GAMECONTROLLER, .SENSOR}
  66. @(default_calling_convention="c", link_prefix="SDL_")
  67. foreign lib {
  68. Init :: proc(flags: InitFlags) -> c.int ---
  69. InitSubSystem :: proc(flags: InitFlags) -> c.int ---
  70. QuitSubSystem :: proc(flags: InitFlags) ---
  71. WasInit :: proc(flags: InitFlags) -> InitFlags ---
  72. Quit :: proc() ---
  73. }
  74. // Atomic
  75. // NOTE: Prefer the intrinsics built into Odin 'package intrinsics'
  76. SpinLock :: distinct c.int
  77. atomic_t :: struct { value: c.int }
  78. @(default_calling_convention="c", link_prefix="SDL_")
  79. foreign lib {
  80. AtomicTryLock :: proc(lock: ^SpinLock) -> bool ---
  81. AtomicLock :: proc(lock: ^SpinLock) ---
  82. AtomicUnlock :: proc(lock: ^SpinLock) ---
  83. MemoryBarrierReleaseFunction :: proc() ---
  84. MemoryBarrierAcquireFunction :: proc() ---
  85. AtomicCAS :: proc(a: ^atomic_t, oldval, newval: c.int) -> bool ---
  86. AtomicSet :: proc(a: ^atomic_t, v: c.int) -> c.int ---
  87. AtomicGet :: proc(a: ^atomic_t) -> c.int ---
  88. AtomicAdd :: proc(a: ^atomic_t, v: c.int) -> c.int ---
  89. AtomicCASPtr :: proc(a: ^rawptr, oldval, newval: rawptr) -> bool ---
  90. AtomicSetPtr :: proc(a: ^rawptr, v: rawptr) -> rawptr ---
  91. AtomicGetPtr :: proc(a: ^rawptr) -> rawptr ---
  92. }
  93. // Bits
  94. MostSignificantBitIndex32 :: #force_inline proc "c" (x: u32) -> c.int {
  95. return c.int(intrinsics.count_leading_zeros(x))
  96. }
  97. HasExactlyOneBitSet32 :: #force_inline proc "c" (x: u32) -> bool {
  98. return intrinsics.count_ones(x) == 1
  99. }
  100. // Clipboard
  101. @(default_calling_convention="c", link_prefix="SDL_")
  102. foreign lib {
  103. SetClipboardText :: proc(text: cstring) -> c.int ---
  104. GetClipboardText :: proc() -> cstring ---
  105. HasClipboardText :: proc() -> bool ---
  106. }
  107. // Error
  108. @(default_calling_convention="c", link_prefix="SDL_")
  109. foreign lib {
  110. SetError :: proc(fmt: cstring, #c_vararg args: ..any) -> c.int ---
  111. GetError :: proc() -> cstring ---
  112. GetErrorMsg :: proc(errstr: [^]u8, maxlen: c.int) -> cstring ---
  113. ClearError :: proc() ---
  114. }
  115. GetErrorString :: proc "c" () -> string {
  116. return string(GetError())
  117. }
  118. GetErrorMsgString :: proc "c" (buf: []u8) -> string {
  119. cstr := GetErrorMsg(raw_data(buf), c.int(len(buf)))
  120. return string(cstr)
  121. }
  122. /**
  123. * \name Internal error functions
  124. *
  125. * \internal
  126. * Private error reporting function - used internally.
  127. */
  128. OutOfMemory :: #force_inline proc "c" () -> c.int { return Error(.ENOMEM) }
  129. Unsupported :: #force_inline proc "c" () -> c.int { return Error(.UNSUPPORTED) }
  130. InvalidParamError :: #force_inline proc "c" (param: cstring) -> c.int { return SetError("Parameter '%s' is invalid", param) }
  131. errorcode :: enum c.int {
  132. ENOMEM,
  133. EFREAD,
  134. EFWRITE,
  135. EFSEEK,
  136. UNSUPPORTED,
  137. LASTERROR,
  138. }
  139. /* SDL_Error() unconditionally returns -1. */
  140. @(default_calling_convention="c", link_prefix="SDL_")
  141. foreign lib {
  142. Error :: proc(code: errorcode) -> c.int ---
  143. }
  144. // Filesystem
  145. @(default_calling_convention="c", link_prefix="SDL_")
  146. foreign lib {
  147. GetBasePath :: proc() -> cstring ---
  148. GetPrefPath :: proc(org, app: cstring) -> cstring ---
  149. }
  150. // loadso
  151. @(default_calling_convention="c", link_prefix="SDL_")
  152. foreign lib {
  153. LoadObject :: proc(sofile: cstring) -> rawptr ---
  154. LoadFunction :: proc(handle: rawptr, name: cstring) -> rawptr ---
  155. UnloadObject :: proc(handle: rawptr) ---
  156. }
  157. // locale
  158. Locale :: struct {
  159. language: cstring, /**< A language name, like "en" for English. */
  160. country: cstring, /**< A country, like "US" for America. Can be NULL. */
  161. }
  162. @(default_calling_convention="c", link_prefix="SDL_")
  163. foreign lib {
  164. GetPreferredLocales :: proc() -> [^]Locale ---
  165. }
  166. // misc
  167. @(default_calling_convention="c", link_prefix="SDL_")
  168. foreign lib {
  169. OpenURL :: proc(url: cstring) -> c.int ---
  170. }
  171. // platform
  172. @(default_calling_convention="c", link_prefix="SDL_")
  173. foreign lib {
  174. GetPlatform :: proc() -> cstring ---
  175. }
  176. // power
  177. PowerState :: enum c.int {
  178. UNKNOWN, /**< cannot determine power status */
  179. ON_BATTERY, /**< Not plugged in, running on the battery */
  180. NO_BATTERY, /**< Plugged in, no battery available */
  181. CHARGING, /**< Plugged in, charging battery */
  182. CHARGED, /**< Plugged in, battery charged */
  183. }
  184. @(default_calling_convention="c", link_prefix="SDL_")
  185. foreign lib {
  186. GetPowerInfo :: proc(secs: ^c.int, pct: ^c.int) -> PowerState ---
  187. }
  188. // quit
  189. QuitRequested :: #force_inline proc "c" () -> bool {
  190. PumpEvents()
  191. return bool(PeepEvents(nil, 0, .PEEKEVENT, .QUIT, .QUIT) > 0)
  192. }
  193. // sensor
  194. Sensor :: struct {}
  195. SensorID :: distinct i32
  196. SensorType :: enum c.int {
  197. INVALID = -1, /**< Returned for an invalid sensor */
  198. UNKNOWN, /**< Unknown sensor type */
  199. ACCEL, /**< Accelerometer */
  200. GYRO, /**< Gyroscope */
  201. }
  202. STANDARD_GRAVITY :: 9.80665
  203. @(default_calling_convention="c", link_prefix="SDL_")
  204. foreign lib {
  205. LockSensors :: proc() ---
  206. UnlockSensors :: proc() ---
  207. NumSensors :: proc() -> c.int ---
  208. SensorGetDeviceName :: proc(device_index: c.int) -> cstring ---
  209. SensorGetDeviceType :: proc(device_index: c.int) -> SensorType ---
  210. SensorGetDeviceNonPortableType :: proc(device_index: c.int) -> c.int ---
  211. SensorGetDeviceInstanceID :: proc(device_index: c.int) -> SensorID ---
  212. SensorOpen :: proc(device_index: c.int) -> ^Sensor ---
  213. SensorFromInstanceID :: proc(instance_id: SensorID) -> ^Sensor ---
  214. SensorGetName :: proc(sensor: ^Sensor) -> cstring ---
  215. SensorGetType :: proc(sensor: ^Sensor) -> SensorType ---
  216. SensorGetNonPortableType :: proc(sensor: ^Sensor) -> c.int ---
  217. SensorGetInstanceID :: proc(sensor: ^Sensor) -> SensorID ---
  218. SensorGetData :: proc(sensor: ^Sensor, data: [^]f32, num_values: c.int) -> c.int ---
  219. SensorClose :: proc(sensor: ^Sensor) ---
  220. SensorUpdate :: proc() ---
  221. }
  222. // shape
  223. NONSHAPEABLE_WINDOW :: -1
  224. INVALID_SHAPE_ARGUMENT :: -2
  225. WINDOW_LACKS_SHAPE :: -3
  226. WindowShapeModeEnum :: enum c.int {
  227. /** \brief The default mode, a binarized alpha cutoff of 1. */
  228. Default,
  229. /** \brief A binarized alpha cutoff with a given integer value. */
  230. BinarizeAlpha,
  231. /** \brief A binarized alpha cutoff with a given integer value, but with the opposite comparison. */
  232. ReverseBinarizeAlpha,
  233. /** \brief A color key is applied. */
  234. ColorKey,
  235. }
  236. SHAPEMODEALPHA :: #force_inline proc "c" (mode: WindowShapeModeEnum) -> bool {
  237. return bool(mode == .Default || mode == .BinarizeAlpha || mode == .ReverseBinarizeAlpha)
  238. }
  239. WindowShapeParams :: struct #raw_union {
  240. binarizationCutoff: u8,
  241. colorKey: Color,
  242. }
  243. WindowShapeMode :: struct {
  244. mode: WindowShapeModeEnum,
  245. parameters: WindowShapeParams,
  246. }
  247. @(default_calling_convention="c", link_prefix="SDL_")
  248. foreign lib {
  249. CreateShapedWindow :: proc(title: cstring, x, y, w, h: c.uint, flags: WindowFlags) -> ^Window ---
  250. IsShapedWindow :: proc(window: ^Window) -> bool ---
  251. SetWindowShape :: proc(window: ^Window, shape: ^Surface, shape_mode: ^WindowShapeMode) -> c.int ---
  252. GetShapedWindowMode :: proc(window: ^Window, shape_mode: ^WindowShapeMode) -> c.int ---
  253. }