Skip to content

Commit 2acfcd0

Browse files
committed
perf: 60 to 40 cpu %
1 parent 36ab74e commit 2acfcd0

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

Cargo.lock

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
image = "0.25.5"
88
minifb = "0.27.0"
99
rand = "0.8.5"
10+
rusttype = "0.9.3"
1011

1112
[dev-dependencies]
1213
cross = "0.2"

src/image_utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ pub fn get_background_for_score<'a>(
6565
mouse2_background: &'a [u32],
6666
original_background: &'a [u32],
6767
winner_background: &'a [u32],
68-
6968
) -> Vec<u32> {
7069
match score {
7170
s if s > 29 => winner_background.to_vec(),

src/main.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use image::GenericImageView;
22
use minifb::{Key, Window, WindowOptions};
33
use rand::Rng;
4-
use std::time::Instant;
4+
use std::time::{Duration, Instant};
55

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

1718
struct Raindrop {
1819
x: u32,
@@ -64,12 +65,16 @@ fn main() {
6465
.map(|i| Raindrop {
6566
x: rand::thread_rng().gen_range(0..width - DROP_SIZE),
6667
y: 0,
67-
start_time: Instant::now() + std::time::Duration::from_secs_f32(i as f32 * DROP_DELAY),
68+
start_time: Instant::now() + Duration::from_secs_f32(i as f32 * DROP_DELAY),
6869
last_update: Instant::now(),
6970
})
7071
.collect();
7172

73+
let frame_duration = Duration::from_secs_f32(1.0 / TARGET_FPS);
74+
7275
while window.is_open() && !window.is_key_down(Key::Escape) {
76+
let frame_start = Instant::now();
77+
7378
if score >= WINNING_SCORE {
7479
println!("You win! Final Score: {}", score);
7580

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

97102
// Cursor movement
98103
if window.is_key_down(Key::Up) && cursor_y > 0 {
99-
cursor_y -= 1;
104+
cursor_y = cursor_y.saturating_sub(5);
100105
}
101106
if window.is_key_down(Key::Down) && cursor_y + square_size < height {
102-
cursor_y += 1;
107+
cursor_y = cursor_y.saturating_add(5);
103108
}
104109
if window.is_key_down(Key::Left) && cursor_x > 0 {
105-
cursor_x -= 1;
110+
cursor_x = cursor_x.saturating_sub(5);
106111
}
107112
if window.is_key_down(Key::Right) && cursor_x + square_size < width {
108-
cursor_x += 1;
113+
cursor_x = cursor_x.saturating_add(5);
109114
}
110115

111116
let cat_rect = (cat_x, cat_y, cat_width, square_size);
@@ -115,12 +120,13 @@ fn main() {
115120
// Update raindrops
116121
for drop in raindrops.iter_mut() {
117122
if drop.start_time.elapsed().as_secs_f32() > 0.0 {
118-
if drop.y < height - DROP_SIZE
119-
&& drop.last_update.elapsed().as_secs_f32() > DROP_SPEED
120-
{
121-
drop.y += 1;
123+
let elapsed = drop.last_update.elapsed().as_secs_f32();
124+
let pixels_to_move = (elapsed * DROP_SPEED).round() as u32;
125+
126+
if drop.y < height - DROP_SIZE {
127+
drop.y += pixels_to_move;
122128
drop.last_update = Instant::now();
123-
} else if drop.y >= height - DROP_SIZE {
129+
} else {
124130
drop.y = 0;
125131
drop.x = rand::thread_rng().gen_range(0..width - DROP_SIZE);
126132
}
@@ -155,7 +161,10 @@ fn main() {
155161
.update_with_buffer(&buffer, width as usize, height as usize)
156162
.expect("Failed to update window buffer");
157163

158-
print!("\x1B[2J\x1B[1;1H");
159-
println!("Score: {}", score);
164+
// Cap FPS
165+
let elapsed = frame_start.elapsed();
166+
if elapsed < frame_duration {
167+
std::thread::sleep(frame_duration - elapsed);
168+
}
160169
}
161170
}

0 commit comments

Comments
 (0)