swapped out pixels for softbuffer

This commit is contained in:
dani 2023-07-01 18:25:09 +00:00
parent 3d39d0a04f
commit 2ee1c46cb9
4 changed files with 261 additions and 581 deletions

785
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,9 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pixels = "0.13.0"
winit = "0.28.6"
gif = "0.12.0"
rand = "0.8.5"
num = "0.4.0"
measure_time = "0.8.2"
measure_time = "0.8.2"
softbuffer = "0.3.0"

View File

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

View File

@ -1,7 +1,7 @@
use crate::image::Image;
use crate::vec2::Vec2;
use pixels::{Pixels, SurfaceTexture};
use rand::Rng;
use std::num::NonZeroU32;
use winit::dpi::LogicalSize;
use winit::event::VirtualKeyCode::Escape;
use winit::event::{
@ -32,19 +32,20 @@ pub trait Game {
}
pub struct WindowState {
palette: [[u8; 4]; 256],
palette: [u32; 256],
mouse_pos: Vec2<i32>,
}
impl WindowState {
fn new() -> Self {
WindowState {
palette: [[0u8; 4]; 256],
palette: [0u32; 256],
mouse_pos: Vec2::zero(),
}
}
pub fn set_palette(&mut self, index: u8, r: u8, g: u8, b: u8) {
self.palette[index as usize] = [r, g, b, 0xffu8];
self.palette[index as usize] =
(r as u32) | ((g as u32) << 8) | ((b as u32) << 16) | 0xff000000;
}
pub fn scramble_palette(&mut self) {
@ -67,19 +68,15 @@ pub fn run<T: Game + 'static>(width: i32, height: i32) {
.with_title("Skunk 2D")
.with_inner_size(size)
.with_min_inner_size(size)
.with_decorations(false)
.with_maximized(true)
//.with_decorations(false)
//.with_maximized(true)
.build(&event_loop)
.unwrap()
};
//todo: replace Pixels with custom thingie (startup time slow because wgpu?)
let mut pixels = {
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(width as u32, height as u32, surface_texture).unwrap()
};
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let mut screen = Image::new(Vec2 {
x: width,
y: height,
@ -95,9 +92,7 @@ pub fn run<T: Game + 'static>(width: i32, height: i32) {
..
} => {
match window_event {
WindowEvent::Resized(size) => {
pixels.resize_surface(size.width, size.height).unwrap();
}
WindowEvent::Resized(_) => {}
WindowEvent::Moved(_) => {}
WindowEvent::CloseRequested => {}
WindowEvent::Destroyed => {}
@ -116,12 +111,9 @@ pub fn run<T: Game + 'static>(width: i32, height: i32) {
WindowEvent::ModifiersChanged(_) => {}
WindowEvent::Ime(_) => {}
WindowEvent::CursorMoved { position, .. } => {
let (x, y) = pixels
.window_pos_to_pixel((position.x as f32, position.y as f32))
.unwrap_or_else(|pos| pixels.clamp_pixel_pos(pos));
window_state.mouse_pos = Vec2 {
x: x as i32,
y: y as i32,
x: position.x as i32,
y: position.y as i32,
}
}
WindowEvent::CursorEntered { .. } => {}
@ -163,10 +155,23 @@ pub fn run<T: Game + 'static>(width: i32, height: i32) {
WinitEvent::MainEventsCleared => {}
WinitEvent::RedrawRequested(_) => {
game.draw(&mut screen);
for (i, pixel) in pixels.frame_mut().chunks_exact_mut(4).enumerate() {
pixel.copy_from_slice(&window_state.palette[screen.data()[i] as usize]);
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut buf = surface.buffer_mut().unwrap();
for i in 0..(width * height) as usize {
buf[i] = window_state.palette[screen.data()[i] as usize];
}
pixels.render().unwrap();
buf.present().unwrap();
}
WinitEvent::RedrawEventsCleared => {}
WinitEvent::LoopDestroyed => {}