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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#![allow(dead_code)]
pub use crate::core;
pub use crate::core::{Bgra8, Rgba, Rgba8};

pub mod shape2d;
pub mod sprite2d;

use crate::math::{Matrix4, Ortho};

use std::time;

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Origin {
    Center,
    BottomLeft,
    TopLeft,
}

#[derive(PartialEq, Clone, Debug)]
pub struct Repeat {
    pub x: f32,
    pub y: f32,
}

impl Repeat {
    pub fn new(x: f32, y: f32) -> Self {
        Repeat { x, y }
    }
}

impl Default for Repeat {
    fn default() -> Self {
        Repeat { x: 1.0, y: 1.0 }
    }
}

///////////////////////////////////////////////////////////////////////////
// Animation
///////////////////////////////////////////////////////////////////////////

#[derive(Clone, Debug)]
pub enum AnimationState {
    Playing(u64, time::Duration),
    Paused(u64, time::Duration),
    Stopped,
}

#[derive(Clone, Debug)]
pub struct Animation<T> {
    pub state: AnimationState,
    pub delay: time::Duration,
    pub frames: Vec<T>,
}

impl<T> Animation<T> {
    pub fn new(frames: &[T], delay: time::Duration) -> Self
    where
        T: Clone,
    {
        Self {
            state: AnimationState::Playing(0, time::Duration::from_secs(0)),
            delay,
            frames: frames.to_vec(),
        }
    }

    pub fn step(&mut self, delta: time::Duration) {
        if let AnimationState::Playing(_, elapsed) = self.state {
            let elapsed = elapsed + delta;
            let fraction = elapsed.as_micros() / self.delay.as_micros();
            self.state = AnimationState::Playing(fraction as u64, elapsed);
        }
    }

    pub fn pause(&mut self) {
        if let AnimationState::Playing(_, elapsed) = self.state {
            self.state = AnimationState::Paused(0, elapsed);
        }
    }

    pub fn play(&mut self) {
        match self.state {
            AnimationState::Paused(_, elapsed) => self.state = AnimationState::Playing(0, elapsed),
            AnimationState::Stopped => {
                self.state = AnimationState::Playing(0, time::Duration::new(0, 0))
            }
            _ => {}
        }
    }

    pub fn stop(&mut self) {
        self.state = AnimationState::Stopped;
    }

    pub fn val(&self) -> T
    where
        T: Copy,
    {
        self.frames[self.cursor() as usize]
    }

    pub fn len(&self) -> usize {
        self.frames.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn is_playing(&self) -> bool {
        match self.state {
            AnimationState::Playing(_, _) => true,
            _ => false,
        }
    }

    pub fn elapsed(&self) -> time::Duration {
        match self.state {
            AnimationState::Playing(_, elapsed) => elapsed,
            AnimationState::Paused(_, elapsed) => elapsed,
            AnimationState::Stopped => time::Duration::new(0, 0),
        }
    }

    pub fn cursor(&self) -> u64 {
        let cursor = match self.state {
            AnimationState::Playing(cursor, _) => cursor,
            AnimationState::Paused(cursor, _) => cursor,
            AnimationState::Stopped => 0,
        };
        cursor % self.len() as u64
    }

    pub fn push_frame(&mut self, frame: T) {
        self.frames.push(frame);
    }

    pub fn pop_frame(&mut self) -> Option<T> {
        self.frames.pop()
    }
}

///////////////////////////////////////////////////////////////////////////////

pub fn ortho(w: u32, h: u32) -> Matrix4<f32> {
    Ortho::<f32> {
        left: 0.0,
        right: w as f32,
        bottom: h as f32,
        top: 0.0,
        near: -1.0,
        far: 1.0,
    }
    .into()
}

///////////////////////////////////////////////////////////////////////////////

#[derive(Copy, Clone)]
pub struct AlignedBuffer {
    // TODO: Make this generic when rust-lang#43408 is fixed.
    data: Matrix4<f32>,
    padding: [u8; AlignedBuffer::PAD],
}

impl AlignedBuffer {
    pub const ALIGNMENT: u64 = 256;
    pub const PAD: usize = Self::ALIGNMENT as usize - std::mem::size_of::<Matrix4<f32>>();

    pub fn new(data: Matrix4<f32>) -> Self {
        Self {
            data,
            padding: [0u8; AlignedBuffer::PAD],
        }
    }
}

struct Model {
    buf: core::UniformBuffer,
    binding: core::BindingGroup,
    size: usize,
}

impl Model {
    fn new(
        layout: &core::BindingGroupLayout,
        transforms: &[Matrix4<f32>],
        dev: &core::Device,
    ) -> Self {
        let aligned = Self::aligned(transforms);
        let buf = dev.create_uniform_buffer(aligned.as_slice());
        let binding = dev.create_binding_group(&layout, &[&buf]);
        let size = transforms.len();
        Self { buf, binding, size }
    }

    fn aligned(transforms: &[Matrix4<f32>]) -> Vec<AlignedBuffer> {
        let mut aligned = Vec::with_capacity(transforms.len());
        for t in transforms {
            aligned.push(AlignedBuffer::new(*t));
        }
        aligned
    }
}