anim.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::collections::HashMap;
  19. use super::{BufferId, DrawCall, GfxDrawCall, TextureId};
  20. #[derive(Debug, Clone)]
  21. pub struct Frame {
  22. /// Duration of this frame in ms
  23. duration: u32,
  24. dc: DrawCall,
  25. }
  26. impl Frame {
  27. pub fn new(duration: u32, dc: DrawCall) -> Self {
  28. Self { duration, dc }
  29. }
  30. }
  31. #[derive(Debug, Clone)]
  32. pub(super) struct GfxSeqAnim {
  33. oneshot: bool,
  34. frames: Vec<Option<GfxFrame>>,
  35. /// Timer between frames
  36. timer: std::time::Instant,
  37. current_idx: usize,
  38. pub(super) is_visible: bool,
  39. }
  40. impl GfxSeqAnim {
  41. pub fn new(frames_len: usize, oneshot: bool) -> Self {
  42. let frames = vec![None; frames_len];
  43. Self {
  44. oneshot,
  45. frames,
  46. timer: std::time::Instant::now(),
  47. current_idx: 0,
  48. is_visible: false,
  49. }
  50. }
  51. pub fn set(
  52. &mut self,
  53. frame_idx: usize,
  54. frame: Frame,
  55. textures: &HashMap<TextureId, miniquad::TextureId>,
  56. buffers: &HashMap<BufferId, miniquad::BufferId>,
  57. ) {
  58. assert!(frame_idx < self.frames.len());
  59. let duration = std::time::Duration::from_millis(frame.duration as u64);
  60. let dc = frame.dc.compile(textures, buffers, 0);
  61. self.frames[frame_idx] = Some(GfxFrame { duration, dc });
  62. //t!("got frame {frame_idx}");
  63. }
  64. fn curr_frame(&self) -> Option<&GfxFrame> {
  65. assert!(self.current_idx < self.frames.len());
  66. self.frames[self.current_idx].as_ref()
  67. }
  68. pub fn tick(&mut self) -> Option<GfxDrawCall> {
  69. if self.curr_frame().is_none() {
  70. assert_eq!(self.current_idx, 0);
  71. return None
  72. };
  73. self.increment();
  74. let curr_frame = self.curr_frame().unwrap().clone();
  75. Some(curr_frame.dc)
  76. }
  77. fn increment(&mut self) {
  78. // One shot anims dont loop
  79. if self.oneshot && self.current_idx + 1 == self.frames.len() {
  80. return
  81. }
  82. let elapsed = self.timer.elapsed();
  83. let frame = self.curr_frame().unwrap();
  84. let curr_duration = frame.duration;
  85. if elapsed >= curr_duration {
  86. let next_idx = (self.current_idx + 1) % self.frames.len();
  87. // Only advance when the next frame is Some
  88. // Otherwise stay on the same frame
  89. if self.frames[next_idx].is_some() {
  90. self.current_idx = next_idx;
  91. // Reset the timer now we changed frame
  92. self.timer = std::time::Instant::now();
  93. }
  94. }
  95. }
  96. }
  97. #[derive(Debug, Clone)]
  98. struct GfxFrame {
  99. duration: std::time::Duration,
  100. dc: GfxDrawCall,
  101. }