Non movable callback
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
tournament = 'tournoi-kiouze-test'
|
||||
update = 15
|
||||
update = 20
|
||||
fullscreen = false
|
||||
smash_key = 'Bearer API_TOKEN'
|
||||
font_size = 40
|
||||
font_size_departure = 60
|
||||
smash_key = 'Bearer c4b3c20f0be75100d7ce375eb8effc1a'
|
||||
margin = 10
|
||||
|
||||
77
src/main.rs
77
src/main.rs
@@ -17,12 +17,14 @@ use serde::Deserialize;
|
||||
|
||||
|
||||
#[derive(Deserialize,Clone)]
|
||||
struct Config {
|
||||
pub struct Config {
|
||||
tournament: String,
|
||||
update: Option<u32>,
|
||||
fullscreen: Option<bool>,
|
||||
smash_key: String,
|
||||
font_size: u16
|
||||
font_size: u16,
|
||||
font_size_departure : u16,
|
||||
margin: u32
|
||||
|
||||
}
|
||||
|
||||
@@ -37,12 +39,12 @@ struct Line<'a> {
|
||||
|
||||
impl Config {
|
||||
fn new() -> Self {
|
||||
Self { tournament: String::new(), update: Some(1),fullscreen:Some(false), smash_key: String::new(), font_size: 30 }
|
||||
Self { tournament: String::new(), update: Some(1),fullscreen:Some(false), smash_key: String::new(), font_size: 30 , font_size_departure : 45, margin: 10 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashrequest::SmashQueue>,font : &sdl2::ttf::Font<'_, '_> ){
|
||||
pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashrequest::SmashQueue>,font : &sdl2::ttf::Font<'_, '_> , font_departure : &sdl2::ttf::Font<'_, '_>, config : &Config ){
|
||||
|
||||
|
||||
let texture_creator = canvas.texture_creator();
|
||||
@@ -65,7 +67,8 @@ pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashreq
|
||||
let textq : TextureQuery = text.query();
|
||||
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+config.margin * 2;
|
||||
|
||||
//compute line
|
||||
for current_match in &sq.matches {
|
||||
|
||||
@@ -88,18 +91,16 @@ pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashreq
|
||||
lines.push(line);
|
||||
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
let _ = canvas.copy(&backline_text, None, Rect::new(0, y as i32 - config.margin as i32 , canvas.viewport().width(), l.height + config.margin ));
|
||||
}
|
||||
y += l.height + 5;
|
||||
y += l.height + (config.margin*2);
|
||||
}
|
||||
// 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_surface = font_departure.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();
|
||||
|
||||
@@ -113,8 +114,8 @@ pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashreq
|
||||
// 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;
|
||||
let _ = canvas.copy(&l.texture, None, Rect::new(10, y as i32 - config.margin as i32/2 , l.width, l.height));
|
||||
y += l.height + (config.margin *2) ;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -124,21 +125,26 @@ pub fn render(canvas : &mut Canvas<sdl2::video::Window> , queues : &Vec<smashreq
|
||||
|
||||
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Read config");
|
||||
|
||||
let config_file = fs::read_to_string("config.toml");
|
||||
|
||||
let config : Config = if config_file.is_ok() {
|
||||
toml::from_str(&config_file.unwrap()).unwrap()
|
||||
let box_config : Box<Config> = if config_file.is_ok() {
|
||||
Box::new(toml::from_str(&config_file.unwrap()).unwrap())
|
||||
} else {
|
||||
Config::new()
|
||||
Box::new(Config::new())
|
||||
};
|
||||
let update_timer = match box_config.update {
|
||||
Some(v) => v,
|
||||
None => 10
|
||||
} * 1000 ;
|
||||
|
||||
println!("{}",config.tournament);
|
||||
if config.tournament.is_empty(){
|
||||
println!("{}",box_config.tournament);
|
||||
if box_config.tournament.is_empty(){
|
||||
println!("Empty Tournament , Exiting.");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let fullscreen = match config.fullscreen {
|
||||
let fullscreen = match box_config.fullscreen {
|
||||
Some(v) => v,
|
||||
None => false
|
||||
};
|
||||
@@ -165,42 +171,40 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let mut canvas: Canvas<sdl2::video::Window> = window.into_canvas().build().unwrap();
|
||||
let ttf_context = sdl2::ttf::init().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 font : sdl2::ttf::Font<'_, '_> = ttf_context.load_font("font/achemine_bold.ttf", box_config.font_size).unwrap();
|
||||
let font_departure : sdl2::ttf::Font<'_, '_> = ttf_context.load_font("font/achemine_bold.ttf", box_config.font_size_departure).unwrap();
|
||||
|
||||
let timer = sdl_context.timer()?;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
let test_clone = Arc::clone(&test);
|
||||
|
||||
let _timer_cb = &timer.add_timer(1,Box::new(move || {
|
||||
println!("timer");
|
||||
let data = smashrequest::get_matches(config.tournament.as_str(), config.smash_key.as_str());
|
||||
println!("{:?}", data);
|
||||
let callback = Box::new(|| {
|
||||
println!("Smash!");
|
||||
let data = smashrequest::get_matches(box_config.tournament.as_str(), box_config.smash_key.as_str());
|
||||
let mut a = test_clone.lock().unwrap();
|
||||
a.clone_from(&data);
|
||||
println!("{:?}",a);
|
||||
return config.update.ok_or(10).unwrap()*1000;
|
||||
})
|
||||
);
|
||||
return update_timer
|
||||
});
|
||||
|
||||
//callback is called because timer is fucked up ?
|
||||
callback();
|
||||
|
||||
let _timer_cb = &timer.add_timer(1,callback);
|
||||
|
||||
|
||||
canvas.set_draw_color(Color::RGB(0, 255, 255));
|
||||
canvas.clear();
|
||||
canvas.present();
|
||||
println!("Start Rendering");
|
||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
'running: loop {
|
||||
canvas.set_draw_color(Color::RGB(0,136,206));
|
||||
canvas.clear();
|
||||
let test_c = Arc::clone(&test);
|
||||
let a = test_c.lock().unwrap();
|
||||
render(&mut canvas,&a,&font);
|
||||
//render(&mut canvas,&stream_queues,&font);
|
||||
timer.ticks();
|
||||
//println!("{:?}",&a);
|
||||
render(&mut canvas,&a,&font,&font_departure, &box_config);
|
||||
|
||||
for event in event_pump.poll_iter() {
|
||||
match event {
|
||||
Event::Quit {..} |
|
||||
@@ -210,7 +214,6 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// The rest of the game loop goes here...
|
||||
|
||||
canvas.present();
|
||||
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
|
||||
|
||||
@@ -88,7 +88,7 @@ pub fn get_matches(tournament : &str, key : &str) -> Vec<SmashQueue> {
|
||||
};
|
||||
|
||||
|
||||
if !(p1name.is_empty() & p2name.is_empty()){
|
||||
if !(p1name.is_empty() || p2name.is_empty()){
|
||||
|
||||
let mat = Match { full_round_test : String::from(set.full_round_text.unwrap()) ,
|
||||
player1: String::from(p1name),
|
||||
|
||||
Reference in New Issue
Block a user