]> git.tdb.fi Git - libs/demoscene.git/commitdiff
Merge CameraControl functionality into other classes
authorMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2019 20:30:59 +0000 (23:30 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2019 20:42:59 +0000 (23:42 +0300)
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 [new file with mode: 0644]
source/animate.h [new file with mode: 0644]
source/cameracontrol.cpp [deleted file]
source/cameracontrol.h [deleted file]
source/demo.cpp
source/demo.h
source/sequencer.cpp
source/stage.cpp
source/stage.h

diff --git a/source/animate.cpp b/source/animate.cpp
new file mode 100644 (file)
index 0000000..d74c239
--- /dev/null
@@ -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<Animate, Action::Loader>(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<GL::Animation>(n);
+}
+
+void Animate::Loader::target(const string &n)
+{
+       obj.target = &demo.get_thing<GL::Placeable>(n);
+}
diff --git a/source/animate.h b/source/animate.h
new file mode 100644 (file)
index 0000000..d386a7a
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef MSP_DEMOSCENE_ANIMATE_H_
+#define MSP_DEMOSCENE_ANIMATE_H_
+
+#include <msp/gl/animation.h>
+#include <msp/gl/animationplayer.h>
+#include "action.h"
+
+class Animate: public Action
+{
+public:
+       class Loader: public Msp::DataFile::DerivedObjectLoader<Animate, Action::Loader>
+       {
+       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 (file)
index 8b30627..0000000
+++ /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 (file)
index ead4d0e..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-#ifndef MSP_DEMOSCENE_CAMERACONTROL_H_
-#define MSP_DEMOSCENE_CAMERACONTROL_H_
-
-#include <msp/gl/animationplayer.h>
-#include <msp/gl/camera.h>
-#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
index 2072f297bc9ca1e84edeb9a3f5bd81cd17abcbee..08a92b4f0d6bd925709de855a30cec20201adcf2 100644 (file)
@@ -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<GL::View *>(&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);
+}
index 77eb671601fc52f79386b341acf06777d7e1c07a..e9d369bf9b337b202480b8ef1cf128a47127fb80 100644 (file)
@@ -8,11 +8,27 @@
 #include <msp/al/source.h>
 #include <msp/al/streamer.h>
 #include <msp/datafile/collection.h>
+#include <msp/gl/animationplayer.h>
 #include <msp/gl/windowview.h>
+#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<void> 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<typename T>
        T &get_thing(const std::string &);
 
index 8bca70bb368764d7a94058b95aaf595623a95913..eeb41d20da6865b72eb0ab0c5097ad1a0805600a 100644 (file)
@@ -1,6 +1,7 @@
 #include <cmath>
 #include <msp/core/algorithm.h>
 #include <msp/core/maputils.h>
+#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>("animate");
        register_action_type<FadeOverlay::Fade>("fade");
        register_action_type<Stage::UseInView>("use_stage");
+       register_action_type<Stage::SetCamera>("set_camera");
 }
 
 void Sequencer::set_beats_per_minute(float bpm)
index 94782add256f16ddc7cd2ed5c06905ae3dd4fd54..dfb668852760d3526f20c2939c2f8d2b7889e88b 100644 (file)
@@ -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<UseInView, Action::Loader>(u, d)
 {
@@ -56,3 +88,21 @@ void Stage::UseInView::Loader::view(const string &n)
 {
        obj.view = &demo.get_thing<GL::View>(n);
 }
+
+
+Stage::SetCamera::Loader::Loader(SetCamera &s, Demo &d):
+       DataFile::DerivedObjectLoader<SetCamera, Action::Loader>(s, d)
+{
+       add("camera", &Loader::camera);
+       add("stage", &Loader::stage);
+}
+
+void Stage::SetCamera::Loader::stage(const string &n)
+{
+       obj.stage = &demo.get_thing<Stage>(n);
+}
+
+void Stage::SetCamera::Loader::camera(const string &n)
+{
+       obj.camera = &demo.get_resources().get<GL::Camera>(n);
+}
index 1ab32d83d27d07fa50be5510c5b7bf8216ab43db..daf4f4b982c72ab52fa07008c79a10d2d2b1ba69 100644 (file)
@@ -1,8 +1,8 @@
 #ifndef MSP_DEMOSCENE_STAGE_H_
 #define MSP_DEMOSCENE_STAGE_H_
 
+#include <msp/gl/camera.h>
 #include <msp/gl/pipeline.h>
-#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<SetCamera, Action::Loader>
+               {
+               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