Skip to content

Commit

Permalink
perf: 60 to 40 cpu %
Browse files Browse the repository at this point in the history
  • Loading branch information
RGGH committed Nov 18, 2024
1 parent 36ab74e commit 2acfcd0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
image = "0.25.5"
minifb = "0.27.0"
rand = "0.8.5"
rusttype = "0.9.3"

[dev-dependencies]
cross = "0.2"
1 change: 0 additions & 1 deletion src/image_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub fn get_background_for_score<'a>(
mouse2_background: &'a [u32],
original_background: &'a [u32],
winner_background: &'a [u32],

) -> Vec<u32> {
match score {
s if s > 29 => winner_background.to_vec(),
Expand Down
37 changes: 23 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use image::GenericImageView;
use minifb::{Key, Window, WindowOptions};
use rand::Rng;
use std::time::Instant;
use std::time::{Duration, Instant};

use image_utils::{convert_to_mono, draw_raindrop, draw_square, is_collision};
use image_utils::{get_background_for_score, load_background_data};
Expand All @@ -11,8 +11,9 @@ mod image_utils;
const DROP_SIZE: u32 = 6;
const NUM_DROPS: usize = 8;
const DROP_DELAY: f32 = 0.5; // Delay in seconds for staggered start of each drop
const DROP_SPEED: f32 = 0.0001; // Time-based speed (larger values make drops fall slower)
const DROP_SPEED: f32 = 200.0; // Speed in pixels per second
const WINNING_SCORE: i32 = 30; // Score required to win
const TARGET_FPS: f32 = 60.0;

struct Raindrop {
x: u32,
Expand Down Expand Up @@ -64,12 +65,16 @@ fn main() {
.map(|i| Raindrop {
x: rand::thread_rng().gen_range(0..width - DROP_SIZE),
y: 0,
start_time: Instant::now() + std::time::Duration::from_secs_f32(i as f32 * DROP_DELAY),
start_time: Instant::now() + Duration::from_secs_f32(i as f32 * DROP_DELAY),
last_update: Instant::now(),
})
.collect();

let frame_duration = Duration::from_secs_f32(1.0 / TARGET_FPS);

while window.is_open() && !window.is_key_down(Key::Escape) {
let frame_start = Instant::now();

if score >= WINNING_SCORE {
println!("You win! Final Score: {}", score);

Expand All @@ -96,16 +101,16 @@ fn main() {

// Cursor movement
if window.is_key_down(Key::Up) && cursor_y > 0 {
cursor_y -= 1;
cursor_y = cursor_y.saturating_sub(5);
}
if window.is_key_down(Key::Down) && cursor_y + square_size < height {
cursor_y += 1;
cursor_y = cursor_y.saturating_add(5);
}
if window.is_key_down(Key::Left) && cursor_x > 0 {
cursor_x -= 1;
cursor_x = cursor_x.saturating_sub(5);
}
if window.is_key_down(Key::Right) && cursor_x + square_size < width {
cursor_x += 1;
cursor_x = cursor_x.saturating_add(5);
}

let cat_rect = (cat_x, cat_y, cat_width, square_size);
Expand All @@ -115,12 +120,13 @@ fn main() {
// Update raindrops
for drop in raindrops.iter_mut() {
if drop.start_time.elapsed().as_secs_f32() > 0.0 {
if drop.y < height - DROP_SIZE
&& drop.last_update.elapsed().as_secs_f32() > DROP_SPEED
{
drop.y += 1;
let elapsed = drop.last_update.elapsed().as_secs_f32();
let pixels_to_move = (elapsed * DROP_SPEED).round() as u32;

if drop.y < height - DROP_SIZE {
drop.y += pixels_to_move;
drop.last_update = Instant::now();
} else if drop.y >= height - DROP_SIZE {
} else {
drop.y = 0;
drop.x = rand::thread_rng().gen_range(0..width - DROP_SIZE);
}
Expand Down Expand Up @@ -155,7 +161,10 @@ fn main() {
.update_with_buffer(&buffer, width as usize, height as usize)
.expect("Failed to update window buffer");

print!("\x1B[2J\x1B[1;1H");
println!("Score: {}", score);
// Cap FPS
let elapsed = frame_start.elapsed();
if elapsed < frame_duration {
std::thread::sleep(frame_duration - elapsed);
}
}
}

0 comments on commit 2acfcd0

Please sign in to comment.