Remove FindSDL2 find-module, use sdl2-config.cmake instead
This requires SDL >= 2.0.4.
Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in
SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was
not itself built using CMake: it installs a sdl2-config.cmake file to
${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and
library, analogous to a pkg-config .pc file.
As a result, we no longer need to copy/paste a "find-module package"
to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2).
Find-module packages are now discouraged by the CMake developers, in
favour of having upstream projects behave as config-file packages.
This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR
and SDL2_LIBRARY, but the standard behaviour for config-file packages is
to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword
to make sure we search in config-file package mode, and will not find a
FindSDL2.cmake in some other directory that implements the old interface.
In addition to deleting redundant code, this avoids some assumptions in
FindSDL2 about the layout of a SDL installation. The current libsdl2-dev
package in Debian breaks those assumptions; this is considered a bug
and will hopefully be fixed soon, but it illustrates how fragile these
assumptions can be. We can be more robust against different installation
layouts by relying on SDL's own CMake integration.
When linking to a copy of CMake in a non-standard location, users can
now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point
to it; previously, these users would have used the SDL2DIR environment
variable. This continues to be unnecessary if using matching system-wide
installations of CMake and SDL2, for example both from Debian.
use std::{
ops::{Index, IndexMut},
slice::SliceIndex
};
use integral_geometry::Size;
pub struct Vec2D<T> {
data: Vec<T>,
size: Size,
}
impl<T> Index<usize> for Vec2D<T> {
type Output = [T];
#[inline]
fn index(&self, row: usize) -> &[T] {
debug_assert!(row < self.height());
let pos = row * self.width();
&self.data[pos..pos + self.width()]
}
}
impl<T> IndexMut<usize> for Vec2D<T> {
#[inline]
fn index_mut(&mut self, row: usize) -> &mut [T] {
debug_assert!(row < self.height());
let pos = row * self.width();
&mut self.data[pos..pos + self.size.width]
}
}
impl <T> Vec2D<T> {
#[inline]
pub fn width(&self) -> usize {
self.size.width
}
#[inline]
pub fn height(&self) -> usize {
self.size.height
}
#[inline]
pub fn size(&self) -> Size {
self.size
}
}
impl<T: Copy> Vec2D<T> {
pub fn new(size: Size, value: T) -> Self {
Self { size, data: vec![value; size.area()] }
}
#[inline]
pub fn as_slice(&self) -> &[T] {
self.data.as_slice()
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
self.data.as_mut_slice()
}
#[inline]
pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
self.data.get(row * self.width() + column)
}
#[inline]
pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut <usize as SliceIndex<[T]>>::Output> {
self.data.get_mut(row * self.size.width + column)
}
#[inline]
pub unsafe fn get_unchecked(&self, row: usize, column: usize) -> &<usize as SliceIndex<[T]>>::Output {
self.data.get_unchecked(row * self.width() + column)
}
#[inline]
pub unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut <usize as SliceIndex<[T]>>::Output {
self.data.get_unchecked_mut(row * self.size.width + column)
}
#[inline]
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[T]> {
self.data.chunks_exact(self.width())
}
#[inline]
pub fn rows_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut [T]> {
let width = self.width();
self.data.chunks_exact_mut(width)
}
#[inline]
pub unsafe fn as_bytes(&self) -> &[u8] {
use std::{
slice,
mem
};
slice::from_raw_parts(
self.data.as_ptr() as *const u8,
self.data.len() * mem::size_of::<T>(),
)
}
}
impl<T: Copy> AsRef<[T]> for Vec2D<T> {
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}
impl<T: Copy> AsMut<[T]> for Vec2D<T> {
fn as_mut(&mut self) -> &mut [T] {
self.as_mut_slice()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basics() {
let mut v: Vec2D<u8> = Vec2D::new(Size::new(2, 3), 0xff);
assert_eq!(v.width(), 2);
assert_eq!(v.height(), 3);
assert_eq!(v[0][0], 0xff);
assert_eq!(v[2][1], 0xff);
v[2][1] = 0;
assert_eq!(v[2][0], 0xff);
assert_eq!(v[2][1], 0);
v.get_mut(2, 1).map(|v| *v = 1);
assert_eq!(v[2][1], 1);
assert_eq!(v.get_mut(2, 2), None);
}
}