Skip to content

Commit 9e33166

Browse files
authored
Projectm rs 2.0.1 alpha (#7)
* sdl3 * sdl3 * WIP * bindgen * WIP * update sdl3 * Update to use pm-rs 2.0.0-alpha * WIP * use Rc<T> to safely pass around references to pm
1 parent 16290fe commit 9e33166

File tree

8 files changed

+126
-93
lines changed

8 files changed

+126
-93
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode/
22
target/
3+
.nvim-dap.lua

Cargo.lock

+76-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ rust-version = "1.68.2"
88

99
[dependencies]
1010
libc = "*"
11-
projectm = "1.0.5"
12-
# projectm = { path = "../projectm-rs" }
13-
#projectm = { git = "https://github.com/projectM-visualizer/projectm" }
11+
# projectm = "1.0.5"
12+
projectm = { path = "../projectm-rs", version = "2.0.1-alpha", features = [] }
13+
#projectm = { git = "https://github.com/projectM-visualizer/projectm" nd}
1414
# sdl3 = { git = "https://github.com/revmischa/sdl3-rs.git", features = ["use-bindgen"] }
15-
sdl3 = "0.5.0"
16-
# sdl3 = { path = "../../sdl3-rs" }
17-
15+
# sdl3 = "0.5.0"
16+
sdl3 = { path = "../../sdl3-rs", version = "0.6.0", features = ["use-bindgen"] }
17+
rand = "0.8.5"
18+
include_dir = "0.7.3"
1819
# gl = "0.14.0"
1920

2021
[features]

src/app.rs

+13-29
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use std::sync::{Arc, Mutex};
2-
3-
use projectm::core::{ProjectMHandle, Projectm};
4-
use sdl3::video::GLProfile;
1+
use projectm::core::ProjectM;
2+
use sdl3::video::{GLProfile, WindowPos};
3+
use std::convert::TryInto;
4+
use std::rc::Rc;
55

66
pub mod audio;
77
pub mod config;
88
pub mod main_loop;
99
pub mod playlist;
1010
pub mod video;
1111

12-
/// Thread-safe wrapper around the projectM instance.
13-
pub type ProjectMWrapped = Arc<Mutex<ProjectMHandle>>;
12+
pub type ProjectMWrapped = Rc<ProjectM>;
1413

1514
/// Application state
1615
pub struct App {
@@ -49,23 +48,11 @@ impl App {
4948
assert_eq!(gl_attr.context_version(), (3, 3));
5049

5150
// create window
52-
// get screen dimensions
53-
let display_index = 0;
54-
let driver = video_subsystem.current_video_driver();
55-
println!("Using video driver: {}", driver);
56-
let display_id = video_subsystem.get_primary_display_id();
57-
let display_mode = video_subsystem.current_display_mode(display_id).unwrap();
58-
let window_width = 100; // display_mode.w as u32;
59-
let window_height = 100; // display_mode.h as u32;
60-
println!(
61-
"Display {} is {}x{}",
62-
display_index, window_width, window_height
63-
);
6451
let window = video_subsystem
65-
.window("ProjectM", window_width, window_height)
52+
.window("ProjectM", 0, 0)
6653
.opengl()
6754
.maximized()
68-
.position_centered()
55+
.fullscreen()
6956
// .allow_highdpi()
7057
.build()
7158
.expect("could not initialize video subsystem");
@@ -75,23 +62,20 @@ impl App {
7562
window.gl_make_current(&gl_context).unwrap();
7663

7764
// initialize projectM
78-
let pm = Projectm::create();
65+
let pm = Rc::new(ProjectM::create());
7966

8067
// and a preset playlist
81-
let playlist = projectm::playlist::Playlist::create(pm);
68+
let playlist = projectm::playlist::Playlist::create(&pm);
8269

8370
// get/set window size
84-
let (width, height) = window.size(); // TODO: need high DPI support here https://github.com/libsdl-org/SDL/issues/7134
85-
Projectm::set_window_size(pm, width.try_into().unwrap(), height.try_into().unwrap());
86-
87-
// create a mutex to protect the projectM instance
88-
let pm = Arc::new(Mutex::new(pm));
71+
let (width, height) = window.size_in_pixels();
72+
pm.set_window_size(width.try_into().unwrap(), height.try_into().unwrap());
8973

9074
// initialize audio
91-
let audio = audio::Audio::new(&sdl_context, pm.clone());
75+
let audio = audio::Audio::new(&sdl_context, Rc::clone(&pm));
9276

9377
Self {
94-
pm: pm.clone(),
78+
pm,
9579
playlist,
9680
sdl_context,
9781
window,

src/app/audio.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use projectm::core::ProjectMHandle;
1+
use projectm::core::ProjectM;
22
use sdl3::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
3-
use std::sync::Arc;
4-
use std::sync::Mutex;
53

64
use super::config::FrameRate;
75
use super::ProjectMWrapped;
86

7+
use std::rc::Rc;
8+
99
type AudioDeviceIndex = u32;
1010
type SampleFormat = f32; // format of audio samples
1111

@@ -110,9 +110,7 @@ impl Audio {
110110
// how many samples to capture at a time
111111
// should be enough for 1 frame or less
112112
// should not be larger than max_samples / channels
113-
let max_samples: usize = projectm::core::Projectm::pcm_get_max_samples()
114-
.try_into()
115-
.unwrap();
113+
let max_samples: usize = ProjectM::pcm_get_max_samples().try_into().unwrap();
116114
let samples_per_frame = (sample_rate / frame_rate) as usize;
117115
let buffer_size = std::cmp::min(max_samples / 2, samples_per_frame);
118116
println!("Buffer size: {}", buffer_size);
@@ -135,7 +133,7 @@ impl Audio {
135133

136134
// return callback fn
137135
AudioCaptureCallback {
138-
pm: self.projectm.clone(),
136+
pm: Rc::clone(&self.projectm),
139137
}
140138
}) {
141139
Ok(device) => device,
@@ -176,7 +174,7 @@ impl Audio {
176174
struct AudioCaptureCallback {
177175
// we need to keep a reference to the projectm instance to
178176
// add the audio data to it
179-
pm: Arc<Mutex<ProjectMHandle>>,
177+
pm: ProjectMWrapped,
180178
}
181179
unsafe impl Send for AudioCaptureCallback {}
182180
unsafe impl Sync for AudioCaptureCallback {}
@@ -187,7 +185,6 @@ impl AudioCallback for AudioCaptureCallback {
187185
// we are receiving some chunk of audio data
188186
// we need to pass it to projectm
189187
fn callback(&mut self, out: &mut [SampleFormat]) {
190-
let pm = *self.pm.lock().unwrap();
191-
projectm::core::Projectm::pcm_add_float(pm, out.to_vec(), 2);
188+
self.pm.pcm_add_float(out.to_vec(), 2);
192189
}
193190
}

src/app/config.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::app::App;
2-
use projectm::core::Projectm;
32
use std::path::Path;
43

54
pub type FrameRate = u32;
@@ -57,7 +56,7 @@ impl Default for Config {
5756

5857
impl App {
5958
pub fn load_config(&self, config: &Config) {
60-
let pm = *self.pm.lock().unwrap();
59+
let pm = &self.pm;
6160

6261
// load presets if provided
6362
if let Some(preset_path) = &config.preset_path {
@@ -66,32 +65,31 @@ impl App {
6665

6766
// set frame rate if provided
6867
if let Some(frame_rate) = config.frame_rate {
69-
Projectm::set_fps(pm, frame_rate)
68+
pm.set_fps(frame_rate);
7069
}
7170

7271
// load textures if provided
7372
if let Some(texture_path) = &config.texture_path {
7473
let mut paths: Vec<String> = Vec::new();
7574
paths.push(texture_path.into());
76-
Projectm::set_texture_search_paths(pm, &paths, 1);
75+
pm.set_texture_search_paths(&paths, 1);
7776
}
7877

7978
// set beat sensitivity if provided
8079
if let Some(beat_sensitivity) = config.beat_sensitivity {
81-
Projectm::set_beat_sensitivity(pm, beat_sensitivity);
80+
pm.set_beat_sensitivity(beat_sensitivity);
8281
}
8382

8483
// set preset duration if provided
8584
if let Some(preset_duration) = config.preset_duration {
86-
Projectm::set_preset_duration(pm, preset_duration);
85+
pm.set_preset_duration(preset_duration);
8786
}
8887

8988
// set preset shuffle mode
9089
// self.playlist.set_shuffle(true);
9190
}
9291

9392
pub fn get_frame_rate(&self) -> FrameRate {
94-
let pm = *self.pm.lock().unwrap();
95-
Projectm::get_fps(pm)
93+
self.pm.get_fps()
9694
}
9795
}

0 commit comments

Comments
 (0)