Skip to content

Commit

Permalink
Replace cgmath::Vector with custom types in interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed Feb 28, 2024
1 parent f3f30b2 commit 56cc79d
Show file tree
Hide file tree
Showing 111 changed files with 1,388 additions and 853 deletions.
2 changes: 1 addition & 1 deletion procedural/src/prototype/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn derive_prototype_window_struct(
#window_class_ref_option
}

fn to_window(&self, window_cache: &crate::interface::WindowCache, interface_settings: &crate::interface::InterfaceSettings, available_space: crate::interface::Size) -> crate::interface::Window {
fn to_window(&self, window_cache: &crate::interface::WindowCache, interface_settings: &crate::interface::InterfaceSettings, available_space: crate::interface::ScreenSize) -> crate::interface::Window {
let scroll_view = crate::interface::ScrollView::new(vec![#(#initializers),*], constraint!(100%, ?));
let elements: Vec<crate::interface::ElementCell> = vec![std::rc::Rc::new(std::cell::RefCell::new(scroll_view))];

Expand Down
13 changes: 10 additions & 3 deletions src/graphics/cameras/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, Ra

use super::Camera;
use crate::graphics::Transform;
use crate::interface::{ScreenPosition, ScreenSize};

const LOOK_AROUND_SPEED: f32 = 0.005;
const FLY_SPEED_FAST: f32 = 1000.0;
Expand Down Expand Up @@ -165,12 +166,18 @@ impl Camera for DebugCamera {
(top_left_position, bottom_right_position)
}

fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (Vector2<f32>, Vector2<f32>) {
fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (ScreenPosition, ScreenSize) {
let top_left_position = self.clip_to_screen_space(top_left_position);
let bottom_right_position = self.clip_to_screen_space(bottom_right_position);

let screen_position = top_left_position;
let screen_size = bottom_right_position - top_left_position;
let screen_position = ScreenPosition {
left: top_left_position.x,
top: top_left_position.y,
};
let screen_size = ScreenSize {
width: bottom_right_position.x - top_left_position.x,
height: bottom_right_position.y - top_left_position.y,
};

(screen_position, screen_size)
}
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/cameras/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use self::player::PlayerCamera;
pub use self::shadow::ShadowCamera;
pub use self::start::StartCamera;
use crate::graphics::{SmoothedValue, Transform};
use crate::interface::{ScreenPosition, ScreenSize};

fn direction(vector: Vector2<f32>) -> usize {
let inverted = false;
Expand All @@ -34,7 +35,7 @@ pub trait Camera {

fn billboard_coordinates(&self, position: Vector3<f32>, size: f32) -> (Vector4<f32>, Vector4<f32>);

fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (Vector2<f32>, Vector2<f32>);
fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (ScreenPosition, ScreenSize);

fn distance_to(&self, position: Vector3<f32>) -> f32;

Expand Down
13 changes: 10 additions & 3 deletions src/graphics/cameras/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, Ra

use super::{Camera, SmoothedValue};
use crate::graphics::Transform;
use crate::interface::{ScreenPosition, ScreenSize};

const ZOOM_SPEED: f32 = 2.0;
const ROTATION_SPEED: f32 = 0.02;
Expand Down Expand Up @@ -162,12 +163,18 @@ impl Camera for PlayerCamera {
(top_left_position, bottom_right_position)
}

fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (Vector2<f32>, Vector2<f32>) {
fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (ScreenPosition, ScreenSize) {
let top_left_position = self.clip_to_screen_space(top_left_position);
let bottom_right_position = self.clip_to_screen_space(bottom_right_position);

let screen_position = top_left_position;
let screen_size = bottom_right_position - top_left_position;
let screen_position = ScreenPosition {
left: top_left_position.x,
top: top_left_position.y,
};
let screen_size = ScreenSize {
width: bottom_right_position.x - top_left_position.x,
height: bottom_right_position.y - top_left_position.y,
};

(screen_position, screen_size)
}
Expand Down
13 changes: 10 additions & 3 deletions src/graphics/cameras/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, Sq

use super::Camera;
use crate::graphics::Transform;
use crate::interface::{ScreenPosition, ScreenSize};

pub struct ShadowCamera {
focus_point: Point3<f32>,
Expand Down Expand Up @@ -120,12 +121,18 @@ impl Camera for ShadowCamera {
(top_left_position, bottom_right_position)
}

fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (Vector2<f32>, Vector2<f32>) {
fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (ScreenPosition, ScreenSize) {
let top_left_position = self.clip_to_screen_space(top_left_position);
let bottom_right_position = self.clip_to_screen_space(bottom_right_position);

let screen_position = top_left_position;
let screen_size = bottom_right_position - top_left_position;
let screen_position = ScreenPosition {
left: top_left_position.x,
top: top_left_position.y,
};
let screen_size = ScreenSize {
width: bottom_right_position.x - top_left_position.x,
height: bottom_right_position.y - top_left_position.y,
};

(screen_position, screen_size)
}
Expand Down
13 changes: 10 additions & 3 deletions src/graphics/cameras/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, Ra

use super::Camera;
use crate::graphics::Transform;
use crate::interface::{ScreenPosition, ScreenSize};

const DEFAULT_ZOOM: f32 = 150.0;
const ROTATION_SPEED: f32 = 0.03;
Expand Down Expand Up @@ -132,12 +133,18 @@ impl Camera for StartCamera {
(top_left_position, bottom_right_position)
}

fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (Vector2<f32>, Vector2<f32>) {
fn screen_position_size(&self, top_left_position: Vector4<f32>, bottom_right_position: Vector4<f32>) -> (ScreenPosition, ScreenSize) {
let top_left_position = self.clip_to_screen_space(top_left_position);
let bottom_right_position = self.clip_to_screen_space(bottom_right_position);

let screen_position = top_left_position;
let screen_size = bottom_right_position - top_left_position;
let screen_position = ScreenPosition {
left: top_left_position.x,
top: top_left_position.y,
};
let screen_size = ScreenSize {
width: bottom_right_position.x - top_left_position.x,
height: bottom_right_position.y - top_left_position.y,
};

(screen_position, screen_size)
}
Expand Down
12 changes: 12 additions & 0 deletions src/graphics/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ impl Color {
}
}

impl From<Color> for [f32; 3] {
fn from(val: Color) -> Self {
[val.red_f32(), val.green_f32(), val.blue_f32()]
}
}

impl From<Color> for [f32; 4] {
fn from(val: Color) -> Self {
[val.red_f32(), val.green_f32(), val.blue_f32(), val.alpha_f32()]
}
}

#[allow(dead_code)]
#[derive(Clone, Debug, Named, ByteConvertable, PrototypeElement)]
pub struct ColorBGRA {
Expand Down
34 changes: 22 additions & 12 deletions src/graphics/particles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::collections::HashMap;

use cgmath::{Array, Vector2, Vector3, Vector4, Zero};
use cgmath::{Vector2, Vector3};
use derive_new::new;
use procedural::profile;
use rand::{thread_rng, Rng};

use crate::graphics::*;
use crate::interface::{ScreenClip, ScreenPosition, ScreenSize};
use crate::loaders::{GameFileLoader, TextureLoader};
use crate::network::{EntityId, QuestColor, QuestEffectPacket};
use crate::world::*;
Expand All @@ -18,7 +19,7 @@ pub trait Particle {
render_target: &mut <DeferredRenderer as Renderer>::Target,
renderer: &DeferredRenderer,
camera: &dyn Camera,
window_size: Vector2<f32>,
window_size: ScreenSize,
);
}

Expand Down Expand Up @@ -53,7 +54,7 @@ impl Particle for DamageNumber {
render_target: &mut <DeferredRenderer as Renderer>::Target,
renderer: &DeferredRenderer,
camera: &dyn Camera,
window_size: Vector2<f32>,
window_size: ScreenSize,
) {
let (view_matrix, projection_matrix) = camera.view_projection_matrices();
let clip_space_position = (projection_matrix * view_matrix) * self.position.extend(1.0);
Expand All @@ -62,7 +63,10 @@ impl Particle for DamageNumber {
clip_space_position.y / clip_space_position.w + 1.0,
);
let screen_position = screen_position / 2.0;
let final_position = Vector2::new(screen_position.x * window_size.x, screen_position.y * window_size.y);
let final_position = ScreenPosition {
left: screen_position.x * window_size.width,
top: screen_position.y * window_size.height,
};

renderer.render_damage_text(render_target, &self.damage_amount, final_position, Color::monochrome(255), 16.0);
}
Expand Down Expand Up @@ -93,7 +97,7 @@ impl Particle for HealNumber {
render_target: &mut <DeferredRenderer as Renderer>::Target,
renderer: &DeferredRenderer,
camera: &dyn Camera,
window_size: Vector2<f32>,
window_size: ScreenSize,
) {
let (view_matrix, projection_matrix) = camera.view_projection_matrices();
let clip_space_position = (projection_matrix * view_matrix) * self.position.extend(1.0);
Expand All @@ -102,7 +106,10 @@ impl Particle for HealNumber {
clip_space_position.y / clip_space_position.w + 1.0,
);
let screen_position = screen_position / 2.0;
let final_position = Vector2::new(screen_position.x * window_size.x, screen_position.y * window_size.y);
let final_position = ScreenPosition {
left: screen_position.x * window_size.width,
top: screen_position.y * window_size.height,
};

renderer.render_damage_text(render_target, &self.heal_amount, final_position, Color::rgb(30, 255, 30), 16.0);
}
Expand Down Expand Up @@ -144,7 +151,7 @@ impl QuestIcon {
render_target: &mut <DeferredRenderer as Renderer>::Target,
renderer: &DeferredRenderer,
camera: &dyn Camera,
window_size: Vector2<f32>,
window_size: ScreenSize,
) {
let (view_matrix, projection_matrix) = camera.view_projection_matrices();
let clip_space_position = (projection_matrix * view_matrix) * self.position.extend(1.0);
Expand All @@ -153,14 +160,17 @@ impl QuestIcon {
clip_space_position.y / clip_space_position.w + 1.0,
);
let screen_position = screen_position / 2.0;
let final_position = Vector2::new(screen_position.x * window_size.x, screen_position.y * window_size.y);
let final_position = ScreenPosition {
left: screen_position.x * window_size.width,
top: screen_position.y * window_size.height,
};

renderer.render_sprite(
render_target,
self.texture.clone(),
final_position - Vector2::from_value(15.0),
Vector2::from_value(30.0),
Vector4::zero(),
final_position - ScreenSize::uniform(15.0),
ScreenSize::uniform(30.0),
ScreenClip::default(),
self.color,
true,
);
Expand Down Expand Up @@ -211,7 +221,7 @@ impl ParticleHolder {
render_target: &mut <DeferredRenderer as Renderer>::Target,
renderer: &DeferredRenderer,
camera: &dyn Camera,
window_size: Vector2<f32>,
window_size: ScreenSize,
entities: &[Entity],
) {
self.particles
Expand Down
4 changes: 1 addition & 3 deletions src/graphics/renderers/deferred/ambient/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ impl AmbientLightRenderer {
WriteDescriptorSet::image_view(1, render_target.normal_image.clone()),
]);

let constants = Constants {
color: [color.red_f32(), color.green_f32(), color.blue_f32()],
};
let constants = Constants { color: color.into() };

render_target
.state
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/renderers/deferred/box/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl BoxRenderer {

let constants = Constants {
world: world_matrix.into(),
color: [color.red_f32(), color.green_f32(), color.blue_f32()],
color: color.into(),
};

render_target
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/renderers/deferred/directional/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl DirectionalLightRenderer {
]);

let constants = Constants {
direction: Padded([direction.x, direction.y, direction.z]),
direction: Padded(direction.into()),
color: [
color.red_f32() * intensity,
color.green_f32() * intensity,
Expand Down
7 changes: 4 additions & 3 deletions src/graphics/renderers/deferred/effect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::DeferredSubrenderer;
use crate::graphics::renderers::pipeline::PipelineBuilder;
use crate::graphics::renderers::sampler::{create_new_sampler, SamplerType};
use crate::graphics::*;
use crate::interface::ScreenSize;

pub struct EffectRenderer {
memory_allocator: Arc<MemoryAllocator>,
Expand Down Expand Up @@ -76,7 +77,7 @@ impl EffectRenderer {
&self,
render_target: &mut <DeferredRenderer as Renderer>::Target,
texture: Arc<ImageView>,
window_size: Vector2<usize>,
window_size: ScreenSize,
mut screen_positions: [Vector2<f32>; 4],
texture_coordinates: [Vector2<f32>; 4],
screen_space_position: Vector2<f32>,
Expand All @@ -90,7 +91,7 @@ impl EffectRenderer {
self.bind_pipeline(render_target);
}

let half_screen = Vector2::new(window_size.x as f32 / 2.0, window_size.y as f32 / 2.0);
let half_screen = Vector2::new(window_size.width / 2.0, window_size.height / 2.0);

// TODO: move this calculation to the loading
let rotation_matrix = Matrix2::from_angle(cgmath::Deg(angle / (1024.0 / 360.0)));
Expand Down Expand Up @@ -122,7 +123,7 @@ impl EffectRenderer {
t_bottom_left: texture_coordinates[3],
t_top_right: texture_coordinates[1],
t_bottom_right: texture_coordinates[0],
color: [color.red_f32(), color.green_f32(), color.blue_f32(), color.alpha_f32()],
color: color.into(),
};

render_target
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/renderers/deferred/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ impl EntityRenderer {

let constants = Constants {
world: world_matrix.into(),
texture_position: [texture_position.x, texture_position.y],
texture_size: [texture_size.x, texture_size.y],
texture_position: texture_position.into(),
texture_size: texture_size.into(),
depth_offset,
curvature,
mirror: mirror as u32,
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/renderers/deferred/indicator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl IndicatorRenderer {
]);

let constants = Constants {
color: Padded([color.red_f32(), color.green_f32(), color.blue_f32()]),
color: Padded(color.into()),
upper_left: Padded(upper_left.into()),
upper_right: Padded(upper_right.into()),
lower_left: Padded(lower_left.into()),
Expand Down
Loading

0 comments on commit 56cc79d

Please sign in to comment.