Implement data path
authorunc0rr
Sat, 12 Nov 2022 15:40:20 +0100
changeset 15890 8a6a2d931bae
parent 15889 eb52cf05e454
child 15891 d52f5d8e75e6
Implement data path
rust/hedgewars-engine/src/main.rs
rust/lib-hedgewars-engine/src/render/gear.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/hedgewars-engine/src/main.rs	Sat Nov 05 17:48:45 2022 +0100
+++ b/rust/hedgewars-engine/src/main.rs	Sat Nov 12 15:40:20 2022 +0100
@@ -112,7 +112,7 @@
 
         let engine_state = &mut *engine.start_engine.deref()();
 
-        let port: String = matches.opt_str("port").unwrap();
+        let port: String = matches.opt_str("port").expect("Need IPC port number!");
 
         println!("PORT: {}", port);
 
--- a/rust/lib-hedgewars-engine/src/render/gear.rs	Sat Nov 05 17:48:45 2022 +0100
+++ b/rust/lib-hedgewars-engine/src/render/gear.rs	Sat Nov 12 15:40:20 2022 +0100
@@ -73,19 +73,19 @@
 const SPRITE_LOAD_LIST: &[(SpriteId, &str)] = &[
     (
         SpriteId::Mine,
-        "../../share/hedgewars/Data/Graphics/MineOn.png",
+        "Graphics/MineOn.png",
     ),
     (
         SpriteId::Grenade,
-        "../../share/hedgewars/Data/Graphics/Bomb.png",
+        "Graphics/Bomb.png",
     ),
     (
         SpriteId::Cheese,
-        "../../share/hedgewars/Data/Graphics/cheese.png",
+        "Graphics/cheese.png",
     ),
     (
         SpriteId::Cleaver,
-        "../../share/hedgewars/Data/Graphics/cleaver.png",
+        "Graphics/cleaver.png",
     ),
 ];
 
@@ -124,7 +124,7 @@
 const ATLAS_SIZE: Size = Size::square(2048);
 
 impl GearRenderer {
-    pub fn new() -> Self {
+    pub fn new(data_path: &Path) -> Self {
         let mut atlas = AtlasCollection::new(ATLAS_SIZE);
 
         let texture = Texture2D::new(
@@ -136,15 +136,15 @@
         let mut allocation = Box::new([Default::default(); MAX_SPRITES]);
 
         for (sprite, file) in SPRITE_LOAD_LIST {
-            let path = Path::new(file);
-            let size = load_sprite_size(path).expect(&format!("Unable to open {}", file));
+            let path = data_path.join(Path::new(file));
+            let size = load_sprite_size(path.as_path()).expect(&format!("Unable to open {}", file));
             let index = atlas
                 .insert_sprite(size)
                 .expect(&format!("Could not store sprite {:?}", sprite));
             let (texture_index, rect) = atlas.get_rect(index).unwrap();
 
             let mut pixels = vec![255u8; size.area() * 4].into_boxed_slice();
-            load_sprite_pixels(path, &mut pixels).expect("Unable to load Graphics");
+            load_sprite_pixels(path.as_path(), &mut pixels).expect("Unable to load Graphics");
 
             texture.update(
                 rect,
@@ -262,7 +262,6 @@
         let _buffer_bind = self.layout.bind(&[(0, &self.vertex_buffer)], None);
 
         let _state = PipelineState::new().with_blend();
-
         unsafe {
             gl::DrawArrays(gl::TRIANGLES, 0, entries.len() as i32 * 6);
         }
--- a/rust/lib-hedgewars-engine/src/world.rs	Sat Nov 05 17:48:45 2022 +0100
+++ b/rust/lib-hedgewars-engine/src/world.rs	Sat Nov 12 15:40:20 2022 +0100
@@ -11,6 +11,7 @@
     LandGenerationParameters, LandGenerator,
 };
 use lfprng::LaggedFibonacciPRNG;
+use std::path::{Path, PathBuf};
 
 use crate::render::{camera::Camera, GearEntry, GearRenderer, MapRenderer};
 
@@ -34,6 +35,7 @@
     gear_renderer: Option<GearRenderer>,
     camera: Camera,
     gear_entries: Vec<GearEntry>,
+    data_path: PathBuf,
 }
 
 impl World {
@@ -47,23 +49,23 @@
             gear_renderer: None,
             camera: Camera::new(),
             gear_entries: vec![],
+            data_path: PathBuf::from("../../share/hedgewars/Data"),
         }
     }
 
     pub fn create_renderer(&mut self, width: u16, height: u16) {
         let land_tile_size = Size::square(512);
         self.map_renderer = Some(MapRenderer::new(land_tile_size));
-        self.gear_renderer = Some(GearRenderer::new());
+        self.gear_renderer = Some(GearRenderer::new(&self.data_path.as_path()));
         self.camera = Camera::with_size(Size::new(width as usize, height as usize));
 
         use mapgen::{theme::Theme, MapGenerator};
-        use std::path::Path;
 
         if let Some(ref state) = self.game_state {
             self.camera.position = state.land.play_box().center();
 
             let theme =
-                Theme::load(Path::new("../../share/hedgewars/Data/Themes/Cheese/")).unwrap();
+                Theme::load(self.data_path.join(Path::new("Themes/Cheese/")).as_path()).unwrap();
             let texture = MapGenerator::new().make_texture(&state.land, &theme);
             if let Some(ref mut renderer) = self.map_renderer {
                 renderer.init(&texture);