-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapp.rs
More file actions
113 lines (95 loc) · 3.62 KB
/
Copy pathapp.rs
File metadata and controls
113 lines (95 loc) · 3.62 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::app::config::Config;
use projectm::core::ProjectM;
use sdl3::video::{GLProfile, WindowPos};
use std::convert::TryInto;
use std::rc::Rc;
pub mod audio;
pub mod config;
pub mod main_loop;
pub mod playlist;
pub mod video;
pub type ProjectMWrapped = Rc<ProjectM>;
/// Application state
pub struct App {
pm: ProjectMWrapped,
playlist: projectm::playlist::Playlist,
sdl_context: sdl3::Sdl,
window: sdl3::video::Window,
config: config::Config,
audio: audio::Audio,
_gl_context: sdl3::video::GLContext,
}
impl App {
pub fn new(config: Config) -> Self {
// setup sdl
let sdl_context = sdl3::init().unwrap();
// print SDL version
let version = sdl3::version::version();
println!(
"SDL version: {}.{}.{}",
version.major, version.minor, version.patch
);
let video_subsystem = sdl_context.video().unwrap();
// request GL version
// TODO: deal with OpenGL ES here
let gl_attr = video_subsystem.gl_attr();
gl_attr.set_context_profile(GLProfile::Core);
gl_attr.set_context_version(3, 3);
gl_attr.set_context_flags().debug().set();
assert_eq!(gl_attr.context_profile(), GLProfile::Core);
assert_eq!(gl_attr.context_version(), (3, 3));
// Determine initial window size from config or use defaults
let initial_width = config.window_width.unwrap_or(1024);
let initial_height = config.window_height.unwrap_or(768);
// create window
let mut window = video_subsystem
.window("ProjectM", initial_width, initial_height)
.opengl()
.build()
.expect("could not initialize video subsystem");
// create openGL context
let gl_context = window.gl_create_context().unwrap();
window.gl_make_current(&gl_context).unwrap();
// initialize projectM
let pm = Rc::new(ProjectM::create());
// and a preset playlist
let playlist = projectm::playlist::Playlist::create(&pm);
// Apply window position if override is requested
if config.window_override_position.unwrap_or(false) {
let x = config.window_left.unwrap_or(0);
let y = config.window_top.unwrap_or(0);
window.set_position(WindowPos::Positioned(x), WindowPos::Positioned(y));
} else if config.window_width.is_none() {
// Only go full-size if no explicit window size was given
let primary_display = video_subsystem.get_primary_display().unwrap();
let display_bounds = primary_display.get_usable_bounds().unwrap();
window
.set_size(display_bounds.width(), display_bounds.height())
.unwrap();
window.set_position(WindowPos::Centered, WindowPos::Centered);
}
window
.set_display_mode(None)
.expect("could not set display mode");
// initialize audio, passing optional device name
let audio = audio::Audio::new(&sdl_context, Rc::clone(&pm), config.audio_device.clone());
println!("Application initialized with configuration:\n{}", config);
Self {
pm,
playlist,
sdl_context,
window,
config,
audio,
_gl_context: gl_context, // keep this around to keep the context alive
}
}
pub fn init(&mut self) {
// load config
let config = self.config.clone();
self.apply_config(&config);
// initialize audio
self.audio.init(self.get_frame_rate());
self.update_projectm_window_size();
}
}