diff --git a/crates/galactica/src/game.rs b/crates/galactica/src/game.rs index a7c1e7a..effa176 100644 --- a/crates/galactica/src/game.rs +++ b/crates/galactica/src/game.rs @@ -4,12 +4,12 @@ use galactica_system::data::ShipPersonality; use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources}; use galactica_util::timing::Timing; use nalgebra::Point2; -use std::rc::Rc; +use std::sync::Arc; use std::time::Instant; pub struct Game { // Core game data - ct: Rc, + ct: Arc, phys_sim: PhysSim, timing: Timing, start_instant: Instant, @@ -44,7 +44,7 @@ impl<'a> Game { return player; } - pub fn new(ct: Rc) -> Self { + pub fn new(ct: Arc) -> Self { let mut phys_sim = PhysSim::new(&ct, SystemHandle { index: 0 }); let a = phys_sim.add_ship( diff --git a/crates/galactica/src/main.rs b/crates/galactica/src/main.rs index 9581f5d..75c64a5 100644 --- a/crates/galactica/src/main.rs +++ b/crates/galactica/src/main.rs @@ -21,7 +21,7 @@ use nalgebra::Vector2; use std::{ fs, path::{Path, PathBuf}, - rc::Rc, + sync::Arc, time::Instant, }; use winit::{ @@ -113,7 +113,7 @@ fn try_main() -> Result<()> { } // TODO: pretty error if missing (also in cli) - let content = Rc::new(Content::load_dir( + let content = Arc::new(Content::load_dir( PathBuf::from("./content"), PathBuf::from("./assets"), atlas_index, @@ -128,12 +128,12 @@ fn try_main() -> Result<()> { let mut game = game::Game::new(content.clone()); let p = game.make_player(); - let mut player = Rc::new(PlayerAgent::new(p.0)); - Rc::get_mut(&mut player).unwrap().set_camera_aspect( + let mut player = Arc::new(PlayerAgent::new(p.0)); + Arc::get_mut(&mut player).unwrap().set_camera_aspect( gpu.window().inner_size().width as f32 / gpu.window().inner_size().height as f32, ); - let mut phys_img = Rc::new(PhysImage::new()); + let mut phys_img = Arc::new(PhysImage::new()); let mut last_run = Instant::now(); event_loop.run(move |event, _, control_flow| { @@ -162,9 +162,9 @@ fn try_main() -> Result<()> { } Event::MainEventsCleared => { - game.update_player_controls(Rc::get_mut(&mut player).unwrap()); + game.update_player_controls(Arc::get_mut(&mut player).unwrap()); game.step(&phys_img); - game.update_image(Rc::get_mut(&mut phys_img).unwrap()); + game.update_image(Arc::get_mut(&mut phys_img).unwrap()); // TODO: clean up let player_status = { @@ -193,10 +193,10 @@ fn try_main() -> Result<()> { }; // This must be updated BEFORE rendering! - Rc::get_mut(&mut player) + Arc::get_mut(&mut player) .unwrap() .step(&content, player_status); - Rc::get_mut(&mut player).unwrap().input.clear_inputs(); + Arc::get_mut(&mut player).unwrap().input.clear_inputs(); gpu.window().request_redraw(); } @@ -217,39 +217,39 @@ fn try_main() -> Result<()> { }, .. } => { - Rc::get_mut(&mut player) + Arc::get_mut(&mut player) .unwrap() .input .process_key(state, key); } WindowEvent::CursorMoved { position, .. } => { - Rc::get_mut(&mut player) + Arc::get_mut(&mut player) .unwrap() .input .process_mouse(position); } WindowEvent::MouseInput { state, button, .. } => { - Rc::get_mut(&mut player) + Arc::get_mut(&mut player) .unwrap() .input .process_click(state, button); } WindowEvent::MouseWheel { delta, phase, .. } => { - Rc::get_mut(&mut player) + Arc::get_mut(&mut player) .unwrap() .input .process_scroll(delta, phase); } WindowEvent::Resized(_) => { gpu.resize(&content); - Rc::get_mut(&mut player).unwrap().set_camera_aspect( + Arc::get_mut(&mut player).unwrap().set_camera_aspect( gpu.window().inner_size().width as f32 / gpu.window().inner_size().height as f32, ); } WindowEvent::ScaleFactorChanged { .. } => { gpu.resize(&content); - Rc::get_mut(&mut player).unwrap().set_camera_aspect( + Arc::get_mut(&mut player).unwrap().set_camera_aspect( gpu.window().inner_size().width as f32 / gpu.window().inner_size().height as f32, ); diff --git a/crates/render/src/gpustate.rs b/crates/render/src/gpustate.rs index f7b30d0..950fa4a 100644 --- a/crates/render/src/gpustate.rs +++ b/crates/render/src/gpustate.rs @@ -1,5 +1,3 @@ -use std::{iter, rc::Rc}; - use anyhow::Result; use bytemuck; use galactica_content::Content; @@ -7,6 +5,7 @@ use galactica_system::data::ShipState; use galactica_util::to_radians; use glyphon::{FontSystem, Resolution, SwashCache, TextAtlas, TextRenderer}; use nalgebra::{Point2, Point3}; +use std::{iter, sync::Arc}; use wgpu; use winit; @@ -38,7 +37,7 @@ pub struct GPUState { impl GPUState { /// Make a new GPUState that draws on `window` - pub async fn new(window: winit::window::Window, ct: Rc) -> Result { + pub async fn new(window: winit::window::Window, ct: Arc) -> Result { let window_size = window.inner_size(); let window_aspect = window_size.width as f32 / window_size.height as f32; @@ -278,7 +277,7 @@ impl GPUState { /// Main render function. Draws sprites on a window. pub fn render(&mut self, input: RenderInput) -> Result<(), wgpu::SurfaceError> { - let input = Rc::new(input); + let input = Arc::new(input); // Update global values self.state.queue.write_buffer( diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index b2dfc11..23d0b2f 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -1,4 +1,3 @@ -use std::rc::Rc; use wgpu; use crate::vertexbuffer::VertexBuffer; @@ -16,7 +15,7 @@ pub struct PipelineBuilder<'a> { // These must be provided shader: Option<&'a str>, format: Option, - vertex_buffer: Option<&'a Rc>, + vertex_buffer: Option<&'a VertexBuffer>, } impl<'a> PipelineBuilder<'a> { @@ -49,7 +48,7 @@ impl<'a> PipelineBuilder<'a> { self } - pub fn set_vertex_buffer(mut self, vertex_buffer: &'a Rc) -> Self { + pub fn set_vertex_buffer(mut self, vertex_buffer: &'a VertexBuffer) -> Self { self.vertex_buffer = Some(vertex_buffer); self } diff --git a/crates/render/src/renderinput.rs b/crates/render/src/renderinput.rs index 19d9db6..f19e04a 100644 --- a/crates/render/src/renderinput.rs +++ b/crates/render/src/renderinput.rs @@ -1,4 +1,4 @@ -use std::rc::Rc; +use std::sync::Arc; use galactica_content::{Content, SystemHandle}; use galactica_playeragent::PlayerAgent; @@ -13,7 +13,7 @@ pub struct RenderInput { pub camera_pos: Vector2, /// Player ship data - pub player: Rc, + pub player: Arc, /// The system we're currently in pub current_system: SystemHandle, @@ -22,7 +22,7 @@ pub struct RenderInput { pub camera_zoom: f32, /// The world state to render - pub phys_img: Rc, + pub phys_img: Arc, // TODO: handle overflow. is it a problem? /// The current time, in seconds @@ -32,7 +32,7 @@ pub struct RenderInput { pub time_since_last_run: f32, /// Game content - pub ct: Rc, + pub ct: Arc, /// Time we spent in each part of the game loop pub timing: Timing, diff --git a/crates/render/src/renderstate.rs b/crates/render/src/renderstate.rs index ce63e0d..1cced31 100644 --- a/crates/render/src/renderstate.rs +++ b/crates/render/src/renderstate.rs @@ -3,7 +3,6 @@ use galactica_util::constants::{ OBJECT_SPRITE_INSTANCE_LIMIT, RADIALBAR_SPRITE_INSTANCE_LIMIT, UI_SPRITE_INSTANCE_LIMIT, }; use glyphon::{FontSystem, SwashCache, TextAtlas, TextRenderer}; -use std::rc::Rc; use wgpu::BufferAddress; use winit::window::Window; @@ -26,10 +25,10 @@ pub(crate) struct VertexBuffers { starfield_counter: BufferAddress, starfield_limit: BufferAddress, - object: Rc, - starfield: Rc, - ui: Rc, - radialbar: Rc, + object: VertexBuffer, + starfield: VertexBuffer, + ui: VertexBuffer, + radialbar: VertexBuffer, } impl<'a> VertexBuffers { @@ -41,53 +40,53 @@ impl<'a> VertexBuffers { starfield_counter: 0, starfield_limit: ct.get_config().starfield_instance_limit, - object: Rc::new(VertexBuffer::new::( + object: VertexBuffer::new::( "object", &device, Some(SPRITE_VERTICES), Some(SPRITE_INDICES), OBJECT_SPRITE_INSTANCE_LIMIT, - )), + ), - starfield: Rc::new(VertexBuffer::new::( + starfield: VertexBuffer::new::( "starfield", &device, Some(SPRITE_VERTICES), Some(SPRITE_INDICES), ct.get_config().starfield_instance_limit, - )), + ), - ui: Rc::new(VertexBuffer::new::( + ui: VertexBuffer::new::( "ui", &device, Some(SPRITE_VERTICES), Some(SPRITE_INDICES), UI_SPRITE_INSTANCE_LIMIT, - )), + ), - radialbar: Rc::new(VertexBuffer::new::( + radialbar: VertexBuffer::new::( "radial bar", &device, Some(SPRITE_VERTICES), Some(SPRITE_INDICES), 10, - )), + ), } } - pub fn get_ui(&'a self) -> &'a Rc { + pub fn get_ui(&'a self) -> &'a VertexBuffer { &self.ui } - pub fn get_object(&'a self) -> &'a Rc { + pub fn get_object(&'a self) -> &'a VertexBuffer { &self.object } - pub fn get_radialbar(&'a self) -> &'a Rc { + pub fn get_radialbar(&'a self) -> &'a VertexBuffer { &self.radialbar } - pub fn get_starfield(&'a self) -> &'a Rc { + pub fn get_starfield(&'a self) -> &'a VertexBuffer { &self.starfield } } diff --git a/crates/render/src/ui/api/radialelement.rs b/crates/render/src/ui/api/radialelement.rs index 21d2f66..336e9a4 100644 --- a/crates/render/src/ui/api/radialelement.rs +++ b/crates/render/src/ui/api/radialelement.rs @@ -1,13 +1,13 @@ use galactica_content::Content; use rhai::{CustomType, TypeBuilder}; -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, rc::Rc, sync::Arc}; use crate::ui::util::RadialBar; #[derive(Debug, Clone)] pub struct RadialElement { pub bar: Rc>, - pub ct: Rc, + pub ct: Arc, } // TODO: remove this @@ -15,7 +15,7 @@ unsafe impl Send for RadialElement {} unsafe impl Sync for RadialElement {} impl RadialElement { - pub fn new(ct: Rc, bar: Rc>) -> Self { + pub fn new(ct: Arc, bar: Rc>) -> Self { Self { ct, bar } } } diff --git a/crates/render/src/ui/api/spriteelement.rs b/crates/render/src/ui/api/spriteelement.rs index 63c1213..d0ef235 100644 --- a/crates/render/src/ui/api/spriteelement.rs +++ b/crates/render/src/ui/api/spriteelement.rs @@ -1,14 +1,14 @@ use galactica_content::{resolve_edge_as_edge, Content}; use log::error; use rhai::{CustomType, TypeBuilder}; -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, rc::Rc, sync::Arc}; use crate::ui::util::Sprite; #[derive(Debug, Clone)] pub struct SpriteElement { pub sprite: Rc>, - pub ct: Rc, + pub ct: Arc, } // TODO: remove this @@ -16,7 +16,7 @@ unsafe impl Send for SpriteElement {} unsafe impl Sync for SpriteElement {} impl SpriteElement { - pub fn new(ct: Rc, sprite: Rc>) -> Self { + pub fn new(ct: Arc, sprite: Rc>) -> Self { Self { ct, sprite } } diff --git a/crates/render/src/ui/api/state.rs b/crates/render/src/ui/api/state.rs index a8ac112..cb3ef9e 100644 --- a/crates/render/src/ui/api/state.rs +++ b/crates/render/src/ui/api/state.rs @@ -5,14 +5,14 @@ use galactica_system::{ }; use log::error; use rhai::{CustomType, TypeBuilder}; -use std::rc::Rc; +use std::sync::Arc; use crate::RenderInput; #[derive(Debug, Clone)] pub struct ShipState { ship: Option, - input: Rc, + input: Arc, } // TODO: remove this @@ -94,7 +94,7 @@ impl CustomType for ShipState { #[derive(Debug, Clone)] pub struct SystemObjectState { object: Option, - input: Rc, + input: Arc, } // TODO: remove this @@ -152,7 +152,7 @@ impl CustomType for SystemObjectState { #[derive(Debug, Clone)] pub struct State { - input: Rc, + input: Arc, } // TODO: remove this @@ -160,7 +160,7 @@ unsafe impl Send for State {} unsafe impl Sync for State {} impl State { - pub fn new(input: Rc) -> Self { + pub fn new(input: Arc) -> Self { Self { input } } diff --git a/crates/render/src/ui/manager.rs b/crates/render/src/ui/manager.rs index c7405fe..8c8cb8c 100644 --- a/crates/render/src/ui/manager.rs +++ b/crates/render/src/ui/manager.rs @@ -4,7 +4,7 @@ use galactica_system::phys::PhysSimShipHandle; use glyphon::TextArea; use log::{debug, error, trace}; use rhai::{Array, Dynamic, Engine, Map, Scope}; -use std::{collections::HashSet, num::NonZeroU32, rc::Rc}; +use std::{collections::HashSet, num::NonZeroU32, sync::Arc}; use super::{ api::{ @@ -28,7 +28,7 @@ pub(crate) struct UiManager { current_scene_config: SceneConfig, engine: Engine, scope: Scope<'static>, - ct: Rc, + ct: Arc, /// UI elements elements: Vec, @@ -42,7 +42,7 @@ pub(crate) struct UiManager { } impl UiManager { - pub fn new(ct: Rc, state: &mut RenderState) -> Self { + pub fn new(ct: Arc, state: &mut RenderState) -> Self { let scope = Scope::new(); let mut engine = Engine::new_raw(); @@ -70,7 +70,7 @@ impl UiManager { pub fn set_scene( &mut self, state: &mut RenderState, - input: Rc, + input: Arc, scene: String, ) -> Result<()> { if !self.ct.get_config().ui_scenes.contains_key(&scene) { @@ -207,7 +207,7 @@ impl UiManager { } /// Draw all ui elements - pub fn draw(&mut self, input: Rc, state: &mut RenderState) -> Result<()> { + pub fn draw(&mut self, input: Arc, state: &mut RenderState) -> Result<()> { // Initialize start scene if we haven't yet if self.current_scene.is_none() { self.set_scene( @@ -372,7 +372,7 @@ impl UiManager { fn handle_action( &mut self, state: &mut RenderState, - input: Rc, + input: Arc, action: SceneAction, ) -> Result { Ok(match action { diff --git a/crates/system/src/phys/stepresources.rs b/crates/system/src/phys/stepresources.rs index abf3e7e..d8f2b4f 100644 --- a/crates/system/src/phys/stepresources.rs +++ b/crates/system/src/phys/stepresources.rs @@ -1,4 +1,4 @@ -use std::rc::Rc; +use std::sync::Arc; use galactica_content::Content; use galactica_util::timing::Timing; @@ -6,7 +6,7 @@ use galactica_util::timing::Timing; /// External resources we need to compute time steps pub struct PhysStepResources<'a> { /// Game content - pub ct: Rc, + pub ct: Arc, /// Length of time step pub t: f32,