Added FPS controll

This commit is contained in:
2022-05-23 15:00:49 +02:00
parent 44792e5fb7
commit b328ab3fb0
2 changed files with 24 additions and 2 deletions

View File

@@ -24,6 +24,10 @@ Space : Start/Stop simulation
R : Create a random scene
Up : Increase FPS (be carefull it goes fast)
Down : Decrease FPS (be carefull it goes fast)
## How to play
Add cells when the simualtion is not started and then start it

View File

@@ -70,6 +70,7 @@ impl Map{
}
}
pub struct Game{
fps:i32,
draw_debug:bool,
started:bool,
map:Map,
@@ -158,6 +159,12 @@ fn is_tab_pressed(e: &sdl2::EventPump)-> bool{
fn is_space_pressed(e: &sdl2::EventPump)-> bool{
e.keyboard_state().is_scancode_pressed(Scancode::Space)
}
fn is_up_pressed (e: &sdl2::EventPump)-> bool{
e.keyboard_state().is_scancode_pressed(Scancode::Up)
}
fn is_down_pressed (e: &sdl2::EventPump)-> bool{
e.keyboard_state().is_scancode_pressed(Scancode::Down)
}
fn check_mouse_left_pressed(e: &sdl2::EventPump) -> Point{
if e.mouse_state().left(){
return Point::new(e.mouse_state().x(),e.mouse_state().y());
@@ -181,6 +188,16 @@ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pu
if is_random_pressed(event_pump){
game.map.randomize();
}
if is_up_pressed(event_pump){
game.fps += 5;
println!("[DEBUG] fps : {}",game.fps);
}
if is_down_pressed(event_pump){
if game.fps > 5{
game.fps -= 5;
println!("[DEBUG] fps : {}",game.fps);
}
}
if is_space_pressed(event_pump){
if space_state == false{
//KeyDown
@@ -225,7 +242,8 @@ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pu
// MAIN LOOP
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
//::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / game.fps as u32));
if quit{
break;
@@ -246,6 +264,6 @@ pub fn main() {
let mut event_pump = sdl_context.event_pump().unwrap();
canvas.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
let mut game = Game{started:false,draw_debug:false,map:Map::new()};
let mut game = Game{fps:120,started:false,draw_debug:false,map:Map::new()};
main_loop(&mut canvas,&mut event_pump,&mut game);
}