Skip to content

Commit 8615e7a

Browse files
committed
Chapter 9 (PC)
1 parent 8133b79 commit 8615e7a

Some content is hidden

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

67 files changed

+395
-302
lines changed

Chapter09/Actor.cpp

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44
//
55
// Released under the BSD License
6-
// See LICENSE.txt for full details.
6+
// See LICENSE in root directory for full details.
77
// ----------------------------------------------------------------
88

99
#include "Actor.h"
@@ -17,6 +17,7 @@ Actor::Actor(Game* game)
1717
,mRotation(Quaternion::Identity)
1818
,mScale(1.0f)
1919
,mGame(game)
20+
,mRecomputeWorldTransform(true)
2021
{
2122
mGame->AddActor(this);
2223
}
@@ -36,8 +37,12 @@ void Actor::Update(float deltaTime)
3637
{
3738
if (mState == EActive)
3839
{
40+
ComputeWorldTransform();
41+
3942
UpdateComponents(deltaTime);
4043
UpdateActor(deltaTime);
44+
45+
ComputeWorldTransform();
4146
}
4247
}
4348

@@ -53,20 +58,60 @@ void Actor::UpdateActor(float deltaTime)
5358
{
5459
}
5560

61+
void Actor::ProcessInput(const uint8_t* keyState)
62+
{
63+
if (mState == EActive)
64+
{
65+
// First process input for components
66+
for (auto comp : mComponents)
67+
{
68+
comp->ProcessInput(keyState);
69+
}
70+
71+
ActorInput(keyState);
72+
}
73+
}
74+
75+
void Actor::ActorInput(const uint8_t* keyState)
76+
{
77+
}
78+
5679
void Actor::ComputeWorldTransform()
5780
{
58-
// Scale, then rotate, then translate
59-
mWorldTransform = Matrix4::CreateScale(mScale);
60-
mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
61-
mWorldTransform *= Matrix4::CreateTranslation(mPosition);
81+
if (mRecomputeWorldTransform)
82+
{
83+
mRecomputeWorldTransform = false;
84+
// Scale, then rotate, then translate
85+
mWorldTransform = Matrix4::CreateScale(mScale);
86+
mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
87+
mWorldTransform *= Matrix4::CreateTranslation(mPosition);
88+
89+
// Inform components world transform updated
90+
for (auto comp : mComponents)
91+
{
92+
comp->OnUpdateWorldTransform();
93+
}
94+
}
6295
}
6396

6497
void Actor::AddComponent(Component* component)
6598
{
66-
mComponents.emplace_back(component);
67-
std::sort(mComponents.begin(), mComponents.end(), [](Component* a, Component* b) {
68-
return a->GetUpdateOrder() < b->GetUpdateOrder();
69-
});
99+
// Find the insertion point in the sorted vector
100+
// (The first element with a order higher than me)
101+
int myOrder = component->GetUpdateOrder();
102+
auto iter = mComponents.begin();
103+
for (;
104+
iter != mComponents.end();
105+
++iter)
106+
{
107+
if (myOrder < (*iter)->GetUpdateOrder())
108+
{
109+
break;
110+
}
111+
}
112+
113+
// Inserts element before position of iterator
114+
mComponents.insert(iter, component);
70115
}
71116

72117
void Actor::RemoveComponent(Component* component)

Chapter09/Actor.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44
//
55
// Released under the BSD License
6-
// See LICENSE.txt for full details.
6+
// See LICENSE in root directory for full details.
77
// ----------------------------------------------------------------
88

99
#pragma once
1010
#include <vector>
1111
#include "Math.h"
12+
#include <cstdint>
13+
1214
class Actor
1315
{
1416
public:
@@ -28,16 +30,19 @@ class Actor
2830
void UpdateComponents(float deltaTime);
2931
// Any actor-specific update code (overridable)
3032
virtual void UpdateActor(float deltaTime);
33+
34+
// ProcessInput function called from Game (not overridable)
35+
void ProcessInput(const uint8_t* keyState);
3136
// Any actor-specific input code (overridable)
32-
virtual void ProcessInput(const uint8_t* keys) { }
37+
virtual void ActorInput(const uint8_t* keyState);
3338

3439
// Getters/setters
3540
const Vector3& GetPosition() const { return mPosition; }
36-
void SetPosition(const Vector3& pos) { mPosition = pos; ComputeWorldTransform(); }
41+
void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeWorldTransform = true; }
3742
float GetScale() const { return mScale; }
38-
void SetScale(float scale) { mScale = scale; ComputeWorldTransform(); }
43+
void SetScale(float scale) { mScale = scale; mRecomputeWorldTransform = true; }
3944
const Quaternion& GetRotation() const { return mRotation; }
40-
void SetRotation(const Quaternion& rotation) { mRotation = rotation; ComputeWorldTransform(); }
45+
void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeWorldTransform = true; }
4146

4247
void ComputeWorldTransform();
4348
const Matrix4& GetWorldTransform() const { return mWorldTransform; }
@@ -63,6 +68,7 @@ class Actor
6368
Vector3 mPosition;
6469
Quaternion mRotation;
6570
float mScale;
71+
bool mRecomputeWorldTransform;
6672

6773
std::vector<class Component*> mComponents;
6874
class Game* mGame;

Chapter09/Assets/Cube.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"textures":[
66
"Assets/Cube.png"
77
],
8+
"specularPower":100.0,
89
"vertices":[
910
[-0.5,-0.5,-0.5,0,0,-1,0,0],
1011
[0.5,-0.5,-0.5,0,0,-1,1,0],

Chapter09/Assets/Plane.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"textures":[
66
"Assets/Plane.png"
77
],
8+
"specularPower":100.0,
89
"vertices":[
910
[50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,1.000000],
1011
[50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,0.750000],

Chapter09/Assets/RacingCar.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"textures":[
66
"Assets/RacingCar.png"
77
],
8+
"specularPower":100.0,
89
"vertices":[
910
[-165.691406,-24.391600,89.160400,-0.882353,-0.003922,0.466667,0.261230,0.676270],
1011
[-165.691406,-37.677399,89.160400,-0.882353,-0.003922,0.466667,0.241821,0.678223],

Chapter09/Assets/Rifle.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"textures":[
66
"Assets/Rifle.png"
77
],
8+
"specularPower":100.0,
89
"vertices":[
910
[60.004414,-0.751908,0.341529,0.992157,0.003922,-0.003922,0.611816,0.761719],
1011
[60.003975,-0.533524,0.153569,0.992157,-0.011765,0.003922,0.614746,0.767578],

Chapter09/Assets/Sphere.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"Assets/Sphere.png",
77
"Assets/Default.png"
88
],
9+
"specularPower":100.0,
910
"vertices":[
1011
[0.000000,0.000001,12.500000,-0.003922,-0.003922,0.992157,0.968750,0.000000],
1112
[0.475748,-2.391772,12.259815,0.027451,-0.215686,0.968628,0.968750,0.062500],

Chapter09/AudioComponent.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44
//
55
// Released under the BSD License
6-
// See LICENSE.txt for full details.
6+
// See LICENSE in root directory for full details.
77
// ----------------------------------------------------------------
88

99
#include "AudioComponent.h"
@@ -39,7 +39,7 @@ void AudioComponent::Update(float deltaTime)
3939
}
4040
}
4141

42-
// Update 3D events (remove if needed)
42+
// Remove invalid 3D events
4343
iter = mEvents3D.begin();
4444
while (iter != mEvents3D.end())
4545
{
@@ -49,12 +49,24 @@ void AudioComponent::Update(float deltaTime)
4949
}
5050
else
5151
{
52-
iter->Set3DAttributes(mOwner->GetWorldTransform());
5352
++iter;
5453
}
5554
}
5655
}
5756

57+
void AudioComponent::OnUpdateWorldTransform()
58+
{
59+
// Update 3D events' world transforms
60+
Matrix4 world = mOwner->GetWorldTransform();
61+
for (auto& event : mEvents3D)
62+
{
63+
if (event.IsValid())
64+
{
65+
event.Set3DAttributes(world);
66+
}
67+
}
68+
}
69+
5870
SoundEvent AudioComponent::PlayEvent(const std::string& name)
5971
{
6072
SoundEvent e = mOwner->GetGame()->GetAudioSystem()->PlayEvent(name);

Chapter09/AudioComponent.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44
//
55
// Released under the BSD License
6-
// See LICENSE.txt for full details.
6+
// See LICENSE in root directory for full details.
77
// ----------------------------------------------------------------
88

99
#pragma once
@@ -19,6 +19,7 @@ class AudioComponent : public Component
1919
~AudioComponent();
2020

2121
void Update(float deltaTime) override;
22+
void OnUpdateWorldTransform() override;
2223

2324
SoundEvent PlayEvent(const std::string& name);
2425
void StopAllEvents();

Chapter09/AudioSystem.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44
//
55
// Released under the BSD License
6-
// See LICENSE.txt for full details.
6+
// See LICENSE in root directory for full details.
77
// ----------------------------------------------------------------
88

99
#include "AudioSystem.h"
1010
#include <SDL/SDL_log.h>
11-
#include <FMOD/fmod_studio.hpp>
12-
#include <FMOD/fmod_errors.h>
11+
#include <fmod_studio.hpp>
12+
#include <fmod_errors.h>
1313
#include <vector>
1414

1515
unsigned int AudioSystem::sNextID = 0;
@@ -92,6 +92,7 @@ void AudioSystem::LoadBank(const std::string& name)
9292
&bank // Save pointer to bank
9393
);
9494

95+
const int maxPathLength = 512;
9596
if (result == FMOD_OK)
9697
{
9798
// Add bank to map
@@ -106,12 +107,12 @@ void AudioSystem::LoadBank(const std::string& name)
106107
// Get list of event descriptions in this bank
107108
std::vector<FMOD::Studio::EventDescription*> events(numEvents);
108109
bank->getEventList(events.data(), numEvents, &numEvents);
109-
char eventName[512];
110+
char eventName[maxPathLength];
110111
for (int i = 0; i < numEvents; i++)
111112
{
112113
FMOD::Studio::EventDescription* e = events[i];
113114
// Get the path of this event (like event:/Explosion2D)
114-
e->getPath(eventName, 512, nullptr);
115+
e->getPath(eventName, maxPathLength, nullptr);
115116
// Add to event map
116117
mEvents.emplace(eventName, e);
117118
}

0 commit comments

Comments
 (0)