Motive Animation System
An open source project by FPL.
 All Classes Functions Variables Typedefs Friends Pages
engine.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef MOTIVE_ENGINE_H_
16 #define MOTIVE_ENGINE_H_
17 
18 #include <map>
19 #include <set>
20 
21 #include "motive/common.h"
22 #include "motive/processor.h"
23 
24 namespace motive {
25 
26 struct MotiveVersion;
27 
28 /// @class MotiveEngine
29 /// @brief Hold and update all animation data.
30 ///
31 /// The engine holds all of the MotiveProcessors, and updates them all when
32 /// AdvanceFrame() is called. The processing is kept central, in this manner,
33 /// for scalability. The engine is not a singleton, but you should try to
34 /// minimize the number of engines in your game. As more Motivators are added to
35 /// the processors, you start to get economies of scale.
36 class MotiveEngine {
37  struct ProcessorDetails {
38  MotiveProcessor* processor;
39  bool operator<(const ProcessorDetails& rhs) const {
40  return processor->Priority() < rhs.processor->Priority();
41  }
42  };
43  typedef std::map<MotivatorType, MotiveProcessor*> ProcessorMap;
44  typedef std::pair<MotivatorType, MotiveProcessor*> ProcessorPair;
45  typedef std::multiset<ProcessorDetails> ProcessorSet;
46  typedef std::map<MotivatorType, MotiveProcessorFunctions> FunctionMap;
47  typedef std::pair<MotivatorType, MotiveProcessorFunctions> FunctionPair;
48 
49  public:
50  MotiveEngine();
51  ~MotiveEngine() { Reset(); }
52 
53  /// Deallocate all MotiveProcessors, which, in turn, resets all Motivators
54  /// that use those MotiveProcessors.
55  void Reset();
56 
57  /// Update all the MotiveProcessors by 'delta_time'. This advances all
58  /// Motivators created with this MotiveEngine.
59  /// @param delta_time Elapsed time since the last call to AdvanceFrame().
60  /// Time units are defined by the user. When using spline
61  /// animations, for instance, the time unit is the unit of
62  /// the x-axis.
63  void AdvanceFrame(MotiveTime delta_time);
64 
65  /// @private For internal use only.
66  MotiveProcessor* Processor(MotivatorType type);
67 
68  /// @private For internal use only.
69  static void RegisterProcessorFactory(MotivatorType type,
70  const MotiveProcessorFunctions& fns);
71 
72  private:
73  /// Map from the MotivatorType to the MotiveProcessor. Only one
74  /// MotiveProcessor per type per engine. This is to maximize centralization
75  /// of data.
76  ProcessorMap mapped_processors_;
77 
78  /// Sort the MotiveProcessors by priority. Low numbered priorities run first.
79  /// This allows high number priorities to have child motivators, as long as
80  /// the child motivators have lower priority.
81  ProcessorSet sorted_processors_;
82 
83  /// Current version of the Motive Animation System.
84  const MotiveVersion* version_;
85 
86  /// ProcessorMap from the MotivatorType to the factory that creates the
87  /// MotiveProcessor. We only create an MotiveProcessor when one is needed.
88  static FunctionMap function_map_;
89 };
90 
91 } // namespace motive
92 
93 #endif // MOTIVE_ENGINE_H_
Current version information for the Motive animation system.
Definition: version.h:25
void AdvanceFrame(MotiveTime delta_time)
A MotiveProcessor processes all instances of one type of Motivator.
Definition: processor.h:58
Definition: processor.h:434
Hold and update all animation data.
Definition: engine.h:36