Skip to content

Commit

Permalink
chore: take score text fn out of main
Browse files Browse the repository at this point in the history
  • Loading branch information
RGGH committed Nov 18, 2024
1 parent 2acfcd0 commit d262ea0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ rusttype = "0.9.3"

[dev-dependencies]
cross = "0.2"

[profile.release]
opt-level = "z" # Optimize for binary size; change to "3" for maximum speed
lto = true # Enable Link-Time Optimization
codegen-units = 1 # Better optimization by reducing parallelism
panic = "abort" # Reduce overhead of unwinding
strip = true # Strip debugging symbols
debug = false # Disable debug information in the release binary

Binary file modified assets/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/image_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
use crate::Raindrop;
use crate::Scale;
use crate::Font;
use crate::DROP_SIZE;

use image::{GenericImageView, Rgba};
// Render text using rusttype
pub fn draw_text(buffer: &mut Vec<u32>, width: u32, height: u32, text: &str, x: u32, y: u32) {
let font_data = include_bytes!("../assets/FiraSans-SemiBold.otf");
let font = Font::try_from_bytes(font_data as &[u8]).expect("Failed to load font");

let scale = Scale::uniform(31.0); // Font size
let start = rusttype::point(x as f32, y as f32);
let layout = font.layout(text, scale, start);

for glyph in layout {
if let Some(bounding_box) = glyph.pixel_bounding_box() {
glyph.draw(|gx, gy, v| {
let px = bounding_box.min.x + gx as i32;
let py = bounding_box.min.y + gy as i32;

if px >= 0 && py >= 0 && px < width as i32 && py < height as i32 {
let idx = (py as u32 * width + px as u32) as usize;
let alpha = (v * 255.0) as u32;
buffer[idx] = (255 << 24) | (alpha << 16) | (alpha << 8) | alpha; // White text
}
});
}
}
}


pub fn convert_to_mono(image_data: &[u8]) -> Vec<u32> {
let img = image::load_from_memory(image_data).expect("Failed to load image");
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use image::GenericImageView;
use minifb::{Key, Window, WindowOptions};
use rand::Rng;
use rusttype::{Font, Scale};
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};
use crate::image_utils::draw_text;

mod image_utils;

Expand All @@ -22,6 +24,8 @@ struct Raindrop {
last_update: Instant, // Last time the drop's position was updated
}



fn main() {
let mut score: i32 = 0;
let mut square_size: u32 = 30;
Expand Down Expand Up @@ -151,6 +155,9 @@ fn main() {
draw_raindrop(&mut buffer, width, height, drop);
}

// Render the score on the screen
draw_text(&mut buffer, width, height, &format!("Score: {}", score), 10, 30);

if score > 10 {
square_size = 50;
} else {
Expand All @@ -168,3 +175,4 @@ fn main() {
}
}
}

0 comments on commit d262ea0

Please sign in to comment.