Skip to content

Commit

Permalink
refactor: ♻️ don't take an &'static reference to graphics state
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Oct 18, 2023
1 parent 93add75 commit 94deff3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
45 changes: 27 additions & 18 deletions crates/luminol-components/src/map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,38 @@ pub enum SelectedLayer {
}

impl MapView {
pub fn new(
graphics_state: &luminol_graphics::GraphicsState,
filesystem: &impl luminol_filesystem::FileSystem,
pub fn new<W, T>(
update_state: &mut luminol_core::UpdateState<'_, W, T>,
map: &luminol_data::rpg::Map,
tileset: &luminol_data::rpg::Tileset,
) -> Result<MapView, String> {
// Get tilesets.

let use_push_constants = graphics_state
let use_push_constants = update_state
.graphics
.render_state
.device
.features()
.contains(wgpu::Features::PUSH_CONSTANTS);
let atlas = graphics_state
.atlas_cache
.load_atlas(graphics_state, filesystem, tileset)?;
let atlas = update_state.graphics.atlas_cache.load_atlas(
&update_state.graphics,
update_state.filesystem,
tileset,
)?;
let events = map
.events
.iter()
.map(|(id, e)| {
let sprite = luminol_graphics::Event::new(
graphics_state,
filesystem,
&update_state.graphics,
update_state.filesystem,
e,
&atlas,
use_push_constants,
);
let preview_sprite = luminol_graphics::Event::new(
graphics_state,
filesystem,
&update_state.graphics,
update_state.filesystem,
e,
&atlas,
use_push_constants,
Expand All @@ -108,8 +110,8 @@ impl MapView {
.flatten_ok()
.try_collect()?;
let map = luminol_graphics::Map::new(
graphics_state,
filesystem,
&update_state.graphics,
update_state.filesystem,
map,
tileset,
use_push_constants,
Expand Down Expand Up @@ -141,10 +143,10 @@ impl MapView {
})
}

pub fn ui(
pub fn ui<W, T>(
&mut self,
ui: &mut egui::Ui,
graphics_state: &'static luminol_graphics::GraphicsState,
update_state: &mut luminol_core::UpdateState<'_, W, T>,
map: &luminol_data::rpg::Map,
tilepicker: &crate::Tilepicker,
dragging_event: bool,
Expand Down Expand Up @@ -257,6 +259,9 @@ impl MapView {
let proj_center_y = height2 * 32. - self.pan.y / scale;
let proj_width2 = canvas_rect.width() / scale / 2.;
let proj_height2 = canvas_rect.height() / scale / 2.;

let graphics_state = update_state.graphics.clone();

self.map.set_proj(
&graphics_state.render_state,
glam::Mat4::orthographic_rh(
Expand All @@ -269,7 +274,7 @@ impl MapView {
),
);
self.map.paint(
graphics_state,
graphics_state.clone(),
ui.painter(),
match self.selected_layer {
SelectedLayer::Events => None,
Expand Down Expand Up @@ -366,7 +371,7 @@ impl MapView {
1.,
),
);
sprite.paint(graphics_state, ui.painter(), canvas_rect);
sprite.paint(graphics_state.clone(), ui.painter(), canvas_rect);
}

if matches!(self.selected_layer, SelectedLayer::Events)
Expand Down Expand Up @@ -419,7 +424,11 @@ impl MapView {
);
if let Some((_, preview_sprite)) = sprites {
if ui.ctx().screen_rect().contains_rect(response.rect) {
preview_sprite.paint(graphics_state, &painter, response.rect);
preview_sprite.paint(
graphics_state.clone(),
&painter,
response.rect,
);
}
}
match self.selected_event_id {
Expand Down
14 changes: 12 additions & 2 deletions crates/luminol-components/src/tilepicker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ impl Tilepicker {
}
}

pub fn ui(
pub fn ui<W, T>(
&mut self,
update_state: &mut luminol_core::UpdateState<'_, W, T>,
ui: &mut egui::Ui,
graphics_state: &'static luminol_graphics::GraphicsState,
scroll_rect: egui::Rect,
) -> egui::Response {
let time = ui.ctx().input(|i| i.time);
let graphics_state = update_state.graphics.clone();

if let Some(ani_time) = self.ani_time {
if time - ani_time >= 16. / 60. {
self.ani_time = Some(time);
Expand Down Expand Up @@ -179,6 +181,7 @@ impl Tilepicker {
1.,
),
);
// FIXME: move this into graphics
ui.painter().add(egui::PaintCallback {
rect: scroll_rect.translate(canvas_rect.min.to_vec2()),
callback: Arc::new(
Expand All @@ -190,6 +193,8 @@ impl Tilepicker {
let id = res_hash.insert(resources.clone());
prepare_id.set(id).expect("resources id already set?");

paint_callback_resources.insert(graphics_state.clone());

vec![]
})
.paint(move |_info, render_pass, paint_callback_resources| {
Expand All @@ -200,6 +205,11 @@ impl Tilepicker {
tiles, viewport, ..
} = resources.as_ref();

let graphics_state: &Arc<luminol_graphics::GraphicsState> =
paint_callback_resources
.get()
.expect("graphics state unset");

viewport.bind(render_pass);
tiles.draw(graphics_state, viewport, &[true], None, render_pass);
}),
Expand Down
8 changes: 5 additions & 3 deletions crates/luminol-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// terms of the Steamworks API by Valve Corporation, the licensors of this
// Program grant you additional permission to convey the resulting work.

use std::sync::Arc;

mod tab;
pub use tab::{Tab, Tabs};

Expand All @@ -38,7 +40,7 @@ pub use toasts::Toasts;
pub struct UpdateState<'res, W, T> {
pub audio: &'res mut luminol_audio::Audio,

pub graphics: &'res mut luminol_graphics::GraphicsState, // FIXME: certain functions were written with this being a static in mind
pub graphics: Arc<luminol_graphics::GraphicsState>,
pub filesystem: &'res mut luminol_filesystem::project::FileSystem, // FIXME: this is probably wrong
pub data: &'res mut luminol_data::data_cache::Cache,

Expand Down Expand Up @@ -79,7 +81,7 @@ impl<'res, W, T> UpdateState<'res, W, T> {
) -> UpdateState<'this, O, T> {
UpdateState {
audio: &mut *self.audio,
graphics: &mut *self.graphics,
graphics: self.graphics.clone(),
filesystem: &mut *self.filesystem,
data: &mut *self.data,
edit_tabs: &mut *self.edit_tabs,
Expand All @@ -97,7 +99,7 @@ impl<'res, W, T> UpdateState<'res, W, T> {
) -> UpdateState<'this, W, O> {
UpdateState {
audio: &mut *self.audio,
graphics: &mut *self.graphics,
graphics: self.graphics.clone(),
filesystem: &mut *self.filesystem,
data: &mut *self.data,
edit_tabs,
Expand Down
2 changes: 1 addition & 1 deletion crates/luminol-filesystem/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl crate::File for File {

impl<T> crate::FileSystem for FileSystem<T>
where
T: Read + Write + Seek + Send + Sync,
T: Read + Write + Seek + Send + Sync + 'static,
{
type File = File;

Expand Down
9 changes: 8 additions & 1 deletion crates/luminol-graphics/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Event {

pub fn paint(
&self,
graphics_state: &'static crate::GraphicsState,
graphics_state: Arc<crate::GraphicsState>,
painter: &egui::Painter,
rect: egui::Rect,
) {
Expand All @@ -140,6 +140,9 @@ impl Event {
.or_insert_with(Default::default);
let id = res_hash.insert(resources.clone());
prepare_id.set(id).expect("resources id already set?");

paint_callback_resources.insert(graphics_state.clone());

vec![]
})
.paint(move |_info, render_pass, paint_callback_resources| {
Expand All @@ -148,6 +151,10 @@ impl Event {
let resources = &res_hash[id];
let Resources { viewport, sprite } = resources.as_ref();

let graphics_state: &Arc<crate::GraphicsState> = paint_callback_resources
.get()
.expect("graphics state is unset");

viewport.bind(render_pass);
sprite.draw(graphics_state, viewport, render_pass);
});
Expand Down
9 changes: 8 additions & 1 deletion crates/luminol-graphics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.

use crate::Plane;
use std::sync::Arc;

use std::time::Duration;

Expand Down Expand Up @@ -139,7 +140,7 @@ impl Map {

pub fn paint(
&mut self,
graphics_state: &'static crate::GraphicsState,
graphics_state: Arc<crate::GraphicsState>,
painter: &egui::Painter,
selected_layer: Option<usize>,
rect: egui::Rect,
Expand Down Expand Up @@ -179,6 +180,8 @@ impl Map {
let id = res_hash.insert(resources.clone());
prepare_id.set(id).expect("resources id already set?");

paint_callback_resources.insert(graphics_state.clone()); // ugh

vec![]
})
.paint(move |_info, render_pass, paint_callback_resources| {
Expand All @@ -193,6 +196,10 @@ impl Map {
..
} = resources.as_ref();

let graphics_state: &Arc<crate::GraphicsState> = paint_callback_resources
.get()
.expect("graphics state is unset");

viewport.bind(render_pass);

if pano_enabled {
Expand Down

0 comments on commit 94deff3

Please sign in to comment.