From 34051ffdca091ad3971c1382c71c5fc58b7ade0f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 22 May 2019 23:30:59 +0300 Subject: [PATCH] Merge CameraControl functionality into other classes Rather than each stage having a separate animation player for animating its camera, use a central player in the demo. This allows more easily animating other objects as well. --- source/animate.cpp | 52 +++++++++++++++++++++++++++++++++++++++ source/animate.h | 35 ++++++++++++++++++++++++++ source/cameracontrol.cpp | 43 -------------------------------- source/cameracontrol.h | 53 ---------------------------------------- source/demo.cpp | 12 +++++++++ source/demo.h | 21 ++++++++++++++++ source/sequencer.cpp | 3 +++ source/stage.cpp | 52 ++++++++++++++++++++++++++++++++++++++- source/stage.h | 32 ++++++++++++++++++++++-- 9 files changed, 204 insertions(+), 99 deletions(-) create mode 100644 source/animate.cpp create mode 100644 source/animate.h delete mode 100644 source/cameracontrol.cpp delete mode 100644 source/cameracontrol.h diff --git a/source/animate.cpp b/source/animate.cpp new file mode 100644 index 0000000..d74c239 --- /dev/null +++ b/source/animate.cpp @@ -0,0 +1,52 @@ +#include "animate.h" +#include "demo.h" + +using namespace std; +using namespace Msp; + +Animate::Animate(): + target(0), + anim(0), + player(0) +{ } + +Animate::Animate(GL::Placeable &t, const GL::Animation &a, GL::AnimationPlayer &p): + target(&t), + anim(&a), + player(&p) +{ } + +void Animate::validate() const +{ + if(!target) + throw logic_error("null target"); + if(!anim) + throw logic_error("null animation"); + if(!player) + throw logic_error("null player"); +} + +void Animate::start(float, float d) +{ + float speed = (d ? (anim->get_duration()/Time::sec)/d : 1.0f); + player->play(*target, *anim, speed); +} + + +Animate::Loader::Loader(Animate &a, Demo &d): + DataFile::DerivedObjectLoader(a, d) +{ + a.player = &demo.get_animation_player(); + add("animation", &Loader::animation); + add("target", &Loader::target); +} + +void Animate::Loader::animation(const string &n) +{ + obj.anim = &demo.get_resources().get(n); +} + +void Animate::Loader::target(const string &n) +{ + obj.target = &demo.get_thing(n); +} diff --git a/source/animate.h b/source/animate.h new file mode 100644 index 0000000..d386a7a --- /dev/null +++ b/source/animate.h @@ -0,0 +1,35 @@ +#ifndef MSP_DEMOSCENE_ANIMATE_H_ +#define MSP_DEMOSCENE_ANIMATE_H_ + +#include +#include +#include "action.h" + +class Animate: public Action +{ +public: + class Loader: public Msp::DataFile::DerivedObjectLoader + { + public: + Loader(Animate &, Demo &); + + private: + void animation(const std::string &); + void target(const std::string &); + }; + +private: + Msp::GL::Placeable *target; + const Msp::GL::Animation *anim; + Msp::GL::AnimationPlayer *player; + +public: + Animate(); + Animate(Msp::GL::Placeable &, const Msp::GL::Animation &, Msp::GL::AnimationPlayer &); + + virtual void validate() const; + + virtual void start(float, float); +}; + +#endif diff --git a/source/cameracontrol.cpp b/source/cameracontrol.cpp deleted file mode 100644 index 8b30627..0000000 --- a/source/cameracontrol.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "cameracontrol.h" - -using namespace Msp; - -void CameraControl::set_camera(const GL::Camera &c) -{ - camera.set_object_matrix(c.get_object_matrix()); - camera.set_up_direction(c.get_up_direction()); - camera.set_field_of_view(c.get_field_of_view()); - camera.set_depth_clip(c.get_near_clip(), c.get_far_clip()); -} - -void CameraControl::animate_camera(const GL::Animation &anim, float speed) -{ - anim_player.play(camera, anim, speed); -} - -void CameraControl::tick(float, float d) -{ - anim_player.tick(d*Time::sec); -} - - -CameraControl::SetCamera::SetCamera(CameraControl &cc, const GL::Camera &c): - control(cc), - camera(c) -{ } - -void CameraControl::SetCamera::start(float, float) -{ - control.set_camera(camera); -} - - -CameraControl::AnimateCamera::AnimateCamera(CameraControl &cc, const GL::Animation &a): - control(cc), - anim(a) -{ } - -void CameraControl::AnimateCamera::start(float, float d) -{ - control.animate_camera(anim, (d ? (anim.get_duration()/Time::sec)/d : 1.0f)); -} diff --git a/source/cameracontrol.h b/source/cameracontrol.h deleted file mode 100644 index ead4d0e..0000000 --- a/source/cameracontrol.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef MSP_DEMOSCENE_CAMERACONTROL_H_ -#define MSP_DEMOSCENE_CAMERACONTROL_H_ - -#include -#include -#include "action.h" - -class CameraControl: public Action -{ -public: - class SetCamera: public Action - { - private: - CameraControl &control; - const Msp::GL::Camera &camera; - - public: - SetCamera(CameraControl &, const Msp::GL::Camera &); - - virtual void validate() const { } - - virtual void start(float, float); - }; - - class AnimateCamera: public Action - { - private: - CameraControl &control; - const Msp::GL::Animation &anim; - - public: - AnimateCamera(CameraControl &, const Msp::GL::Animation &); - - virtual void validate() const { } - - virtual void start(float, float); - }; - -private: - Msp::GL::Camera camera; - Msp::GL::AnimationPlayer anim_player; - -public: - void set_camera(const Msp::GL::Camera &); - void animate_camera(const Msp::GL::Animation &, float); - Msp::GL::Camera &get_camera() { return camera; } - - virtual void validate() const { } - - virtual void tick(float, float); -}; - -#endif diff --git a/source/demo.cpp b/source/demo.cpp index 2072f29..08a92b4 100644 --- a/source/demo.cpp +++ b/source/demo.cpp @@ -7,6 +7,7 @@ using namespace Msp; Demo::Demo(Graphics::Window &window, Graphics::GLContext &gl_ctx, DataFile::Collection &r): resources(r), view(window, gl_ctx), + anim_action(anim_player), music_source(0), streamer(0), music_io(0), @@ -14,6 +15,7 @@ Demo::Demo(Graphics::Window &window, Graphics::GLContext &gl_ctx, DataFile::Coll { things["window"] = static_cast(&view); + sequencer.add_static_action(anim_action); sequencer.signal_finished.connect(signal_finished); } @@ -77,3 +79,13 @@ void Demo::seek(const Time::TimeDelta &pos) { sequencer.seek(pos*sequencer.get_beats_per_minute()/Time::min); } + + +Demo::AnimationAction::AnimationAction(GL::AnimationPlayer &p): + player(p) +{ } + +void Demo::AnimationAction::tick(float, float d) +{ + player.tick(d*Time::sec); +} diff --git a/source/demo.h b/source/demo.h index 77eb671..e9d369b 100644 --- a/source/demo.h +++ b/source/demo.h @@ -8,11 +8,27 @@ #include #include #include +#include #include +#include "action.h" #include "sequencer.h" class Demo { +private: + class AnimationAction: public Action + { + private: + Msp::GL::AnimationPlayer &player; + + public: + AnimationAction(Msp::GL::AnimationPlayer &); + + virtual void validate() const { } + + virtual void tick(float, float); + }; + public: sigc::signal signal_finished; @@ -20,6 +36,8 @@ protected: Sequencer sequencer; Msp::DataFile::Collection &resources; Msp::GL::WindowView view; + Msp::GL::AnimationPlayer anim_player; + AnimationAction anim_action; Msp::AL::Source *music_source; Msp::AL::Streamer *streamer; Msp::IO::Seekable *music_io; @@ -34,6 +52,9 @@ protected: public: virtual ~Demo(); + Msp::DataFile::Collection &get_resources() const { return resources; } + Msp::GL::AnimationPlayer &get_animation_player() { return anim_player; } + template T &get_thing(const std::string &); diff --git a/source/sequencer.cpp b/source/sequencer.cpp index 8bca70b..eeb41d2 100644 --- a/source/sequencer.cpp +++ b/source/sequencer.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "animate.h" #include "fadeoverlay.h" #include "sequencer.h" #include "stage.h" @@ -15,8 +16,10 @@ Sequencer::Sequencer(float bpm): { set_beats_per_minute(bpm); + register_action_type("animate"); register_action_type("fade"); register_action_type("use_stage"); + register_action_type("set_camera"); } void Sequencer::set_beats_per_minute(float bpm) diff --git a/source/stage.cpp b/source/stage.cpp index 94782ad..dfb6688 100644 --- a/source/stage.cpp +++ b/source/stage.cpp @@ -14,6 +14,14 @@ Stage::~Stage() delete pipeline; } +void Stage::set_camera(const GL::Camera &c) +{ + camera.set_object_matrix(c.get_object_matrix()); + camera.set_up_direction(c.get_up_direction()); + camera.set_field_of_view(c.get_field_of_view()); + camera.set_depth_clip(c.get_near_clip(), c.get_far_clip()); +} + Stage::UseInView::UseInView(): view(0), @@ -35,11 +43,35 @@ void Stage::UseInView::validate() const void Stage::UseInView::start(float, float) { - view->set_camera(&stage->camera_control.get_camera()); + view->set_camera(&stage->camera); view->set_content(stage->pipeline); } +Stage::SetCamera::SetCamera(): + stage(0), + camera(0) +{ } + +Stage::SetCamera::SetCamera(Stage &s, const GL::Camera &c): + stage(&s), + camera(&c) +{ } + +void Stage::SetCamera::validate() const +{ + if(!stage) + throw logic_error("null stage"); + if(!camera) + throw logic_error("null camera"); +} + +void Stage::SetCamera::start(float, float) +{ + stage->set_camera(*camera); +} + + Stage::UseInView::Loader::Loader(UseInView &u, Demo &d): DataFile::DerivedObjectLoader(u, d) { @@ -56,3 +88,21 @@ void Stage::UseInView::Loader::view(const string &n) { obj.view = &demo.get_thing(n); } + + +Stage::SetCamera::Loader::Loader(SetCamera &s, Demo &d): + DataFile::DerivedObjectLoader(s, d) +{ + add("camera", &Loader::camera); + add("stage", &Loader::stage); +} + +void Stage::SetCamera::Loader::stage(const string &n) +{ + obj.stage = &demo.get_thing(n); +} + +void Stage::SetCamera::Loader::camera(const string &n) +{ + obj.camera = &demo.get_resources().get(n); +} diff --git a/source/stage.h b/source/stage.h index 1ab32d8..daf4f4b 100644 --- a/source/stage.h +++ b/source/stage.h @@ -1,8 +1,8 @@ #ifndef MSP_DEMOSCENE_STAGE_H_ #define MSP_DEMOSCENE_STAGE_H_ +#include #include -#include "cameracontrol.h" #include "action.h" struct Stage @@ -33,11 +33,39 @@ struct Stage virtual void start(float, float); }; + class SetCamera: public Action + { + public: + class Loader: public Msp::DataFile::DerivedObjectLoader + { + public: + Loader(SetCamera &, Demo &); + + private: + void camera(const std::string &); + void stage(const std::string &); + }; + + private: + Stage *stage; + const Msp::GL::Camera *camera; + + public: + SetCamera(); + SetCamera(Stage &, const Msp::GL::Camera &); + + virtual void validate() const; + + virtual void start(float, float); + }; + Msp::GL::Pipeline *pipeline; - CameraControl camera_control; + Msp::GL::Camera camera; Stage(); ~Stage(); + + void set_camera(const Msp::GL::Camera &); }; #endif -- 2.43.0