Skip to content

Commit

Permalink
maybe some performance gains
Browse files Browse the repository at this point in the history
  • Loading branch information
sverrejb committed Oct 11, 2024
1 parent 5ff305b commit ab621b9
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 53 deletions.
Binary file modified img/not-found.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/seasons/halloween/bat-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/wolt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/xkcd2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 9 additions & 46 deletions src/seasons/halloween.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{ui::*, StaticAssets};
use crate::ui::*;
use rand::Rng;
use slint::{Image, LogicalPosition, Rgba8Pixel, SharedPixelBuffer, Timer, TimerMode};
use slint::{LogicalPosition, Timer, TimerMode};

pub fn setup_halloween_spooky_face(main_window: &MainWindow) -> Timer {
let halloween_timer = Timer::default();
Expand Down Expand Up @@ -44,62 +44,25 @@ pub fn setup_halloween_bat(main_window: &MainWindow) -> Timer {

let timer = Timer::default();
let mut rng = rand::thread_rng();
let frames = read_bat_frames();
let handle = main_window.as_weak();
let mut frame_number = 0;
let mut current_pos = LogicalPosition { x: 0.0, y: 0.0 };

timer.start(
TimerMode::Repeated,
std::time::Duration::from_millis(100),
std::time::Duration::from_secs(20),
move || {
let frame = &frames.clone()[frame_number % 9];
let new_pos = LogicalPosition {
x: rng.gen_range(0.0..width as f32),
y: rng.gen_range(0.0..height as f32),
};

let bat = Bat {
frame: frame.clone(),
pos: current_pos,
pos: new_pos,
size: 200 as f32,
};

if frame_number % 200 == 0 {
current_pos = LogicalPosition {
x: rng.gen_range(0.0..width as f32),
y: rng.gen_range(0.0..height as f32),
};
}

frame_number += 1;

handle.unwrap().set_bat(bat)
},
);

timer
}

fn read_bat_frames() -> Vec<Image> {
let mut frames = vec![];

for i in 0..9 {
let bat_frame_path = std::format!("seasons/halloween/bat-{}.png", i);
let frame_data = match StaticAssets::get(&bat_frame_path) {
Some(icon_data) => icon_data.data.into_owned(),
None => StaticAssets::get("not-found.png")
.unwrap()
.data
.into_owned(),
};

let bat_frame = image::load_from_memory_with_format(&frame_data, image::ImageFormat::Png)
.unwrap()
.into_rgba8();

let buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
bat_frame.as_raw(),
bat_frame.width(),
bat_frame.height(),
);

frames.push(Image::from_rgba8(buffer))
}
frames
}
26 changes: 19 additions & 7 deletions ui/seasons.slint
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export struct SpookyFace {

export struct Bat {
pos: Point,
size: length,
frame: image}
size: length}

component Flake inherits Rectangle {
width: 10px;
Expand Down Expand Up @@ -40,7 +39,6 @@ export component SeasonEffects inherits Rectangle {
y: spooky_face.pos.y;
z: 200.0;
width: spooky_face.size;
colorize: darkorange;
opacity: spooky_face.hidden ? 0% : 30%;

animate opacity {
Expand All @@ -50,15 +48,29 @@ export component SeasonEffects inherits Rectangle {
}

Image {
source: bat.frame;
width: 200px;

property <[image]> frames: [
@image-url("../img/seasons/halloween/bat-0.png"),
@image-url("../img/seasons/halloween/bat-1.png"),
@image-url("../img/seasons/halloween/bat-2.png"),
@image-url("../img/seasons/halloween/bat-3.png"),
@image-url("../img/seasons/halloween/bat-4.png"),
@image-url("../img/seasons/halloween/bat-5.png"),
@image-url("../img/seasons/halloween/bat-6.png"),
@image-url("../img/seasons/halloween/bat-7.png"),
@image-url("../img/seasons/halloween/bat-8.png"),
];
property <duration> duration: 1000ms;
property <int> total-frames: frames.length - 1;
source: frames[(total-frames * (animation-tick() / duration)).mod(total-frames)];

x: bat.pos.x;
y: bat.pos.y;
z: 200;

animate x, y {
duration: 2s;
easing: ease-out-bounce;
duration: 10s;
easing: ease;
}
}
}

0 comments on commit ab621b9

Please sign in to comment.