mirror of
https://github.com/JanNeuendorf/SVC16.git
synced 2025-06-04 18:45:27 +00:00
Input working correctly with resize
This commit is contained in:
parent
07c6223a87
commit
8300a6ea96
97
src/main.rs
97
src/main.rs
@ -1,13 +1,14 @@
|
||||
mod cli;
|
||||
mod engine;
|
||||
mod utils;
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use clap::Parser;
|
||||
use cli::Cli;
|
||||
use engine::Engine;
|
||||
use minifb::{Scale, Window, WindowOptions};
|
||||
use pixels::{Error, Pixels, SurfaceTexture};
|
||||
use std::time::Instant;
|
||||
use utils::*;
|
||||
use winit::dpi::LogicalSize;
|
||||
use winit::error::EventLoopError;
|
||||
use winit::event::{Event, WindowEvent};
|
||||
@ -56,7 +57,6 @@ fn main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// Handle input events
|
||||
if input.update(&event) {
|
||||
// Close events
|
||||
if input.key_pressed(KeyCode::Escape) || input.close_requested() {
|
||||
@ -77,7 +77,7 @@ fn main() -> Result<()> {
|
||||
while !engine.wants_to_sync() {
|
||||
engine.step().unwrap();
|
||||
}
|
||||
let (c1, c2) = get_input_code(&input).unwrap();
|
||||
let (c1, c2) = get_input_code(&input, &pixels).unwrap();
|
||||
let nb = engine.perform_sync(c1, c2);
|
||||
update_image_buffer(pixels.frame_mut(), &nb);
|
||||
|
||||
@ -87,94 +87,3 @@ fn main() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_u16s_from_file(file_path: &str) -> Result<Vec<u16>> {
|
||||
use std::io::{BufReader, Read};
|
||||
let file = std::fs::File::open(file_path)?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut buffer = [0u8; 2];
|
||||
let mut u16s = Vec::new();
|
||||
while reader.read_exact(&mut buffer).is_ok() {
|
||||
let value = u16::from_le_bytes(buffer);
|
||||
u16s.push(value);
|
||||
}
|
||||
Ok(u16s)
|
||||
}
|
||||
|
||||
fn rgb565_to_argb(rgb565: u16) -> (u8, u8, u8) {
|
||||
let r = ((rgb565 >> 11) & 0x1F) as u8;
|
||||
let g = ((rgb565 >> 5) & 0x3F) as u8;
|
||||
let b = (rgb565 & 0x1F) as u8;
|
||||
let r = (r << 3) | (r >> 2);
|
||||
let g = (g << 2) | (g >> 4);
|
||||
let b = (b << 3) | (b >> 2);
|
||||
(r, g, b)
|
||||
// (0xFF << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32)
|
||||
}
|
||||
|
||||
fn update_image_buffer(imbuff: &mut [u8], screen: &[u16; RES * RES]) {
|
||||
for i in 0..RES * RES {
|
||||
let col = rgb565_to_argb(screen[i]);
|
||||
*imbuff.get_mut(4 * i).expect("Error with image buffer") = col.0;
|
||||
*imbuff.get_mut(4 * i + 1).expect("Error with image buffer") = col.1;
|
||||
*imbuff.get_mut(4 * i + 2).expect("Error with image buffer") = col.2;
|
||||
*imbuff.get_mut(4 * i + 3).expect("Error with image buffer") = 255;
|
||||
}
|
||||
}
|
||||
|
||||
fn get_input_code(input: &WinitInputHelper) -> Result<(u16, u16)> {
|
||||
let mp = input.cursor().unwrap_or((0., 0.));
|
||||
// let mp = (100., 100.);
|
||||
dbg!(mp);
|
||||
let pos_code = (mp.1 as u16 * 256) + mp.0 as u16;
|
||||
let mut key_code = 0_u16;
|
||||
if input.key_pressed(KeyCode::Space) || input.mouse_pressed(winit::event::MouseButton::Left) {
|
||||
key_code += 1;
|
||||
}
|
||||
// if window.get_mouse_down(minifb::MouseButton::Left) || window.is_key_down(Key::Space) {
|
||||
// key_code += 1;
|
||||
// }
|
||||
// if window.get_mouse_down(minifb::MouseButton::Right) || window.is_key_down(Key::B) {
|
||||
// key_code += 2;
|
||||
// }
|
||||
// if window.is_key_down(Key::Up) || window.is_key_down(Key::W) {
|
||||
// key_code += 4;
|
||||
// }
|
||||
// if window.is_key_down(Key::Down) || window.is_key_down(Key::S) {
|
||||
// key_code += 8;
|
||||
// }
|
||||
// if window.is_key_down(Key::Left) || window.is_key_down(Key::A) {
|
||||
// key_code += 16;
|
||||
// }
|
||||
// if window.is_key_down(Key::Right) || window.is_key_down(Key::D) {
|
||||
// key_code += 32;
|
||||
// }
|
||||
// if window.is_key_down(Key::N) {
|
||||
// key_code += 64;
|
||||
// }
|
||||
// if window.is_key_down(Key::M) {
|
||||
// key_code += 128;
|
||||
// }
|
||||
|
||||
// todo!();
|
||||
Ok((pos_code, key_code))
|
||||
}
|
||||
|
||||
fn print_debug_info(debug_vals: &Vec<u16>, engine: &Engine) {
|
||||
let ptr = engine.get_instruction_pointer();
|
||||
let inst = engine.read_instruction();
|
||||
for d in debug_vals {
|
||||
println!("@{}={}", d, engine.get(*d));
|
||||
}
|
||||
println!(
|
||||
"prt:{}, opcode:{}, args:[{},{},{}], @args:[{},{},{}]",
|
||||
ptr,
|
||||
inst[0],
|
||||
inst[1],
|
||||
inst[2],
|
||||
inst[3],
|
||||
engine.get(inst[1]),
|
||||
engine.get(inst[2]),
|
||||
engine.get(inst[3])
|
||||
);
|
||||
}
|
||||
|
73
src/utils.rs
Normal file
73
src/utils.rs
Normal file
@ -0,0 +1,73 @@
|
||||
use crate::RES;
|
||||
use anyhow::Result;
|
||||
use pixels::Pixels;
|
||||
use winit::{event::MouseButton, keyboard::Key, keyboard::KeyCode};
|
||||
use winit_input_helper::WinitInputHelper;
|
||||
|
||||
pub fn read_u16s_from_file(file_path: &str) -> Result<Vec<u16>> {
|
||||
use std::io::{BufReader, Read};
|
||||
let file = std::fs::File::open(file_path)?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut buffer = [0u8; 2];
|
||||
let mut u16s = Vec::new();
|
||||
while reader.read_exact(&mut buffer).is_ok() {
|
||||
let value = u16::from_le_bytes(buffer);
|
||||
u16s.push(value);
|
||||
}
|
||||
Ok(u16s)
|
||||
}
|
||||
|
||||
fn rgb565_to_argb(rgb565: u16) -> (u8, u8, u8) {
|
||||
let r = ((rgb565 >> 11) & 0x1F) as u8;
|
||||
let g = ((rgb565 >> 5) & 0x3F) as u8;
|
||||
let b = (rgb565 & 0x1F) as u8;
|
||||
let r = (r << 3) | (r >> 2);
|
||||
let g = (g << 2) | (g >> 4);
|
||||
let b = (b << 3) | (b >> 2);
|
||||
(r, g, b)
|
||||
}
|
||||
|
||||
pub fn update_image_buffer(imbuff: &mut [u8], screen: &[u16; RES * RES]) {
|
||||
for i in 0..RES * RES {
|
||||
let col = rgb565_to_argb(screen[i]);
|
||||
*imbuff.get_mut(4 * i).expect("Error with image buffer") = col.0;
|
||||
*imbuff.get_mut(4 * i + 1).expect("Error with image buffer") = col.1;
|
||||
*imbuff.get_mut(4 * i + 2).expect("Error with image buffer") = col.2;
|
||||
*imbuff.get_mut(4 * i + 3).expect("Error with image buffer") = 255;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_input_code(input: &WinitInputHelper, pxls: &Pixels) -> Result<(u16, u16)> {
|
||||
let raw_mp = input.cursor().unwrap_or((0., 0.));
|
||||
let mp = match pxls.window_pos_to_pixel(raw_mp) {
|
||||
Ok(p) => p,
|
||||
Err(ev) => pxls.clamp_pixel_pos(ev),
|
||||
};
|
||||
let pos_code = (mp.1 as u16 * 256) + mp.0 as u16;
|
||||
let mut key_code = 0_u16;
|
||||
if input.key_held(KeyCode::Space) || input.mouse_held(MouseButton::Left) {
|
||||
key_code += 1;
|
||||
}
|
||||
if input.key_held_logical(Key::Character("b")) || input.mouse_held(MouseButton::Right) {
|
||||
key_code += 2;
|
||||
}
|
||||
if input.key_held_logical(Key::Character("w")) || input.key_held(KeyCode::ArrowUp) {
|
||||
key_code += 4;
|
||||
}
|
||||
if input.key_held_logical(Key::Character("s")) || input.key_held(KeyCode::ArrowDown) {
|
||||
key_code += 8;
|
||||
}
|
||||
if input.key_held_logical(Key::Character("a")) || input.key_held(KeyCode::ArrowLeft) {
|
||||
key_code += 16;
|
||||
}
|
||||
if input.key_held_logical(Key::Character("d")) || input.key_held(KeyCode::ArrowRight) {
|
||||
key_code += 32;
|
||||
}
|
||||
if input.key_held_logical(Key::Character("n")) {
|
||||
key_code += 64;
|
||||
}
|
||||
if input.key_held_logical(Key::Character("m")) {
|
||||
key_code += 128;
|
||||
}
|
||||
Ok((pos_code, key_code))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user