added load_data for image

This commit is contained in:
dani 2023-07-01 18:42:02 +00:00
parent 2ee1c46cb9
commit a8aecacbdf
3 changed files with 14 additions and 6 deletions

View File

@ -14,10 +14,10 @@ fn main() {
impl Game for World {
fn new(window_state: &mut WindowState) -> Self {
window_state.scramble_palette();
window_state.set_palette(0, 0xc0, 0xf0, 0xd0);
window_state.scramble_palette();
Self {
img: Image::load("examples/assets/test.gif"),
img: Image::load_data(include_bytes!("assets/test.gif")),
pos: Vec2::zero(),
}
}

View File

@ -1,4 +1,5 @@
use crate::vec2::Vec2;
use std::fs;
use std::fs::File;
//todo: make dynamically different bitdepths
@ -16,10 +17,13 @@ impl Image {
}
pub fn load(path: &str) -> Self {
let input = File::open(path).unwrap();
Self::load_data(fs::read(path).unwrap().as_slice())
}
pub fn load_data(data: &[u8]) -> Self {
let mut options = gif::DecodeOptions::new();
options.set_color_output(gif::ColorOutput::Indexed);
let mut decoder = options.read_info(input).unwrap();
let mut decoder = options.read_info(data).unwrap();
let x = decoder.width();
let y = decoder.height();

View File

@ -103,8 +103,12 @@ pub fn run<T: Game + 'static>(width: i32, height: i32) {
WindowEvent::Focused(_) => {}
WindowEvent::KeyboardInput { input, .. } => {
//todo: actually do input handling
if input.virtual_keycode.unwrap() == Escape {
if let Some(k) = input.virtual_keycode {
if k == Escape {
*control_flow = ControlFlow::Exit;
}
};
if *control_flow == ControlFlow::Exit {
return;
}
}