Add tile graphic

This commit is contained in:
crumblingstatue 2023-04-01 21:01:14 +02:00
parent d78726eb5b
commit fbc7e35f9e
11 changed files with 812 additions and 20 deletions

View file

@ -3,20 +3,24 @@ use sfml::{
window::Event,
};
use crate::graphics;
use crate::{game::GameState, graphics, res::Res};
/// Application level state (includes game and ui state, etc.)
pub struct App {
rw: RenderWindow,
should_quit: bool,
game: GameState,
res: Res,
}
impl App {
pub fn new() -> Self {
Self {
pub fn new() -> anyhow::Result<Self> {
Ok(Self {
rw: graphics::make_window(),
should_quit: false,
}
game: GameState::default(),
res: Res::load()?,
})
}
pub fn do_game_loop(&mut self) {
@ -40,6 +44,7 @@ impl App {
fn do_rendering(&mut self) {
self.rw.clear(Color::BLACK);
self.game.draw_world(&mut self.rw, &self.res);
self.rw.display();
}
}

View file

@ -1,16 +1,22 @@
use crate::{math::WorldPos, world::World};
use sfml::graphics::{RenderTarget, RenderWindow, Sprite};
use crate::{math::WorldPos, res::Res, world::World};
pub struct GameState {
transient: TransientGameState,
persistent: PersistentGameState,
}
/// Transient game state, not saved to disk
pub struct TransientGameState {
camera_offset: WorldPos,
}
/// Persistent game state, saved to disk
pub struct PersistentGameState {
world: World,
}
impl GameState {
pub(crate) fn draw_world(&mut self, rw: &mut RenderWindow, res: &Res) {
rw.draw(&Sprite::with_texture(&res.tile_atlas));
}
}
impl Default for GameState {
fn default() -> Self {
Self {
camera_offset: WorldPos { x: 0, y: 0 },
world: Default::default(),
}
}
}

View file

@ -2,11 +2,22 @@ mod app;
mod game;
mod graphics;
mod math;
mod res;
mod world;
use app::App;
fn main() {
let mut app = App::new();
fn try_main() -> anyhow::Result<()> {
let mut app = App::new()?;
app.do_game_loop();
Ok(())
}
fn main() {
if let Err(e) = try_main() {
rfd::MessageDialog::new()
.set_title("Fatal error")
.set_description(&e.to_string())
.show();
}
}

View file

@ -1,6 +1,6 @@
pub type WorldPosScalar = i32;
pub struct WorldPos {
x: WorldPosScalar,
y: WorldPosScalar,
pub x: WorldPosScalar,
pub y: WorldPosScalar,
}

13
src/res.rs Normal file
View file

@ -0,0 +1,13 @@
use sfml::{graphics::Texture, SfBox};
pub struct Res {
pub tile_atlas: SfBox<Texture>,
}
impl Res {
pub fn load() -> anyhow::Result<Self> {
Ok(Self {
tile_atlas: Texture::from_file("res/tiles.png")?,
})
}
}

View file

@ -1,4 +1,5 @@
use fnv::FnvHashMap;
use rand::{thread_rng, Rng};
type ChunkPosScalar = i16;
@ -12,6 +13,14 @@ pub struct World {
chunks: FnvHashMap<ChunkPos, Chunk>,
}
impl Default for World {
fn default() -> Self {
Self {
chunks: Default::default(),
}
}
}
const CHUNK_EXTENT: u16 = 256;
const CHUNK_N_BLOCKS: usize = CHUNK_EXTENT as usize * CHUNK_EXTENT as usize;
@ -21,8 +30,20 @@ pub struct Chunk {
blocks: ChunkBlocks,
}
impl Chunk {
pub fn new_rand() -> Self {
let mut rng = thread_rng();
let mut blocks = [Block { id: 0 }; CHUNK_N_BLOCKS];
for b in &mut blocks {
b.id = rng.gen();
}
Self { blocks }
}
}
type BlockId = u16;
#[derive(Clone, Copy)]
pub struct Block {
id: BlockId,
}