💿🐜 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.

218 lines
6.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017 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. #include "title-state.hpp"
  20. #include "main-menu-state.hpp"
  21. #include "../application.hpp"
  22. #include "../camera-controller.hpp"
  23. #include <iostream>
  24. #include <SDL2/SDL.h>
  25. const float blankDuration = 0.0f;
  26. const float fadeInDuration = 0.5f;
  27. const float hangDuration = 1.0f;
  28. const float fadeOutDuration = 0.5f;
  29. const float titleDelay = 2.0f;
  30. const float copyrightDelay = 3.0f;
  31. const float pressAnyKeyDelay = 5.0f;
  32. TitleState::TitleState(Application* application):
  33. ApplicationState(application)
  34. {}
  35. TitleState::~TitleState()
  36. {}
  37. void TitleState::enter()
  38. {
  39. // Setup screen fade-in transition
  40. fadeIn = false;
  41. fadeOut = false;
  42. application->backgroundLayer->addObject(&application->bgCamera);
  43. application->backgroundLayer->addObject(&application->bgBatch);
  44. // Title ant hill
  45. application->defaultLayer->addObject(&application->antHillModelInstance);
  46. application->inputManager->addWindowObserver(this);
  47. windowResized(application->width, application->height);
  48. // Setup camera controller
  49. application->surfaceCam->setCamera(&application->camera);
  50. application->surfaceCam->setFocalPoint(Vector3(0.0f));
  51. application->surfaceCam->setFocalDistance(50.0f);
  52. application->surfaceCam->setElevation(glm::radians(0.0f));
  53. application->surfaceCam->setAzimuth(glm::radians(0.0f));
  54. application->surfaceCam->setTargetFocalPoint(application->surfaceCam->getFocalPoint());
  55. application->surfaceCam->setTargetFocalDistance(application->surfaceCam->getFocalDistance());
  56. application->surfaceCam->setTargetElevation(application->surfaceCam->getElevation());
  57. application->surfaceCam->setTargetAzimuth(application->surfaceCam->getAzimuth());
  58. application->surfaceCam->update(0.0f);
  59. // Setup fade-in
  60. application->blackoutImage->setVisible(true);
  61. application->fadeInTween->start();
  62. // Start timer
  63. stateTime = 0.0f;
  64. substate = 0;
  65. }
  66. void TitleState::execute()
  67. {
  68. // Add dt to state time
  69. stateTime += application->dt;
  70. if (substate == 0 || substate == 1)
  71. {
  72. if (stateTime >= titleDelay && !application->titleImage->isVisible())
  73. {
  74. application->titleImage->setVisible(true);
  75. application->titleFadeInTween->start();
  76. application->titleScreenInfoContainer->setVisible(true);
  77. application->copyrightFadeInTween->start();
  78. }
  79. if (stateTime >= pressAnyKeyDelay && !application->anyKeyLabel->isVisible())
  80. {
  81. application->anyKeyLabel->setVisible(true);
  82. application->anyKeyFadeInTween->start();
  83. }
  84. }
  85. if (substate == 0 && stateTime >= titleDelay && application->titleFadeInTween->isStopped())
  86. {
  87. substate = 1;
  88. }
  89. // Listen for fade-in skip and "press any key"
  90. if (substate < 2)
  91. {
  92. InputEvent event;
  93. application->inputManager->listen(&event);
  94. if (event.type != InputEvent::Type::NONE)
  95. {
  96. application->menuControlProfile->update();
  97. application->inputManager->update();
  98. // Check if application was closed
  99. if (application->escape.isTriggered())
  100. {
  101. application->close(EXIT_SUCCESS);
  102. return;
  103. }
  104. // Check if fullscreen was toggled
  105. else if (application->toggleFullscreen.isTriggered() && !application->toggleFullscreen.wasTriggered())
  106. {
  107. application->changeFullscreen();
  108. }
  109. else if (!application->menuCancel.isTriggered())
  110. {
  111. if (substate == 0)
  112. {
  113. // Remove fade-in
  114. substate = 1;
  115. application->fadeInTween->stop();
  116. application->blackoutImage->setTintColor(Vector4(0.0f));
  117. application->blackoutImage->setVisible(false);
  118. application->titleFadeInTween->stop();
  119. application->titleImage->setVisible(true);
  120. application->titleImage->setTintColor(Vector4(1.0f));
  121. application->anyKeyFadeInTween->start();
  122. application->anyKeyLabel->setVisible(true);
  123. application->copyrightFadeInTween->stop();
  124. application->titleScreenInfoContainer->setVisible(true);
  125. application->titleScreenInfoContainer->setTintColor(Vector4(0.0f, 0.0f, 0.0f, 0.5f));
  126. }
  127. else if (substate == 1)
  128. {
  129. // Enter main menu
  130. substate = 2;
  131. application->titleFadeInTween->stop();
  132. application->titleFadeOutTween->start();
  133. application->anyKeyFadeInTween->stop();
  134. application->anyKeyFadeOutTween->stop();
  135. application->anyKeyLabel->setVisible(false);
  136. application->copyrightFadeInTween->stop();
  137. application->copyrightFadeOutTween->start();
  138. application->antHillZoomInTween->start();
  139. application->blackoutImage->setVisible(true);
  140. application->antHillFadeOutTween->start();
  141. }
  142. }
  143. }
  144. }
  145. application->surfaceCam->update(application->dt);
  146. }
  147. void TitleState::exit()
  148. {
  149. application->titleImage->setVisible(false);
  150. application->anyKeyLabel->setVisible(false);
  151. application->titleScreenInfoContainer->setVisible(false);
  152. // Remove objects from scene
  153. application->defaultLayer->removeObject(&application->antHillModelInstance);
  154. application->backgroundLayer->removeObject(&application->bgCamera);
  155. application->backgroundLayer->removeObject(&application->bgBatch);
  156. application->inputManager->removeWindowObserver(this);
  157. }
  158. void TitleState::windowClosed()
  159. {
  160. application->close(EXIT_SUCCESS);
  161. }
  162. void TitleState::windowResized(int width, int height)
  163. {
  164. // Update application dimensions
  165. application->width = width;
  166. application->height = height;
  167. if (application->fullscreen)
  168. {
  169. application->fullscreenWidth = width;
  170. application->fullscreenHeight = height;
  171. }
  172. else
  173. {
  174. application->windowedWidth = width;
  175. application->windowedHeight = height;
  176. }
  177. // Setup default render target
  178. application->defaultRenderTarget.width = application->width;
  179. application->defaultRenderTarget.height = application->height;
  180. // Resize UI
  181. application->resizeUI();
  182. // 3D camera
  183. application->camera.setPerspective(
  184. glm::radians(25.0f),
  185. (float)application->width / (float)application->height,
  186. 0.1f,
  187. 1000.0f);
  188. }