Motive Animation System
An open source project by FPL.
 All Classes Functions Variables Typedefs Friends Pages
common.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_COMMON_H_
16 #define MOTIVE_COMMON_H_
17 
18 #include <cstdint>
19 
20 #include "mathfu/glsl_mappings.h"
21 
22 namespace motive {
23 
24 class MotiveProcessor;
25 class Motivator;
26 
27 /// @typedef MotivatorType
28 /// MotivatorType is used for run-time type information. It's implemented as a
29 /// pointer to a string. Each derivation of MotivatorInit supplies a `kType`
30 /// static member that's identifies the corresponding MotiveProcessor.
31 /// We put `kType` in MotivatorInit instead of MotiveProcessor because
32 /// only MotivatorInit is in the external API.
33 typedef const char** MotivatorType;
34 static const MotivatorType kMotivatorTypeInvalid = nullptr;
35 
36 /// @typedef MotiveIndex
37 /// The MotiveIndex identifies an Motivator inside a MotiveProcessor. The
38 /// MotiveProcessor holds all Motivators of its type. Calls to Motivators are
39 /// proxied to the MotiveProcessor, along with their MotiveIndex.
40 typedef int32_t MotiveIndex;
41 static const MotiveIndex kMotiveIndexInvalid = static_cast<MotiveIndex>(-1);
42 
43 /// @typedef MotiveDimension
44 /// Identify how many slots in the MotiveProcessor a Motivator occupies.
45 /// A Motivator3f occupies three slots, for example. Arithmetic is mixed
46 /// between MotiveIndex and MotiveDimension in fplutil::IndexAllocator, so they
47 /// should be the same type.
48 typedef MotiveIndex MotiveDimension;
49 
50 /// @typedef MotiveChildIndex
51 /// Motivators can have child components. For example, a matrix motivator is
52 /// composed of a series of basic matrix operations. Each operation is a
53 /// child component.
54 typedef uint32_t MotiveChildIndex;
55 
56 /// @typedef MotiveTime
57 /// Time units are defined by the user. We use integer instead of floating
58 /// point to avoid a loss of precision as time accumulates.
59 typedef int MotiveTime;
60 static const MotiveTime kMotiveTimeEndless =
61  std::numeric_limits<MotiveTime>::max();
62 
63 /// @typedef BoneIndex
64 /// Identify bone for skeletal animation. Each non-root bone has a parent
65 /// whose BoneIndex is less than its own. Each bone has a transformation matrix.
66 /// By traversing up the tree to a root bone, multiplying the transformation
67 /// matrices as you go, you can get the global transform for the bone.
68 /// We support up to 254 bones.
69 typedef uint8_t BoneIndex;
70 static const BoneIndex kMaxNumBones = 254;
71 static const BoneIndex kInvalidBoneIdx = 255;
72 
73 /// @typedef MatrixOpId
74 /// Identify an operation in an animation so that it can be blended with the
75 /// same operation in another animation. For example, an animation may have
76 /// three kTranslateX operations for a single matrix: one for translating to
77 /// the scale pivot, one for translating from the scale pivot, and one for the
78 /// final SQT translation. If another animation has no scale operations,
79 /// however, that other animation may have only the one SQT translation.
80 /// We need the MatrixOpId id so that we know how to match the SQT translations
81 /// when blending from one animation to the other.
82 typedef uint8_t MatrixOpId;
83 static const MatrixOpId kMaxMatrixOpId = 254;
84 static const MatrixOpId kInvalidMatrixOpId = 255;
85 
86 /// @class MotivatorInit
87 /// Base class for Motivator parameterization. Every motivator type has a
88 /// its own init class that derives from MotivatorInit. In the derivation,
89 /// the MOTIVE_INTERFACE() macro should be added to the public interface.
90 /// This macro will define `kName`, `kType`, and Register() calls for the
91 /// motivator type.
93  public:
94  /// The derived class's constructor should set 'type'.
95  explicit MotivatorInit(MotivatorType type) : type_(type) {}
96 
97  MotivatorType type() const { return type_; }
98  void set_type(MotivatorType type) { type_ = type; }
99 
100  private:
101  MotivatorType type_;
102 };
103 
104 /// Add this to the public interface of your derivation of Init. It defines
105 /// a unique identifier for this type as kType. Your derivation's constructor
106 /// should construct base class with Init(kType).
107 #define MOTIVE_INTERFACE() \
108  static const char* kName; \
109  static const motive::MotivatorType kType; \
110  static void Register()
111 
112 /// Add this to the source file with your processor code. It instantiates the
113 /// static variables and functions declared in MOTIVE_INTERFACE.
114 /// Example usage,
115 /// MOTIVE_INSTANCE(AwesomeInit, AwesomeMotiveProcessor);
116 #define MOTIVE_INSTANCE(InitType, ProcessorType) \
117  static motive::MotiveProcessor* ProcessorType##Create() { \
118  return new ProcessorType(); \
119  } \
120  static void ProcessorType##Destroy(motive::MotiveProcessor* p) { delete p; } \
121  void InitType::Register() { \
122  const motive::MotiveProcessorFunctions functions(ProcessorType##Create, \
123  ProcessorType##Destroy); \
124  motive::MotiveEngine::RegisterProcessorFactory(InitType::kType, \
125  functions); \
126  } \
127  const char* InitType::kName = #ProcessorType; \
128  const motive::MotivatorType InitType::kType = &InitType::kName
129 
130 /// Return the number of elements in an array 'a', as type `size_t`.
131 /// If 'a' is not an array, generates an error by dividing by zero.
132 #define MOTIVE_ARRAY_SIZE(a) \
133  ((sizeof(a) / sizeof(*(a))) / \
134  static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
135 
136 /// Disallow the copy constructor and operator= functions.
137 /// Used in the `private` declarations for a class.
138 #define MOTIVE_DISALLOW_COPY_AND_ASSIGN(TypeName) \
139  TypeName(const TypeName&); \
140  void operator=(const TypeName&)
141 
142 /// Print ASCII curves in motive tests.
143 #define MOTIVE_OUTPUT_DEBUG_CURVES_IN_TESTS
144 
145 } // namespace motive
146 
147 #endif // MOTIVE_COMMON_H_
MotivatorInit(MotivatorType type)
The derived class's constructor should set 'type'.
Definition: common.h:95
Definition: common.h:92