~starkingdoms/starkingdoms

1e6a61f142fe56a016a52d218b4941f375c62a52 — ghostly_zsh 8 months ago 5f30061
starfield exists
1 files changed, 40 insertions(+), 2 deletions(-)

M crates/client/src/rendering/mod.rs
M crates/client/src/rendering/mod.rs => crates/client/src/rendering/mod.rs +40 -2
@@ 16,7 16,7 @@ use glutin::surface::{Surface, WindowSurface, GlSurface, SwapInterval};
use glutin::{config::{ConfigTemplateBuilder, GlConfig}, context::{ContextApi, ContextAttributesBuilder, PossiblyCurrentContext}, display::GetGlDisplay, prelude::{GlDisplay, NotCurrentGlContext}};
#[cfg(not(target_arch = "wasm32"))]
use glutin_winit::{DisplayBuilder, GlWindow};
use nalgebra::{Vector3, Vector4};
use nalgebra::{Matrix4, Scale3, Translation2, Translation3, Vector3, Vector4};
use starkingdoms_common::packet::{ButtonType, Packet};
use starkingdoms_common::PlanetType;
#[cfg(target_arch = "wasm32")]


@@ 415,9 415,47 @@ impl ApplicationHandler for App {
            gl.active_texture(glow::TEXTURE0);
            
            let view_loc = gl.get_uniform_location(self.program.unwrap(), "view");
            gl.uniform_matrix_4_f32_slice(view_loc.as_ref(), true, view);
            let model_loc = gl.get_uniform_location(self.program.unwrap(), "model");

            gl.uniform_matrix_4_f32_slice(view_loc.as_ref(), true, view);

            if !self.textures.contains_key("starfield.svg") {
                let assets = self.world.resource::<Assets>();
                match assets.get("starfield.svg") {
                    Some(image) => {
                        let texture_object = gl.create_texture().expect("Failed to create texture object");
                        gl.bind_texture(glow::TEXTURE_2D, Some(texture_object));
                        gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, glow::LINEAR_MIPMAP_LINEAR as i32);
                        gl.tex_image_2d(glow::TEXTURE_2D, 0, glow::RGBA as i32,
                            image.width as i32, image.height as i32, 0, glow::RGBA,
                            glow::UNSIGNED_BYTE, PixelUnpackData::Slice(Some(&image.bytes)));
                        gl.generate_mipmap(glow::TEXTURE_2D);

                        self.textures.insert("starfield.svg".to_string(), texture_object);
                    }
                    None => {}
                }
            }
            if self.textures.contains_key("starfield.svg") {
                gl.bind_texture(glow::TEXTURE_2D, self.textures.get("starfield.svg").copied());

                let camera = self.world.get_resource::<Camera>().unwrap();
                let x = -(camera.x + camera.x.signum() * 200.0)
                    + camera.x % 400.0;
                let y = -(camera.y + camera.y.signum() * 200.0)
                    + camera.y % 400.0;
                let x_range = camera.width as f32 / camera.zoom / 400.0;
                let y_range = camera.height as f32 / camera.zoom / 400.0;
                for i in ((-x_range/2.0) as i32 - 1)..=((x_range/2.0) as i32 + 1) {
                    for j in ((-y_range/2.0) as i32 - 1)..=((y_range/2.0) as i32 + 1) {
                        let model = Translation3::new(x + (i*400) as f32, y + (j*400) as f32, 0.0).to_homogeneous()
                            * Scale3::new(200.0, 200.0, 1.0).to_homogeneous();
                        gl.uniform_matrix_4_f32_slice(model_loc.as_ref(), false, model.as_slice());
                        gl.draw_elements(glow::TRIANGLES, 6, glow::UNSIGNED_INT, 0);
                    }
                }
            }

            for (transform, texture) in sprites {
                if !self.textures.contains_key(&texture.name) {
                    let assets = self.world.resource::<Assets>();