Skip to content

Commit

Permalink
refactor(tilemap): ♻️ don't pass viewports around constantly
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Nov 30, 2023
1 parent 65f501d commit e76f3b3
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 130 deletions.
37 changes: 14 additions & 23 deletions crates/components/src/tilepicker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ pub struct Tilepicker {
drag_origin: Option<egui::Pos2>,

resources: Arc<Resources>,
viewport: Arc<luminol_graphics::viewport::Viewport>,
ani_time: Option<f64>,
}

#[derive(Debug)]
struct Resources {
tiles: luminol_graphics::tiles::Tiles,
collision: luminol_graphics::collision::Collision,
viewport: luminol_graphics::viewport::Viewport,
}

struct Callback {
Expand All @@ -61,20 +61,14 @@ impl egui_wgpu::CallbackTrait for Callback {
render_pass: &mut wgpu::RenderPass<'a>,
_callback_resources: &'a egui_wgpu::CallbackResources,
) {
self.resources.tiles.draw(
&self.graphics_state,
&self.resources.viewport,
&[true],
None,
render_pass,
);
self.resources
.tiles
.draw(&self.graphics_state, &[true], None, render_pass);

if self.coll_enabled {
self.resources.collision.draw(
&self.graphics_state,
&self.resources.viewport,
render_pass,
);
self.resources
.collision
.draw(&self.graphics_state, render_pass);
}
}
}
Expand Down Expand Up @@ -136,15 +130,15 @@ impl Tilepicker {
tilepicker_data,
);

let viewport = luminol_graphics::viewport::Viewport::new(
let viewport = Arc::new(luminol_graphics::viewport::Viewport::new(
&update_state.graphics,
256.,
atlas.tileset_height as f32 + 32.,
);
));

let tiles = luminol_graphics::tiles::Tiles::new(
&update_state.graphics,
&viewport,
viewport.clone(),
atlas,
&tilepicker_data,
);
Expand All @@ -167,16 +161,13 @@ impl Tilepicker {
.copy_from_slice(&tileset.passages.as_slice()[384..384 + length]);
let collision = luminol_graphics::collision::Collision::new(
&update_state.graphics,
&viewport,
viewport.clone(),
&passages,
);

Ok(Self {
resources: Arc::new(Resources {
tiles,
collision,
viewport,
}),
resources: Arc::new(Resources { tiles, collision }),
viewport,
ani_time: None,
selected_tiles_left: 0,
selected_tiles_top: 0,
Expand Down Expand Up @@ -233,7 +224,7 @@ impl Tilepicker {
.intersect(scroll_rect.translate(canvas_rect.min.to_vec2()));
let scroll_rect = absolute_scroll_rect.translate(-canvas_rect.min.to_vec2());

self.resources.viewport.set_proj(
self.viewport.set_proj(
&graphics_state.render_state,
glam::Mat4::orthographic_rh(
scroll_rect.left(),
Expand Down
6 changes: 3 additions & 3 deletions crates/graphics/src/atlas_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
// You should have received a copy of the GNU General Public License
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.
use crate::tiles::Atlas;
use crate::{tiles::Atlas, GraphicsState};

#[derive(Default, Debug)]
pub struct Cache {
Expand All @@ -24,7 +24,7 @@ pub struct Cache {
impl Cache {
pub fn load_atlas(
&self,
graphics_state: &crate::GraphicsState,
graphics_state: &GraphicsState,
filesystem: &impl luminol_filesystem::FileSystem,
tileset: &luminol_data::rpg::Tileset,
) -> anyhow::Result<Atlas> {
Expand All @@ -37,7 +37,7 @@ impl Cache {

pub fn reload_atlas(
&self,
graphics_state: &crate::GraphicsState,
graphics_state: &GraphicsState,
filesystem: &impl luminol_filesystem::FileSystem,
tileset: &luminol_data::rpg::Tileset,
) -> anyhow::Result<Atlas> {
Expand Down
23 changes: 9 additions & 14 deletions crates/graphics/src/collision/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
// You should have received a copy of the GNU General Public License
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.

use std::sync::Arc;

use crate::{
viewport::{self, Viewport},
BindGroupBuilder, BindGroupLayoutBuilder,
BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState,
};

use instance::Instances;
Expand All @@ -31,6 +33,7 @@ mod vertex;
#[derive(Debug)]
pub struct Collision {
pub instances: Instances,
pub viewport: Arc<Viewport>,
pub bind_group: wgpu::BindGroup,
}

Expand Down Expand Up @@ -145,8 +148,8 @@ pub fn calculate_passage(layers: impl Iterator<Item = (i16, i16, CollisionType)>

impl Collision {
pub fn new(
graphics_state: &crate::GraphicsState,
viewport: &Viewport,
graphics_state: &GraphicsState,
viewport: Arc<Viewport>,
passages: &luminol_data::Table2,
) -> Self {
let instances = Instances::new(&graphics_state.render_state, passages);
Expand All @@ -163,6 +166,7 @@ impl Collision {

Self {
instances,
viewport,
bind_group,
}
}
Expand All @@ -178,25 +182,16 @@ impl Collision {

pub fn draw<'rpass>(
&'rpass self,
graphics_state: &'rpass crate::GraphicsState,
viewport: &crate::viewport::Viewport,
graphics_state: &'rpass GraphicsState,
render_pass: &mut wgpu::RenderPass<'rpass>,
) {
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct VertexPushConstant {
viewport: [u8; 64],
}

render_pass.push_debug_group("tilemap collision renderer");
render_pass.set_pipeline(&graphics_state.pipelines.collision);
if graphics_state.push_constants_supported() {
render_pass.set_push_constants(
wgpu::ShaderStages::VERTEX,
0,
bytemuck::bytes_of(&VertexPushConstant {
viewport: viewport.as_bytes(),
}),
&self.viewport.as_bytes(),
);
}
self.instances.draw(render_pass);
Expand Down
46 changes: 20 additions & 26 deletions crates/graphics/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,17 @@

use std::sync::Arc;

use crate::{sprite::Sprite, viewport::Viewport, GraphicsState};
use crate::{quad::Quad, sprite::Sprite, tiles::Atlas, viewport::Viewport, GraphicsState};

#[derive(Debug)]
pub struct Event {
resources: Arc<Resources>,
sprite: Arc<Sprite>,
viewport: Arc<Viewport>,
pub sprite_size: egui::Vec2,
}

#[derive(Debug)]
struct Resources {
sprite: Sprite,
viewport: Viewport,
}

struct Callback {
resources: Arc<Resources>,
sprite: Arc<Sprite>,
graphics_state: Arc<GraphicsState>,
}

Expand All @@ -51,19 +46,17 @@ impl egui_wgpu::CallbackTrait for Callback {
render_pass: &mut wgpu::RenderPass<'a>,
_callback_resources: &'a egui_wgpu::CallbackResources,
) {
self.resources
.sprite
.draw(&self.graphics_state, &self.resources.viewport, render_pass);
self.sprite.draw(&self.graphics_state, render_pass);
}
}

impl Event {
// code smell, fix
pub fn new(
graphics_state: &crate::GraphicsState,
graphics_state: &GraphicsState,
filesystem: &impl luminol_filesystem::FileSystem,
event: &luminol_data::rpg::Event,
atlas: &crate::tiles::Atlas,
atlas: &Atlas,
) -> anyhow::Result<Option<Self>> {
let Some(page) = event.pages.first() else {
anyhow::bail!("event does not have first page");
Expand All @@ -85,7 +78,7 @@ impl Event {
// Why does this have to be + 1?
let quad = atlas.calc_quad((id + 1) as i16);

let viewport = Viewport::new(graphics_state, 32., 32.);
let viewport = Arc::new(Viewport::new(graphics_state, 32., 32.));

(quad, viewport, egui::vec2(32., 32.))
} else {
Expand All @@ -107,47 +100,48 @@ impl Event {
),
egui::vec2(cw - 0.02, ch - 0.02),
);
let quad = crate::quad::Quad::new(pos, tex_coords, 0.0);
let quad = Quad::new(pos, tex_coords, 0.0);

let viewport = Viewport::new(graphics_state, cw, ch);
let viewport = Arc::new(Viewport::new(graphics_state, cw, ch));

(quad, viewport, egui::vec2(cw, ch))
};

let sprite = Sprite::new(
let sprite = Arc::new(Sprite::new(
graphics_state,
&viewport,
viewport.clone(),
quads,
texture,
page.graphic.blend_type,
page.graphic.character_hue,
page.graphic.opacity,
);
));

Ok(Some(Self {
resources: Arc::new(Resources { sprite, viewport }),
sprite,
viewport,
sprite_size,
}))
}

pub fn sprite(&self) -> &crate::sprite::Sprite {
&self.resources.sprite
pub fn sprite(&self) -> &Sprite {
&self.sprite
}

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

pub fn paint(
&self,
graphics_state: Arc<crate::GraphicsState>,
graphics_state: Arc<GraphicsState>,
painter: &egui::Painter,
rect: egui::Rect,
) {
painter.add(egui_wgpu::Callback::new_paint_callback(
rect,
Callback {
resources: self.resources.clone(),
sprite: self.sprite.clone(),
graphics_state,
},
));
Expand Down
Loading

0 comments on commit e76f3b3

Please sign in to comment.