Debian Codegraph @

src/sound_player.rs

raw symbols SCIP
1 /*
2 * Copyright 2024 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use crate::config::SOUND_DIRECTORY;
21 use gtk::{gdk, prelude::*};
22 use std::path::Path;
23
24 #[derive(Clone, Copy)]
25 pub enum Sound {
26 Victory,
27 Die,
28 Teleport,
29 Splat,
30 Yahoo,
31 Bad,
32 }
33
34 pub struct SoundPlayer {
35 victory: gtk::MediaFile,
36 die: gtk::MediaFile,
37 teleport: gtk::MediaFile,
38 splat: gtk::MediaFile,
39 yahoo: gtk::MediaFile,
40 }
41
42 impl SoundPlayer {
43 pub fn new() -> Self {
44 let path = Path::new(SOUND_DIRECTORY);
45 Self {
46 victory: gtk::MediaFile::for_filename(path.join("victory.ogg")),
47 die: gtk::MediaFile::for_filename(path.join("die.ogg")),
48 teleport: gtk::MediaFile::for_filename(path.join("teleport.ogg")),
49 splat: gtk::MediaFile::for_filename(path.join("splat.ogg")),
50 yahoo: gtk::MediaFile::for_filename(path.join("yahoo.ogg")),
51 }
52 }
53
54 pub fn beep(&self) {
55 if let Some(display) = gdk::Display::default() {
56 display.beep();
57 }
58 }
59
60 pub fn play(&self, sound: Sound) {
61 match sound {
62 Sound::Victory => self.victory.play(),
63 Sound::Die => self.die.play(),
64 Sound::Teleport => self.teleport.play(),
65 Sound::Splat => self.splat.play(),
66 Sound::Yahoo => self.yahoo.play(),
67 Sound::Bad => self.beep(),
68 }
69 }
70 }