1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![no_std]
#![allow(bad_style)]

//! Bindings to SDL2.
//!
//! This version exposes bindings for `SDL2-2.0.12`, and if you avoid calling
//! any of the newer functions you can also use older versions of SDL2.
//!
//! The default docs are for the x86_64 **Windows** MSVC version, but this crate
//! also works just fine on **Mac** and **Linux**.
//!
//! Things are about 95% the same from target to target. Some functions only
//! exist on specific platforms (eg: getting DirectX or Metal info), and others
//! have signatures that vary by platform (eg: creating a thread). Usually
//! nothing a little `cfg` can't fix.
//!
//! [The SDL2 Wiki](https://wiki.libsdl.org/) has information on function usage.
//! What's here is just the function signatures and structs.

use core::fmt::Debug;

pub use chlorine::*;

pick! {
  if #[cfg(feature = "use_bindgen_bin")] {
    include!(concat!(env!("OUT_DIR"),"/SDL2-2.0.12-",env!("TARGET"),".rs"));
  } else {
    include!(concat!("SDL2-2.0.12-",env!("TARGET"),".rs"));
  }
}

// Note(Lokathor): Sometimes this is defined already within the bindings, but
// other types bindgen just uses `usize` directly. This alias lets us always
// define things in terms of `size_t`.
#[cfg(any(target_os = "macos", target_os = "linux"))]
pub type size_t = usize;

/// `SDL_touch.h`: Used as the device ID for mouse events simulated with touch
/// input
pub const SDL_TOUCH_MOUSEID: u32 = -1i32 as u32;

/// `SDL_touch.h`: Used as the SDL_TouchID for touch events simulated with mouse
/// input
///
/// * `2.0.10` or later
pub const SDL_MOUSE_TOUCHID: i64 = -1i64;

/// This is the common alias for the `SDL_WINDOWPOS_CENTERED_MASK` value.
///
/// It's `i32` rather than `u32` because the primary use for it is to be passed
/// to `SDL_CreateWindow`.
pub const SDL_WINDOWPOS_CENTERED: i32 = SDL_WINDOWPOS_CENTERED_MASK as i32;

/// `SDL_surface.h`: Evaluates to true if the surface needs to be locked before
/// access.
///
/// ## Safety
///
/// This must be a valid `SDL_Surface` pointer.
#[inline(always)]
pub unsafe fn SDL_MUSTLOCK(surface: *const SDL_Surface) -> bool {
  (*surface).flags & SDL_RLEACCEL != 0
}

/// `SDL_pixels.h`: "internal" macro to check if a value is a pixel format
/// value.
#[inline(always)]
pub const fn SDL_PIXELFLAG(format: SDL_PixelFormatEnum) -> SDL_PixelFormatEnum {
  (format >> 28) & 0x0F
}

/// `SDL_pixels.h`: Pixel type of this format.
#[inline(always)]
pub const fn SDL_PIXELTYPE(format: SDL_PixelFormatEnum) -> SDL_PixelFormatEnum {
  (format >> 24) & 0x0F
}

/// `SDL_pixels.h`: Component ordering of this format.
#[inline(always)]
pub const fn SDL_PIXELORDER(
  format: SDL_PixelFormatEnum,
) -> SDL_PixelFormatEnum {
  (format >> 20) & 0x0F
}

/// `SDL_pixels.h`: Channel width layout of this format.
#[inline(always)]
pub const fn SDL_PIXELLAYOUT(
  format: SDL_PixelFormatEnum,
) -> SDL_PixelFormatEnum {
  (format >> 16) & 0x0F
}

/// `SDL_pixels.h`: Bits per pixel.
#[inline(always)]
pub const fn SDL_BITSPERPIXEL(
  format: SDL_PixelFormatEnum,
) -> SDL_PixelFormatEnum {
  (format >> 8) & 0xFF
}

/// `SDL_pixels.h`: Bytes per pixel.
#[inline(always)]
pub fn SDL_BYTESPERPIXEL(format: SDL_PixelFormatEnum) -> SDL_PixelFormatEnum {
  if SDL_ISPIXELFORMAT_FOURCC(format) {
    if format == SDL_PIXELFORMAT_YUY2
      || format == SDL_PIXELFORMAT_UYVY
      || format == SDL_PIXELFORMAT_YVYU
    {
      2
    } else {
      1
    }
  } else {
    format & 0xFF
  }
}

/// `SDL_pixels.h`: Is this an indexed format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_INDEXED(format: SDL_PixelFormatEnum) -> bool {
  !SDL_ISPIXELFORMAT_FOURCC(format)
    && (SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX1
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX4
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX8)
}

/// `SDL_pixels.h`: Is this a packed format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_PACKED(format: SDL_PixelFormatEnum) -> bool {
  !SDL_ISPIXELFORMAT_FOURCC(format)
    && (SDL_PIXELTYPE(format) == SDL_PIXELTYPE_PACKED8
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_PACKED16
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_PACKED32)
}

/// `SDL_pixels.h`: Is this an array format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_ARRAY(format: SDL_PixelFormatEnum) -> bool {
  !SDL_ISPIXELFORMAT_FOURCC(format)
    && (SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYU8
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYU16
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYU32
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF16
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF32)
}

/// `SDL_pixels.h`: Does this format have an alpha channel?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_ALPHA(format: SDL_PixelFormatEnum) -> bool {
  (SDL_ISPIXELFORMAT_PACKED(format)
    && (SDL_PIXELORDER(format) == SDL_PACKEDORDER_ARGB
      || SDL_PIXELORDER(format) == SDL_PACKEDORDER_RGBA
      || SDL_PIXELORDER(format) == SDL_PACKEDORDER_ABGR
      || SDL_PIXELORDER(format) == SDL_PACKEDORDER_BGRA))
    || (SDL_ISPIXELFORMAT_ARRAY(format)
      && (SDL_PIXELORDER(format) == SDL_ARRAYORDER_ARGB
        || SDL_PIXELORDER(format) == SDL_ARRAYORDER_RGBA
        || SDL_PIXELORDER(format) == SDL_ARRAYORDER_ABGR
        || SDL_PIXELORDER(format) == SDL_ARRAYORDER_BGRA))
}

/// `SDL_pixels.h`: Is this a FourCC format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_FOURCC(format: SDL_PixelFormatEnum) -> bool {
  (format != 0) && (SDL_PIXELFLAG(format) != 1)
}