Skip to content

Commit 8245d1e

Browse files
committed
Split up app a little
1 parent c32eaed commit 8245d1e

File tree

4 files changed

+87
-40
lines changed

4 files changed

+87
-40
lines changed

src/app.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
use sdl2::event::Event;
2-
use sdl2::keyboard::Keycode;
1+
use projectm_rs::core::{projectm, projectm_handle};
2+
use projectm_rs::playlist;
33
use sdl2::video::GLProfile;
44

5-
use projectm_rs::core::{projectm, projectm_handle};
5+
pub mod main_loop;
66

7-
use crate::dummy_audio;
7+
pub struct Config {
8+
pub preset_path: Option<String>,
9+
}
10+
11+
impl Default for Config {
12+
fn default() -> Self {
13+
// default preset path
14+
Self {
15+
// load from home dir or w/e
16+
preset_path: Some(String::from("/usr/local/share/projectM/presets")),
17+
}
18+
}
19+
}
820

921
pub struct App {
1022
pm: projectm_handle,
23+
playlist: playlist::Playlist,
1124
sdl_context: sdl2::Sdl,
1225
gl_context: sdl2::video::GLContext,
1326
window: sdl2::video::Window,
1427
}
1528

1629
impl App {
17-
pub fn new() -> Self {
30+
pub fn new(config: Option<Config>) -> Self {
1831
// setup sdl
1932
let sdl_context = sdl2::init().unwrap();
2033
let video_subsystem = sdl_context.video().unwrap();
@@ -35,7 +48,7 @@ impl App {
3548
let window_width = display_mode.w as u32;
3649
let window_height = display_mode.h as u32;
3750
let window = video_subsystem
38-
.window("frontend-sdl2-rust", window_width, window_height)
51+
.window("ProjectM", window_width, window_height)
3952
.opengl()
4053
.position_centered()
4154
.allow_highdpi()
@@ -48,48 +61,40 @@ impl App {
4861

4962
// initialize projectM
5063
let pm = projectm::create();
64+
// and a preset playlist
65+
let mut playlist = projectm_rs::playlist::Playlist::create(pm);
5166

5267
// get/set window size
5368
let (width, height) = window.drawable_size(); // highDPI aware
5469
projectm::set_window_size(pm, width.try_into().unwrap(), height.try_into().unwrap());
5570

56-
println!("projectm initialized!");
57-
Self {
71+
let mut this = Self {
5872
pm,
73+
playlist,
5974
sdl_context,
6075
gl_context,
6176
window,
62-
}
63-
}
64-
65-
pub fn main_loop(&mut self) {
66-
// events
67-
let mut event_pump = self.sdl_context.event_pump().unwrap();
68-
69-
// renderLoop
70-
'running: loop {
71-
// check for event
72-
for event in event_pump.poll_iter() {
73-
match event {
74-
Event::Quit { .. }
75-
| Event::KeyDown {
76-
keycode: Some(Keycode::Escape),
77-
..
78-
} => {
79-
break 'running;
80-
}
81-
_ => {}
82-
}
83-
}
77+
};
8478

85-
// generate random audio
86-
dummy_audio::generate_random_audio_data(self.pm);
79+
// read config
80+
if let Some(config) = config {
81+
this.load_config(config);
82+
}
8783

88-
// render a frame
89-
projectm::render_frame(self.pm);
84+
this
85+
}
9086

91-
// swap buffers
92-
self.window.gl_swap_window();
87+
pub fn load_config(&mut self, config: Config) {
88+
// load presets if provided
89+
if let Some(preset_path) = config.preset_path {
90+
self.add_preset_path(&preset_path);
9391
}
9492
}
93+
94+
/// Add presets to the playlist recursively skipping duplicates.
95+
pub fn add_preset_path(&mut self, preset_path: &str) {
96+
self.playlist.add_path(preset_path, true);
97+
println!("added preset path: {}", preset_path);
98+
println!("playlist size: {}", self.playlist.len());
99+
}
95100
}

src/app/main_loop.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use projectm_rs::core::projectm;
2+
use sdl2::event::Event;
3+
use sdl2::keyboard::Keycode;
4+
5+
use crate::app::App;
6+
use crate::dummy_audio;
7+
8+
impl App {
9+
pub fn main_loop(&mut self) {
10+
// events
11+
let mut event_pump = self.sdl_context.event_pump().unwrap();
12+
13+
// renderLoop
14+
'running: loop {
15+
// check for event
16+
for event in event_pump.poll_iter() {
17+
match event {
18+
Event::Quit { .. }
19+
| Event::KeyDown {
20+
keycode: Some(Keycode::Escape),
21+
..
22+
} => {
23+
break 'running;
24+
}
25+
_ => {}
26+
}
27+
}
28+
29+
// generate random audio
30+
dummy_audio::generate_random_audio_data(self.pm);
31+
32+
// render a frame
33+
projectm::render_frame(self.pm);
34+
35+
// swap buffers
36+
self.window.gl_swap_window();
37+
}
38+
}
39+
}

src/lib.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
use projectm_sdl::app::App;
1+
mod app;
2+
mod dummy_audio;
23

34
fn main() -> Result<(), String> {
4-
let mut app = App::new();
5+
let config = app::Config::default();
6+
// TODO: parse args here for config
7+
// config.preset_path = Some("/usr/local/share/projectM/presets".to_string());
8+
9+
let mut app = app::App::new(Some(config));
510

611
app.main_loop();
712

0 commit comments

Comments
 (0)