| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- // ----------------------------------------------------------------
- // 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 "SDL/SDL_image.h"
- #include <algorithm>
- #include "Actor.h"
- #include "SpriteComponent.h"
- #include "Grid.h"
- #include "Enemy.h"
- #include "AIComponent.h"
- #include "AIState.h"
- Game::Game()
- :mWindow(nullptr)
- ,mRenderer(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;
- }
-
- mWindow = SDL_CreateWindow("Game Programming in C++ (Chapter 4)", 100, 100, 1024, 768, 0);
- if (!mWindow)
- {
- SDL_Log("Failed to create window: %s", SDL_GetError());
- return false;
- }
-
- mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- if (!mRenderer)
- {
- SDL_Log("Failed to create renderer: %s", SDL_GetError());
- return false;
- }
-
- if (IMG_Init(IMG_INIT_PNG) == 0)
- {
- SDL_Log("Unable to initialize SDL_image: %s", SDL_GetError());
- return false;
- }
- LoadData();
- mTicksCount = SDL_GetTicks();
-
- return true;
- }
- void Game::RunLoop()
- {
- while (mIsRunning)
- {
- ProcessInput();
- UpdateGame();
- GenerateOutput();
- }
- }
- void Game::ProcessInput()
- {
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- mIsRunning = false;
- break;
- }
- }
-
- const Uint8* keyState = SDL_GetKeyboardState(NULL);
- if (keyState[SDL_SCANCODE_ESCAPE])
- {
- mIsRunning = false;
- }
-
- if (keyState[SDL_SCANCODE_B])
- {
- mGrid->BuildTower();
- }
-
- // Process mouse
- int x, y;
- Uint32 buttons = SDL_GetMouseState(&x, &y);
- if (SDL_BUTTON(buttons) & SDL_BUTTON_LEFT)
- {
- mGrid->ProcessClick(x, y);
- }
- mUpdatingActors = true;
- for (auto actor : mActors)
- {
- actor->ProcessInput(keyState);
- }
- mUpdatingActors = false;
- }
- 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)
- {
- 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;
- }
- }
- void Game::GenerateOutput()
- {
- SDL_SetRenderDrawColor(mRenderer, 34, 139, 34, 255);
- SDL_RenderClear(mRenderer);
-
- // Draw all sprite components
- for (auto sprite : mSprites)
- {
- sprite->Draw(mRenderer);
- }
- SDL_RenderPresent(mRenderer);
- }
- void Game::LoadData()
- {
- mGrid = new Grid(this);
- // For testing AIComponent
- //Actor* a = new Actor(this);
- //AIComponent* aic = new AIComponent(a);
- //// Register states with AIComponent
- //aic->RegisterState(new AIPatrol(aic));
- //aic->RegisterState(new AIDeath(aic));
- //aic->RegisterState(new AIAttack(aic));
- //// Start in patrol state
- //aic->ChangeState("Patrol");
- }
- void Game::UnloadData()
- {
- // Delete actors
- // Because ~Actor calls RemoveActor, have to use a different style loop
- while (!mActors.empty())
- {
- delete mActors.back();
- }
- // Destroy textures
- for (auto i : mTextures)
- {
- SDL_DestroyTexture(i.second);
- }
- mTextures.clear();
- }
- SDL_Texture* Game::GetTexture(const std::string& fileName)
- {
- SDL_Texture* tex = nullptr;
- // Is the texture already in the map?
- auto iter = mTextures.find(fileName);
- if (iter != mTextures.end())
- {
- tex = iter->second;
- }
- else
- {
- // Load from file
- SDL_Surface* surf = IMG_Load(fileName.c_str());
- if (!surf)
- {
- SDL_Log("Failed to load texture file %s", fileName.c_str());
- return nullptr;
- }
- // Create texture from surface
- tex = SDL_CreateTextureFromSurface(mRenderer, surf);
- SDL_FreeSurface(surf);
- if (!tex)
- {
- SDL_Log("Failed to convert surface to texture for %s", fileName.c_str());
- return nullptr;
- }
- mTextures.emplace(fileName.c_str(), tex);
- }
- return tex;
- }
- void Game::Shutdown()
- {
- UnloadData();
- IMG_Quit();
- SDL_DestroyRenderer(mRenderer);
- SDL_DestroyWindow(mWindow);
- 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();
- }
- }
- void Game::AddSprite(SpriteComponent* sprite)
- {
- // 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 Game::RemoveSprite(SpriteComponent* sprite)
- {
- // (We can't swap because it ruins ordering)
- auto iter = std::find(mSprites.begin(), mSprites.end(), sprite);
- mSprites.erase(iter);
- }
- Enemy* Game::GetNearestEnemy(const Vector2& pos)
- {
- Enemy* best = nullptr;
-
- if (mEnemies.size() > 0)
- {
- best = mEnemies[0];
- // Save the distance squared of first enemy, and test if others are closer
- float bestDistSq = (pos - mEnemies[0]->GetPosition()).LengthSq();
- for (size_t i = 1; i < mEnemies.size(); i++)
- {
- float newDistSq = (pos - mEnemies[i]->GetPosition()).LengthSq();
- if (newDistSq < bestDistSq)
- {
- bestDistSq = newDistSq;
- best = mEnemies[i];
- }
- }
- }
-
- return best;
- }
|