Game.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #pragma once
  9. #include <unordered_map>
  10. #include <string>
  11. #include <vector>
  12. #include "Math.h"
  13. #include "SoundEvent.h"
  14. #include <SDL/SDL_types.h>
  15. class Game
  16. {
  17. public:
  18. Game();
  19. bool Initialize();
  20. void RunLoop();
  21. void Shutdown();
  22. void AddActor(class Actor* actor);
  23. void RemoveActor(class Actor* actor);
  24. class Renderer* GetRenderer() { return mRenderer; }
  25. class AudioSystem* GetAudioSystem() { return mAudioSystem; }
  26. class PhysWorld* GetPhysWorld() { return mPhysWorld; }
  27. class HUD* GetHUD() { return mHUD; }
  28. // Manage UI stack
  29. const std::vector<class UIScreen*>& GetUIStack() { return mUIStack; }
  30. void PushUI(class UIScreen* screen);
  31. class FollowActor* GetPlayer() { return mFollowActor; }
  32. enum GameState
  33. {
  34. EGameplay,
  35. EPaused,
  36. EQuit
  37. };
  38. GameState GetState() const { return mGameState; }
  39. void SetState(GameState state) { mGameState = state; }
  40. class Font* GetFont(const std::string& fileName);
  41. void LoadText(const std::string& fileName);
  42. const std::string& GetText(const std::string& key);
  43. class Skeleton* GetSkeleton(const std::string& fileName);
  44. class Animation* GetAnimation(const std::string& fileName);
  45. // Game-specific
  46. void AddPlane(class PlaneActor* plane);
  47. void RemovePlane(class PlaneActor* plane);
  48. std::vector<class PlaneActor*>& GetPlanes() { return mPlanes; }
  49. private:
  50. void ProcessInput();
  51. void HandleKeyPress(int key);
  52. void UpdateGame();
  53. void GenerateOutput();
  54. void LoadData();
  55. void UnloadData();
  56. // All the actors in the game
  57. std::vector<class Actor*> mActors;
  58. std::vector<class UIScreen*> mUIStack;
  59. // Map for fonts
  60. std::unordered_map<std::string, class Font*> mFonts;
  61. // Map of loaded skeletons
  62. std::unordered_map<std::string, class Skeleton*> mSkeletons;
  63. // Map of loaded animations
  64. std::unordered_map<std::string, class Animation*> mAnims;
  65. // Map for text localization
  66. std::unordered_map<std::string, std::string> mText;
  67. // Any pending actors
  68. std::vector<class Actor*> mPendingActors;
  69. class Renderer* mRenderer;
  70. class AudioSystem* mAudioSystem;
  71. class PhysWorld* mPhysWorld;
  72. class HUD* mHUD;
  73. Uint32 mTicksCount;
  74. GameState mGameState;
  75. // Track if we're updating actors right now
  76. bool mUpdatingActors;
  77. // Game-specific code
  78. class FollowActor* mFollowActor;
  79. std::vector<class PlaneActor*> mPlanes;
  80. class SpriteComponent* mCrosshair;
  81. SoundEvent mMusicEvent;
  82. };