1+ use sdl2:: event:: Event ;
2+ use sdl2:: keyboard:: Keycode ;
3+ use projectm_rs;
4+
5+ fn main ( ) -> Result < ( ) , String > {
6+ // setup sdl
7+ let sdl_context = sdl2:: init ( ) ?;
8+ let video_subsystem = sdl_context. video ( ) ?;
9+ // let audio_subsystem = sdl_context.audio()?;
10+
11+ // create window
12+ let window = video_subsystem. window ( "frontend-sdl2-rust" , 800 , 600 )
13+ . position_centered ( )
14+ . build ( )
15+ . expect ( "could not initialize video subsystem" ) ;
16+
17+ // create canvas/renderer
18+ let mut canvas = window. into_canvas ( ) . build ( )
19+ . expect ( "could not make a canvas" ) ;
20+
21+ // projectm::settings
22+ let settings = projectm_rs:: projectm_settings {
23+ mesh_x : 96 ,
24+ mesh_y : 54 ,
25+ fps : 30 ,
26+ texture_size : 512 ,
27+ window_width : 640 ,
28+ window_height : 360 ,
29+ preset_duration : 15.0 ,
30+ soft_cut_duration : 15.0 ,
31+ hard_cut_duration : 60.0 ,
32+ hard_cut_enabled : false ,
33+ hard_cut_sensitivity : 0.0 ,
34+ beat_sensitivity : 0.5 ,
35+ aspect_correction : true ,
36+ easter_egg : 0.5 ,
37+ shuffle_enabled : true ,
38+ soft_cut_ratings_enabled : true ,
39+ preset_url : "/presets" . as_bytes ( ) . as_ptr ( ) as * mut i8 ,
40+ title_font_url : "" . as_bytes ( ) . as_ptr ( ) as * mut i8 ,
41+ menu_font_url : "" . as_bytes ( ) . as_ptr ( ) as * mut i8 ,
42+ data_dir : "./" . as_bytes ( ) . as_ptr ( ) as * mut i8 ,
43+ } ;
44+ // print_settings(settings);
45+
46+ // projectm::init
47+ let projectm_handle = unsafe {
48+ projectm_rs:: projectm_create_settings ( & settings, 0 )
49+ } ;
50+
51+ unsafe {
52+ projectm_rs:: projectm_select_random_preset ( projectm_handle, true ) ;
53+ projectm_rs:: projectm_set_window_size ( projectm_handle, settings. window_width . try_into ( ) . unwrap ( ) , settings. window_height . try_into ( ) . unwrap ( ) )
54+ }
55+ println ! ( "projectm initialized!" ) ;
56+
57+ // events
58+ let mut event_pump = sdl_context. event_pump ( ) ?;
59+
60+ // renderLoop
61+ ' running: loop {
62+
63+ // check for event
64+ for event in event_pump. poll_iter ( ) {
65+ match event {
66+ Event :: Quit { ..} |
67+ Event :: KeyDown { keycode : Some ( Keycode :: Escape ) , .. } => {
68+ break ' running;
69+ } ,
70+ _ => { }
71+ }
72+ }
73+
74+ // generate random audio
75+ generate_random_audio_data ( projectm_handle) ;
76+
77+ // projectm::render
78+ unsafe {
79+ projectm_rs:: projectm_render_frame ( projectm_handle) ;
80+ }
81+
82+
83+
84+ canvas. clear ( ) ;
85+ canvas. present ( ) ;
86+ }
87+
88+ // finish okay
89+ Ok ( ( ) )
90+ }
91+
92+ fn generate_random_audio_data ( projectm_handle : * mut projectm_rs:: projectm )
93+ {
94+ let mut pcm_data: [ [ libc:: c_short ; 512 ] ; 2 ] = [ [ 0 ; 512 ] ; 2 ] ;
95+ let mut i: libc:: c_int = 0 as libc:: c_int ;
96+ while i < 512 as libc:: c_int {
97+ if i % 2 as libc:: c_int == 1 as libc:: c_int {
98+ pcm_data[ 0 as libc:: c_int as usize ] [ i as usize ] =
99+ -( pcm_data[ 0 as libc:: c_int as usize ] [ i as usize ] as
100+ libc:: c_int ) as libc:: c_short ;
101+ pcm_data[ 1 as libc:: c_int as usize ] [ i as usize ] =
102+ -( pcm_data[ 1 as libc:: c_int as usize ] [ i as usize ] as
103+ libc:: c_int ) as libc:: c_short
104+ }
105+ i += 1
106+ } ;
107+
108+ unsafe {
109+ projectm_rs:: projectm_pcm_add_int16 ( projectm_handle, & pcm_data[ 0 ] [ 0 ] , 512 , 2 )
110+ }
111+ }
0 commit comments