switched bg rendering to iterate over pixels rather than over tiles

This commit is contained in:
dani 2023-08-14 17:43:05 +00:00
parent a20bb4c921
commit 1c8a6bd391
3 changed files with 46 additions and 21 deletions

View File

@ -4,22 +4,11 @@ use gsa::{run, Gsa, FACE_DOWN};
struct Game {} struct Game {}
fn init(gsa: &mut Gsa) -> Game { fn init(gsa: &mut Gsa) -> Game {
gsa.sprites[0].tile = 0x0300; gsa.bg[0].fill(0x0101);
gsa.sprites[1].tile = 0x0200; //gsa.write_string(IVec2::ONE, "Hello world nyaa~");
gsa.bgs[0].tiles[1][1] = 0x0300;
gsa.bgs[1].half_tile = true;
gsa.load_map(1337);
gsa.write_string(1, IVec2::ONE, "Hello world nyaa~");
Game {} Game {}
} }
fn update(_game: &mut Game, gsa: &mut Gsa) { fn update(_game: &mut Game, gsa: &mut Gsa) {}
gsa.sprites[0].pos.x = (gsa.sprites[0].pos.x + 1) % 300;
gsa.sprites[1].pos += gsa.input_dir();
//gsa.bgs[1].scroll.x += 1;
if gsa.button_pressed(FACE_DOWN) {
gsa.sprites[1].tile += 1;
}
}
run!(init, update); run!(init, update);

View File

@ -1,4 +1,5 @@
use std::usize; use std::usize;
//use rayon::prelude::*;
use crate::{ use crate::{
Background, Gsa, Rgb, EMPTY_TILE, HALF_TILE_SIZE, MAX_BACKGROUNDS, TILESET_SIZE, TILE_SIZE, Background, Gsa, Rgb, EMPTY_TILE, HALF_TILE_SIZE, MAX_BACKGROUNDS, TILESET_SIZE, TILE_SIZE,
@ -32,7 +33,7 @@ fn draw_tile(
} }
} }
fn render_map(target: &mut [u8], map: &Background, tileset: &[u8], screen_size: IVec2) { fn render_bg(target: &mut [u8], map: &Background, tileset: &[u8], screen_size: IVec2) {
let tcmult = if map.half_tile { 2 } else { 1 }; let tcmult = if map.half_tile { 2 } else { 1 };
let tilesize = if map.half_tile { let tilesize = if map.half_tile {
HALF_TILE_SIZE HALF_TILE_SIZE
@ -71,12 +72,44 @@ fn render_map(target: &mut [u8], map: &Background, tileset: &[u8], screen_size:
} }
} }
fn render_bg2(target: &mut [u8], map: &Background, tileset: &[u8], screen_size: IVec2) {
let tilesize = if map.half_tile {
HALF_TILE_SIZE
} else {
TILE_SIZE
} as i32;
//target.par_chunks_exact_mut(screen_size.x as usize).enumerate().for_each({|(screeny, row)|
target.chunks_exact_mut(screen_size.x as usize).enumerate().for_each({|(screeny, row)|
for screenx in 0..screen_size.x {
let x = screenx + map.scroll.x;
let y = screeny as i32 + map.scroll.y;
let tilex = x / tilesize;
let tiley = y / tilesize;
let pixx = x % tilesize;
let pixy = y % tilesize;
let tile = if tilex >= 0 && tiley >= 0 && tilex < map.size.x && tiley < map.size.y {
map.tiles[tilex as usize][tiley as usize]
} else {
EMPTY_TILE
};
if tile != EMPTY_TILE {
let tilesetx = tile as usize % 0x100 * tilesize as usize + pixx as usize;
let tilesety = tile as usize / 0x100 * tilesize as usize + pixy as usize;
let p = tileset[tilesetx + tilesety * (TILESET_SIZE * TILE_SIZE)];
if p != TRANSPARENT {
row[x as usize] = p;
}
}
}
});
}
pub(crate) fn render_to_screen(target: &mut [u8], gsa: &Gsa, tileset: &[u8], screen_size: IVec2) { pub(crate) fn render_to_screen(target: &mut [u8], gsa: &Gsa, tileset: &[u8], screen_size: IVec2) {
for i in 0..MAX_BACKGROUNDS { for i in 0..MAX_BACKGROUNDS {
if gsa.bgs[i].active { if gsa.bg[i].active {
render_map(target, &gsa.bgs[i], tileset, screen_size); render_bg2(target, &gsa.bg[i], tileset, screen_size);
} }
for sprite in &gsa.sprites { for sprite in &gsa.sprite {
if sprite.tile != EMPTY_TILE && sprite.priority == i as u8 { if sprite.tile != EMPTY_TILE && sprite.priority == i as u8 {
draw_tile(target, sprite.pos, sprite.tile, tileset, false, screen_size); draw_tile(target, sprite.pos, sprite.tile, tileset, false, screen_size);
} }

View File

@ -47,15 +47,18 @@ pub fn run<TGame: 'static>(
) { ) {
let (tileset, palette) = load_tileset(image_data); let (tileset, palette) = load_tileset(image_data);
//rayon::ThreadPoolBuilder::new().num_threads(32).build_global().unwrap();
let mut gsa = Gsa { let mut gsa = Gsa {
sprites: [Sprite::default(); MAX_SPRITES], sprite: [Sprite::default(); MAX_SPRITES],
bgs: Default::default(), bg: Default::default(),
palette, palette,
font: FONT_BOLD, font: FONT_BOLD,
pressed: 0, pressed: 0,
released: 0, released: 0,
down: 0, down: 0,
maps: postcard::from_bytes(maps_data).unwrap(), maps: postcard::from_bytes(maps_data).unwrap(),
str_bg: 3,
}; };
gsa.reset_bgs(); gsa.reset_bgs();
@ -97,7 +100,7 @@ pub fn run<TGame: 'static>(
let mut last_second = Instant::now(); let mut last_second = Instant::now();
let mut last_frame = last_second; let mut last_frame = last_second;
let target_frame_duration = Duration::from_secs(1).div_f64(60.0); let target_frame_duration = Duration::from_secs(1).div_f64(60000.0);
let mut scale = 1usize; let mut scale = 1usize;
let mut off_x = 0usize; let mut off_x = 0usize;