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

380 lines
11 KiB

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 "experiment-state.hpp"
  21. #include "../application.hpp"
  22. #include <iostream>
  23. #include <SDL.h>
  24. const float blankDuration = 0.0f;
  25. const float fadeInDuration = 0.5f;
  26. const float hangDuration = 1.0f;
  27. const float fadeOutDuration = 0.5f;
  28. const float titleDelay = 2.0f;
  29. const float copyrightDelay = 3.0f;
  30. const float pressAnyKeyDelay = 5.0f;
  31. TitleState::TitleState(Application* application):
  32. ApplicationState(application)
  33. {}
  34. TitleState::~TitleState()
  35. {}
  36. void TitleState::enter()
  37. {
  38. std::cout << "Entering TitleState..." << std::endl;
  39. // Setup screen fade-in transition
  40. fadeIn = false;
  41. fadeOut = false;
  42. /*
  43. // Load tunnel texture
  44. GLuint tunnelTexture;
  45. loadTexture("data/textures/soil-01.png", &tunnelTexture);
  46. // Setup triplanar texturing
  47. Material* material = (Material*)application->displayModel->getMaterial(0);
  48. material->setRoughness(0.75f);
  49. material->setDiffuseColor(glm::vec3(1.0f));
  50. material->setSpecularColor(glm::vec3(0.001f));
  51. material->setOpacity(0.999f);
  52. Texture* texture = material->createTexture();
  53. texture->setTextureID(tunnelTexture);
  54. texture->setCoordinateSource(TextureCoordinateSource::TRIPLANAR_PROJECTION);
  55. texture->setCoordinateScale(glm::vec3(1.0f / 2.0f));
  56. texture->setDiffuseInfluence(1.0f);
  57. */
  58. application->displayModelInstance->setModel(application->displayModel);
  59. application->displayModelInstance->setTransform(Transform::getIdentity());
  60. application->antModelInstance->setModel(application->antModel);
  61. application->antModelInstance->setTransform(Transform::getIdentity());
  62. // Setup lighting
  63. application->sunlight.setColor(glm::vec3(1.0f));
  64. application->sunlight.setDirection(glm::normalize(glm::vec3(0.5, -1, -0.5)));
  65. // Setup lighting pass
  66. application->lightingPass.setRenderTarget(&application->defaultRenderTarget);
  67. application->lightingPass.setShadowMap(0);
  68. application->lightingPass.setShadowCamera(&application->camera);
  69. application->defaultCompositor.addPass(&application->lightingPass);
  70. application->camera.lookAt(
  71. glm::vec3(0.0f, 0.0f, 10.0f),
  72. glm::vec3(0.0f, 0.0f, 0.0f),
  73. glm::vec3(0.0f, 1.0f, 0.0f));
  74. application->camera.setCompositor(&application->defaultCompositor);
  75. application->camera.setCompositeIndex(0);
  76. // Setup scene
  77. DirectionalLight* lightA = new DirectionalLight();
  78. DirectionalLight* lightB = new DirectionalLight();
  79. DirectionalLight* lightC = new DirectionalLight();
  80. lightA->setColor(glm::vec3(1.0f));
  81. lightB->setColor(glm::vec3(0.25f));
  82. lightC->setColor(glm::vec3(1.0f, 1.0f, 1.0f));
  83. lightA->setDirection(glm::normalize(glm::vec3(0.0, -0.8, -0.2)));
  84. lightB->setDirection(glm::normalize(glm::vec3(1.0, -.2, 0.0f)));
  85. lightC->setDirection(glm::normalize(glm::vec3(0.0, 1.0, 0.0)));
  86. //application->scene.addObject(&application->sunlight);
  87. application->scene.getLayer(0)->addObject(lightA);
  88. application->scene.getLayer(0)->addObject(lightB);
  89. application->scene.getLayer(0)->addObject(lightC);
  90. application->scene.getLayer(0)->addObject(application->displayModelInstance);
  91. application->scene.getLayer(0)->addObject(application->antModelInstance);
  92. application->scene.getLayer(0)->addObject(&application->camera);
  93. // Load compositor
  94. RenderQueue renderQueue;
  95. const std::list<SceneObject*>* objects = application->scene.getLayer(0)->getObjects();
  96. for (const SceneObject* object: *objects)
  97. renderQueue.queue(object);
  98. RenderContext renderContext;
  99. renderContext.camera = nullptr;
  100. renderContext.layer = application->scene.getLayer(0);
  101. renderContext.queue = &renderQueue;
  102. application->defaultCompositor.load(&renderContext);
  103. // Setup fade-in
  104. application->blackoutImage->setVisible(true);
  105. application->fadeInTween->start();
  106. application->inputManager->addWindowObserver(this);
  107. windowResized(application->width, application->height);
  108. // Start timer
  109. stateTime = 0.0f;
  110. application->frameTimer.reset();
  111. application->frameTimer.start();
  112. substate = 0;
  113. }
  114. void TitleState::execute()
  115. {
  116. // Calculate delta time (in seconds)
  117. float dt = static_cast<float>(application->frameTimer.microseconds().count()) / 1000000.0f;
  118. application->frameTimer.reset();
  119. // Add dt to state time
  120. stateTime += dt;
  121. if (substate == 0 || substate == 1)
  122. {
  123. if (stateTime >= titleDelay && !application->titleImage->isVisible())
  124. {
  125. application->titleImage->setVisible(true);
  126. application->titleFadeInTween->start();
  127. }
  128. if (stateTime >= copyrightDelay && !application->copyrightImage->isVisible())
  129. {
  130. //application->copyrightImage->setVisible(true);
  131. //application->copyrightFadeInTween->start();
  132. }
  133. if (stateTime >= pressAnyKeyDelay && !application->anyKeyLabel->isVisible())
  134. {
  135. application->anyKeyLabel->setVisible(true);
  136. application->anyKeyFadeInTween->start();
  137. }
  138. }
  139. if (substate == 0 && stateTime >= titleDelay && application->titleFadeInTween->isStopped())
  140. {
  141. substate = 1;
  142. }
  143. // Listen for fade-in skip and "press any key"
  144. if (substate < 2)
  145. {
  146. InputEvent event;
  147. application->inputManager->listen(&event);
  148. if (event.type != InputEvent::Type::NONE)
  149. {
  150. application->menuControlProfile->update();
  151. application->inputManager->update();
  152. // Check if application was closed
  153. if (application->escape.isTriggered())
  154. {
  155. application->close(EXIT_SUCCESS);
  156. return;
  157. }
  158. // Check if fullscreen was toggled
  159. else if (application->toggleFullscreen.isTriggered() && !application->toggleFullscreen.wasTriggered())
  160. {
  161. application->changeFullscreen();
  162. }
  163. else if (!application->menuCancel.isTriggered())
  164. {
  165. if (substate == 0)
  166. {
  167. // Remove fade-in
  168. substate = 1;
  169. application->fadeInTween->stop();
  170. application->blackoutImage->setTintColor(Vector4(0.0f));
  171. application->blackoutImage->setVisible(false);
  172. application->titleFadeInTween->stop();
  173. application->titleImage->setVisible(true);
  174. application->titleImage->setTintColor(Vector4(1.0f));
  175. application->anyKeyFadeInTween->start();
  176. application->anyKeyLabel->setVisible(true);
  177. }
  178. else if (substate == 1)
  179. {
  180. substate = 2;
  181. application->titleFadeInTween->stop();
  182. application->titleFadeOutTween->start();
  183. //application->copyrightFadeInTween->stop();
  184. //application->copyrightFadeOutTween->start();
  185. application->anyKeyFadeInTween->stop();
  186. application->anyKeyFadeOutTween->stop();
  187. application->anyKeyLabel->setVisible(false);
  188. application->enterMenu(0);
  189. application->menuSelectorLabel->setVisible(true);
  190. //enterMenu(application, &mainMenu);
  191. }
  192. }
  193. }
  194. }
  195. // Check state time
  196. if (!fadeIn && stateTime >= blankDuration)
  197. {
  198. // Begin fade-in
  199. fadeIn = true;
  200. }
  201. // Update display model
  202. Transform transform = application->displayModelInstance->getTransform();
  203. transform.translation = Vector3(0, 0.0f, 0);
  204. transform.scale = Vector3(0.75f);
  205. transform.rotation = glm::angleAxis(stateTime * glm::radians(360.0f) / 60.0f, glm::vec3(0, 1, 0));
  206. application->displayModelInstance->setTransform(transform);
  207. transform.scale = Vector3(0.25f);
  208. transform.translation.y = 0.0f;
  209. //application->antModelInstance->setTransform(transform);
  210. // Update menu controls
  211. application->menuControlProfile->update();
  212. // Update input
  213. application->inputManager->update();
  214. // Check if application was closed
  215. if (application->inputManager->wasClosed() || application->escape.isTriggered())
  216. {
  217. application->close(EXIT_SUCCESS);
  218. return;
  219. }
  220. // Check if fullscreen was toggled
  221. if (application->toggleFullscreen.isTriggered() && !application->toggleFullscreen.wasTriggered())
  222. {
  223. application->changeFullscreen();
  224. }
  225. // Navigate menu
  226. if (application->menuDown.isTriggered() && !application->menuDown.wasTriggered())
  227. {
  228. if (application->selectedMenuItemIndex < application->currentMenu->getItemCount() - 1)
  229. {
  230. application->selectMenuItem(application->selectedMenuItemIndex + 1);
  231. }
  232. else
  233. {
  234. application->selectMenuItem(0);
  235. }
  236. }
  237. else if (application->menuUp.isTriggered() && !application->menuUp.wasTriggered())
  238. {
  239. if (application->selectedMenuItemIndex > 0)
  240. {
  241. application->selectMenuItem(application->selectedMenuItemIndex - 1);
  242. }
  243. else
  244. {
  245. application->selectMenuItem(application->currentMenu->getItemCount() - 1);
  246. }
  247. }
  248. if (application->menuSelect.isTriggered() && !application->menuSelect.wasTriggered())
  249. {
  250. application->activateMenuItem(application->selectedMenuItemIndex);
  251. }
  252. else if (application->menuCancel.isTriggered() && !application->menuCancel.wasTriggered())
  253. {
  254. }
  255. float lineHeight = application->menuFont->getMetrics().getHeight();
  256. const UIContainer* container = application->menuContainers[application->currentMenuIndex];
  257. application->menuSelectorLabel->setTranslation(
  258. Vector2(container->getPosition().x - application->menuSelectorLabel->getDimensions().x * 1.5f,
  259. container->getPosition().y + lineHeight * 0.5f - application->menuSelectorLabel->getDimensions().y * 0.5f + lineHeight * application->selectedMenuItemIndex));
  260. // Perform tweening
  261. application->tweener->update(dt);
  262. // Update UI
  263. application->uiRootElement->update();
  264. // Clear to black
  265. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  266. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  267. // Render scene
  268. application->renderer.render(application->scene);
  269. // Form billboard batch for UI then render UI scene
  270. application->uiBatcher->batch(application->uiBatch, application->uiRootElement);
  271. application->renderer.render(application->uiScene);
  272. // Swap buffers
  273. SDL_GL_SwapWindow(application->window);
  274. }
  275. void TitleState::exit()
  276. {
  277. std::cout << "Exiting TitleState..." << std::endl;
  278. application->inputManager->removeWindowObserver(this);
  279. application->exitMenu(application->currentMenuIndex);
  280. application->menuSelectorLabel->setVisible(false);
  281. application->scene.removeLayers();
  282. }
  283. void TitleState::windowClosed()
  284. {
  285. application->close(EXIT_SUCCESS);
  286. }
  287. void TitleState::windowResized(int width, int height)
  288. {
  289. // Update application dimensions
  290. application->width = width;
  291. application->height = height;
  292. if (application->fullscreen)
  293. {
  294. application->fullscreenWidth = width;
  295. application->fullscreenHeight = height;
  296. }
  297. else
  298. {
  299. application->windowedWidth = width;
  300. application->windowedHeight = height;
  301. }
  302. // Setup default render target
  303. application->defaultRenderTarget.width = application->width;
  304. application->defaultRenderTarget.height = application->height;
  305. // Resize UI
  306. application->resizeUI();
  307. // 3D camera
  308. application->camera.setPerspective(
  309. glm::radians(25.0f),
  310. (float)application->width / (float)application->height,
  311. 0.1f,
  312. 1000.0f);
  313. }