From bfbcfa2678c70d661cb9104b2ef677d7d7b5a637 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 4 Dec 2022 17:12:59 +0200 Subject: [PATCH] Convert components to buffered where appropriate --- examples/bassteroids/source/damagedealer.cpp | 3 ++ examples/bassteroids/source/hitpoints.cpp | 12 ++++--- examples/bassteroids/source/hitpoints.h | 10 ++++-- examples/bassteroids/source/physics.cpp | 4 +++ .../bassteroids/source/playercontroller.cpp | 5 ++- examples/bassteroids/source/rigidbody.cpp | 20 ++++++----- examples/bassteroids/source/rigidbody.h | 22 +++++++----- source/game/camera.cpp | 30 +++++++++------- source/game/camera.h | 34 ++++++++++++------- source/game/transform.cpp | 19 ++++++----- source/game/transform.h | 16 +++++---- source/game/transformpropagator.cpp | 2 ++ source/gameview/renderer.cpp | 4 +++ 13 files changed, 112 insertions(+), 69 deletions(-) diff --git a/examples/bassteroids/source/damagedealer.cpp b/examples/bassteroids/source/damagedealer.cpp index 37be786..7c8e873 100644 --- a/examples/bassteroids/source/damagedealer.cpp +++ b/examples/bassteroids/source/damagedealer.cpp @@ -9,6 +9,9 @@ DamageDealer::DamageDealer(Game::Stage &s): System(s), observer(stage.get_event_bus()) { + declare_dependency(UPDATE); + declare_dependency(READ_OLD); + observer.observe([this](auto &e){ collision(e); }); } diff --git a/examples/bassteroids/source/hitpoints.cpp b/examples/bassteroids/source/hitpoints.cpp index ccd0441..c7ece0d 100644 --- a/examples/bassteroids/source/hitpoints.cpp +++ b/examples/bassteroids/source/hitpoints.cpp @@ -3,15 +3,17 @@ using namespace Msp; HitPoints::HitPoints(Game::Handle e, const Setup &s): - Component(e), - setup(s), - remaining_hits(setup.max_hits) -{ } + BufferedComponent(e), + setup(s) +{ + write().remaining_hits = setup.max_hits; +} void HitPoints::take_damage(unsigned amount, unsigned type) { if(!(setup.vulnerable_to&(1< { public: using Setup = HitPointsSetup; private: const Setup &setup; - unsigned remaining_hits; public: HitPoints(Msp::Game::Handle, const Setup &); void take_damage(unsigned, unsigned); - bool is_alive() const { return remaining_hits; } + bool is_alive() const { return read().remaining_hits; } }; #endif diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index 73d8582..158b745 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -11,6 +11,10 @@ Physics::Physics(Game::Stage &s): event_source(stage.get_event_bus()), observer(stage.get_event_bus()) { + declare_dependency(UPDATE); + declare_dependency(UPDATE); + declare_dependency(READ_OLD); + observer.observe([this](auto &e){ entity_added(e); }); observer.observe([this](auto &e){ entity_removed(e); }); diff --git a/examples/bassteroids/source/playercontroller.cpp b/examples/bassteroids/source/playercontroller.cpp index cd0c62b..b90992e 100644 --- a/examples/bassteroids/source/playercontroller.cpp +++ b/examples/bassteroids/source/playercontroller.cpp @@ -14,7 +14,10 @@ PlayerController::PlayerController(Game::Stage &s): bullet_setup{ .physical={ .body={ .mass=0.05f, .moment_of_inertia=0.04f }, .collider={ .type=ColliderType::CIRCLE, .radius=0.2f }}, .hittable={ .damaging=true, .hits={ .max_hits=1 }, .damage={ .amount=1, .type=0 }}, .mesh={ .object_name="Quaver.object" }} -{ } +{ + declare_dependency(READ_OLD); + declare_dependency(UPDATE); +} void PlayerController::set_controls(Controls *c) { diff --git a/examples/bassteroids/source/rigidbody.cpp b/examples/bassteroids/source/rigidbody.cpp index 391707a..d677fb7 100644 --- a/examples/bassteroids/source/rigidbody.cpp +++ b/examples/bassteroids/source/rigidbody.cpp @@ -5,39 +5,41 @@ using namespace Msp; RigidBody::RigidBody(Game::Handle e, const Setup &s): - Component(e), + BufferedComponent(e), setup(s) { } void RigidBody::set_velocity(const LinAl::Vector &v) { - velocity = v; + write().velocity = v; } void RigidBody::set_angular_velocity(Geometry::Angle as) { - angular_velocity = as; + write().angular_velocity = as; } void RigidBody::add_force(const LinAl::Vector &f) { - force += f; + write().force += f; } void RigidBody::add_force(const LinAl::Vector &f, const LinAl::Vector &p) { - force += f; + Data &d = write(); + d.force += f; LinAl::Vector r = p-entity->get_transform()->get_position().slice<2>(0); - torque += r.x*f.y-r.y*f.x; + d.torque += r.x*f.y-r.y*f.x; } void RigidBody::add_torque(float t) { - torque += t; + write().torque += t; } void RigidBody::clear_forces() { - force = LinAl::Vector(); - torque = 0.0f; + Data &d = write(); + d.force = LinAl::Vector(); + d.torque = 0.0f; } diff --git a/examples/bassteroids/source/rigidbody.h b/examples/bassteroids/source/rigidbody.h index aaffdc0..b75d034 100644 --- a/examples/bassteroids/source/rigidbody.h +++ b/examples/bassteroids/source/rigidbody.h @@ -11,17 +11,21 @@ struct RigidBodySetup float moment_of_inertia = 0.5f; }; -class RigidBody: public Msp::Game::Component +struct RigidBodyData +{ + Msp::LinAl::Vector velocity; + Msp::Geometry::Angle angular_velocity; + Msp::LinAl::Vector force; + float torque = 0.0f; +}; + +class RigidBody: public Msp::Game::BufferedComponent { public: using Setup = RigidBodySetup; private: const Setup &setup; - Msp::LinAl::Vector velocity; - Msp::Geometry::Angle angular_velocity; - Msp::LinAl::Vector force; - float torque = 0.0f; public: RigidBody(Msp::Game::Handle, const Setup &); @@ -34,10 +38,10 @@ public: void add_force(const Msp::LinAl::Vector &, const Msp::LinAl::Vector &); void add_torque(float); void clear_forces(); - const Msp::LinAl::Vector &get_velocity() const { return velocity; } - Msp::Geometry::Angle get_angular_velocity() const { return angular_velocity; } - const Msp::LinAl::Vector &get_force() const { return force; } - float get_torque() const { return torque; } + const Msp::LinAl::Vector &get_velocity() const { return read().velocity; } + Msp::Geometry::Angle get_angular_velocity() const { return read().angular_velocity; } + const Msp::LinAl::Vector &get_force() const { return read().force; } + float get_torque() const { return read().torque; } }; #endif diff --git a/source/game/camera.cpp b/source/game/camera.cpp index 37c0f4d..02880ce 100644 --- a/source/game/camera.cpp +++ b/source/game/camera.cpp @@ -5,32 +5,36 @@ using namespace std; namespace Msp::Game { Camera::Camera(Handle e, const CameraSetup &s): - Component(e), - setup(s), - fov_y(setup.field_of_view_y), - size(setup.size), - near_clip(setup.near_clip), - far_clip(setup.far_clip) + BufferedComponent(e), + setup(s) { - if(!is_orthographic()) - size = { get_aspect(), 1.0f }; + CameraData &d = write(); + d.fov_y = setup.field_of_view_y; + d.size = setup.size; + d.near_clip = setup.near_clip; + d.far_clip = setup.far_clip; + + if(!d.is_orthographic()) + d.size = { d.get_aspect(), 1.0f }; } void Camera::set_field_of_view(Geometry::Angle f, float a) { - if(is_orthographic()) + Data &d = write(); + if(d.is_orthographic()) throw logic_error("Camera is not perspective"); - fov_y = f; - size = { a, 1.0f }; + d.fov_y = f; + d.size = { a, 1.0f }; } void Camera::set_size(const LinAl::Vector &s) { - if(!is_orthographic()) + Data &d = write(); + if(!d.is_orthographic()) throw logic_error("Camera is not orthographic"); - size = s; + d.size = s; } } // namespace Msp::Game diff --git a/source/game/camera.h b/source/game/camera.h index 1e8efdd..f6df873 100644 --- a/source/game/camera.h +++ b/source/game/camera.h @@ -25,31 +25,39 @@ struct CameraSetup std::string sequence_name; }; -class Camera: public Component +struct CameraData { -public: - using Setup = CameraSetup; - -private: - const Setup &setup; Geometry::Angle fov_y; LinAl::Vector size; float height; float near_clip; float far_clip; + bool is_orthographic() const { return fov_y==Geometry::Angle::zero(); } + Geometry::Angle get_fov_horizontal() const { return Geometry::atan(tan(fov_y/2.0f)*get_aspect())*2.0f; } + float get_aspect() const { return size.x/size.y; } +}; + +class Camera: public BufferedComponent +{ +public: + using Setup = CameraSetup; + +private: + const Setup &setup; + public: Camera(Handle, const Setup &); void set_field_of_view(Geometry::Angle, float); void set_size(const LinAl::Vector &); - bool is_orthographic() const { return fov_y==Geometry::Angle::zero(); } - Geometry::Angle get_fov_vertical() const { return fov_y; } - Geometry::Angle get_fov_horizontal() const { return Geometry::atan(tan(fov_y/2.0f)*get_aspect())*2.0f; } - const LinAl::Vector &get_size() const { return size; } - float get_aspect() const { return size.x/size.y; } - float get_near_clip() const { return near_clip; } - float get_far_clip() const { return far_clip; } + bool is_orthographic() const { return read().is_orthographic(); } + Geometry::Angle get_fov_vertical() const { return read().fov_y; } + Geometry::Angle get_fov_horizontal() const { return read().get_fov_horizontal(); } + const LinAl::Vector &get_size() const { return read().size; } + float get_aspect() const { return read().get_aspect(); } + float get_near_clip() const { return read().near_clip; } + float get_far_clip() const { return read().far_clip; } CameraScaling get_scaling() const { return setup.scaling; } const std::string &get_sequence_name() const { return setup.sequence_name; } }; diff --git a/source/game/transform.cpp b/source/game/transform.cpp index e5655f9..5b55ab2 100644 --- a/source/game/transform.cpp +++ b/source/game/transform.cpp @@ -4,39 +4,40 @@ namespace Msp::Game { Transform::Transform(Handle e): - Component(e) + BufferedComponent(e) { } void Transform::set_values(const TransformValues &v) { - values = v; + write().values = v; } void Transform::set_position(const LinAl::Vector &p) { - values.position = p; + write().values.position = p; } void Transform::set_rotation(const Geometry::Quaternion &r) { - values.rotation = normalize(r); + write().values.rotation = normalize(r); } void Transform::set_scale(const LinAl::Vector &s) { - values.scale = s; + write().values.scale = s; } void Transform::update_world_matrix(const Transform *parent) { using Affine = Geometry::AffineTransform; - local_matrix = Affine::translation(values.position)* - Affine::rotation(values.rotation)*Affine::scaling(values.scale); + Data &d = write(); + d.local_matrix = Affine::translation(d.values.position)* + Affine::rotation(d.values.rotation)*Affine::scaling(d.values.scale); if(parent) - world_matrix = parent->get_world_matrix()*local_matrix; + d.world_matrix = parent->get_world_matrix()*d.local_matrix; else - world_matrix = local_matrix; + d.world_matrix = d.local_matrix; } } // namespace Msp::Game diff --git a/source/game/transform.h b/source/game/transform.h index b1766ae..1239546 100644 --- a/source/game/transform.h +++ b/source/game/transform.h @@ -22,13 +22,15 @@ struct TransformValues { } }; -class Transform: public Component +struct TransformData { -private: TransformValues values; LinAl::Matrix local_matrix = LinAl::Matrix::identity(); LinAl::Matrix world_matrix = LinAl::Matrix::identity(); +}; +class Transform: public BufferedComponent +{ public: Transform(Handle); @@ -36,11 +38,11 @@ public: void set_position(const LinAl::Vector &); void set_rotation(const Geometry::Quaternion &); void set_scale(const LinAl::Vector &); - const TransformValues &get_values() const { return values; } - const LinAl::Vector &get_position() const { return values.position; } - const Geometry::Quaternion &get_rotation() const { return values.rotation; } - const LinAl::Vector &get_scale() const { return values.scale; } - const LinAl::Matrix &get_world_matrix() const { return world_matrix; } + const TransformValues &get_values() const { return read().values; } + const LinAl::Vector &get_position() const { return read().values.position; } + const Geometry::Quaternion &get_rotation() const { return read().values.rotation; } + const LinAl::Vector &get_scale() const { return read().values.scale; } + const LinAl::Matrix &get_world_matrix() const { return read().world_matrix; } void update_world_matrix(const Transform *); }; diff --git a/source/game/transformpropagator.cpp b/source/game/transformpropagator.cpp index 631e529..cadac04 100644 --- a/source/game/transformpropagator.cpp +++ b/source/game/transformpropagator.cpp @@ -12,6 +12,8 @@ TransformPropagator::TransformPropagator(Stage &s): System(s), observer(s.get_event_bus()) { + declare_dependency(CHAINED_UPDATE); + observer.observe([this](auto &){ transforms_dirty = true; }); observer.observe([this](auto &){ transforms_dirty = true; }); } diff --git a/source/gameview/renderer.cpp b/source/gameview/renderer.cpp index ca4e8a9..5ddfec1 100644 --- a/source/gameview/renderer.cpp +++ b/source/gameview/renderer.cpp @@ -15,6 +15,10 @@ Renderer::Renderer(Game::Stage &s, GL::View &v): view(v), event_observer(s.get_event_bus()) { + declare_dependency(READ_FRESH); + declare_dependency(READ_FRESH); + declare_dependency(WRITE); + event_observer.observe([this](auto &e){ entity_created(e); }); event_observer.observe([this](auto &e){ entity_destroyed(e); }); event_observer.observe([this](auto &e){ camera_changed(e); }); -- 2.43.0