changed tilemap to background

This commit is contained in:
dani 2023-07-24 17:26:09 +00:00
parent d2155a718b
commit 8372690bc0
7 changed files with 33 additions and 32 deletions

View File

@ -6,8 +6,8 @@ struct Game {}
fn init(gsa: &mut Gsa) -> Game {
gsa.sprites[0].tile = 0x0300;
gsa.sprites[1].tile = 0x0200;
gsa.maps[0].tiles[1][1] = 0x0300;
gsa.maps[1].half_tile = true;
gsa.bgs[0].tiles[1][1] = 0x0300;
gsa.bgs[1].half_tile = true;
gsa.write_string(1, IVec2::ONE, "Hello world nyaa~");
Game {}
}
@ -15,7 +15,7 @@ fn init(gsa: &mut Gsa) -> Game {
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.maps[1].scroll.x += 1;
gsa.bgs[1].scroll.x += 1;
if gsa.button_pressed(FACE_DOWN) {
gsa.sprites[1].tile += 1;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -1,9 +1,9 @@
use crate::TILEMAP_MAX_SIZE;
use crate::BACKGROUND_MAX_SIZE;
use glam::IVec2;
/// Tilemap which will be rendered on screen
pub struct Tilemap {
/// Tiles in idx, accessible via \[x\]\[y\], x and y 0..[TILEMAP_MAX_SIZE]
pub struct Background {
/// Tiles in idx, accessible via \[x\]\[y\], x and y 0..[BACKGROUND_MAX_SIZE]
pub tiles: Vec<Vec<u16>>,
/// Camera scroll (negative draw offset) for rendering
pub scroll: IVec2,
@ -11,10 +11,10 @@ pub struct Tilemap {
pub half_tile: bool,
}
impl Default for Tilemap {
impl Default for Background {
fn default() -> Self {
let row = vec![0u16; TILEMAP_MAX_SIZE];
let tiles = vec![row; TILEMAP_MAX_SIZE];
let row = vec![0u16; BACKGROUND_MAX_SIZE];
let tiles = vec![row; BACKGROUND_MAX_SIZE];
Self {
tiles,
scroll: IVec2::ZERO,

View File

@ -1,8 +1,9 @@
use crate::background::Background;
use crate::rgb::Rgb;
use crate::sprite::Sprite;
use crate::tilemap::Tilemap;
use crate::{
Buttons, DPAD_DOWN, DPAD_LEFT, DPAD_RIGHT, DPAD_UP, MAX_SPRITES, MAX_TILEMAPS, TILEMAP_MAX_SIZE,
Buttons, BACKGROUND_MAX_SIZE, DPAD_DOWN, DPAD_LEFT, DPAD_RIGHT, DPAD_UP, MAX_BACKGROUNDS,
MAX_SPRITES,
};
use ascii::{AsciiChar, AsciiStr};
use glam::IVec2;
@ -16,7 +17,7 @@ pub struct Gsa {
pub palette: [Rgb; 256],
/// Tilemap layers available
pub maps: [Tilemap; MAX_TILEMAPS],
pub bgs: [Background; MAX_BACKGROUNDS],
/// Currently selected font
///
@ -36,9 +37,9 @@ impl Gsa {
/// Sets all tiles of map to val
pub fn fill_map(&mut self, map: usize, val: u16) {
for x in 0..TILEMAP_MAX_SIZE {
for y in 0..TILEMAP_MAX_SIZE {
self.maps[map].tiles[x][y] = val;
for x in 0..BACKGROUND_MAX_SIZE {
for y in 0..BACKGROUND_MAX_SIZE {
self.bgs[map].tiles[x][y] = val;
}
}
}
@ -72,7 +73,7 @@ impl Gsa {
pub fn write_string(&mut self, map: usize, pos: IVec2, str: &str) {
let str = AsciiStr::from_ascii(str).unwrap();
for (i, ch) in str.into_iter().enumerate() {
self.maps[map].tiles[pos.x as usize + i][pos.y as usize] = self.get_char_tile(*ch);
self.bgs[map].tiles[pos.x as usize + i][pos.y as usize] = self.get_char_tile(*ch);
}
}

View File

@ -1,6 +1,6 @@
use crate::{
Gsa, Rgb, Tilemap, HALF_TILE_SIZE, MAX_TILEMAPS, SCREEN_HEIGHT, SCREEN_WIDTH, TILEMAP_MAX_SIZE,
TILESET_SIZE, TILE_SIZE,
Background, Gsa, Rgb, BACKGROUND_MAX_SIZE, HALF_TILE_SIZE, MAX_BACKGROUNDS, SCREEN_HEIGHT,
SCREEN_WIDTH, TILESET_SIZE, TILE_SIZE,
};
use glam::IVec2;
use softbuffer::Buffer;
@ -24,7 +24,7 @@ fn draw_tile(target: &mut [u8], pos: IVec2, tile: u16, tileset: &[u8], half: boo
}
}
fn render_map(target: &mut [u8], map: &Tilemap, tileset: &[u8]) {
fn render_map(target: &mut [u8], map: &Background, tileset: &[u8]) {
let tcmult = if map.half_tile { 2 } else { 1 };
let tilesize = if map.half_tile {
HALF_TILE_SIZE
@ -33,8 +33,8 @@ fn render_map(target: &mut [u8], map: &Tilemap, tileset: &[u8]) {
} as i32;
let mut startx = map.scroll.x / tilesize;
let mut starty = map.scroll.y / tilesize;
let endx = (TILEMAP_MAX_SIZE as i32).min(startx + 20 * tcmult);
let endy = (TILEMAP_MAX_SIZE as i32).min(starty + 12 * tcmult);
let endx = (BACKGROUND_MAX_SIZE as i32).min(startx + 20 * tcmult);
let endy = (BACKGROUND_MAX_SIZE as i32).min(starty + 12 * tcmult);
startx = 0.max(startx);
starty = 0.max(starty);
for x in startx..endx {
@ -57,8 +57,8 @@ fn render_map(target: &mut [u8], map: &Tilemap, tileset: &[u8]) {
}
pub(crate) fn render_to_screen(target: &mut [u8], gsa: &Gsa, tileset: &[u8]) {
for i in 0..MAX_TILEMAPS {
render_map(target, &gsa.maps[i], tileset);
for i in 0..MAX_BACKGROUNDS {
render_map(target, &gsa.bgs[i], tileset);
for sprite in &gsa.sprites {
if sprite.tile > 0 && sprite.priority == i as u8 {
draw_tile(target, sprite.pos, sprite.tile, tileset, false);

View File

@ -9,10 +9,10 @@
//! - Tilesize: 16x16 (or 8x8 for half-tiles)
//! - Tileset: 65536 tiles, indexed via 0xYYXX
//! - Sprites: 256 of size 16x16 (pondering allowing larger sprites)
//! - Tilemaps: 4 of size 1024x1024, scrollable
//! - Backgrounds: 4 of size 1024x1024, scrollable
//!
//! ## Features not yet implemented
//! - Tilemap effects
//! - Background effects
//! - Rotation? Scaling?
//! - Mosaic?
//! - Mode7?
@ -27,21 +27,21 @@
//! - Gamepad text keyboard input
//! - Menus
mod background;
mod buttons;
mod gsa;
mod gsa_render_to_screen;
mod rgb;
mod run;
mod sprite;
mod tilemap;
mod tileset;
pub use crate::background::*;
pub use crate::buttons::*;
pub use crate::gsa::*;
pub use crate::rgb::*;
pub use crate::run::run;
pub use crate::sprite::*;
pub use crate::tilemap::*;
/// Amount of sprites in [Gsa::sprites]
pub const MAX_SPRITES: usize = 0xff;
@ -52,8 +52,8 @@ pub const SCREEN_WIDTH: usize = 304;
/// Screen Height in pixels
pub const SCREEN_HEIGHT: usize = 176;
/// X and y dimensions of maps in [Gsa::maps]
pub const TILEMAP_MAX_SIZE: usize = 1024;
/// X and y dimensions of maps in [Gsa::bgs]
pub const BACKGROUND_MAX_SIZE: usize = 1024;
/// Width and height (in tiles) of tileset
pub const TILESET_SIZE: usize = 0x100;
@ -64,5 +64,5 @@ pub const TILE_SIZE: usize = 16;
/// Width and height of a tile in half-tile mode
pub const HALF_TILE_SIZE: usize = 8;
/// Amount of tile maps in [Gsa::maps]
pub const MAX_TILEMAPS: usize = 4;
/// Amount of tile maps in [Gsa::bgs]
pub const MAX_BACKGROUNDS: usize = 4;

View File

@ -39,7 +39,7 @@ pub fn run<TGame: 'static>(
let mut gsa = Gsa {
sprites: [Sprite::default(); MAX_SPRITES],
maps: Default::default(),
bgs: Default::default(),
palette,
font: 0x1010,
pressed: 0,