💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
10 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef GAME_HPP
  20. #define GAME_HPP
  21. #include <emergent/emergent.hpp>
  22. using namespace Emergent;
  23. #include "entity/entity-id.hpp"
  24. #include <map>
  25. #include <string>
  26. #include <vector>
  27. class GameState;
  28. class SplashState;
  29. class SandboxState;
  30. class UIContainer;
  31. class UIBatcher;
  32. class UIImage;
  33. class UILabel;
  34. class UIRenderPass;
  35. class ClearRenderPass;
  36. class SkyRenderPass;
  37. class ShadowMapRenderPass;
  38. class LightingRenderPass;
  39. class SilhouetteRenderPass;
  40. class FinalRenderPass;
  41. class ResourceManager;
  42. typedef std::vector<std::vector<std::string>> CSVTable;
  43. class CameraRig;
  44. class OrbitCam;
  45. class FreeCam;
  46. class Tool;
  47. class Lens;
  48. class Forceps;
  49. class Brush;
  50. class ParticleSystem;
  51. class EntityManager;
  52. class ComponentManager;
  53. class SystemManager;
  54. class SoundSystem;
  55. class CollisionSystem;
  56. class RenderSystem;
  57. class ToolSystem;
  58. class BehaviorSystem;
  59. class SteeringSystem;
  60. class LocomotionSystem;
  61. class TestEvent: public Event<TestEvent>
  62. {
  63. public:
  64. inline EventBase* clone() const
  65. {
  66. TestEvent* event = new TestEvent();
  67. event->id = id;
  68. return event;
  69. }
  70. int id;
  71. };
  72. class Game:
  73. public Application,
  74. public EventHandler<TestEvent>
  75. {
  76. public:
  77. /**
  78. * Creates a game instance.
  79. *
  80. * @param argc Argument count
  81. * @param argv Argument list
  82. */
  83. Game(int argc, char* argv[]);
  84. /// Destroys a game instance.
  85. virtual ~Game();
  86. /**
  87. * Gets a string in the specified language.
  88. *
  89. * @param languageIndex Index of a language.
  90. * @param name Name of the string.
  91. * @return String in the specified language.
  92. */
  93. std::string getString(std::size_t languageIndex, const std::string& name) const;
  94. /**
  95. * Changes the current language.
  96. *
  97. * @param languageIndex Index of the language to use.
  98. */
  99. void changeLanguage(std::size_t languageIndex);
  100. /// Returns the number of available languages.
  101. std::size_t getLanguageCount() const;
  102. /// Returns the index of the current language.
  103. std::size_t getCurrentLanguage() const;
  104. void toggleFullscreen();
  105. void setUpdateRate(double frequency);
  106. /**
  107. * Changes the game state.
  108. *
  109. * @param state New game state.
  110. */
  111. void changeState(GameState* state);
  112. const EventDispatcher* getEventDispatcher() const;
  113. EventDispatcher* getEventDispatcher();
  114. const Animator* getAnimator() const;
  115. Animator* getAnimator();
  116. void fadeIn(float duration, const Vector3& color, std::function<void()> callback);
  117. void fadeOut(float duration, const Vector3& color, std::function<void()> callback);
  118. void selectTool(int toolIndex);
  119. void pushNotification(const std::string& text);
  120. void popNotification();
  121. private:
  122. virtual void setup();
  123. virtual void input();
  124. virtual void update(float t, float dt);
  125. virtual void render();
  126. virtual void exit();
  127. virtual void handleEvent(const WindowResizedEvent& event);
  128. virtual void handleEvent(const KeyPressedEvent& event);
  129. virtual void handleEvent(const TestEvent& event);
  130. void resizeUI(int w, int h);
  131. void restringUI();
  132. void resizeRenderTargets();
  133. void setTimeOfDay(float time);
  134. void toggleWireframe();
  135. void screenshot();
  136. void queueScreenshot();
  137. public:
  138. EntityID createInstanceOf(const std::string& templateName);
  139. void destroyInstance(EntityID entity);
  140. void setTranslation(EntityID entity, const Vector3& translation);
  141. void setRotation(EntityID entity, const Quaternion& rotation);
  142. void setScale(EntityID entity, const Vector3& scale);
  143. void boxSelect(float x, float y, float w, float h);
  144. public:
  145. // States
  146. GameState* currentState;
  147. SplashState* splashState;
  148. SandboxState* sandboxState;
  149. // Paths
  150. std::string dataPath;
  151. std::string configPath;
  152. // Localization
  153. CSVTable* stringTable;
  154. std::map<std::string, std::size_t> stringMap;
  155. std::size_t languageCount;
  156. std::size_t currentLanguage;
  157. // Window management
  158. Window* window;
  159. bool fullscreen;
  160. std::string title;
  161. int w, h;
  162. float dpi;
  163. float fontSizePT;
  164. float fontSizePX;
  165. // Input
  166. Mouse* mouse;
  167. Keyboard* keyboard;
  168. ControlProfile controlProfile;
  169. Control fullscreenControl;
  170. Control closeControl;
  171. Control openRadialMenuControl;
  172. Control moveForwardControl;
  173. Control moveBackControl;
  174. Control moveLeftControl;
  175. Control moveRightControl;
  176. Control rotateCCWControl;
  177. Control rotateCWControl;
  178. Control zoomInControl;
  179. Control zoomOutControl;
  180. Control adjustCameraControl;
  181. Control dragCameraControl;
  182. Control toggleNestViewControl;
  183. Control toggleWireframeControl;
  184. Control screenshotControl;
  185. Control toggleEditModeControl;
  186. // Logic
  187. float time;
  188. float timestep;
  189. // UI
  190. Typeface* labelTypeface;
  191. Font* labelFont;
  192. Typeface* debugTypeface;
  193. Font* debugFont;
  194. BillboardBatch* uiBatch;
  195. UIBatcher* uiBatcher;
  196. UIContainer* uiRootElement;
  197. UIImage* splashBackgroundImage;
  198. UIImage* splashImage;
  199. UIContainer* hudContainer;
  200. UIImage* toolIndicatorBGImage;
  201. UIImage* toolIndicatorIconImage;
  202. Rect* toolIndicatorsBounds;
  203. UIImage* toolIconBrushImage;
  204. UIImage* toolIconLensImage;
  205. UIImage* toolIconForcepsImage;
  206. UIImage* toolIconSpadeImage;
  207. UIImage* toolIconCameraImage;
  208. UIImage* toolIconTestTubeImage;
  209. UIContainer* buttonContainer;
  210. UIImage* playButtonBGImage;
  211. UIImage* fastForwardButtonBGImage;
  212. UIImage* pauseButtonBGImage;
  213. UIImage* playButtonImage;
  214. UIImage* fastForwardButtonImage;
  215. UIImage* pauseButtonImage;
  216. UIContainer* radialMenuContainer;
  217. UIImage* radialMenuBackgroundImage;
  218. UIImage* radialMenuImage;
  219. UIImage* radialMenuSelectorImage;
  220. UIImage* blackoutImage;
  221. UIImage* cameraFlashImage;
  222. UIImage* notificationBoxImage;
  223. int notificationCount;
  224. UIContainer* antTag;
  225. UIContainer* antLabelContainer;
  226. UILabel* fpsLabel;
  227. UILabel* antLabel;
  228. UIImage* antLabelTL; // Top-left
  229. UIImage* antLabelTR; // Top-right
  230. UIImage* antLabelBL; // Bottom-left
  231. UIImage* antLabelBR; // Bottom-right
  232. UIImage* antLabelCC; // Center-center
  233. UIImage* antLabelCT; // Center-top
  234. UIImage* antLabelCB; // Center-bottom
  235. UIImage* antLabelCL; // Center-left
  236. UIImage* antLabelCR; // Center-right
  237. UIImage* antLabelPinHole;
  238. UIImage* antPin;
  239. UIImage* boxSelectionImageBackground;
  240. UIImage* boxSelectionImageTop;
  241. UIImage* boxSelectionImageBottom;
  242. UIImage* boxSelectionImageLeft;
  243. UIImage* boxSelectionImageRight;
  244. UIContainer* boxSelectionContainer;
  245. float boxSelectionBorderWidth;
  246. UIImage* cameraGridY0Image;
  247. UIImage* cameraGridY1Image;
  248. UIImage* cameraGridX0Image;
  249. UIImage* cameraGridX1Image;
  250. UIContainer* cameraGridContainer;
  251. Vector4 cameraGridColor;
  252. // Rendering
  253. Renderer renderer;
  254. RenderTarget defaultRenderTarget;
  255. ClearRenderPass* clearPass;
  256. ClearRenderPass* clearSilhouettePass;
  257. SkyRenderPass* skyPass;
  258. UIRenderPass* uiPass;
  259. Compositor uiCompositor;
  260. Compositor defaultCompositor;
  261. int shadowMapResolution;
  262. GLuint shadowMapDepthTextureID;
  263. GLuint shadowMapFramebuffer;
  264. RenderTarget shadowMapRenderTarget;
  265. ShadowMapRenderPass* shadowMapPass;
  266. Compositor shadowMapCompositor;
  267. Texture2D shadowMapDepthTexture;
  268. LightingRenderPass* lightingPass;
  269. SilhouetteRenderPass* silhouettePass;
  270. FinalRenderPass* finalPass;
  271. RenderTarget silhouetteRenderTarget;
  272. // Scene
  273. Scene* scene;
  274. SceneLayer* defaultLayer;
  275. SceneLayer* uiLayer;
  276. DirectionalLight sunlight;
  277. Camera camera;
  278. Camera sunlightCamera;
  279. Camera uiCamera;
  280. // Animation
  281. Animator animator;
  282. Animation<float> fadeInAnimation;
  283. Animation<float> fadeOutAnimation;
  284. AnimationClip<float> fadeInClip;
  285. AnimationClip<float> fadeOutClip;
  286. std::function<void()> fadeInEndCallback;
  287. std::function<void()> fadeOutEndCallback;
  288. Animation<Vector2> showNotificationAnimation;
  289. Animation<float> hideNotificationAnimation;
  290. AnimationClip<Vector2> showNotificationClip;
  291. AnimationClip<float> hideNotificationClip;
  292. Animation<float> cameraFlashAnimation;
  293. AnimationClip<float> cameraFlashClip;
  294. // Assets
  295. ResourceManager* resourceManager;
  296. Texture2D* splashTexture;
  297. Texture2D* hudSpriteSheetTexture;
  298. TextureAtlas hudTextureAtlas;
  299. Material* smokeMaterial;
  300. Model* lensModel;
  301. Model* forcepsModel;
  302. Model* brushModel;
  303. // Game
  304. CameraRig* cameraRig;
  305. OrbitCam* orbitCam;
  306. FreeCam* freeCam;
  307. Tool* currentTool;
  308. Lens* lens;
  309. Forceps* forceps;
  310. Brush* brush;
  311. ParticleSystem* particleSystem;
  312. // ECS
  313. EntityManager* entityManager;
  314. ComponentManager* componentManager;
  315. SystemManager* systemManager;
  316. SoundSystem* soundSystem;
  317. CollisionSystem* collisionSystem;
  318. RenderSystem* renderSystem;
  319. ToolSystem* toolSystem;
  320. BehaviorSystem* behaviorSystem;
  321. SteeringSystem* steeringSystem;
  322. LocomotionSystem* locomotionSystem;
  323. EntityID lollipop;
  324. bool wireframe;
  325. bool screenshotQueued;
  326. private:
  327. static void saveScreenshot(const std::string& filename, unsigned int width, unsigned int height, unsigned char* pixels);
  328. };
  329. inline const EventDispatcher* Game::getEventDispatcher() const
  330. {
  331. return &eventDispatcher;
  332. }
  333. inline EventDispatcher* Game::getEventDispatcher()
  334. {
  335. return &eventDispatcher;
  336. }
  337. inline const Animator* Game::getAnimator() const
  338. {
  339. return &animator;
  340. }
  341. inline Animator* Game::getAnimator()
  342. {
  343. return &animator;
  344. }
  345. inline std::size_t Game::getLanguageCount() const
  346. {
  347. return languageCount;
  348. }
  349. inline std::size_t Game::getCurrentLanguage() const
  350. {
  351. return currentLanguage;
  352. }
  353. #endif // GAME_HPP