Skip to content

Commit 9d1361e

Browse files
committed
More updates
1 parent ab0e663 commit 9d1361e

48 files changed

Lines changed: 277 additions & 58869 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Chapter14/Actor.cpp‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "Actor.h"
1010
#include "Game.h"
1111
#include "Component.h"
12-
#include <algorithm>
1312
#include "LevelLoader.h"
1413

1514
const char* Actor::TypeNames[NUM_ACTOR_TYPES] = {
@@ -67,6 +66,25 @@ void Actor::UpdateActor(float deltaTime)
6766
{
6867
}
6968

69+
void Actor::ProcessInput(const uint8_t* keyState)
70+
{
71+
if (mState == EActive)
72+
{
73+
// First process input for components
74+
for (auto comp : mComponents)
75+
{
76+
comp->ProcessInput(keyState);
77+
}
78+
79+
ActorInput(keyState);
80+
}
81+
}
82+
83+
void Actor::ActorInput(const uint8_t* keyState)
84+
{
85+
86+
}
87+
7088
void Actor::ComputeWorldTransform()
7189
{
7290
mRecomputeTransform = false;
@@ -108,10 +126,22 @@ void Actor::RotateToNewForward(const Vector3& forward)
108126

109127
void Actor::AddComponent(Component* component)
110128
{
111-
mComponents.emplace_back(component);
112-
std::sort(mComponents.begin(), mComponents.end(), [](Component* a, Component* b) {
113-
return a->GetUpdateOrder() < b->GetUpdateOrder();
114-
});
129+
// Find the insertion point in the sorted vector
130+
// (The first element with a order higher than me)
131+
int myOrder = component->GetUpdateOrder();
132+
auto iter = mComponents.begin();
133+
for (;
134+
iter != mComponents.end();
135+
++iter)
136+
{
137+
if (myOrder < (*iter)->GetUpdateOrder())
138+
{
139+
break;
140+
}
141+
}
142+
143+
// Inserts element before position of iterator
144+
mComponents.insert(iter, component);
115145
}
116146

117147
void Actor::RemoveComponent(Component* component)

‎Chapter14/Actor.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ class Actor
4444
void UpdateComponents(float deltaTime);
4545
// Any actor-specific update code (overridable)
4646
virtual void UpdateActor(float deltaTime);
47+
// ProcessInput function called from Game (not overridable)
48+
void ProcessInput(const uint8_t* keyState);
4749
// Any actor-specific input code (overridable)
48-
virtual void ProcessInput(const uint8_t* keys) { }
50+
virtual void ActorInput(const uint8_t* keyState);
4951

5052
// Getters/setters
5153
const Vector3& GetPosition() const { return mPosition; }

‎Chapter14/Animation.cpp‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
19
#include "Animation.h"
210
#include "Skeleton.h"
311
#include <rapidjson/document.h>
@@ -41,8 +49,9 @@ bool Animation::Load(const std::string& fileName)
4149
}
4250

4351
mNumFrames = frames.GetUint();
44-
mLength = length.GetDouble();
52+
mDuration = length.GetDouble();
4553
mNumBones = bonecount.GetUint();
54+
mFrameDuration = mDuration / (mNumFrames - 1);
4655

4756
mTracks.resize(mNumBones);
4857

@@ -106,21 +115,19 @@ bool Animation::Load(const std::string& fileName)
106115
return true;
107116
}
108117

109-
void Animation::GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, Skeleton* inSkeleton, float inTime)
118+
void Animation::GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, const Skeleton* inSkeleton, float inTime) const
110119
{
111120
if (outPoses.size() != mNumBones)
112121
{
113122
outPoses.resize(mNumBones);
114123
}
115124

116-
// Duration of each frame
117-
float timePerFrame = (mLength / (mNumFrames - 1));
118125
// Figure out the current frame index and next frame
119126
// (This assumes inTime is bounded by [0, AnimDuration]
120-
size_t frame = static_cast<size_t>(inTime / timePerFrame);
127+
size_t frame = static_cast<size_t>(inTime / mFrameDuration);
121128
size_t nextFrame = frame + 1;
122129
// Calculate fractional value between frame and next frame
123-
float pct = Math::Abs(frame - inTime / timePerFrame);
130+
float pct = inTime / mFrameDuration - frame;
124131

125132
// Setup the pose for the root
126133
if (mTracks[0].size() > 0)
@@ -139,7 +146,7 @@ void Animation::GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, Skeleton* in
139146
// Now setup the poses for the rest
140147
for (size_t bone = 1; bone < mNumBones; bone++)
141148
{
142-
Matrix4 localMat;
149+
Matrix4 localMat; // (Defaults to identity)
143150
if (mTracks[bone].size() > 0)
144151
{
145152
BoneTransform interp = BoneTransform::Interpolate(mTracks[bone][frame],

‎Chapter14/Animation.h‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
19
#pragma once
210
#include "BoneTransform.h"
311
#include <vector>
@@ -10,21 +18,23 @@ class Animation
1018

1119
size_t GetNumBones() const { return mNumBones; }
1220
size_t GetNumFrames() const { return mNumFrames; }
13-
float GetLength() const { return mLength; }
21+
float GetDuration() const { return mDuration; }
22+
float GetFrameDuration() const { return mFrameDuration; }
1423

1524
// Fills the provided vector with the global (current) pose matrices for each
1625
// bone at the specified time in the animation. It is expected that the time
17-
// is >= 0.0f and <= mLength
18-
void GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, class Skeleton* inSkeleton, float inTime);
19-
2026
const std::string& GetFileName() const { return mFileName; }
27+
// is >= 0.0f and <= mDuration
28+
void GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, const class Skeleton* inSkeleton, float inTime) const;
2129
private:
2230
// Number of bones for the animation
2331
size_t mNumBones;
2432
// Number of frames in the animation
2533
size_t mNumFrames;
26-
// Length of the animation in seconds
27-
float mLength;
34+
// Duration of the animation in seconds
35+
float mDuration;
36+
// Duration of each frame in the animation
37+
float mFrameDuration;
2838
// Transform information for each frame on the track
2939
// Each index in the outer vector is a bone, inner vector
3040
// is a frame

‎Chapter14/Assets/LICENSE.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)