optimised draw to screen, and prepared for threaded

This commit is contained in:
dani 2023-07-09 23:45:12 +00:00
parent 09dada68a9
commit 9b59214c1d
4 changed files with 142 additions and 4 deletions

97
Cargo.lock generated
View File

@ -236,6 +236,49 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset 0.9.0",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if",
]
[[package]]
name = "dispatch"
version = "0.2.0"
@ -257,6 +300,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]]
name = "either"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]]
name = "equivalent"
version = "1.0.0"
@ -505,6 +554,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
@ -687,6 +745,16 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "num_enum"
version = "0.5.11"
@ -898,6 +966,28 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]]
name = "rayon"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "redox_syscall"
version = "0.3.5"
@ -913,6 +1003,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sctk-adwaita"
version = "0.5.4"
@ -940,6 +1036,7 @@ dependencies = [
"measure_time",
"num",
"rand",
"rayon",
"softbuffer",
"winit",
]

View File

@ -12,3 +12,4 @@ rand = "0.8.5"
num = "0.4.0"
measure_time = "0.8.2"
softbuffer = "0.3.0"
rayon = "1.7.0"

View File

@ -14,7 +14,7 @@ struct World {
fn main() {
rand::thread_rng().gen::<Direction>();
run::<World>(WIDTH, HEIGHT, 240);
run::<World>(WIDTH, HEIGHT, 10000);
}
impl Game for World {

View File

@ -3,6 +3,7 @@ use crate::image::Image;
use crate::vec2::Vec2;
use crate::Tileset;
use rand::Rng;
use rayon::prelude::*;
use std::cmp::{max, min};
use std::num::NonZeroU32;
use std::time::{Duration, Instant};
@ -222,7 +223,46 @@ pub fn run<T: Game + 'static>(width: i32, height: i32, target_fps: u32) {
*off_y = (size.height as usize - height as usize * *scale) / 2;
let mut buf = surface.buffer_mut().unwrap();
for y in 0..height as usize {
//for y in 0..height as usize {
{
let width = width as usize;
let height = height as usize;
let window_width = size.width as usize;
let window_height = size.height as usize;
let scale = *scale;
let chunk_size = scale * window_width;
let screen_data = screen.data();
buf.chunks_exact_mut(chunk_size)
.into_iter()
.enumerate()
.for_each(|(y, chunk)| {
for x in 0..width {
//let p = window_state.palette[screen_data[x + y * width] as usize];
let p = 0;
for scaley in 0..scale {
for scalex in 0..scale {
let sx = x * scale + scalex;
chunk[sx + scaley * window_width] = p;
}
}
}
/*
for (chunk_i, data) in chunk.iter_mut().enumerate() {
let i = chunk_i + chunk_idx * chunk_size;
let sx = i % window_width;
let sy = i / window_width;
let bx = sx / scale;
let by = sy / scale;
let p =
window_state.palette[screen_data[bx + by * width] as usize];
*data = p;
}
*/
});
}
/*(0..height as usize).for_each(|y| {
for x in 0..width as usize {
let p = window_state.palette
[screen.data()[x + y * width as usize] as usize];
@ -234,7 +274,7 @@ pub fn run<T: Game + 'static>(width: i32, height: i32, target_fps: u32) {
}
}
}
}
});*/
buf.present().unwrap();
frames_since_last_second += 1;
last_frame += target_frame_duration;