restructure stuff

This commit is contained in:
griffi-gh 2024-02-21 03:51:05 +01:00
parent e2bfa0c6f0
commit 8f5f8b07fb
5 changed files with 121 additions and 122 deletions

View file

@ -1,15 +1,12 @@
use std::{io::{BufReader, prelude::*}, path::Path};
use hui::{text::FontHandle, UiInstance};
use hui::UiInstance;
use hui_glium::GliumUiRenderer;
use shipyard::{AllStoragesView, Unique, UniqueView, NonSendSync, UniqueViewMut};
use crate::{filesystem::AssetManager, rendering::{RenderTarget, Renderer, WindowSize}};
use crate::rendering::{RenderTarget, Renderer, WindowSize};
#[derive(Unique)]
pub struct UiState {
pub hui: UiInstance,
pub renderer: GliumUiRenderer,
//HACK: This is a temporary solution, i don't think fonts should be stored here
pub fonts: Vec<FontHandle>,
pub renderer: GliumUiRenderer
}
pub fn kubi_ui_init(
@ -19,22 +16,9 @@ pub fn kubi_ui_init(
storages.add_unique_non_send_sync(UiState {
hui: UiInstance::new(),
renderer: GliumUiRenderer::new(&renderer.display),
fonts: vec![],
});
}
//TODO: Use prefab system for this
pub fn kubi_ui_load_assets(
asset_manager: UniqueView<AssetManager>,
mut ui: NonSendSync<UniqueViewMut<UiState>>
) {
let asset_handle = asset_manager.open_asset(Path::new("fonts/Crisp.ttf")).unwrap();
let mut font_data = vec![];
BufReader::new(asset_handle).read_to_end(&mut font_data).unwrap();
let font_handle = ui.hui.add_font_from_bytes(&font_data);
ui.fonts.push(font_handle);
}
pub fn kubi_ui_begin(
mut ui: NonSendSync<UniqueViewMut<UiState>>
) {

View file

@ -13,7 +13,13 @@ use winit::{
use glam::vec3;
use std::time::Instant;
pub use kubi_shared::transform;
pub(crate) use kubi_shared::transform;
mod ui {
pub(crate) mod loading_screen;
pub(crate) mod connecting_screen;
}
pub(crate) use ui::{loading_screen, connecting_screen};
pub(crate) mod rendering;
pub(crate) mod world;
@ -33,8 +39,6 @@ pub(crate) mod hui_integration;
pub(crate) mod networking;
pub(crate) mod init;
pub(crate) mod color;
pub(crate) mod loading_screen;
pub(crate) mod connecting_screen;
pub(crate) mod fixed_timestamp;
pub(crate) mod filesystem;
pub(crate) mod client_physics;
@ -77,7 +81,7 @@ use control_flow::{exit_on_esc, insert_control_flow_unique, RequestExit};
use state::{is_ingame, is_ingame_or_loading, is_loading, init_state, update_state, is_connecting};
use networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer};
use init::initialize_from_args;
use hui_integration::{kubi_ui_begin, kubi_ui_draw, kubi_ui_end, kubi_ui_init, kubi_ui_load_assets};
use hui_integration::{kubi_ui_begin, kubi_ui_draw, kubi_ui_end, kubi_ui_init};
use loading_screen::update_loading_screen;
use connecting_screen::update_connecting_screen;
use fixed_timestamp::init_fixed_timestamp_storage;
@ -97,7 +101,6 @@ fn startup() -> Workload {
initial_resize_event,
init_window_size,
kubi_ui_init,
kubi_ui_load_assets,
load_prefabs,
init_primitives,
insert_lock_state,

View file

@ -1,87 +1,103 @@
use shipyard::{NonSendSync, UniqueView, Unique, AllStoragesView};
use glium::{texture::{SrgbTexture2dArray, MipmapsOption}, Program};
use kubi_shared::block::BlockTexture;
use crate::{rendering::Renderer, filesystem::AssetManager};
mod texture;
mod shaders;
use texture::load_texture2darray_prefab;
use shaders::include_shader_prefab;
pub trait AssetPaths {
fn file_name(self) -> &'static str;
}
impl AssetPaths for BlockTexture {
fn file_name(self) -> &'static str {
match self {
Self::Stone => "stone.png",
Self::Dirt => "dirt.png",
Self::GrassTop => "grass_top.png",
Self::GrassSide => "grass_side.png",
Self::Sand => "sand.png",
Self::Bedrock => "bedrock.png",
Self::Wood => "wood.png",
Self::WoodTop => "wood_top.png",
Self::Leaf => "leaf.png",
Self::Torch => "torch.png",
Self::TallGrass => "tall_grass.png",
Self::Snow => "snow.png",
Self::GrassSideSnow => "grass_side_snow.png",
Self::Cobblestone => "cobblestone.png",
Self::Planks => "planks.png",
Self::WaterSolid => "solid_water.png",
}
}
}
#[derive(Unique)]
#[repr(transparent)]
pub struct BlockTexturesPrefab(pub SrgbTexture2dArray);
#[derive(Unique)]
#[repr(transparent)]
pub struct ChunkShaderPrefab(pub Program);
#[derive(Unique)]
#[repr(transparent)]
pub struct ColoredShaderPrefab(pub Program);
pub fn load_prefabs(
storages: AllStoragesView,
renderer: NonSendSync<UniqueView<Renderer>>,
assman: UniqueView<AssetManager>
) {
log::info!("Loading textures...");
storages.add_unique_non_send_sync(BlockTexturesPrefab(
load_texture2darray_prefab::<BlockTexture, _>(
&assman,
"blocks".into(),
&renderer.display,
MipmapsOption::AutoGeneratedMipmaps
)
));
log::info!("Compiling shaders...");
storages.add_unique_non_send_sync(ChunkShaderPrefab(
include_shader_prefab!(
"world",
"../shaders/world.vert",
"../shaders/world.frag",
&renderer.display
)
));
storages.add_unique_non_send_sync(ColoredShaderPrefab(
include_shader_prefab!(
"colored",
"../shaders/colored.vert",
"../shaders/colored.frag",
&renderer.display
)
));
log::info!("releasing shader compiler");
renderer.display.release_shader_compiler();
}
use std::{io::{BufReader, Read}, path::Path};
use hui::text::FontHandle;
use shipyard::{AllStoragesView, NonSendSync, Unique, UniqueView, UniqueViewMut};
use glium::{texture::{SrgbTexture2dArray, MipmapsOption}, Program};
use kubi_shared::block::BlockTexture;
use crate::{filesystem::AssetManager, hui_integration::UiState, rendering::Renderer};
mod texture;
mod shaders;
use texture::load_texture2darray_prefab;
use shaders::include_shader_prefab;
pub trait AssetPaths {
fn file_name(self) -> &'static str;
}
impl AssetPaths for BlockTexture {
fn file_name(self) -> &'static str {
match self {
Self::Stone => "stone.png",
Self::Dirt => "dirt.png",
Self::GrassTop => "grass_top.png",
Self::GrassSide => "grass_side.png",
Self::Sand => "sand.png",
Self::Bedrock => "bedrock.png",
Self::Wood => "wood.png",
Self::WoodTop => "wood_top.png",
Self::Leaf => "leaf.png",
Self::Torch => "torch.png",
Self::TallGrass => "tall_grass.png",
Self::Snow => "snow.png",
Self::GrassSideSnow => "grass_side_snow.png",
Self::Cobblestone => "cobblestone.png",
Self::Planks => "planks.png",
Self::WaterSolid => "solid_water.png",
}
}
}
#[derive(Unique)]
#[repr(transparent)]
pub struct BlockTexturesPrefab(pub SrgbTexture2dArray);
#[derive(Unique)]
#[repr(transparent)]
pub struct ChunkShaderPrefab(pub Program);
#[derive(Unique)]
#[repr(transparent)]
pub struct ColoredShaderPrefab(pub Program);
#[derive(Unique)]
#[repr(transparent)]
pub struct UiFontPrefab(pub FontHandle);
pub fn load_prefabs(
storages: AllStoragesView,
renderer: NonSendSync<UniqueView<Renderer>>,
mut ui: NonSendSync<UniqueViewMut<UiState>>,
assman: UniqueView<AssetManager>
) {
log::info!("Loading textures...");
storages.add_unique_non_send_sync(BlockTexturesPrefab(
load_texture2darray_prefab::<BlockTexture, _>(
&assman,
"blocks".into(),
&renderer.display,
MipmapsOption::AutoGeneratedMipmaps
)
));
log::info!("Loading the UI stuff...");
{
let asset_handle = assman.open_asset(Path::new("fonts/Crisp.ttf")).unwrap();
let mut font_data = vec![];
BufReader::new(asset_handle).read_to_end(&mut font_data).unwrap();
let font_handle = ui.hui.add_font_from_bytes(&font_data);
storages.add_unique(UiFontPrefab(font_handle));
}
log::info!("Compiling shaders...");
storages.add_unique_non_send_sync(ChunkShaderPrefab(
include_shader_prefab!(
"world",
"../shaders/world.vert",
"../shaders/world.frag",
&renderer.display
)
));
storages.add_unique_non_send_sync(ColoredShaderPrefab(
include_shader_prefab!(
"colored",
"../shaders/colored.vert",
"../shaders/colored.frag",
&renderer.display
)
));
log::info!("releasing shader compiler");
renderer.display.release_shader_compiler();
}

View file

@ -5,6 +5,7 @@ use crate::{
hui_integration::UiState,
loading_screen::loading_screen_base,
networking::ServerAddress,
prefabs::UiFontPrefab,
rendering::WindowSize,
state::{GameState, NextState}
};
@ -12,9 +13,9 @@ use crate::{
fn render_connecting_ui(
addr: UniqueView<ServerAddress>,
mut ui: NonSendSync<UniqueViewMut<UiState>>,
font: UniqueView<UiFontPrefab>,
size: UniqueView<WindowSize>,
) {
let font_handle = ui.fonts[0];
ui.hui.add(
loading_screen_base(vec![
Box::new(Text {
@ -22,7 +23,7 @@ fn render_connecting_ui(
"Connecting to {}...",
addr.0,
).into(),
font: font_handle,
font: font.0,
text_size: 16,
..Default::default()
}),

View file

@ -6,12 +6,7 @@ use hui::{
use shipyard::{UniqueView, UniqueViewMut, Workload, NonSendSync, IntoWorkload};
use winit::keyboard::KeyCode;
use crate::{
hui_integration::UiState,
input::RawKbmInputState,
networking::ServerAddress,
rendering::WindowSize,
state::{GameState, NextState},
world::ChunkStorage
hui_integration::UiState, input::RawKbmInputState, networking::ServerAddress, prefabs::UiFontPrefab, rendering::WindowSize, state::{GameState, NextState}, world::ChunkStorage
};
pub fn loading_screen_base(elements: Vec<Box<dyn UiElement>>, bg_alpha: f32) -> Container {
@ -37,6 +32,7 @@ fn render_loading_ui(
addr: Option<UniqueView<ServerAddress>>,
world: UniqueView<ChunkStorage>,
mut ui: NonSendSync<UniqueViewMut<UiState>>,
font: UniqueView<UiFontPrefab>,
size: UniqueView<WindowSize>
) {
let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| {
@ -46,14 +42,13 @@ fn render_loading_ui(
let value = loaded as f32 / total as f32;
let percentage = value * 100.;
let font_handle = ui.fonts[0];
ui.hui.add(loading_screen_base(vec![
Box::new(Text {
text: match addr {
Some(addr) => format!("Connected to {}\nDownloading world data...", addr.0).into(),
_ => "Loading...".into(),
},
font: font_handle,
font: font.0,
text_size: 16,
..Default::default()
}),
@ -70,7 +65,7 @@ fn render_loading_ui(
elements: vec![
Box::new(Text {
text: format!("{loaded}/{total} ({percentage:.1}%)").into(),
font: font_handle,
font: font.0,
text_size: 16,
..Default::default()
})