| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- // ----------------------------------------------------------------
- // 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 "Game.h"
- #include <algorithm>
- #include "Renderer.h"
- #include "AudioSystem.h"
- #include "PhysWorld.h"
- #include "Actor.h"
- #include "SpriteComponent.h"
- #include "MeshComponent.h"
- #include "FPSActor.h"
- #include "PlaneActor.h"
- #include "TargetActor.h"
- #include "BallActor.h"
- Game::Game()
- :mRenderer(nullptr)
- ,mAudioSystem(nullptr)
- ,mPhysWorld(nullptr)
- ,mIsRunning(true)
- ,mUpdatingActors(false)
- {
-
- }
- bool Game::Initialize()
- {
- if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0)
- {
- SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
- return false;
- }
- // Create the renderer
- mRenderer = new Renderer(this);
- if (!mRenderer->Initialize(1024.0f, 768.0f))
- {
- SDL_Log("Failed to initialize renderer");
- delete mRenderer;
- mRenderer = nullptr;
- return false;
- }
- // Create the audio system
- mAudioSystem = new AudioSystem(this);
- if (!mAudioSystem->Initialize())
- {
- SDL_Log("Failed to initialize audio system");
- mAudioSystem->Shutdown();
- delete mAudioSystem;
- mAudioSystem = nullptr;
- return false;
- }
- // Create the physics world
- mPhysWorld = new PhysWorld(this);
- LoadData();
- mTicksCount = SDL_GetTicks();
-
- return true;
- }
- void Game::RunLoop()
- {
- while (mIsRunning)
- {
- ProcessInput();
- UpdateGame();
- GenerateOutput();
- }
- }
- void Game::AddPlane(PlaneActor* plane)
- {
- mPlanes.emplace_back(plane);
- }
- void Game::RemovePlane(PlaneActor* plane)
- {
- auto iter = std::find(mPlanes.begin(), mPlanes.end(), plane);
- mPlanes.erase(iter);
- }
- void Game::ProcessInput()
- {
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- mIsRunning = false;
- break;
- // This fires when a key's initially pressed
- case SDL_KEYDOWN:
- if (!event.key.repeat)
- {
- HandleKeyPress(event.key.keysym.sym);
- }
- break;
- case SDL_MOUSEBUTTONDOWN:
- HandleKeyPress(event.button.button);
- break;
- default:
- break;
- }
- }
-
- const Uint8* state = SDL_GetKeyboardState(NULL);
- if (state[SDL_SCANCODE_ESCAPE])
- {
- mIsRunning = false;
- }
- for (auto actor : mActors)
- {
- actor->ProcessInput(state);
- }
- }
- void Game::HandleKeyPress(int key)
- {
- switch (key)
- {
- case '-':
- {
- // Reduce master volume
- float volume = mAudioSystem->GetBusVolume("bus:/");
- volume = Math::Max(0.0f, volume - 0.1f);
- mAudioSystem->SetBusVolume("bus:/", volume);
- break;
- }
- case '=':
- {
- // Increase master volume
- float volume = mAudioSystem->GetBusVolume("bus:/");
- volume = Math::Min(1.0f, volume + 0.1f);
- mAudioSystem->SetBusVolume("bus:/", volume);
- break;
- }
- case SDL_BUTTON_LEFT:
- {
- // Fire weapon
- mFPSActor->Shoot();
- break;
- }
- default:
- break;
- }
- }
- void Game::UpdateGame()
- {
- // Compute delta time
- // Wait until 16ms has elapsed since last frame
- while (!SDL_TICKS_PASSED(SDL_GetTicks(), mTicksCount + 16))
- ;
- float deltaTime = (SDL_GetTicks() - mTicksCount) / 1000.0f;
- if (deltaTime > 0.05f)
- {
- deltaTime = 0.05f;
- }
- mTicksCount = SDL_GetTicks();
- // Update all actors
- 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;
- for (auto actor : mActors)
- {
- if (actor->GetState() == Actor::EDead)
- {
- deadActors.emplace_back(actor);
- }
- }
- // Delete dead actors (which removes them from mActors)
- for (auto actor : deadActors)
- {
- delete actor;
- }
- // Update audio system
- mAudioSystem->Update(deltaTime);
- }
- void Game::GenerateOutput()
- {
- mRenderer->Draw();
- }
- void Game::LoadData()
- {
- // Create actors
- Actor* a = nullptr;
- Quaternion q;
- //MeshComponent* mc = nullptr;
- // Setup floor
- const float start = -1250.0f;
- const float size = 250.0f;
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- a = new PlaneActor(this);
- a->SetPosition(Vector3(start + i * size, start + j * size, -100.0f));
- }
- }
- // Left/right walls
- q = Quaternion(Vector3::UnitX, Math::PiOver2);
- for (int i = 0; i < 10; i++)
- {
- a = new PlaneActor(this);
- a->SetPosition(Vector3(start + i * size, start - size, 0.0f));
- a->SetRotation(q);
-
- a = new PlaneActor(this);
- a->SetPosition(Vector3(start + i * size, -start + size, 0.0f));
- a->SetRotation(q);
- }
- q = Quaternion::Concatenate(q, Quaternion(Vector3::UnitZ, Math::PiOver2));
- // Forward/back walls
- for (int i = 0; i < 10; i++)
- {
- a = new PlaneActor(this);
- a->SetPosition(Vector3(start - size, start + i * size, 0.0f));
- a->SetRotation(q);
- a = new PlaneActor(this);
- a->SetPosition(Vector3(-start + size, start + i * size, 0.0f));
- a->SetRotation(q);
- }
- // Setup lights
- mRenderer->SetAmbientLight(Vector3(0.2f, 0.2f, 0.2f));
- DirectionalLight& dir = mRenderer->GetDirectionalLight();
- 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);
- // UI elements
- a = new Actor(this);
- a->SetPosition(Vector3(-350.0f, -350.0f, 0.0f));
- SpriteComponent* sc = new SpriteComponent(a);
- sc->SetTexture(mRenderer->GetTexture("Assets/HealthBar.png"));
- a = new Actor(this);
- a->SetPosition(Vector3(-390.0f, 275.0f, 0.0f));
- a->SetScale(0.75f);
- sc = new SpriteComponent(a);
- sc->SetTexture(mRenderer->GetTexture("Assets/Radar.png"));
- a = new Actor(this);
- a->SetScale(2.0f);
- mCrosshair = new SpriteComponent(a);
- mCrosshair->SetTexture(mRenderer->GetTexture("Assets/Crosshair.png"));
- // Start music
- mMusicEvent = mAudioSystem->PlayEvent("event:/Music");
- // Enable relative mouse mode for camera look
- SDL_SetRelativeMouseMode(SDL_TRUE);
- // Make an initial call to get relative to clear out
- SDL_GetRelativeMouseState(nullptr, nullptr);
- // Different camera actors
- mFPSActor = new FPSActor(this);
- // Create target actors
- a = new TargetActor(this);
- a->SetPosition(Vector3(1450.0f, 0.0f, 100.0f));
- a = new TargetActor(this);
- a->SetPosition(Vector3(1450.0f, 0.0f, 400.0f));
- a = new TargetActor(this);
- a->SetPosition(Vector3(1450.0f, -500.0f, 200.0f));
- a = new TargetActor(this);
- a->SetPosition(Vector3(1450.0f, 500.0f, 200.0f));
- }
- void Game::UnloadData()
- {
- // Delete actors
- // Because ~Actor calls RemoveActor, have to use a different style loop
- while (!mActors.empty())
- {
- delete mActors.back();
- }
- if (mRenderer)
- {
- mRenderer->UnloadData();
- }
- }
- void Game::Shutdown()
- {
- UnloadData();
- delete mPhysWorld;
- if (mRenderer)
- {
- mRenderer->Shutdown();
- }
- if (mAudioSystem)
- {
- mAudioSystem->Shutdown();
- }
- SDL_Quit();
- }
- void Game::AddActor(Actor* 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)
- {
- // 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)
- std::iter_swap(iter, mActors.end() - 1);
- mActors.pop_back();
- }
- }
|