Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Chapter07/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

Game::Game()
:mRenderer(nullptr)
,mAudioSystem(nullptr)
,mIsRunning(true)
,mUpdatingActors(false)
{
Expand Down
1 change: 1 addition & 0 deletions Chapter09/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

Game::Game()
:mRenderer(nullptr)
,mAudioSystem(nullptr)
,mIsRunning(true)
,mUpdatingActors(false)
{
Expand Down
13 changes: 13 additions & 0 deletions Chapter09/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,16 @@ Vector3 Renderer::Unproject(const Vector3& screenPoint) const
unprojection.Invert();
return Vector3::TransformWithPerspDiv(deviceCoord, unprojection);
}

void Renderer::GetScreenDirection(Vector3& outStart, Vector3& outDir) const
{
// Get start point (in center of screen on near plane)
Vector3 screenPoint(0.0f, 0.0f, 0.0f);
outStart = Unproject(screenPoint);
// Get end point (in center of screen, between near and far)
screenPoint.z = 0.9f;
Vector3 end = Unproject(screenPoint);
// Get direction vector
outDir = end - outStart;
outDir.Normalize();
}
1 change: 1 addition & 0 deletions Chapter09/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Renderer
// y = [-screenHeight/2, +screenHeight/2]
// z = [0, 1) -- 0 is closer to camera, 1 is further
Vector3 Unproject(const Vector3& screenPoint) const;
void GetScreenDirection(Vector3& outStart, Vector3& outDir) const;
float GetScreenWidth() const { return mScreenWidth; }
float GetScreenHeight() const { return mScreenHeight; }
private:
Expand Down
149 changes: 149 additions & 0 deletions Chapter10/Actor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#include "Actor.h"
#include "Game.h"
#include "Component.h"
#include <algorithm>

Actor::Actor(Game* game)
:mState(EActive)
,mPosition(Vector3::Zero)
,mRotation(Quaternion::Identity)
,mScale(1.0f)
,mGame(game)
,mRecomputeWorldTransform(true)
{
mGame->AddActor(this);
}

Actor::~Actor()
{
mGame->RemoveActor(this);
// Need to delete components
// Because ~Component calls RemoveComponent, need a different style loop
while (!mComponents.empty())
{
delete mComponents.back();
}
}

void Actor::Update(float deltaTime)
{
if (mState == EActive)
{
ComputeWorldTransform();

UpdateComponents(deltaTime);
UpdateActor(deltaTime);

ComputeWorldTransform();
}
}

void Actor::UpdateComponents(float deltaTime)
{
for (auto comp : mComponents)
{
comp->Update(deltaTime);
}
}

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::RotateToNewForward(const Vector3& forward)
{
// Figure out difference between original (unit x) and new
float dot = Vector3::Dot(Vector3::UnitX, forward);
float angle = Math::Acos(dot);
// Facing down X
if (dot > 0.9999f)
{
SetRotation(Quaternion::Identity);
}
// Facing down -X
else if (dot < -0.9999f)
{
SetRotation(Quaternion(Vector3::UnitZ, Math::Pi));
}
else
{
// Rotate about axis from cross product
Vector3 axis = Vector3::Cross(Vector3::UnitX, forward);
axis.Normalize();
SetRotation(Quaternion(axis, angle));
}
}

void Actor::ComputeWorldTransform()
{
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)
{
// 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)
{
auto iter = std::find(mComponents.begin(), mComponents.end(), component);
if (iter != mComponents.end())
{
mComponents.erase(iter);
}
}
77 changes: 77 additions & 0 deletions Chapter10/Actor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------

#pragma once
#include <vector>
#include "Math.h"
#include <cstdint>

class Actor
{
public:
enum State
{
EActive,
EPaused,
EDead
};

Actor(class Game* game);
virtual ~Actor();

// Update function called from Game (not overridable)
void Update(float deltaTime);
// Updates all the components attached to the actor (not overridable)
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 ActorInput(const uint8_t* keyState);

// Getters/setters
const Vector3& GetPosition() const { return mPosition; }
void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeWorldTransform = true; }
float GetScale() const { return mScale; }
void SetScale(float scale) { mScale = scale; mRecomputeWorldTransform = true; }
const Quaternion& GetRotation() const { return mRotation; }
void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeWorldTransform = true; }

void ComputeWorldTransform();
const Matrix4& GetWorldTransform() const { return mWorldTransform; }

Vector3 GetForward() const { return Vector3::Transform(Vector3::UnitX, mRotation); }
Vector3 GetRight() const { return Vector3::Transform(Vector3::UnitY, mRotation); }

void RotateToNewForward(const Vector3& forward);

State GetState() const { return mState; }
void SetState(State state) { mState = state; }

class Game* GetGame() { return mGame; }


// Add/remove components
void AddComponent(class Component* component);
void RemoveComponent(class Component* component);
private:
// Actor's state
State mState;

// Transform
Matrix4 mWorldTransform;
Vector3 mPosition;
Quaternion mRotation;
float mScale;
bool mRecomputeWorldTransform;

std::vector<class Component*> mComponents;
class Game* mGame;
};
Binary file added Chapter10/Assets/Crosshair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions Chapter10/Assets/Cube.gpmesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"version":1,
"vertexformat":"PosNormTex",
"shader":"BasicMesh",
"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],
[-0.5,0.5,-0.5,0,0,-1,0,-1],
[0.5,0.5,-0.5,0,0,-1,1,-1],
[-0.5,0.5,0.5,0,1,0,0,-1],
[0.5,0.5,0.5,0,1,0,1,-1],
[-0.5,-0.5,0.5,0,0,1,0,0],
[0.5,-0.5,0.5,0,0,1,1,0],
[-0.5,0.5,-0.5,0,0,-1,0,-1],
[0.5,-0.5,-0.5,0,0,-1,1,0],
[-0.5,0.5,-0.5,0,1,0,0,-1],
[0.5,0.5,-0.5,0,1,0,1,-1],
[-0.5,0.5,0.5,0,1,0,0,-1],
[-0.5,0.5,0.5,0,0,1,0,-1],
[0.5,0.5,0.5,0,0,1,1,-1],
[-0.5,-0.5,0.5,0,0,1,0,0],
[-0.5,-0.5,0.5,0,-1,0,0,0],
[0.5,-0.5,0.5,0,-1,0,1,0],
[-0.5,-0.5,-0.5,0,-1,0,0,0],
[0.5,-0.5,-0.5,0,-1,0,1,0],
[0.5,-0.5,-0.5,1,0,0,1,0],
[0.5,-0.5,0.5,1,0,0,1,0],
[0.5,0.5,-0.5,1,0,0,1,-1],
[0.5,0.5,0.5,1,0,0,1,-1],
[-0.5,-0.5,0.5,-1,0,0,0,0],
[-0.5,-0.5,-0.5,-1,0,0,0,0],
[-0.5,0.5,0.5,-1,0,0,0,-1],
[-0.5,0.5,-0.5,-1,0,0,0,-1]
],
"indices":[
[2,1,0],
[3,9,8],
[4,11,10],
[5,11,12],
[6,14,13],
[7,14,15],
[18,17,16],
[19,17,18],
[22,21,20],
[23,21,22],
[26,25,24],
[27,25,26]
]
}
Binary file added Chapter10/Assets/Cube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter10/Assets/Default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter10/Assets/HealthBar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Chapter10/Assets/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Racing Car by Willy Decarpentrie, licensed under CC Attribution and downloaded from https://sketchfab.com
Binary file added Chapter10/Assets/Master Bank.bank
Binary file not shown.
Binary file added Chapter10/Assets/Master Bank.strings.bank
Binary file not shown.
70 changes: 70 additions & 0 deletions Chapter10/Assets/Plane.gpmesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"version":1,
"vertexformat":"PosNormTex",
"shader":"BasicMesh",
"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],
[25.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.750000,1.000000],
[25.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.750000,0.750000],
[0.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.500000,1.000000],
[50.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,1.000000,0.500000],
[0.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.500000,0.750000],
[-25.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.250000,1.000000],
[25.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.750000,0.500000],
[50.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,1.000000,0.250000],
[-25.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.250000,0.750000],
[-50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.000000,1.000000],
[-50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.000000,0.750000],
[0.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.500000,0.500000],
[25.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.750000,0.250000],
[50.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,1.000000,0.000000],
[25.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.750000,0.000000],
[-25.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.250000,0.500000],
[-50.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.000000,0.500000],
[0.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.500000,0.250000],
[0.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.500000,0.000000],
[-25.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.250000,0.250000],
[-50.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.000000,0.250000],
[-25.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.250000,0.000000],
[-50.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.000000,0.000000]
],
"indices":[
[0,1,2],
[3,2,1],
[2,3,4],
[1,5,3],
[6,4,3],
[4,6,7],
[8,3,5],
[3,8,6],
[5,9,8],
[10,7,6],
[7,10,11],
[12,11,10],
[13,6,8],
[6,13,10],
[14,8,9],
[8,14,13],
[9,15,14],
[16,14,15],
[10,17,12],
[17,10,13],
[18,12,17],
[19,13,14],
[14,16,19],
[13,19,17],
[20,19,16],
[17,21,18],
[21,17,19],
[19,20,21],
[22,18,21],
[23,21,20],
[21,23,22],
[24,22,23]
]
}
Binary file added Chapter10/Assets/Plane.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading