Ver código fonte

Chapter 9 (PC)

Sanjay Madhav 8 anos atrás
pai
commit
8615e7a973
67 arquivos alterados com 395 adições e 302 exclusões
  1. 54 9
      Chapter09/Actor.cpp
  2. 11 5
      Chapter09/Actor.h
  3. 1 0
      Chapter09/Assets/Cube.gpmesh
  4. 1 0
      Chapter09/Assets/Plane.gpmesh
  5. 1 0
      Chapter09/Assets/RacingCar.gpmesh
  6. 1 0
      Chapter09/Assets/Rifle.gpmesh
  7. 1 0
      Chapter09/Assets/Sphere.gpmesh
  8. 15 3
      Chapter09/AudioComponent.cpp
  9. 2 1
      Chapter09/AudioComponent.h
  10. 6 5
      Chapter09/AudioSystem.cpp
  11. 1 1
      Chapter09/AudioSystem.h
  12. 2 2
      Chapter09/CameraComponent.cpp
  13. 2 2
      Chapter09/CameraComponent.h
  14. 0 0
      Chapter09/Chapter09-windows.sln
  15. 16 3
      Chapter09/CircleComponent.cpp
  16. 6 4
      Chapter09/CircleComponent.h
  17. 0 20
      Chapter09/CollisionComponent.cpp
  18. 0 17
      Chapter09/CollisionComponent.h
  19. 1 1
      Chapter09/Component.cpp
  20. 7 1
      Chapter09/Component.h
  21. 3 3
      Chapter09/FPSActor.cpp
  22. 3 3
      Chapter09/FPSActor.h
  23. 2 2
      Chapter09/FPSCamera.cpp
  24. 2 2
      Chapter09/FPSCamera.h
  25. 2 2
      Chapter09/FollowActor.cpp
  26. 2 2
      Chapter09/FollowActor.h
  27. 1 1
      Chapter09/FollowCamera.cpp
  28. 1 1
      Chapter09/FollowCamera.h
  29. 53 34
      Chapter09/Game.cpp
  30. 8 4
      Chapter09/Game.h
  31. 15 12
      Chapter09/Game.vcxproj
  32. 10 16
      Chapter09/Game.vcxproj.filters
  33. 1 1
      Chapter09/Main.cpp
  34. 17 18
      Chapter09/Math.cpp
  35. 1 1
      Chapter09/Math.h
  36. 6 3
      Chapter09/Mesh.cpp
  37. 5 1
      Chapter09/Mesh.h
  38. 4 2
      Chapter09/MeshComponent.cpp
  39. 2 2
      Chapter09/MeshComponent.h
  40. 2 2
      Chapter09/MoveComponent.cpp
  41. 2 2
      Chapter09/MoveComponent.h
  42. 2 2
      Chapter09/OrbitActor.cpp
  43. 2 2
      Chapter09/OrbitActor.h
  44. 2 2
      Chapter09/OrbitCamera.cpp
  45. 2 2
      Chapter09/OrbitCamera.h
  46. 50 42
      Chapter09/Renderer.cpp
  47. 7 6
      Chapter09/Renderer.h
  48. 12 12
      Chapter09/Shader.cpp
  49. 4 5
      Chapter09/Shader.h
  50. 2 2
      Chapter09/Shaders/BasicMesh.frag
  51. 2 2
      Chapter09/Shaders/BasicMesh.vert
  52. 7 8
      Chapter09/Shaders/Phong.frag
  53. 2 2
      Chapter09/Shaders/Phong.vert
  54. 2 2
      Chapter09/Shaders/Sprite.frag
  55. 2 2
      Chapter09/Shaders/Sprite.vert
  56. 2 2
      Chapter09/SoundEvent.cpp
  57. 1 1
      Chapter09/SoundEvent.h
  58. 2 2
      Chapter09/SplineActor.cpp
  59. 2 2
      Chapter09/SplineActor.h
  60. 2 2
      Chapter09/SplineCamera.cpp
  61. 2 2
      Chapter09/SplineCamera.h
  62. 1 1
      Chapter09/SpriteComponent.cpp
  63. 1 1
      Chapter09/SpriteComponent.h
  64. 2 2
      Chapter09/Texture.cpp
  65. 2 2
      Chapter09/Texture.h
  66. 3 4
      Chapter09/VertexArray.cpp
  67. 7 2
      Chapter09/VertexArray.h

+ 54 - 9
Chapter09/Actor.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Actor.h"
@@ -17,6 +17,7 @@ Actor::Actor(Game* game)
 	,mRotation(Quaternion::Identity)
 	,mScale(1.0f)
 	,mGame(game)
+	,mRecomputeWorldTransform(true)
 {
 	mGame->AddActor(this);
 }
@@ -36,8 +37,12 @@ void Actor::Update(float deltaTime)
 {
 	if (mState == EActive)
 	{
+		ComputeWorldTransform();
+
 		UpdateComponents(deltaTime);
 		UpdateActor(deltaTime);
+
+		ComputeWorldTransform();
 	}
 }
 
@@ -53,20 +58,60 @@ void Actor::UpdateActor(float deltaTime)
 {
 }
 
+void Actor::ProcessInput(const uint8_t* keyState)
+{
+	if (mState == EActive)
+	{
+		// First process input for components
+		for (auto comp : mComponents)
+		{
+			comp->ProcessInput(keyState);
+		}
+
+		ActorInput(keyState);
+	}
+}
+
+void Actor::ActorInput(const uint8_t* keyState)
+{
+}
+
 void Actor::ComputeWorldTransform()
 {
-	// Scale, then rotate, then translate
-	mWorldTransform = Matrix4::CreateScale(mScale);
-	mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
-	mWorldTransform *= Matrix4::CreateTranslation(mPosition);
+	if (mRecomputeWorldTransform)
+	{
+		mRecomputeWorldTransform = false;
+		// Scale, then rotate, then translate
+		mWorldTransform = Matrix4::CreateScale(mScale);
+		mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
+		mWorldTransform *= Matrix4::CreateTranslation(mPosition);
+
+		// Inform components world transform updated
+		for (auto comp : mComponents)
+		{
+			comp->OnUpdateWorldTransform();
+		}
+	}
 }
 
 void Actor::AddComponent(Component* component)
 {
-	mComponents.emplace_back(component);
-	std::sort(mComponents.begin(), mComponents.end(), [](Component* a, Component* b) {
-		return a->GetUpdateOrder() < b->GetUpdateOrder();
-	});
+	// Find the insertion point in the sorted vector
+	// (The first element with a order higher than me)
+	int myOrder = component->GetUpdateOrder();
+	auto iter = mComponents.begin();
+	for (;
+		iter != mComponents.end();
+		++iter)
+	{
+		if (myOrder < (*iter)->GetUpdateOrder())
+		{
+			break;
+		}
+	}
+
+	// Inserts element before position of iterator
+	mComponents.insert(iter, component);
 }
 
 void Actor::RemoveComponent(Component* component)

+ 11 - 5
Chapter09/Actor.h

@@ -3,12 +3,14 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
 #include <vector>
 #include "Math.h"
+#include <cstdint>
+
 class Actor
 {
 public:
@@ -28,16 +30,19 @@ public:
 	void UpdateComponents(float deltaTime);
 	// Any actor-specific update code (overridable)
 	virtual void UpdateActor(float deltaTime);
+
+	// ProcessInput function called from Game (not overridable)
+	void ProcessInput(const uint8_t* keyState);
 	// Any actor-specific input code (overridable)
-	virtual void ProcessInput(const uint8_t* keys) { }
+	virtual void ActorInput(const uint8_t* keyState);
 
 	// Getters/setters
 	const Vector3& GetPosition() const { return mPosition; }
-	void SetPosition(const Vector3& pos) { mPosition = pos; ComputeWorldTransform(); }
+	void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeWorldTransform = true; }
 	float GetScale() const { return mScale; }
-	void SetScale(float scale) { mScale = scale;  ComputeWorldTransform(); }
+	void SetScale(float scale) { mScale = scale;  mRecomputeWorldTransform = true; }
 	const Quaternion& GetRotation() const { return mRotation; }
-	void SetRotation(const Quaternion& rotation) { mRotation = rotation;  ComputeWorldTransform(); }
+	void SetRotation(const Quaternion& rotation) { mRotation = rotation;  mRecomputeWorldTransform = true; }
 	
 	void ComputeWorldTransform();
 	const Matrix4& GetWorldTransform() const { return mWorldTransform; }
@@ -63,6 +68,7 @@ private:
 	Vector3 mPosition;
 	Quaternion mRotation;
 	float mScale;
+	bool mRecomputeWorldTransform;
 
 	std::vector<class Component*> mComponents;
 	class Game* mGame;

+ 1 - 0
Chapter09/Assets/Cube.gpmesh

@@ -5,6 +5,7 @@
 	"textures":[
 		"Assets/Cube.png"
 	],
+	"specularPower":100.0,
 	"vertices":[
 		[-0.5,-0.5,-0.5,0,0,-1,0,0],
 		[0.5,-0.5,-0.5,0,0,-1,1,0],

+ 1 - 0
Chapter09/Assets/Plane.gpmesh

@@ -5,6 +5,7 @@
 	"textures":[
 		"Assets/Plane.png"
 	],
+	"specularPower":100.0,
 	"vertices":[
 		[50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,1.000000],
 		[50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,0.750000],

+ 1 - 0
Chapter09/Assets/RacingCar.gpmesh

@@ -5,6 +5,7 @@
 	"textures":[
 		"Assets/RacingCar.png"
 	],
+	"specularPower":100.0,
 	"vertices":[
 		[-165.691406,-24.391600,89.160400,-0.882353,-0.003922,0.466667,0.261230,0.676270],
 		[-165.691406,-37.677399,89.160400,-0.882353,-0.003922,0.466667,0.241821,0.678223],

+ 1 - 0
Chapter09/Assets/Rifle.gpmesh

@@ -5,6 +5,7 @@
 	"textures":[
 		"Assets/Rifle.png"
 	],
+	"specularPower":100.0,
 	"vertices":[
 		[60.004414,-0.751908,0.341529,0.992157,0.003922,-0.003922,0.611816,0.761719],
 		[60.003975,-0.533524,0.153569,0.992157,-0.011765,0.003922,0.614746,0.767578],

+ 1 - 0
Chapter09/Assets/Sphere.gpmesh

@@ -6,6 +6,7 @@
 		"Assets/Sphere.png",
       "Assets/Default.png"
 	],
+	"specularPower":100.0,
 	"vertices":[
 		[0.000000,0.000001,12.500000,-0.003922,-0.003922,0.992157,0.968750,0.000000],
 		[0.475748,-2.391772,12.259815,0.027451,-0.215686,0.968628,0.968750,0.062500],

+ 15 - 3
Chapter09/AudioComponent.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "AudioComponent.h"
@@ -39,7 +39,7 @@ void AudioComponent::Update(float deltaTime)
 		}
 	}
 
-	// Update 3D events (remove if needed)
+	// Remove invalid 3D events
 	iter = mEvents3D.begin();
 	while (iter != mEvents3D.end())
 	{
@@ -49,12 +49,24 @@ void AudioComponent::Update(float deltaTime)
 		}
 		else
 		{
-			iter->Set3DAttributes(mOwner->GetWorldTransform());
 			++iter;
 		}
 	}
 }
 
+void AudioComponent::OnUpdateWorldTransform()
+{
+	// Update 3D events' world transforms
+	Matrix4 world = mOwner->GetWorldTransform();
+	for (auto& event : mEvents3D)
+	{
+		if (event.IsValid())
+		{
+			event.Set3DAttributes(world);
+		}
+	}
+}
+
 SoundEvent AudioComponent::PlayEvent(const std::string& name)
 {
 	SoundEvent e = mOwner->GetGame()->GetAudioSystem()->PlayEvent(name);

+ 2 - 1
Chapter09/AudioComponent.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -19,6 +19,7 @@ public:
 	~AudioComponent();
 
 	void Update(float deltaTime) override;
+	void OnUpdateWorldTransform() override;
 
 	SoundEvent PlayEvent(const std::string& name);
 	void StopAllEvents();

+ 6 - 5
Chapter09/AudioSystem.cpp

@@ -3,13 +3,13 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "AudioSystem.h"
 #include <SDL/SDL_log.h>
-#include <FMOD/fmod_studio.hpp>
-#include <FMOD/fmod_errors.h>
+#include <fmod_studio.hpp>
+#include <fmod_errors.h>
 #include <vector>
 
 unsigned int AudioSystem::sNextID = 0;
@@ -92,6 +92,7 @@ void AudioSystem::LoadBank(const std::string& name)
 		&bank // Save pointer to bank
 	);
 
+	const int maxPathLength = 512;
 	if (result == FMOD_OK)
 	{
 		// Add bank to map
@@ -106,12 +107,12 @@ void AudioSystem::LoadBank(const std::string& name)
 			// Get list of event descriptions in this bank
 			std::vector<FMOD::Studio::EventDescription*> events(numEvents);
 			bank->getEventList(events.data(), numEvents, &numEvents);
-			char eventName[512];
+			char eventName[maxPathLength];
 			for (int i = 0; i < numEvents; i++)
 			{
 				FMOD::Studio::EventDescription* e = events[i];
 				// Get the path of this event (like event:/Explosion2D)
-				e->getPath(eventName, 512, nullptr);
+				e->getPath(eventName, maxPathLength, nullptr);
 				// Add to event map
 				mEvents.emplace(eventName, e);
 			}

+ 1 - 1
Chapter09/AudioSystem.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter09/CameraComponent.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "CameraComponent.h"

+ 2 - 2
Chapter09/CameraComponent.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 0 - 0
Chapter09/Chapter10-windows.sln → Chapter09/Chapter09-windows.sln


+ 16 - 3
Chapter09/CircleComponent.cpp

@@ -1,16 +1,16 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "CircleComponent.h"
 #include "Actor.h"
 
 CircleComponent::CircleComponent(class Actor* owner)
-:CollisionComponent(owner)
+:Component(owner)
 ,mRadius(0.0f)
 {
 	
@@ -25,3 +25,16 @@ float CircleComponent::GetRadius() const
 {
 	return mOwner->GetScale() * mRadius;
 }
+
+bool Intersect(const CircleComponent& a, const CircleComponent& b)
+{
+	// Calculate distance squared
+	Vector3 diff = a.GetCenter() - b.GetCenter();
+	float distSq = diff.LengthSq();
+
+	// Calculate sum of radii squared
+	float radiiSq = a.GetRadius() + b.GetRadius();
+	radiiSq *= radiiSq;
+
+	return distSq <= radiiSq;
+}

+ 6 - 4
Chapter09/CircleComponent.h

@@ -1,16 +1,16 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
-#include "CollisionComponent.h"
+#include "Component.h"
 #include "Math.h"
 
-class CircleComponent : public CollisionComponent
+class CircleComponent : public Component
 {
 public:
 	CircleComponent(class Actor* owner);
@@ -22,3 +22,5 @@ public:
 private:
 	float mRadius;
 };
+
+bool Intersect(const CircleComponent& a, const CircleComponent& b);

+ 0 - 20
Chapter09/CollisionComponent.cpp

@@ -1,20 +0,0 @@
-// ----------------------------------------------------------------
-// From Game Programming in C++ by Sanjay Madhav
-// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
-// Released under the BSD License
-// See LICENSE.txt for full details.
-// ----------------------------------------------------------------
-
-#include "CollisionComponent.h"
-
-CollisionComponent::CollisionComponent(class Actor* owner,int updateOrder)
-:Component(owner, updateOrder)
-{
-	
-}
-
-CollisionComponent::~CollisionComponent()
-{
-	
-}

+ 0 - 17
Chapter09/CollisionComponent.h

@@ -1,17 +0,0 @@
-// ----------------------------------------------------------------
-// From Game Programming in C++ by Sanjay Madhav
-// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
-// Released under the BSD License
-// See LICENSE.txt for full details.
-// ----------------------------------------------------------------
-
-#pragma once
-#include "Component.h"
-class CollisionComponent : public Component
-{
-public:
-	CollisionComponent(class Actor* owner, int updateOrder = 100);
-	~CollisionComponent();
-};
-

+ 1 - 1
Chapter09/Component.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Component.h"

+ 7 - 1
Chapter09/Component.h

@@ -3,10 +3,12 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
+#include <cstdint>
+
 class Component
 {
 public:
@@ -17,6 +19,10 @@ public:
 	virtual ~Component();
 	// Update this component by delta time
 	virtual void Update(float deltaTime);
+	// Process input for this component
+	virtual void ProcessInput(const uint8_t* keyState) {}
+	// Called when world transform changes
+	virtual void OnUpdateWorldTransform() { }
 
 	int GetUpdateOrder() const { return mUpdateOrder; }
 protected:

+ 3 - 3
Chapter09/FPSActor.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "FPSActor.h"
@@ -60,7 +60,7 @@ void FPSActor::UpdateActor(float deltaTime)
 	mFPSModel->SetRotation(q);
 }
 
-void FPSActor::ProcessInput(const uint8_t* keys)
+void FPSActor::ActorInput(const uint8_t* keys)
 {
 	float forwardSpeed = 0.0f;
 	float strafeSpeed = 0.0f;

+ 3 - 3
Chapter09/FPSActor.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -16,7 +16,7 @@ public:
 	FPSActor(class Game* game);
 
 	void UpdateActor(float deltaTime) override;
-	void ProcessInput(const uint8_t* keys) override;
+	void ActorInput(const uint8_t* keys) override;
 
 	void SetFootstepSurface(float value);
 

+ 2 - 2
Chapter09/FPSCamera.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "FPSCamera.h"

+ 2 - 2
Chapter09/FPSCamera.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter09/FollowActor.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "FollowActor.h"
@@ -25,7 +25,7 @@ FollowActor::FollowActor(Game* game)
 	mCameraComp->SnapToIdeal();
 }
 
-void FollowActor::ProcessInput(const uint8_t* keys)
+void FollowActor::ActorInput(const uint8_t* keys)
 {
 	float forwardSpeed = 0.0f;
 	float angularSpeed = 0.0f;

+ 2 - 2
Chapter09/FollowActor.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -14,7 +14,7 @@ class FollowActor : public Actor
 public:
 	FollowActor(class Game* game);
 
-	void ProcessInput(const uint8_t* keys) override;
+	void ActorInput(const uint8_t* keys) override;
 
 	void SetVisible(bool visible);
 private:

+ 1 - 1
Chapter09/FollowCamera.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "FollowCamera.h"

+ 1 - 1
Chapter09/FollowCamera.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 53 - 34
Chapter09/Game.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Game.h"
@@ -23,6 +23,7 @@
 Game::Game()
 :mRenderer(nullptr)
 ,mIsRunning(true)
+,mUpdatingActors(false)
 {
 	
 }
@@ -37,7 +38,7 @@ bool Game::Initialize()
 
 	// Create the renderer
 	mRenderer = new Renderer(this);
-	if (!mRenderer->Initialize())
+	if (!mRenderer->Initialize(1024.0f, 768.0f))
 	{
 		SDL_Log("Failed to initialize renderer");
 		delete mRenderer;
@@ -87,11 +88,11 @@ void Game::ProcessInput()
 			case SDL_KEYDOWN:
 				if (!event.key.repeat)
 				{
-					HandleKeyPressed(event.key.keysym.sym);
+					HandleKeyPress(event.key.keysym.sym);
 				}
 				break;
 			case SDL_MOUSEBUTTONDOWN:
-				HandleKeyPressed(event.button.button);
+				HandleKeyPress(event.button.button);
 				break;
 			default:
 				break;
@@ -106,14 +107,11 @@ void Game::ProcessInput()
 
 	for (auto actor : mActors)
 	{
-		if (actor->GetState() == Actor::EActive)
-		{
-			actor->ProcessInput(state);
-		}
+		actor->ProcessInput(state);
 	}
 }
 
-void Game::HandleKeyPressed(int key)
+void Game::HandleKeyPress(int key)
 {
 	switch (key)
 	{
@@ -171,15 +169,21 @@ void Game::UpdateGame()
 	}
 	mTicksCount = SDL_GetTicks();
 
-	// Make copy of actor vector
-	// (iterate over this in case new actors are created)
-	std::vector<Actor*> copy = mActors;
-
 	// Update all actors
-	for (auto actor : copy)
+	mUpdatingActors = true;
+	for (auto actor : mActors)
 	{
 		actor->Update(deltaTime);
 	}
+	mUpdatingActors = false;
+
+	// Move any pending actors to mActors
+	for (auto pending : mPendingActors)
+	{
+		pending->ComputeWorldTransform();
+		mActors.emplace_back(pending);
+	}
+	mPendingActors.clear();
 
 	// Add any dead actors to a temp vector
 	std::vector<Actor*> deadActors;
@@ -191,8 +195,7 @@ void Game::UpdateGame()
 		}
 	}
 
-	// Delete any of the dead actors (which will
-	// remove them from mActors)
+	// Delete dead actors (which removes them from mActors)
 	for (auto actor : deadActors)
 	{
 		delete actor;
@@ -209,22 +212,21 @@ void Game::GenerateOutput()
 
 void Game::LoadData()
 {
-	mRenderer->LoadTexture("Assets/Default.png");
-	mRenderer->LoadTexture("Assets/HealthBar.png");
-	mRenderer->LoadTexture("Assets/Radar.png");
-	mRenderer->LoadTexture("Assets/Crosshair.png");
-
-	// Meshes
-	mRenderer->LoadMesh("Assets/Cube.gpmesh");
-	mRenderer->LoadMesh("Assets/Sphere.gpmesh");
-	mRenderer->LoadMesh("Assets/Plane.gpmesh");
-	mRenderer->LoadMesh("Assets/Rifle.gpmesh");
-	mRenderer->LoadMesh("Assets/RacingCar.gpmesh");
-
 	// Create actors
-	Actor* a = nullptr;
-	Quaternion q;
-	MeshComponent* mc = nullptr;
+	Actor* a = new Actor(this);
+	a->SetPosition(Vector3(200.0f, 75.0f, 0.0f));
+	a->SetScale(100.0f);
+	Quaternion q(Vector3::UnitY, -Math::PiOver2);
+	q = Quaternion::Concatenate(q, Quaternion(Vector3::UnitZ, Math::Pi + Math::Pi / 4.0f));
+	a->SetRotation(q);
+	MeshComponent* mc = new MeshComponent(a);
+	mc->SetMesh(mRenderer->GetMesh("Assets/Cube.gpmesh"));
+
+	a = new Actor(this);
+	a->SetPosition(Vector3(200.0f, -75.0f, 0.0f));
+	a->SetScale(3.0f);
+	mc = new MeshComponent(a);
+	mc->SetMesh(mRenderer->GetMesh("Assets/Sphere.gpmesh"));
 
 	// Setup floor
 	const float start = -1250.0f;
@@ -270,7 +272,6 @@ void Game::LoadData()
 	dir.mDirection = Vector3(0.0f, -0.707f, -0.707f);
 	dir.mDiffuseColor = Vector3(0.78f, 0.88f, 1.0f);
 	dir.mSpecColor = Vector3(0.8f, 0.8f, 0.8f);
-	dir.mSpecPower = 100.0f;
 
 	// UI elements
 	a = new Actor(this);
@@ -350,12 +351,30 @@ void Game::Shutdown()
 
 void Game::AddActor(Actor* actor)
 {
-	mActors.emplace_back(actor);
+	// If we're updating actors, need to add to pending
+	if (mUpdatingActors)
+	{
+		mPendingActors.emplace_back(actor);
+	}
+	else
+	{
+		mActors.emplace_back(actor);
+	}
 }
 
 void Game::RemoveActor(Actor* actor)
 {
-	auto iter = std::find(mActors.begin(), mActors.end(), actor);
+	// Is it in pending actors?
+	auto iter = std::find(mPendingActors.begin(), mPendingActors.end(), actor);
+	if (iter != mPendingActors.end())
+	{
+		// Swap to end of vector and pop off (avoid erase copies)
+		std::iter_swap(iter, mPendingActors.end() - 1);
+		mPendingActors.pop_back();
+	}
+
+	// Is it in actors?
+	iter = std::find(mActors.begin(), mActors.end(), actor);
 	if (iter != mActors.end())
 	{
 		// Swap to end of vector and pop off (avoid erase copies)

+ 8 - 4
Chapter09/Game.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -25,11 +25,11 @@ public:
 	void AddActor(class Actor* actor);
 	void RemoveActor(class Actor* actor);
 
-	class Renderer* GetRenderer() const { return mRenderer; }
-	class AudioSystem* GetAudioSystem() const { return mAudioSystem; }
+	class Renderer* GetRenderer() { return mRenderer; }
+	class AudioSystem* GetAudioSystem() { return mAudioSystem; }
 private:
 	void ProcessInput();
-	void HandleKeyPressed(int key);
+	void HandleKeyPress(int key);
 	void UpdateGame();
 	void GenerateOutput();
 	void LoadData();
@@ -37,12 +37,16 @@ private:
 	
 	// All the actors in the game
 	std::vector<class Actor*> mActors;
+	// Any pending actors
+	std::vector<class Actor*> mPendingActors;
 
 	class Renderer* mRenderer;
 	class AudioSystem* mAudioSystem;
 
 	Uint32 mTicksCount;
 	bool mIsRunning;
+	// Track if we're updating actors right now
+	bool mUpdatingActors;
 
 	// Game-specific code
 	class FPSActor* mFPSActor;

+ 15 - 12
Chapter09/Game.vcxproj

@@ -15,12 +15,11 @@
     <ClCompile Include="AudioComponent.cpp" />
     <ClCompile Include="AudioSystem.cpp" />
     <ClCompile Include="CameraComponent.cpp" />
+    <ClCompile Include="CircleComponent.cpp" />
+    <ClCompile Include="Component.cpp" />
     <ClCompile Include="FollowActor.cpp" />
     <ClCompile Include="FollowCamera.cpp" />
     <ClCompile Include="FPSActor.cpp" />
-    <ClCompile Include="CircleComponent.cpp" />
-    <ClCompile Include="CollisionComponent.cpp" />
-    <ClCompile Include="Component.cpp" />
     <ClCompile Include="FPSCamera.cpp" />
     <ClCompile Include="Game.cpp" />
     <ClCompile Include="Main.cpp" />
@@ -45,12 +44,11 @@
     <ClInclude Include="AudioComponent.h" />
     <ClInclude Include="AudioSystem.h" />
     <ClInclude Include="CameraComponent.h" />
+    <ClInclude Include="CircleComponent.h" />
+    <ClInclude Include="Component.h" />
     <ClInclude Include="FollowActor.h" />
     <ClInclude Include="FollowCamera.h" />
     <ClInclude Include="FPSActor.h" />
-    <ClInclude Include="CircleComponent.h" />
-    <ClInclude Include="CollisionComponent.h" />
-    <ClInclude Include="Component.h" />
     <ClInclude Include="FPSCamera.h" />
     <ClInclude Include="Game.h" />
     <ClInclude Include="Math.h" />
@@ -81,6 +79,7 @@
     <ProjectGuid>{BC508D87-495F-4554-932D-DD68388B63CC}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>Game</RootNamespace>
+    <WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -120,21 +119,23 @@
       <Optimization>Disabled</Optimization>
       <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <SDLCheck>true</SDLCheck>
-      <AdditionalIncludeDirectories>..\external\SDL\include;..\external\GLEW\include;..\external\SOIL\include;..\external\rapidjson\include;..\external\FMOD\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>..\external\SDL\include;..\external\GLEW\include;..\external\SOIL\include;..\external\rapidjson\include;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\inc;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\lowlevel\inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeTypeInfo>false</RuntimeTypeInfo>
       <ExceptionHandling>Sync</ExceptionHandling>
     </ClCompile>
     <Link>
       <SubSystem>Console</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalLibraryDirectories>..\external\SDL\lib\win\x86;..\external\GLEW\lib\win\x86;..\external\SOIL\lib\win\x86;..\external\FMOD\lib\win\x86\Logging;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>..\external\SDL\lib\win\x86;..\external\GLEW\lib\win\x86;..\external\SOIL\lib\win\x86;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\lib;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\lowlevel\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <AdditionalDependencies>opengl32.lib;SDL2.lib;SDL2main.lib;SDL2_ttf.lib;SDL2_mixer.lib;SDL2_image.lib;glew32.lib;SOIL.lib;fmodL_vc.lib;fmodstudioL_vc.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalOptions>/NODEFAULTLIB:msvcrt.lib %(AdditionalOptions)</AdditionalOptions>
     </Link>
     <PostBuildEvent>
       <Command>xcopy "$(ProjectDir)\..\external\SDL\lib\win\x86\*.dll" "$(OutDir)" /i /s /y
 xcopy "$(ProjectDir)\..\external\GLEW\lib\win\x86\*.dll" "$(OutDir)" /i /s /y
-xcopy "$(ProjectDir)\..\external\FMOD\lib\win\x86\Logging\*.dll" "$(OutDir)" /i /s /y</Command>
+xcopy "C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\lowlevel\lib\*.dll" "$(OutDir)" /i /s /y
+xcopy "C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\lib\*.dll" "$(OutDir)" /i /s /y
+</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -147,7 +148,7 @@ xcopy "$(ProjectDir)\..\external\FMOD\lib\win\x86\Logging\*.dll" "$(OutDir)" /i
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <SDLCheck>true</SDLCheck>
-      <AdditionalIncludeDirectories>..\external\SDL\include;..\external\GLEW\include;..\external\SOIL\include;..\external\rapidjson\include;..\external\FMOD\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>..\external\SDL\include;..\external\GLEW\include;..\external\SOIL\include;..\external\rapidjson\include;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\inc;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\lowlevel\inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeTypeInfo>false</RuntimeTypeInfo>
       <ExceptionHandling>Sync</ExceptionHandling>
     </ClCompile>
@@ -156,13 +157,15 @@ xcopy "$(ProjectDir)\..\external\FMOD\lib\win\x86\Logging\*.dll" "$(OutDir)" /i
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalLibraryDirectories>..\external\SDL\lib\win\x86;..\external\GLEW\lib\win\x86;..\external\SOIL\lib\win\x86;..\external\FMOD\lib\win\x86\Logging;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>..\external\SDL\lib\win\x86;..\external\GLEW\lib\win\x86;..\external\SOIL\lib\win\x86;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\lib;C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\lowlevel\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <AdditionalDependencies>opengl32.lib;SDL2.lib;SDL2main.lib;SDL2_ttf.lib;SDL2_mixer.lib;SDL2_image.lib;glew32.lib;SOIL.lib;fmodL_vc.lib;fmodstudioL_vc.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>xcopy "$(ProjectDir)\..\external\SDL\lib\win\x86\*.dll" "$(OutDir)" /i /s /y
 xcopy "$(ProjectDir)\..\external\GLEW\lib\win\x86\*.dll" "$(OutDir)" /i /s /y
-xcopy "$(ProjectDir)\..\external\FMOD\lib\win\x86\Logging\*.dll" "$(OutDir)" /i /s /y</Command>
+xcopy "C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\lowlevel\lib\*.dll" "$(OutDir)" /i /s /y
+xcopy "C:\Program Files (x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\lib\*.dll" "$(OutDir)" /i /s /y
+</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 10 - 16
Chapter09/Game.vcxproj.filters

@@ -31,9 +31,6 @@
     <ClCompile Include="CircleComponent.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="CollisionComponent.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="MoveComponent.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
@@ -67,19 +64,19 @@
     <ClCompile Include="AudioComponent.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="FPSActor.cpp">
+    <ClCompile Include="CameraComponent.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="CameraComponent.cpp">
+    <ClCompile Include="FollowActor.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="FPSCamera.cpp">
+    <ClCompile Include="FollowCamera.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="FollowActor.cpp">
+    <ClCompile Include="FPSActor.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="FollowCamera.cpp">
+    <ClCompile Include="FPSCamera.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
     <ClCompile Include="OrbitActor.cpp">
@@ -114,9 +111,6 @@
     <ClInclude Include="CircleComponent.h">
       <Filter>Source Files</Filter>
     </ClInclude>
-    <ClInclude Include="CollisionComponent.h">
-      <Filter>Source Files</Filter>
-    </ClInclude>
     <ClInclude Include="MoveComponent.h">
       <Filter>Source Files</Filter>
     </ClInclude>
@@ -150,19 +144,19 @@
     <ClInclude Include="AudioComponent.h">
       <Filter>Source Files</Filter>
     </ClInclude>
-    <ClInclude Include="FPSActor.h">
+    <ClInclude Include="CameraComponent.h">
       <Filter>Source Files</Filter>
     </ClInclude>
-    <ClInclude Include="CameraComponent.h">
+    <ClInclude Include="FollowActor.h">
       <Filter>Source Files</Filter>
     </ClInclude>
-    <ClInclude Include="FPSCamera.h">
+    <ClInclude Include="FollowCamera.h">
       <Filter>Source Files</Filter>
     </ClInclude>
-    <ClInclude Include="FollowActor.h">
+    <ClInclude Include="FPSActor.h">
       <Filter>Source Files</Filter>
     </ClInclude>
-    <ClInclude Include="FollowCamera.h">
+    <ClInclude Include="FPSCamera.h">
       <Filter>Source Files</Filter>
     </ClInclude>
     <ClInclude Include="OrbitActor.h">

+ 1 - 1
Chapter09/Main.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Game.h"

+ 17 - 18
Chapter09/Math.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Math.h"
@@ -99,12 +99,13 @@ Vector3 Vector3::Transform(const Vector3& v, const Quaternion& q)
 void Matrix4::Invert()
 {
 	// Thanks slow math
-	float tmp[12]; /* temp array for pairs */
-	float src[16]; /* array of transpose source matrix */
-	float dst[16]; /* storage */
-	float det; /* determinant */
-	/* transpose matrix */
+	// This is a really janky way to unroll everything...
+	float tmp[12];
+	float src[16];
+	float dst[16];
+	float det;
 
+	// Transpose matrix
 	// row 1 to col 1
 	src[0] = mat[0][0];
 	src[4] = mat[0][1];
@@ -129,13 +130,7 @@ void Matrix4::Invert()
 	src[11] = mat[3][2];
 	src[15] = mat[3][3];
 
-	// 	for (int i = 0; i < 4; i++) {
-	// 		src[i] = mat[i*4];
-	// 		src[i + 4] = mat[i*4 + 1];
-	// 		src[i + 8] = mat[i*4 + 2];
-	// 		src[i + 12] = mat[i*4 + 3];
-	// 	}
-	/* calculate pairs for first 8 elements (cofactors) */
+	// Calculate cofactors
 	tmp[0] = src[10] * src[15];
 	tmp[1] = src[11] * src[14];
 	tmp[2] = src[9] * src[15];
@@ -148,7 +143,7 @@ void Matrix4::Invert()
 	tmp[9] = src[10] * src[12];
 	tmp[10] = src[8] * src[13];
 	tmp[11] = src[9] * src[12];
-	/* calculate first 8 elements (cofactors) */
+	
 	dst[0] = tmp[0] * src[5] + tmp[3] * src[6] + tmp[4] * src[7];
 	dst[0] -= tmp[1] * src[5] + tmp[2] * src[6] + tmp[5] * src[7];
 	dst[1] = tmp[1] * src[4] + tmp[6] * src[6] + tmp[9] * src[7];
@@ -165,7 +160,7 @@ void Matrix4::Invert()
 	dst[6] -= tmp[2] * src[0] + tmp[7] * src[1] + tmp[10] * src[3];
 	dst[7] = tmp[4] * src[0] + tmp[9] * src[1] + tmp[10] * src[2];
 	dst[7] -= tmp[5] * src[0] + tmp[8] * src[1] + tmp[11] * src[2];
-	/* calculate pairs for second 8 elements (cofactors) */
+	
 	tmp[0] = src[2] * src[7];
 	tmp[1] = src[3] * src[6];
 	tmp[2] = src[1] * src[7];
@@ -178,7 +173,7 @@ void Matrix4::Invert()
 	tmp[9] = src[2] * src[4];
 	tmp[10] = src[0] * src[5];
 	tmp[11] = src[1] * src[4];
-	/* calculate second 8 elements (cofactors) */
+	
 	dst[8] = tmp[0] * src[13] + tmp[3] * src[14] + tmp[4] * src[15];
 	dst[8] -= tmp[1] * src[13] + tmp[2] * src[14] + tmp[5] * src[15];
 	dst[9] = tmp[1] * src[12] + tmp[6] * src[14] + tmp[9] * src[15];
@@ -195,12 +190,16 @@ void Matrix4::Invert()
 	dst[14] -= tmp[10] * src[11] + tmp[2] * src[8] + tmp[7] * src[9];
 	dst[15] = tmp[10] * src[10] + tmp[4] * src[8] + tmp[9] * src[9];
 	dst[15] -= tmp[8] * src[9] + tmp[11] * src[10] + tmp[5] * src[8];
-	/* calculate determinant */
+	
+	// Calculate determinant
 	det = src[0] * dst[0] + src[1] * dst[1] + src[2] * dst[2] + src[3] * dst[3];
-	/* calculate matrix inverse */
+	
+	// Inverse of matrix is divided by determinant
 	det = 1 / det;
 	for (int j = 0; j < 16; j++)
+	{
 		dst[j] *= det;
+	}
 
 	// Set it back
 	for (int i = 0; i < 4; i++)

+ 1 - 1
Chapter09/Math.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 6 - 3
Chapter09/Mesh.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Mesh.h"
@@ -19,6 +19,7 @@
 Mesh::Mesh()
 	:mVertexArray(nullptr)
 	,mRadius(0.0f)
+	,mSpecPower(100.0f)
 {
 }
 
@@ -26,7 +27,7 @@ Mesh::~Mesh()
 {
 }
 
-bool Mesh::Load(const std::string & fileName, Renderer* renderer)
+bool Mesh::Load(const std::string& fileName, Renderer* renderer)
 {
 	std::ifstream file(fileName);
 	if (!file.is_open())
@@ -71,6 +72,8 @@ bool Mesh::Load(const std::string & fileName, Renderer* renderer)
 		return false;
 	}
 
+	mSpecPower = static_cast<float>(doc["specularPower"].GetDouble());
+
 	for (rapidjson::SizeType i = 0; i < textures.Size(); i++)
 	{
 		// Is this texture already loaded?
@@ -79,7 +82,7 @@ bool Mesh::Load(const std::string & fileName, Renderer* renderer)
 		if (t == nullptr)
 		{
 			// Try loading the texture
-			t = renderer->LoadTexture(texName.c_str());
+			t = renderer->GetTexture(texName);
 			if (t == nullptr)
 			{
 				// If it's still null, just use the default texture

+ 5 - 1
Chapter09/Mesh.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -26,6 +26,8 @@ public:
 	const std::string& GetShaderName() const { return mShaderName; }
 	// Get object space bounding sphere radius
 	float GetRadius() const { return mRadius; }
+	// Get specular power of mesh
+	float GetSpecPower() const { return mSpecPower; }
 private:
 	// Textures associated with this mesh
 	std::vector<class Texture*> mTextures;
@@ -35,4 +37,6 @@ private:
 	std::string mShaderName;
 	// Stores object space bounding sphere radius
 	float mRadius;
+	// Specular power of surface
+	float mSpecPower;
 };

+ 4 - 2
Chapter09/MeshComponent.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "MeshComponent.h"
@@ -36,6 +36,8 @@ void MeshComponent::Draw(Shader* shader)
 		// Set the world transform
 		shader->SetMatrixUniform("uWorldTransform", 
 			mOwner->GetWorldTransform());
+		// Set specular power
+		shader->SetFloatUniform("uSpecPower", mMesh->GetSpecPower());
 		// Set the active texture
 		Texture* t = mMesh->GetTexture(mTextureIndex);
 		if (t)

+ 2 - 2
Chapter09/MeshComponent.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter09/MoveComponent.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "MoveComponent.h"

+ 2 - 2
Chapter09/MoveComponent.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter09/OrbitActor.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "OrbitActor.h"
@@ -23,7 +23,7 @@ OrbitActor::OrbitActor(Game* game)
 	mCameraComp = new OrbitCamera(this);
 }
 
-void OrbitActor::ProcessInput(const uint8_t* keys)
+void OrbitActor::ActorInput(const uint8_t* keys)
 {
 	// Mouse rotation
 	// Get relative movement from SDL

+ 2 - 2
Chapter09/OrbitActor.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -14,7 +14,7 @@ class OrbitActor : public Actor
 public:
 	OrbitActor(class Game* game);
 
-	void ProcessInput(const uint8_t* keys) override;
+	void ActorInput(const uint8_t* keys) override;
 
 	void SetVisible(bool visible);
 private:

+ 2 - 2
Chapter09/OrbitCamera.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "OrbitCamera.h"

+ 2 - 2
Chapter09/OrbitCamera.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 50 - 42
Chapter09/Renderer.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Renderer.h"
@@ -27,8 +27,11 @@ Renderer::~Renderer()
 {
 }
 
-bool Renderer::Initialize()
+bool Renderer::Initialize(float screenWidth, float screenHeight)
 {
+	mScreenWidth = screenWidth;
+	mScreenHeight = screenHeight;
+
 	// Set OpenGL attributes
 	// Use the core OpenGL profile
 	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
@@ -47,7 +50,7 @@ bool Renderer::Initialize()
 	SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
 
 	mWindow = SDL_CreateWindow("Game Programming in C++ (Chapter 9)", 100, 100,
-		1024, 768, SDL_WINDOW_OPENGL);
+		static_cast<int>(mScreenWidth), static_cast<int>(mScreenHeight), SDL_WINDOW_OPENGL);
 	if (!mWindow)
 	{
 		SDL_Log("Failed to create window: %s", SDL_GetError());
@@ -162,11 +165,22 @@ void Renderer::Draw()
 
 void Renderer::AddSprite(SpriteComponent* sprite)
 {
-	mSprites.emplace_back(sprite);
-	// Resort sprites by draw order
-	std::sort(mSprites.begin(), mSprites.end(), [](SpriteComponent* a, SpriteComponent* b) {
-		return a->GetDrawOrder() < b->GetDrawOrder();
-	});
+	// Find the insertion point in the sorted vector
+	// (The first element with a higher draw order than me)
+	int myDrawOrder = sprite->GetDrawOrder();
+	auto iter = mSprites.begin();
+	for (;
+		iter != mSprites.end();
+		++iter)
+	{
+		if (myDrawOrder < (*iter)->GetDrawOrder())
+		{
+			break;
+		}
+	}
+
+	// Inserts element before position of iterator
+	mSprites.insert(iter, sprite);
 }
 
 void Renderer::RemoveSprite(SpriteComponent* sprite)
@@ -186,21 +200,6 @@ void Renderer::RemoveMeshComp(MeshComponent* mesh)
 	mMeshComps.erase(iter);
 }
 
-Texture* Renderer::LoadTexture(const char* fileName)
-{
-	Texture* tex = new Texture();
-	if (tex->Load(fileName))
-	{
-		mTextures.emplace(fileName, tex);
-		return tex;
-	}
-	else
-	{
-		delete tex;
-		return nullptr;
-	}
-}
-
 Texture* Renderer::GetTexture(const std::string& fileName)
 {
 	Texture* tex = nullptr;
@@ -209,22 +208,20 @@ Texture* Renderer::GetTexture(const std::string& fileName)
 	{
 		tex = iter->second;
 	}
-	return tex;
-}
-
-Mesh* Renderer::LoadMesh(const char * fileName)
-{
-	Mesh* m = new Mesh();
-	if (m->Load(fileName, this))
-	{
-		mMeshes.emplace(fileName, m);
-		return m;
-	}
 	else
 	{
-		delete m;
-		return nullptr;
+		tex = new Texture();
+		if (tex->Load(fileName))
+		{
+			mTextures.emplace(fileName, tex);
+		}
+		else
+		{
+			delete tex;
+			tex = nullptr;
+		}
 	}
+	return tex;
 }
 
 Mesh* Renderer::GetMesh(const std::string & fileName)
@@ -235,6 +232,19 @@ Mesh* Renderer::GetMesh(const std::string & fileName)
 	{
 		m = iter->second;
 	}
+	else
+	{
+		m = new Mesh();
+		if (m->Load(fileName, this))
+		{
+			mMeshes.emplace(fileName, m);
+		}
+		else
+		{
+			delete m;
+			m = nullptr;
+		}
+	}
 	return m;
 }
 
@@ -242,19 +252,19 @@ bool Renderer::LoadShaders()
 {
 	// Create sprite shader
 	mSpriteShader = new Shader();
-	if (!mSpriteShader->Load("Shaders/Sprite"))
+	if (!mSpriteShader->Load("Shaders/Sprite.vert", "Shaders/Sprite.frag"))
 	{
 		return false;
 	}
 
 	mSpriteShader->SetActive();
 	// Set the view-projection matrix
-	Matrix4 viewProj = Matrix4::CreateSimpleViewProj(1024.f, 768.f);
+	Matrix4 viewProj = Matrix4::CreateSimpleViewProj(mScreenWidth, mScreenHeight);
 	mSpriteShader->SetMatrixUniform("uViewProj", viewProj);
 
 	// Create basic mesh shader
 	mMeshShader = new Shader();
-	if (!mMeshShader->Load("Shaders/Phong"))
+	if (!mMeshShader->Load("Shaders/Phong.vert", "Shaders/Phong.frag"))
 	{
 		return false;
 	}
@@ -263,7 +273,7 @@ bool Renderer::LoadShaders()
 	// Set the view-projection matrix
 	mView = Matrix4::CreateLookAt(Vector3::Zero, Vector3::UnitX, Vector3::UnitZ);
 	mProjection = Matrix4::CreatePerspectiveFOV(Math::ToRadians(70.0f),
-		1024.0f, 768.0f, 10.0f, 10000.0f);
+		mScreenWidth, mScreenHeight, 10.0f, 10000.0f);
 	mMeshShader->SetMatrixUniform("uViewProj", mView * mProjection);
 	return true;
 }
@@ -300,8 +310,6 @@ void Renderer::SetLightUniforms(Shader* shader)
 		mDirLight.mDiffuseColor);
 	shader->SetVectorUniform("uDirLight.mSpecColor",
 		mDirLight.mSpecColor);
-	shader->SetFloatUniform("uDirLight.mSpecPower",
-		mDirLight.mSpecPower);
 }
 
 Vector3 Renderer::Unproject(const Vector3& screenPoint) const

+ 7 - 6
Chapter09/Renderer.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -21,8 +21,6 @@ struct DirectionalLight
 	Vector3 mDiffuseColor;
 	// Specular color
 	Vector3 mSpecColor;
-	// Specular power
-	float mSpecPower;
 };
 
 class Renderer
@@ -31,7 +29,7 @@ public:
 	Renderer(class Game* game);
 	~Renderer();
 
-	bool Initialize();
+	bool Initialize(float screenWidth, float screenHeight);
 	void Shutdown();
 	void UnloadData();
 
@@ -43,9 +41,7 @@ public:
 	void AddMeshComp(class MeshComponent* mesh);
 	void RemoveMeshComp(class MeshComponent* mesh);
 
-	class Texture* LoadTexture(const char* fileName);
 	class Texture* GetTexture(const std::string& fileName);
-	class Mesh* LoadMesh(const char* fileName);
 	class Mesh* GetMesh(const std::string& fileName);
 
 	void SetViewMatrix(const Matrix4& view) { mView = view; }
@@ -60,6 +56,8 @@ public:
 	// y = [-screenHeight/2, +screenHeight/2]
 	// z = [0, 1) -- 0 is closer to camera, 1 is further
 	Vector3 Unproject(const Vector3& screenPoint) const;
+	float GetScreenWidth() const { return mScreenWidth; }
+	float GetScreenHeight() const { return mScreenHeight; }
 private:
 	bool LoadShaders();
 	void CreateSpriteVerts();
@@ -90,6 +88,9 @@ private:
 	// View/projection for 3D shaders
 	Matrix4 mView;
 	Matrix4 mProjection;
+	// Width/height of screen
+	float mScreenWidth;
+	float mScreenHeight;
 
 	// Lighting data
 	Vector3 mAmbientLight;

+ 12 - 12
Chapter09/Shader.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Shader.h"
@@ -25,15 +25,15 @@ Shader::~Shader()
 
 }
 
-bool Shader::Load(const std::string& name)
+bool Shader::Load(const std::string& vertName, const std::string& fragName)
 {
 	// Compile vertex and pixel shaders
-	if (!CompileShader(name + ".vert",
-					   GL_VERTEX_SHADER,
-					   mVertexShader) ||
-		!CompileShader(name + ".frag",
-					   GL_FRAGMENT_SHADER,
-					   mFragShader))
+	if (!CompileShader(vertName,
+		GL_VERTEX_SHADER,
+		mVertexShader) ||
+		!CompileShader(fragName,
+			GL_FRAGMENT_SHADER,
+			mFragShader))
 	{
 		return false;
 	}
@@ -91,14 +91,14 @@ void Shader::SetFloatUniform(const char* name, float value)
 }
 
 bool Shader::CompileShader(const std::string& fileName,
-				   GLenum shaderType,
-				   GLuint& outShader)
+	GLenum shaderType,
+	GLuint& outShader)
 {
 	// Open file
 	std::ifstream shaderFile(fileName);
 	if (shaderFile.is_open())
 	{
-		// Read all of the text into a string
+		// Read all the text into a string
 		std::stringstream sstream;
 		sstream << shaderFile.rdbuf();
 		std::string contents = sstream.str();

+ 4 - 5
Chapter09/Shader.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -16,9 +16,8 @@ class Shader
 public:
 	Shader();
 	~Shader();
-	// Load shader of the specified name, excluding
-	// the .frag/.vert extension
-	bool Load(const std::string& name);
+	// Load the vertex/fragment shaders with the given names
+	bool Load(const std::string& vertName, const std::string& fragName);
 	void Unload();
 	// Set this as the active shader program
 	void SetActive();

+ 2 - 2
Chapter09/Shaders/BasicMesh.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter09/Shaders/BasicMesh.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 7 - 8
Chapter09/Shaders/Phong.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3
@@ -32,15 +32,16 @@ struct DirectionalLight
 	vec3 mDiffuseColor;
 	// Specular color
 	vec3 mSpecColor;
-	// Specular power
-	float mSpecPower;
 };
 
 // Uniforms for lighting
 // Camera position (in world space)
 uniform vec3 uCameraPos;
+// Specular power for this surface
+uniform float uSpecPower;
 // Ambient light level
 uniform vec3 uAmbientLight;
+
 // Directional Light
 uniform DirectionalLight uDirLight;
 
@@ -60,12 +61,10 @@ void main()
 	float NdotL = dot(N, L);
 	if (NdotL > 0)
 	{
-		vec3 Diffuse = uDirLight.mDiffuseColor * dot(N, L);
-		vec3 Specular = uDirLight.mSpecColor * pow(max(0.0, dot(R, V)), uDirLight.mSpecPower);
+		vec3 Diffuse = uDirLight.mDiffuseColor * NdotL;
+		vec3 Specular = uDirLight.mSpecColor * pow(max(0.0, dot(R, V)), uSpecPower);
 		Phong += Diffuse + Specular;
 	}
-	// Clamp light between 0-1 RGB values
-	Phong = clamp(Phong, 0.0, 1.0);
 
 	// Final color is texture color times phong light (alpha = 1)
     outColor = texture(uTexture, fragTexCoord) * vec4(Phong, 1.0f);

+ 2 - 2
Chapter09/Shaders/Phong.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter09/Shaders/Sprite.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter09/Shaders/Sprite.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter09/SoundEvent.cpp

@@ -3,12 +3,12 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "SoundEvent.h"
 #include "AudioSystem.h"
-#include <FMOD/fmod_studio.hpp>
+#include <fmod_studio.hpp>
 
 SoundEvent::SoundEvent(class AudioSystem* system, unsigned int id)
 	:mSystem(system)

+ 1 - 1
Chapter09/SoundEvent.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter09/SplineActor.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "SplineActor.h"
@@ -41,7 +41,7 @@ SplineActor::SplineActor(Game* game)
 	mCameraComp->SetPaused(false);
 }
 
-void SplineActor::ProcessInput(const uint8_t* keys)
+void SplineActor::ActorInput(const uint8_t* keys)
 {
 	
 }

+ 2 - 2
Chapter09/SplineActor.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -14,7 +14,7 @@ class SplineActor : public Actor
 public:
 	SplineActor(class Game* game);
 
-	void ProcessInput(const uint8_t* keys) override;
+	void ActorInput(const uint8_t* keys) override;
 
 	void RestartSpline();
 private:

+ 2 - 2
Chapter09/SplineCamera.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "SplineCamera.h"

+ 2 - 2
Chapter09/SplineCamera.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 1 - 1
Chapter09/SpriteComponent.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "SpriteComponent.h"

+ 1 - 1
Chapter09/SpriteComponent.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter09/Texture.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Texture.h"

+ 2 - 2
Chapter09/Texture.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include <string>

+ 3 - 4
Chapter09/VertexArray.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "VertexArray.h"
@@ -26,7 +26,7 @@ VertexArray::VertexArray(const float* verts, unsigned int numVerts,
 	// Create index buffer
 	glGenBuffers(1, &mIndexBuffer);
 	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
-	glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(GLuint), indices, GL_STATIC_DRAW);
+	glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(unsigned int), indices, GL_STATIC_DRAW);
 
 	// Specify the vertex attributes
 	// (For now, assume one vertex format)
@@ -53,5 +53,4 @@ VertexArray::~VertexArray()
 void VertexArray::SetActive()
 {
 	glBindVertexArray(mVertexArray);
-	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
 }

+ 7 - 2
Chapter09/VertexArray.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -18,9 +18,14 @@ public:
 	unsigned int GetNumIndices() const { return mNumIndices; }
 	unsigned int GetNumVerts() const { return mNumVerts; }
 private:
+	// How many vertices in the vertex buffer?
 	unsigned int mNumVerts;
+	// How many indices in the index buffer
 	unsigned int mNumIndices;
+	// OpenGL ID of the vertex buffer
 	unsigned int mVertexBuffer;
+	// OpenGL ID of the index buffer
 	unsigned int mIndexBuffer;
+	// OpenGL ID of the vertex array object
 	unsigned int mVertexArray;
 };