|
|
@@ -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)
|