aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/startup_intro.rs
blob: d5fa80ef47005b37a26536cdb182c3ff88d50b3f (plain) (blame)
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
//! Startup movie sequence shown before the mission menu.

use fparkan_assets::startup_movies::movie_paths;
use fparkan_platform_winit::{intro_video::IntroVideo, startup_window};
use fparkan_vfs::DirectoryVfs;
use std::{
    collections::VecDeque,
    path::{Path, PathBuf},
};
use winit::window::Window;

pub(super) struct StartupIntro {
    waiting: VecDeque<PathBuf>,
    playing: Option<IntroVideo>,
}

impl StartupIntro {
    pub(super) fn open(root: &Path) -> Option<Self> {
        let paths = match movie_paths(&DirectoryVfs::new(root)) {
            Ok(paths) => paths,
            Err(error) => {
                eprintln!("startup movies: {error}");
                return None;
            }
        };
        if paths.is_empty() {
            return None;
        }
        Some(Self {
            waiting: paths.iter().map(|path| root.join(path.as_str())).collect(),
            playing: None,
        })
    }

    /// Returns whether a movie is still playing.
    pub(super) fn update(&mut self, window: &Window) -> bool {
        if let Some(movie) = &self.playing {
            match movie.finished() {
                Ok(false) => return true,
                Ok(true) => {}
                Err(error) => eprintln!("startup playback: {error}"),
            }
        }
        self.playing = None;
        while let Some(path) = self.waiting.pop_front() {
            if let Err(error) = startup_window::paint(window, [1, 1], &[0]) {
                eprintln!("startup movie background: {error}");
            }
            match IntroVideo::play(window, &path) {
                Ok(movie) => {
                    self.playing = Some(movie);
                    return true;
                }
                Err(error) => eprintln!("startup movie {}: {error}", path.display()),
            }
        }
        false
    }

    pub(super) fn resize(&self, window: &Window) {
        if let Some(movie) = &self.playing {
            if let Err(error) = movie.resize(window) {
                eprintln!("startup movie resize: {error}");
            }
        }
    }
}