sdl2.odin 9.5 KB

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