loading assets "works"

This commit is contained in:
Dominic 2024-07-06 17:18:28 +02:00
parent 4c42512881
commit 44b257c710
Signed by: msrd0
GPG key ID: AAF7C8430CA3345D
5 changed files with 36 additions and 15 deletions

View file

@ -65,11 +65,12 @@ impl AssetsWriter {
) -> io::Result<()> { ) -> io::Result<()> {
for (group_name, group) in &root.groups { for (group_name, group) in &root.groups {
writeln!(file, "{indent}mod {group_name} {{")?; writeln!(file, "{indent}mod {group_name} {{")?;
writeln!(file, "{indent}\tuse super::*;")?;
write_assets_struct(file, group, &format!("{indent}\t"))?; write_assets_struct(file, group, &format!("{indent}\t"))?;
writeln!(file, "}}")?; writeln!(file, "}}")?;
} }
writeln!(file, "{indent}struct Asset {{")?; writeln!(file, "{indent}pub struct Assets {{")?;
for asset_name in root.assets.keys() { for asset_name in root.assets.keys() {
writeln!( writeln!(
file, file,
@ -88,7 +89,7 @@ impl AssetsWriter {
writeln!(file, "{indent}impl Assets {{")?; writeln!(file, "{indent}impl Assets {{")?;
writeln!( writeln!(
file, file,
"{indent}\tpub fn load(c: &mut comfy::EngineContext) {{" "{indent}\tpub fn load(c: &mut comfy::EngineContext<'_>) {{"
)?; )?;
for asset_const_name in root.assets.values() { for asset_const_name in root.assets.values() {
writeln!(file, "{indent}\t\tc.load_texture_from_bytes({asset_const_name:?}, {asset_const_name});")?; writeln!(file, "{indent}\t\tc.load_texture_from_bytes({asset_const_name:?}, {asset_const_name});")?;
@ -101,7 +102,7 @@ impl AssetsWriter {
writeln!( writeln!(
file, file,
"{indent}const ASSETS: comfy::Lazy<Assets> = Lazy::new(|| Assets {{" "{indent}pub static ASSETS: comfy::Lazy<Assets> = comfy::Lazy::new(|| Assets {{"
)?; )?;
for (asset_name, asset_const_name) in &root.assets { for (asset_name, asset_const_name) in &root.assets {
writeln!( writeln!(
@ -113,7 +114,7 @@ impl AssetsWriter {
for group_name in root.groups.keys() { for group_name in root.groups.keys() {
writeln!( writeln!(
file, file,
"{indent}\t{group_name}: {group_name}::ASSETS.force()," "{indent}\t{group_name}: comfy::Lazy::force(&{group_name}::ASSETS),"
)?; )?;
} }
writeln!(file, "{indent}}});")?; writeln!(file, "{indent}}});")?;

View file

@ -1,7 +1,7 @@
use comfy::*; use comfy::{draw_circle, vec2, EngineContext, RED};
pub fn draw(_state: &crate::State, _engine: &comfy::EngineContext) { pub fn draw(_state: &crate::State, _engine: &EngineContext<'_>) {
draw_circle(vec2(0.0, 0.0), 0.5, RED, 0); draw_circle(vec2(0.0, 0.0), 0.5, RED, 0);
} }
pub fn update(_state: &mut crate::State, _engine: &mut comfy::EngineContext) {} pub fn update(_state: &mut crate::State, _engine: &mut EngineContext<'_>) {}

View file

@ -1,9 +1,9 @@
use comfy::*; use comfy::{draw_circle, vec2, EngineContext, GREEN};
pub mod worldgen; pub mod worldgen;
pub fn draw(_state: &crate::State, _engine: &comfy::EngineContext) { pub fn draw(_state: &crate::State, _engine: &EngineContext<'_>) {
draw_circle(vec2(0.0, 0.0), 0.5, GREEN, 0); draw_circle(vec2(0.0, 0.0), 0.5, GREEN, 0);
} }
pub fn update(_state: &mut crate::State, _engine: &mut comfy::EngineContext) {} pub fn update(_state: &mut crate::State, _engine: &mut EngineContext<'_>) {}

View file

@ -5,14 +5,14 @@ use crate::{
State State
}; };
pub fn update(state: &mut State, engine: &mut EngineContext) { pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
match state.activity { match state.activity {
Activity::House => house::update(state, engine), Activity::House => house::update(state, engine),
Activity::Overworld => overworld::update(state, engine) Activity::Overworld => overworld::update(state, engine)
} }
} }
pub fn draw(state: &State, engine: &EngineContext) { pub fn draw(state: &State, engine: &EngineContext<'_>) {
match state.activity { match state.activity {
Activity::House => house::draw(state, engine), Activity::House => house::draw(state, engine),
Activity::Overworld => overworld::draw(state, engine) Activity::Overworld => overworld::draw(state, engine)

View file

@ -1,13 +1,24 @@
#![warn(rust_2018_idioms)]
#![forbid(clippy::wildcard_imports, elided_lifetimes_in_paths, unsafe_code)]
mod assets {
include!(env!("ASSETS_RS"));
}
mod activities; mod activities;
mod game; mod game;
use activities::{overworld::worldgen::Overworld, Activity}; use self::{
use comfy::*; activities::{overworld::worldgen::Overworld, Activity},
assets::Assets
};
use comfy::{GameLoop, EngineState, EngineContext, GameConfig, init_game_config, run_comfy_main_async, pollster};
const GAME_NAME: &str = "Powercreep"; const GAME_NAME: &str = "Powercreep";
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct State { struct State {
setup_called: bool,
activity: Activity, activity: Activity,
overworld: Overworld overworld: Overworld
} }
@ -17,7 +28,12 @@ impl GameLoop for State {
Self::default() Self::default()
} }
fn update(&mut self, engine: &mut EngineContext) { fn update(&mut self, engine: &mut EngineContext<'_>) {
if !self.setup_called {
setup(engine);
self.setup_called = true;
}
game::update(self, engine); game::update(self, engine);
game::draw(self, engine); game::draw(self, engine);
} }
@ -27,6 +43,10 @@ fn config(config: GameConfig) -> GameConfig {
config config
} }
fn setup(ctx: &mut EngineContext<'_>) {
Assets::load(ctx);
}
async fn run() { async fn run() {
init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config); init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config);
let mut engine = EngineState::new(); let mut engine = EngineState::new();