Skip to content

Commit

Permalink
refactor: 🚧 refactor luminol-graphics and it's a mess
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Oct 17, 2023
1 parent 6681c89 commit b7a157d
Show file tree
Hide file tree
Showing 19 changed files with 397 additions and 366 deletions.
23 changes: 22 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/luminol-audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ impl Audio {
pub fn play(
&self,
path: impl AsRef<camino::Utf8Path>,
filesystem: &'static impl luminol_core::filesystem::FileSystem, // FIXME
filesystem: &'static impl luminol_filesystem::FileSystem, // FIXME
volume: u8,
pitch: u8,
source: Source,
) -> Result<(), String> {
let path = path.as_ref();
let file = filesystem
.open_file(path, luminol_core::filesystem::OpenFlags::Read)
.open_file(path, luminol_filesystem::OpenFlags::Read)
.map_err(|e| e.to_string())?;

let is_midi = path
Expand Down
5 changes: 5 additions & 0 deletions crates/luminol-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub use map_view::{MapView, SelectedLayer};
mod tilepicker;
pub use tilepicker::{SelectedTile, Tilepicker};

mod tab;
pub use tab::{Tab, Tabs};
mod window;
pub use window::{Window, Windows};

pub struct EnumMenuButton<'e, T> {
current_value: &'e mut T,
id: egui::Id,
Expand Down
22 changes: 10 additions & 12 deletions crates/luminol-filesystem/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,16 @@ impl FileSystem {
}

#[cfg(not(any(windows, target_arch = "wasm32")))]
fn find_rtp_paths(config: &luminol_config::project::Config) -> Vec<camino::Utf8PathBuf> {
let ini = game_ini!();
let Some(section) = ini.section(Some("Game")) else {
return vec![];
fn find_rtp_paths(
config: &luminol_config::project::Config,
global_config: &luminol_config::global::Config,
) -> (Vec<camino::Utf8PathBuf>, Vec<String>) {
let Some(section) = config.game_ini.section(Some("Game")) else {
return (vec![], vec![]);
};
let mut paths = vec![];
let mut seen_rtps = vec![];
let mut missing_rtps = vec![];
// FIXME: handle vx ace?
for rtp in ["RTP1", "RTP2", "RTP3"] {
if let Some(rtp) = section.get(rtp) {
Expand All @@ -222,23 +225,18 @@ impl FileSystem {
}
seen_rtps.push(rtp);

if let Some(path) = global_config!().rtp_paths.get(rtp) {
if let Some(path) = global_config.rtp_paths.get(rtp) {
let path = camino::Utf8PathBuf::from(path);
if path.exists() {
paths.push(path);
continue;
}
}

state!()
.toasts
.warning(format!("Failed to find suitable path for the RTP {rtp}"));
state!()
.toasts
.info(format!("You may want to set an RTP path for {rtp}"));
missing_rtps.push(rtp.to_string());
}
}
paths
(paths, missing_rtps)
}

#[cfg(target_arch = "wasm32")]
Expand Down
4 changes: 3 additions & 1 deletion crates/luminol-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ image = "0.24.7"

glam = { version = "0.24.2", features = ["bytemuck"] }

const_format = "0.2.32"

egui.workspace = true
egui_extras.workspace = true
egui-wgpu.workspace = true
Expand All @@ -28,4 +30,4 @@ itertools.workspace = true
camino.workspace = true

luminol-data = { version = "0.1.0", path = "../luminol-data/" }
luminol-core = { version = "0.1.0", path = "../luminol-core/" }
luminol-filesystem = { version = "0.1.0", path = "../luminol-filesystem/" }
12 changes: 6 additions & 6 deletions crates/luminol-graphics/src/atlas_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ pub struct Cache {
impl Cache {
pub fn load_atlas(
&self,
render_state: &egui_wgpu::RenderState,
filesystem: &impl luminol_core::filesystem::FileSystem,
graphics_state: &crate::GraphicsState,
filesystem: &impl luminol_filesystem::FileSystem,
image_cache: &crate::image_cache::Cache,
tileset: &luminol_data::rpg::Tileset,
) -> Result<crate::tiles::Atlas, String> {
Ok(self
.atlases
.entry(tileset.id)
.or_try_insert_with(|| {
crate::tiles::Atlas::new(render_state, filesystem, image_cache, tileset)
crate::tiles::Atlas::new(graphics_state, filesystem, image_cache, tileset)
})?
.clone())
}

pub fn reload_atlas(
&self,
render_state: &egui_wgpu::RenderState,
filesystem: &impl luminol_core::filesystem::FileSystem,
graphics_state: &crate::GraphicsState,
filesystem: &impl luminol_filesystem::FileSystem,
image_cache: &crate::image_cache::Cache,
tileset: &luminol_data::rpg::Tileset,
) -> Result<crate::tiles::Atlas, String> {
Ok(self
.atlases
.entry(tileset.id)
.insert(crate::tiles::Atlas::new(
render_state,
graphics_state,
filesystem,
image_cache,
tileset,
Expand Down
12 changes: 7 additions & 5 deletions crates/luminol-graphics/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Event {
// code smell, fix
pub fn new(
graphics_state: &crate::GraphicsState,
filesystem: &impl luminol_core::filesystem::FileSystem,
filesystem: &impl luminol_filesystem::FileSystem,
event: &luminol_data::rpg::Event,
atlas: &crate::tiles::Atlas,
use_push_constants: bool,
Expand All @@ -62,8 +62,9 @@ impl Event {
let quad = atlas.calc_quad((id + 1) as i16);

let viewport = crate::viewport::Viewport::new(
graphics_state,
glam::Mat4::orthographic_rh(0.0, 32., 32., 0., -1., 1.),
use_push_constants
use_push_constants,
);

(quad, viewport, egui::vec2(32., 32.))
Expand All @@ -89,6 +90,7 @@ impl Event {
let quad = crate::quad::Quad::new(pos, tex_coords, 0.0);

let viewport = crate::viewport::Viewport::new(
graphics_state,
glam::Mat4::orthographic_rh(0.0, cw, ch, 0., -1., 1.),
use_push_constants,
);
Expand Down Expand Up @@ -116,13 +118,13 @@ impl Event {
&self.resources.sprite
}

pub fn set_proj(&self, proj: glam::Mat4) {
self.resources.viewport.set_proj(proj);
pub fn set_proj(&self, render_state: &egui_wgpu::RenderState, proj: glam::Mat4) {
self.resources.viewport.set_proj(render_state, proj);
}

pub fn paint(
&self,
graphics_state: &crate::GraphicsState,
graphics_state: &'static crate::GraphicsState,
painter: &egui::Painter,
rect: egui::Rect,
) {
Expand Down
6 changes: 3 additions & 3 deletions crates/luminol-graphics/src/image_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl WgpuTexture {
impl Cache {
pub fn load_egui_image(
&self,
filesystem: &impl luminol_core::filesystem::FileSystem,
filesystem: &impl luminol_filesystem::FileSystem,
directory: impl AsRef<str>,
filename: impl AsRef<str>,
) -> Result<Arc<RetainedImage>, String> {
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Cache {

pub fn load_image(
&self,
filesystem: &impl luminol_core::filesystem::FileSystem,
filesystem: &impl luminol_filesystem::FileSystem,
directory: impl AsRef<camino::Utf8Path>,
filename: impl AsRef<camino::Utf8Path>,
) -> Result<image::DynamicImage, String> {
Expand Down Expand Up @@ -156,7 +156,7 @@ impl Cache {
pub fn load_wgpu_image(
&self,
graphics_state: &crate::GraphicsState,
filesystem: &impl luminol_core::filesystem::FileSystem,
filesystem: &impl luminol_filesystem::FileSystem,
directory: impl AsRef<str>,
filename: impl AsRef<str>,
) -> Result<Arc<WgpuTexture>, String> {
Expand Down
4 changes: 4 additions & 0 deletions crates/luminol-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub struct GraphicsState {

pub struct BindGroupLayouts {
image_cache_texture: wgpu::BindGroupLayout,
viewport: wgpu::BindGroupLayout,
sprite_graphic: wgpu::BindGroupLayout,
atlas_autotiles: wgpu::BindGroupLayout,
tile_layer_opacity: wgpu::BindGroupLayout,
}

pub struct Pipelines {
Expand Down
51 changes: 36 additions & 15 deletions crates/luminol-graphics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,26 @@ type ResourcesSlab = slab::Slab<std::sync::Arc<Resources>>;

impl Map {
pub fn new(
render_state: &egui_wgpu::RenderState,
filesystem: &impl luminol_core::filesystem::FileSystem,
image_cache: &crate::image_cache::Cache,
atlas_cache: &crate::atlas_cache::Cache,
graphics_state: &crate::GraphicsState,
filesystem: &impl luminol_filesystem::FileSystem,
map: &luminol_data::rpg::Map,
tileset: &luminol_data::rpg::Tileset,
use_push_constants: bool,
) -> Result<Self, String> {
let atlas = atlas_cache.load_atlas(render_state, filesystem, image_cache, tileset)?;
let atlas = graphics_state.atlas_cache.load_atlas(
graphics_state,
filesystem,
&graphics_state.image_cache,
tileset,
)?;

let tiles = crate::tiles::Tiles::new(render_state, atlas, &map.data, use_push_constants);
let tiles = crate::tiles::Tiles::new(graphics_state, atlas, &map.data, use_push_constants);

let panorama = if let Some(ref panorama_name) = tileset.panorama_name {
Some(Plane::new(
image_cache.load_wgpu_image(
render_state,
graphics_state,
graphics_state.image_cache.load_wgpu_image(
graphics_state,
filesystem,
"Graphics/Panoramas",
panorama_name,
Expand All @@ -74,7 +78,13 @@ impl Map {
};
let fog = if let Some(ref fog_name) = tileset.fog_name {
Some(Plane::new(
image_cache.load_wgpu_image(render_state, filesystem, "Graphics/Fogs", fog_name)?,
graphics_state,
graphics_state.image_cache.load_wgpu_image(
graphics_state,
filesystem,
"Graphics/Fogs",
fog_name,
)?,
tileset.fog_hue,
tileset.fog_zoom,
tileset.fog_blend_type,
Expand All @@ -87,6 +97,7 @@ impl Map {
None
};
let viewport = crate::viewport::Viewport::new(
graphics_state,
glam::Mat4::orthographic_rh(
0.0,
map.width as f32 * 32.,
Expand Down Expand Up @@ -125,12 +136,13 @@ impl Map {
.set_tile(render_state, tile_id, position);
}

pub fn set_proj(&self, proj: glam::Mat4) {
self.resources.viewport.set_proj(proj);
pub fn set_proj(&self, render_state: &egui_wgpu::RenderState, proj: glam::Mat4) {
self.resources.viewport.set_proj(render_state, proj);
}

pub fn paint(
&mut self,
graphics_state: &'static crate::GraphicsState,
painter: &egui::Painter,
selected_layer: Option<usize>,
rect: egui::Rect,
Expand All @@ -139,7 +151,10 @@ impl Map {
if let Some(ani_time) = self.ani_time {
if time - ani_time >= 16. / 60. {
self.ani_time = Some(time);
self.resources.tiles.autotiles.inc_ani_index();
self.resources
.tiles
.autotiles
.inc_ani_index(&graphics_state.render_state);
}
} else {
self.ani_time = Some(time);
Expand Down Expand Up @@ -185,14 +200,20 @@ impl Map {

if pano_enabled {
if let Some(panorama) = panorama {
panorama.draw(viewport, render_pass);
panorama.draw(graphics_state, viewport, render_pass);
}
}

tiles.draw(viewport, &enabled_layers, selected_layer, render_pass);
tiles.draw(
graphics_state,
viewport,
&enabled_layers,
selected_layer,
render_pass,
);
if fog_enabled {
if let Some(fog) = fog {
fog.draw(viewport, render_pass);
fog.draw(graphics_state, viewport, render_pass);
}
}
});
Expand Down
Loading

0 comments on commit b7a157d

Please sign in to comment.