Departure and 2 pass draw

This commit is contained in:
2025-03-27 23:55:28 +01:00
parent a15dbf5c63
commit 10014e0851

View File

@@ -18,16 +18,26 @@ use serde::Deserialize;
#[derive(Deserialize,Clone)] #[derive(Deserialize,Clone)]
struct Config { struct Config {
tournament: String, tournament: String,
update: Option<u32>, update: Option<u32>,
fullscreen: Option<bool>, fullscreen: Option<bool>,
smash_key: String, smash_key: String,
font_size: u16
} }
struct Line<'a> {
height : u32,
width : u32,
players : String,
alt_color : bool,
texture : sdl2::render::Texture<'a>,
y: u32
}
impl Config { impl Config {
fn new() -> Self { fn new() -> Self {
Self { tournament: String::new(), update: Some(1),fullscreen:Some(false), smash_key: String::new() } Self { tournament: String::new(), update: Some(1),fullscreen:Some(false), smash_key: String::new(), font_size: 30 }
} }
} }
@@ -38,6 +48,7 @@ pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashreq
let texture_creator = canvas.texture_creator(); let texture_creator = canvas.texture_creator();
let mut backline_text = texture_creator.create_texture_streaming(PixelFormatEnum::RGB24, 1, 1).map_err(|e| e.to_string()).unwrap(); let mut backline_text = texture_creator.create_texture_streaming(PixelFormatEnum::RGB24, 1, 1).map_err(|e| e.to_string()).unwrap();
//Back line texture
backline_text.with_lock(None, |buffer : &mut [u8], pitch: usize| { backline_text.with_lock(None, |buffer : &mut [u8], pitch: usize| {
buffer[1] = 5; buffer[1] = 5;
buffer[1] = 30; buffer[1] = 30;
@@ -46,39 +57,68 @@ pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashreq
let mut y : u32 = 0; let mut y : u32 = 0;
let mut index = 0; let mut index = 0;
let mut lines = Vec::<Line>::new();
for sq in queues { for sq in queues {
let surface: sdl2::surface::Surface<'_> = font.render(sq.name.as_str()).blended(Color::RGBA(255, 255, 0, 255)).map_err(|e| e.to_string()).unwrap();
let surface = font.render(sq.name.as_str()).blended(Color::RGBA(255, 255, 0, 255)).map_err(|e| e.to_string()).unwrap();
let text = texture_creator.create_texture_from_surface(&surface).unwrap(); let text = texture_creator.create_texture_from_surface(&surface).unwrap();
let textq : TextureQuery = text.query(); let textq : TextureQuery = text.query();
let _ = canvas.copy(&text, None, Rect::new(0, y.try_into().map_err(|_| 0).unwrap(), textq.width, textq.height)); let _ = canvas.copy(&text, None, Rect::new(0, y.try_into().map_err(|_| 0).unwrap(), textq.width, textq.height));
y += textq.height+10; y += textq.height+10;
//compute line
for current_match in &sq.matches { for current_match in &sq.matches {
index += 1; index += 1;
let switch_col : bool = index%2 == 0; let switch_col : bool = index%2 == 0;
let var_name = format!("{} vs {}", &current_match.player1, current_match.player2); let var_name = format!("{} vs {}", &current_match.player1, current_match.player2);
let surface = font.render(var_name.as_str()).blended(Color::RGBA(255, 255, 255, 255)).map_err(|e| e.to_string()).unwrap(); let surface = font.render(var_name.as_str()).blended(Color::RGBA(255, 255, 255, 255)).map_err(|e| e.to_string()).unwrap();
let text = texture_creator.create_texture_from_surface(&surface).unwrap(); let text = texture_creator.create_texture_from_surface(&surface).unwrap();
let textq : TextureQuery = text.query(); let textq : TextureQuery = text.query();
let line = Line{
height : textq.height,
width : textq.width,
players : var_name,
texture : texture_creator.create_texture_from_surface(&surface).unwrap(),
alt_color : switch_col,
y: 0
};
lines.push(line);
if switch_col {
let _ = canvas.copy(&backline_text, None, Rect::new(0, y.try_into().map_err(|_| 0).unwrap(), canvas.viewport().width(), textq.height));
}
let _ = canvas.copy(&text, None, Rect::new(0, y.try_into().map_err(|e| 0).unwrap(), textq.width, textq.height));
y += textq.height + 5;
} }
let prev_y = y;
// background
for l in &lines {
if l.alt_color {
let _ = canvas.copy(&backline_text, None, Rect::new(0, y.try_into().map_err(|_| 0).unwrap(), canvas.viewport().width(), l.height));
}
y += l.height + 5;
}
// depart !
let d_surface = font.render("Départ / Departure").blended(Color::RGBA(130, 218, 255, 255)).map_err(|e| e.to_string()).unwrap();
let d_text = texture_creator.create_texture_from_surface(&d_surface).unwrap();
let d_textq : TextureQuery = d_text.query();
let angle : f64 = f64::from(-90.0);
let d_x = canvas.viewport().width() as i32 - (d_textq.width as i32/2) - d_textq.height as i32;
let d_y = canvas.viewport().height() as i32 - ( d_textq.width as i32 /2) - d_textq.height as i32;
let dst = Rect::new(d_x, d_y,d_textq.width,d_textq.height);
let _ = canvas.copy_ex(&d_text, None, dst,angle,None,false,false).unwrap();// d_textq.width, d_textq.height));
// foreground
y = prev_y;
for l in &lines {
let _ = canvas.copy(&l.texture, None, Rect::new(10, y.try_into().map_err(|e| 0).unwrap(), l.width, l.height));
y += l.height + 5;
}
} }
} }
@@ -97,7 +137,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Empty Tournament , Exiting."); println!("Empty Tournament , Exiting.");
return Ok(()); return Ok(());
} }
let fullscreen = match config.fullscreen { let fullscreen = match config.fullscreen {
Some(v) => v, Some(v) => v,
None => false None => false
@@ -105,11 +145,13 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Fullscreen : {}",fullscreen); println!("Fullscreen : {}",fullscreen);
println!("Done"); println!("Done");
let test = Arc::new(Mutex::new(Vec::<SmashQueue>::new())); let test = Arc::new(Mutex::new(Vec::<SmashQueue>::new()));
let sdl_context = sdl2::init().unwrap(); let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap(); let video_subsystem = sdl_context.video().unwrap();
let mut build = video_subsystem.window("LaDOSE-SNCF", 1280, 720); let mut build = video_subsystem.window("LaDOSE-SNCF", 1280, 720);
if fullscreen { if fullscreen {
build.fullscreen(); build.fullscreen();
@@ -123,12 +165,12 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut canvas: Canvas<sdl2::video::Window> = window.into_canvas().build().unwrap(); let mut canvas: Canvas<sdl2::video::Window> = window.into_canvas().build().unwrap();
let ttf_context = sdl2::ttf::init().unwrap(); let ttf_context = sdl2::ttf::init().unwrap();
let font : sdl2::ttf::Font<'_, '_> = ttf_context.load_font("font/achemine_bold.ttf", 30).unwrap(); let font : sdl2::ttf::Font<'_, '_> = ttf_context.load_font("font/achemine_bold.ttf", config.font_size).unwrap();
// let stream_queues : Vec<smashrequest::SmashQueue>= smashrequest::get_matches(config.tournament.as_str(),config.smash_key.as_str());
let stream_queues : Vec<smashrequest::SmashQueue>= smashrequest::get_matches(config.tournament.as_str(),config.smash_key.as_str());
let timer = sdl_context.timer()?; let timer = sdl_context.timer()?;
@@ -140,18 +182,13 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let data = smashrequest::get_matches(config.tournament.as_str(), config.smash_key.as_str()); let data = smashrequest::get_matches(config.tournament.as_str(), config.smash_key.as_str());
println!("{:?}", data); println!("{:?}", data);
let mut a = test_clone.lock().unwrap(); let mut a = test_clone.lock().unwrap();
if a.len()>1 { a.clone_from(&data);
a.clear();
}
else{
a.clone_from(&data);
}
println!("{:?}",a); println!("{:?}",a);
return config.update.ok_or(10).unwrap()*1000; return config.update.ok_or(10).unwrap()*1000;
}) })
); );
canvas.set_draw_color(Color::RGB(0, 255, 255)); canvas.set_draw_color(Color::RGB(0, 255, 255));
canvas.clear(); canvas.clear();
canvas.present(); canvas.present();
@@ -167,9 +204,9 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
Event::Quit {..} | Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running Ok(()); break 'running Ok(());
}, },
_ => {} _ => {}
} }
} }