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
+4
View File
@@ -24,6 +24,10 @@ Space : Start/Stop simulation
R : Create a random scene R : Create a random scene
Up : Increase FPS (be carefull it goes fast)
Down : Decrease FPS (be carefull it goes fast)
## How to play ## How to play
Add cells when the simualtion is not started and then start it Add cells when the simualtion is not started and then start it
+20 -2
View File
@@ -70,6 +70,7 @@ impl Map{
} }
} }
pub struct Game{ pub struct Game{
fps:i32,
draw_debug:bool, draw_debug:bool,
started:bool, started:bool,
map:Map, map:Map,
@@ -158,6 +159,12 @@ fn is_tab_pressed(e: &sdl2::EventPump)-> bool{
fn is_space_pressed(e: &sdl2::EventPump)-> bool{ fn is_space_pressed(e: &sdl2::EventPump)-> bool{
e.keyboard_state().is_scancode_pressed(Scancode::Space) 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{ fn check_mouse_left_pressed(e: &sdl2::EventPump) -> Point{
if e.mouse_state().left(){ if e.mouse_state().left(){
return Point::new(e.mouse_state().x(),e.mouse_state().y()); 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){ if is_random_pressed(event_pump){
game.map.randomize(); 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 is_space_pressed(event_pump){
if space_state == false{ if space_state == false{
//KeyDown //KeyDown
@@ -225,7 +242,8 @@ pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pu
// MAIN LOOP // MAIN LOOP
canvas.present(); 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{ if quit{
break; break;
@@ -246,6 +264,6 @@ pub fn main() {
let mut event_pump = sdl_context.event_pump().unwrap(); let mut event_pump = sdl_context.event_pump().unwrap();
canvas.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64)); 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); main_loop(&mut canvas,&mut event_pump,&mut game);
} }