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

655 lines
35 KiB

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 "splash-state.hpp"
  20. #include "title-state.hpp"
  21. #include "experiment-state.hpp"
  22. #include "../application.hpp"
  23. #include "../camera-controller.hpp"
  24. #include "../debug.hpp"
  25. #include "../model-loader.hpp"
  26. #include <iostream>
  27. #include <cstdio>
  28. #include <SDL.h>
  29. const float blankDuration = 0.0f;
  30. const float fadeInDuration = 0.5f;
  31. const float hangDuration = 1.0f;
  32. const float fadeOutDuration = 0.5f;
  33. SplashState::SplashState(Application* application):
  34. ApplicationState(application)
  35. {}
  36. SplashState::~SplashState()
  37. {}
  38. void SplashState::enter()
  39. {
  40. std::cout << "Entering SplashState..." << std::endl;
  41. application->scene.addLayer();
  42. application->uiScene.addLayer();
  43. // Debug
  44. application->lineBatcher = new LineBatcher(4096);
  45. BillboardBatch* lineBatch = application->lineBatcher->getBatch();
  46. lineBatch->setAlignment(&application->camera, BillboardAlignmentMode::CYLINDRICAL);
  47. lineBatch->setAlignmentVector(Vector3(1, 0, 0));
  48. application->scene.getLayer(0)->addObject(lineBatch);
  49. // Load menu font
  50. application->menuFont = new Font(512, 512);
  51. FontLoader* fontLoader = new FontLoader();
  52. if (!fontLoader->load("data/fonts/Varela-Regular.ttf", application->fontSizePX, application->menuFont))
  53. {
  54. std::cerr << "Failed to load font" << std::endl;
  55. }
  56. delete fontLoader;
  57. // Load splash & title textures
  58. application->textureLoader->setGamma(1.0f);
  59. application->textureLoader->setCubemap(false);
  60. application->textureLoader->setMipmapChain(false);
  61. application->textureLoader->setMaxAnisotropy(1.0f);
  62. application->splashTexture = application->textureLoader->load("data/textures/galileo_cross.hdr");
  63. application->titleTexture = application->textureLoader->load("data/textures/title.png");
  64. // Get UI strings
  65. std::string pressAnyKeyString;
  66. std::string backString;
  67. std::string challengeString;
  68. std::string experimentString;
  69. std::string settingsString;
  70. std::string quitString;
  71. std::string loadString;
  72. std::string newString;
  73. std::string videoString;
  74. std::string audioString;
  75. std::string controlsString;
  76. std::string gameString;
  77. std::string resumeString;
  78. std::string returnToMainMenuString;
  79. std::string quitToDesktopString;
  80. application->strings.get("press-any-key", &pressAnyKeyString);
  81. application->strings.get("back", &backString);
  82. application->strings.get("challenge", &challengeString);
  83. application->strings.get("experiment", &experimentString);
  84. application->strings.get("settings", &settingsString);
  85. application->strings.get("quit", &quitString);
  86. application->strings.get("load", &loadString);
  87. application->strings.get("new", &newString);
  88. application->strings.get("video", &videoString);
  89. application->strings.get("audio", &audioString);
  90. application->strings.get("controls", &controlsString);
  91. application->strings.get("game", &gameString);
  92. application->strings.get("resume", &resumeString);
  93. application->strings.get("return-to-main-menu", &returnToMainMenuString);
  94. application->strings.get("quit-to-desktop", &quitToDesktopString);
  95. // Colors
  96. application->selectedColor = Vector4(1.0f, 1.0f, 1.0f, 1.0f);
  97. application->deselectedColor = Vector4(0.35f, 0.35f, 0.35f, 1.0f);
  98. // Build UI
  99. application->uiRootElement = new UIContainer();
  100. application->uiRootElement->setDimensions(Vector2(application->width, application->height));
  101. application->mouse->addMouseMotionObserver(application->uiRootElement);
  102. application->mouse->addMouseButtonObserver(application->uiRootElement);
  103. application->blackoutImage = new UIImage();
  104. application->blackoutImage->setDimensions(Vector2(application->width, application->height));
  105. application->blackoutImage->setLayerOffset(99);
  106. application->blackoutImage->setTintColor(Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  107. application->blackoutImage->setVisible(false);
  108. application->uiRootElement->addChild(application->blackoutImage);
  109. application->splashImage = new UIImage();
  110. application->splashImage->setAnchor(Anchor::CENTER);
  111. application->splashImage->setDimensions(Vector2(application->splashTexture->getWidth(), application->splashTexture->getHeight()));
  112. application->splashImage->setTexture(application->splashTexture);
  113. application->splashImage->setVisible(false);
  114. application->uiRootElement->addChild(application->splashImage);
  115. application->titleImage = new UIImage();
  116. application->titleImage->setAnchor(Vector2(0.5f, 0.0f));
  117. application->titleImage->setDimensions(Vector2(application->titleTexture->getWidth(), application->titleTexture->getHeight()));
  118. application->titleImage->setTranslation(Vector2(0.0f, (int)(application->height * (1.0f / 3.0f) - application->titleTexture->getHeight())));
  119. application->titleImage->setTexture(application->titleTexture);
  120. application->titleImage->setVisible(false);
  121. application->uiRootElement->addChild(application->titleImage);
  122. /*
  123. application->copyrightImage = new UIImage();
  124. application->copyrightImage->setAnchor(Vector2(0.5f, 1.0f));
  125. application->copyrightImage->setDimensions(Vector2(copyrightTextureWidth, copyrightTextureHeight));
  126. application->copyrightImage->setTranslation(Vector2(-.5f, (int)(-application->height * (1.0f / 10.0f) - copyrightTextureHeight * 0.5f)));
  127. application->copyrightImage->setTexture(nullptr);
  128. application->copyrightImage->setVisible(false);
  129. application->uiRootElement->addChild(application->copyrightImage);
  130. */
  131. application->anyKeyLabel = new UILabel();
  132. application->anyKeyLabel->setAnchor(Vector2(0.5f, 1.0f));
  133. application->anyKeyLabel->setFont(application->menuFont);
  134. application->anyKeyLabel->setTranslation(Vector2(0.0f, (int)(-application->height * (1.0f / 3.0f)/* - application->menuFont->getMetrics().getHeight() * 0.5f*/)));
  135. application->anyKeyLabel->setText(pressAnyKeyString);
  136. application->anyKeyLabel->setVisible(false);
  137. application->uiRootElement->addChild(application->anyKeyLabel);
  138. application->menuSelectorLabel = new UILabel();
  139. application->menuSelectorLabel->setAnchor(Anchor::TOP_LEFT);
  140. application->menuSelectorLabel->setFont(application->menuFont);
  141. application->menuSelectorLabel->setText(">");
  142. /*
  143. application->menuSelectorLabel = new UIImage();
  144. application->menuSelectorLabel->setAnchor(Anchor::TOP_LEFT);
  145. application->menuSelectorLabel->setDimensions(Vector2(selectorTextureWidth, selectorTextureHeight));
  146. application->menuSelectorLabel->setTextureID(selectorTextureID);
  147. */
  148. application->menuSelectorLabel->setVisible(false);
  149. application->uiRootElement->addChild(application->menuSelectorLabel);
  150. application->mainMenuContainer = new UIContainer();
  151. application->mainMenuContainer->setDimensions(Vector2(application->width, application->menuFont->getMetrics().getHeight() * 4));
  152. application->mainMenuContainer->setAnchor(Vector2(0.0f, 0.5f));
  153. application->mainMenuContainer->setVisible(false);
  154. application->mainMenuContainer->setActive(false);
  155. application->uiRootElement->addChild(application->mainMenuContainer);
  156. application->challengeLabel = new UILabel();
  157. application->challengeLabel->setFont(application->menuFont);
  158. application->challengeLabel->setText(challengeString);
  159. application->challengeLabel->setTintColor(application->deselectedColor);
  160. application->experimentLabel = new UILabel();
  161. application->experimentLabel->setFont(application->menuFont);
  162. application->experimentLabel->setText(experimentString);
  163. application->experimentLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight()));
  164. application->experimentLabel->setTintColor(application->deselectedColor);
  165. application->settingsLabel = new UILabel();
  166. application->settingsLabel->setFont(application->menuFont);
  167. application->settingsLabel->setText(settingsString);
  168. application->settingsLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 2));
  169. application->settingsLabel->setTintColor(application->deselectedColor);
  170. application->quitLabel = new UILabel();
  171. application->quitLabel->setFont(application->menuFont);
  172. application->quitLabel->setText(quitString);
  173. application->quitLabel->setTintColor(application->deselectedColor);
  174. application->quitLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 3));
  175. application->mainMenuContainer->addChild(application->challengeLabel);
  176. application->mainMenuContainer->addChild(application->experimentLabel);
  177. application->mainMenuContainer->addChild(application->settingsLabel);
  178. application->mainMenuContainer->addChild(application->quitLabel);
  179. application->challengeMenuContainer = new UIContainer();
  180. application->challengeMenuContainer->setDimensions(Vector2(application->width, application->menuFont->getMetrics().getHeight() * 4));
  181. application->challengeMenuContainer->setAnchor(Vector2(0.0f, 0.5f));
  182. application->challengeMenuContainer->setVisible(false);
  183. application->challengeMenuContainer->setActive(false);
  184. application->uiRootElement->addChild(application->challengeMenuContainer);
  185. application->experimentMenuContainer = new UIContainer();
  186. application->experimentMenuContainer->setDimensions(Vector2(application->width, application->menuFont->getMetrics().getHeight() * 3));
  187. application->experimentMenuContainer->setAnchor(Vector2(0.0f, 0.5f));
  188. application->experimentMenuContainer->setVisible(false);
  189. application->experimentMenuContainer->setActive(false);
  190. application->uiRootElement->addChild(application->experimentMenuContainer);
  191. application->loadLabel = new UILabel();
  192. application->loadLabel->setFont(application->menuFont);
  193. application->loadLabel->setText(loadString);
  194. application->loadLabel->setTintColor(application->deselectedColor);
  195. application->loadLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 0));
  196. application->experimentMenuContainer->addChild(application->loadLabel);
  197. application->newLabel = new UILabel();
  198. application->newLabel->setFont(application->menuFont);
  199. application->newLabel->setText(newString);
  200. application->newLabel->setTintColor(application->deselectedColor);
  201. application->newLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 1));
  202. application->experimentMenuContainer->addChild(application->newLabel);
  203. application->experimentBackLabel = new UILabel();
  204. application->experimentBackLabel->setFont(application->menuFont);
  205. application->experimentBackLabel->setText(backString);
  206. application->experimentBackLabel->setTintColor(application->deselectedColor);
  207. application->experimentBackLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 2));
  208. application->experimentMenuContainer->addChild(application->experimentBackLabel);
  209. application->settingsMenuContainer = new UIContainer();
  210. application->settingsMenuContainer->setDimensions(Vector2(application->width, application->menuFont->getMetrics().getHeight() * 5));
  211. application->settingsMenuContainer->setAnchor(Vector2(0.0f, 0.5f));
  212. application->settingsMenuContainer->setVisible(false);
  213. application->settingsMenuContainer->setActive(false);
  214. application->uiRootElement->addChild(application->settingsMenuContainer);
  215. application->videoLabel = new UILabel();
  216. application->videoLabel->setFont(application->menuFont);
  217. application->videoLabel->setText(videoString);
  218. application->videoLabel->setTintColor(application->deselectedColor);
  219. application->videoLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 0));
  220. application->settingsMenuContainer->addChild(application->videoLabel);
  221. application->audioLabel = new UILabel();
  222. application->audioLabel->setFont(application->menuFont);
  223. application->audioLabel->setText(audioString);
  224. application->audioLabel->setTintColor(application->deselectedColor);
  225. application->audioLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 1));
  226. application->settingsMenuContainer->addChild(application->audioLabel);
  227. application->controlsLabel = new UILabel();
  228. application->controlsLabel->setFont(application->menuFont);
  229. application->controlsLabel->setText(controlsString);
  230. application->controlsLabel->setTintColor(application->deselectedColor);
  231. application->controlsLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 2));
  232. application->settingsMenuContainer->addChild(application->controlsLabel);
  233. application->gameLabel = new UILabel();
  234. application->gameLabel->setFont(application->menuFont);
  235. application->gameLabel->setText(gameString);
  236. application->gameLabel->setTintColor(application->deselectedColor);
  237. application->gameLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 3));
  238. application->settingsMenuContainer->addChild(application->gameLabel);
  239. application->settingsBackLabel = new UILabel();
  240. application->settingsBackLabel->setFont(application->menuFont);
  241. application->settingsBackLabel->setText(backString);
  242. application->settingsBackLabel->setTintColor(application->deselectedColor);
  243. application->settingsBackLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 4));
  244. application->settingsMenuContainer->addChild(application->settingsBackLabel);
  245. application->pauseMenuContainer = new UIContainer();
  246. application->pauseMenuContainer->setDimensions(Vector2(application->width, application->menuFont->getMetrics().getHeight() * 6));
  247. application->pauseMenuContainer->setAnchor(Anchor::CENTER);
  248. application->pauseMenuContainer->setVisible(false);
  249. application->pauseMenuContainer->setActive(false);
  250. application->uiRootElement->addChild(application->pauseMenuContainer);
  251. application->pausedResumeLabel = new UILabel();
  252. application->pausedResumeLabel->setFont(application->menuFont);
  253. application->pausedResumeLabel->setText(resumeString);
  254. application->pausedResumeLabel->setTintColor(application->deselectedColor);
  255. application->pausedResumeLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 0));
  256. application->pauseMenuContainer->addChild(application->pausedResumeLabel);
  257. application->returnToMainMenuLabel = new UILabel();
  258. application->returnToMainMenuLabel->setFont(application->menuFont);
  259. application->returnToMainMenuLabel->setText(returnToMainMenuString);
  260. application->returnToMainMenuLabel->setTintColor(application->deselectedColor);
  261. application->returnToMainMenuLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 1));
  262. application->pauseMenuContainer->addChild(application->returnToMainMenuLabel);
  263. application->quitToDesktopLabel = new UILabel();
  264. application->quitToDesktopLabel->setFont(application->menuFont);
  265. application->quitToDesktopLabel->setText(quitToDesktopString);
  266. application->quitToDesktopLabel->setTintColor(application->deselectedColor);
  267. application->quitToDesktopLabel->setTranslation(Vector2(0.0f, application->menuFont->getMetrics().getHeight() * 2));
  268. application->pauseMenuContainer->addChild(application->quitToDesktopLabel);
  269. /*
  270. UIContainer* pauseMenuContainer;
  271. UILabel* pausedResumeLabel;
  272. UILabel* pausedSaveLabel;
  273. UILabel* pausedNewLabel;
  274. UILabel* pausedSettingsLabel;
  275. UILabel* returnToMainMenuLabel;
  276. UILabel* quitToDesktopLabel;*/
  277. // Setup UI batch
  278. application->uiBatch = new BillboardBatch();
  279. application->uiBatch->resize(256);
  280. application->uiBatcher = new UIBatcher();
  281. // Setup UI render pass and compositor
  282. application->uiPass.setRenderTarget(&application->defaultRenderTarget);
  283. application->uiCompositor.addPass(&application->uiPass);
  284. application->uiCompositor.load(nullptr);
  285. // Setup UI camera
  286. application->uiCamera.lookAt(glm::vec3(0), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0));
  287. application->uiCamera.setCompositor(&application->uiCompositor);
  288. application->uiCamera.setCompositeIndex(0);
  289. // Setup UI scene
  290. application->uiScene.getLayer(0)->addObject(application->uiBatch);
  291. application->uiScene.getLayer(0)->addObject(&application->uiCamera);
  292. // Setup tweening
  293. application->tweener = new Tweener();
  294. application->fadeInTween = new Tween<Vector4>(EaseFunction::IN_CUBIC, 0.0f, 1.5f, Vector4(0.0f, 0.0f, 0.0f, 1.0f), Vector4(0.0f, 0.0f, 0.0f, -1.0f));
  295. application->fadeInTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->blackoutImage, std::placeholders::_1));
  296. application->tweener->addTween(application->fadeInTween);
  297. application->fadeOutTween = new Tween<Vector4>(EaseFunction::OUT_CUBIC, 0.0f, 1.5f, Vector4(0.0f, 0.0f, 0.0f, 0.0f), Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  298. application->fadeOutTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->blackoutImage, std::placeholders::_1));
  299. application->tweener->addTween(application->fadeOutTween);
  300. application->splashFadeInTween = new Tween<Vector4>(EaseFunction::IN_CUBIC, 0.0f, 0.5f, Vector4(1.0f, 1.0f, 1.0f, 0.0f), Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  301. application->splashFadeInTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->splashImage, std::placeholders::_1));
  302. application->tweener->addTween(application->splashFadeInTween);
  303. application->splashFadeOutTween = new Tween<Vector4>(EaseFunction::OUT_CUBIC, 0.0f, 0.5f, Vector4(1.0f, 1.0f, 1.0f, 1.0f), Vector4(0.0f, 0.0f, 0.0f, -1.0f));
  304. application->splashFadeOutTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->splashImage, std::placeholders::_1));
  305. application->tweener->addTween(application->splashFadeOutTween);
  306. application->titleFadeInTween = new Tween<Vector4>(EaseFunction::IN_CUBIC, 0.0f, 2.0f, Vector4(1.0f, 1.0f, 1.0f, 0.0f), Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  307. application->titleFadeInTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->titleImage, std::placeholders::_1));
  308. application->tweener->addTween(application->titleFadeInTween);
  309. application->titleFadeOutTween = new Tween<Vector4>(EaseFunction::OUT_CUBIC, 0.0f, 0.25f, Vector4(1.0f, 1.0f, 1.0f, 1.0f), Vector4(0.0f, 0.0f, 0.0f, -1.0f));
  310. application->titleFadeOutTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->titleImage, std::placeholders::_1));
  311. application->tweener->addTween(application->titleFadeOutTween);
  312. application->copyrightFadeInTween = new Tween<Vector4>(EaseFunction::IN_CUBIC, 0.0f, 1.0f, Vector4(1.0f, 1.0f, 1.0f, 0.0f), Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  313. application->copyrightFadeInTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->copyrightImage, std::placeholders::_1));
  314. application->tweener->addTween(application->copyrightFadeInTween);
  315. application->copyrightFadeOutTween = new Tween<Vector4>(EaseFunction::OUT_CUBIC, 0.0f, 0.25f, Vector4(1.0f, 1.0f, 1.0f, 1.0f), Vector4(0.0f, 0.0f, 0.0f, -1.0f));
  316. application->copyrightFadeOutTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->copyrightImage, std::placeholders::_1));
  317. application->tweener->addTween(application->copyrightFadeOutTween);
  318. application->anyKeyFadeInTween = new Tween<Vector4>(EaseFunction::LINEAR, 0.0f, 1.5f, Vector4(1.0f, 1.0f, 1.0f, 0.0f), Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  319. application->anyKeyFadeInTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->anyKeyLabel, std::placeholders::_1));
  320. application->tweener->addTween(application->anyKeyFadeInTween);
  321. application->anyKeyFadeOutTween = new Tween<Vector4>(EaseFunction::LINEAR, 0.0f, 1.5f, Vector4(1.0f, 1.0f, 1.0f, 1.0f), Vector4(0.0f, 0.0f, 0.0f, -1.0f));
  322. application->anyKeyFadeOutTween->setUpdateCallback(std::bind(UIElement::setTintColor, application->anyKeyLabel, std::placeholders::_1));
  323. application->tweener->addTween(application->anyKeyFadeOutTween);
  324. float menuFadeInDuration = 0.15f;
  325. Vector4 menuFadeInStartColor = Vector4(1.0f, 1.0f, 1.0f, 0.0f);
  326. Vector4 menuFadeInDeltaColor = Vector4(0.0f, 0.0f, 0.0f, 1.0f);
  327. float menuFadeOutDuration = 0.15f;
  328. Vector4 menuFadeOutStartColor = Vector4(1.0f, 1.0f, 1.0f, 1.0f);
  329. Vector4 menuFadeOutDeltaColor = Vector4(0.0f, 0.0f, 0.0f, -1.0f);
  330. float menuSlideInDuration = 0.35f;
  331. Vector2 menuSlideInStartTranslation = Vector2(-64.0f, 0.0f);
  332. Vector2 menuSlideInDeltaTranslation = Vector2(128.0f, 0.0f);
  333. application->menuFadeInTween = new Tween<Vector4>(EaseFunction::OUT_QUINT, 0.0f, menuFadeInDuration, menuFadeInStartColor, menuFadeInDeltaColor);
  334. application->tweener->addTween(application->menuFadeInTween);
  335. application->menuFadeOutTween = new Tween<Vector4>(EaseFunction::OUT_QUINT, 0.0f, menuFadeOutDuration, menuFadeOutStartColor, menuFadeOutDeltaColor);
  336. application->tweener->addTween(application->menuFadeOutTween);
  337. application->menuSlideInTween = new Tween<Vector2>(EaseFunction::OUT_QUINT, 0.0f, menuSlideInDuration, menuSlideInStartTranslation, menuSlideInDeltaTranslation);
  338. application->tweener->addTween(application->menuSlideInTween);
  339. // Link tweens
  340. application->anyKeyFadeInTween->setEndCallback(std::bind(TweenBase::start, application->anyKeyFadeOutTween));
  341. application->anyKeyFadeOutTween->setEndCallback(std::bind(TweenBase::start, application->anyKeyFadeInTween));
  342. // Menus
  343. application->selectedMenuItemIndex = 0;
  344. application->mainMenu = new Menu();
  345. MenuItem* challengeItem = application->mainMenu->addItem();
  346. challengeItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->challengeLabel, application->selectedColor));
  347. challengeItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->challengeLabel, application->deselectedColor));
  348. challengeItem->setActivatedCallback(std::bind(std::printf, "0\n"));
  349. application->challengeLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, challengeItem->getIndex()));
  350. application->challengeLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, challengeItem->getIndex()));
  351. application->challengeLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, challengeItem->getIndex()));
  352. MenuItem* experimentItem = application->mainMenu->addItem();
  353. experimentItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->experimentLabel, application->selectedColor));
  354. experimentItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->experimentLabel, application->deselectedColor));
  355. experimentItem->setActivatedCallback(std::bind(Application::enterMenu, application, 2));
  356. application->experimentLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, experimentItem->getIndex()));
  357. application->experimentLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, experimentItem->getIndex()));
  358. application->experimentLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, experimentItem->getIndex()));
  359. MenuItem* settingsItem = application->mainMenu->addItem();
  360. settingsItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->settingsLabel, application->selectedColor));
  361. settingsItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->settingsLabel, application->deselectedColor));
  362. settingsItem->setActivatedCallback(std::bind(Application::enterMenu, application, 3));
  363. application->settingsLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, settingsItem->getIndex()));
  364. application->settingsLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, settingsItem->getIndex()));
  365. application->settingsLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, settingsItem->getIndex()));
  366. MenuItem* quitItem = application->mainMenu->addItem();
  367. quitItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->quitLabel, application->selectedColor));
  368. quitItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->quitLabel, application->deselectedColor));
  369. quitItem->setActivatedCallback(std::bind(Application::close, application, EXIT_SUCCESS));
  370. application->quitLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, quitItem->getIndex()));
  371. application->quitLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, quitItem->getIndex()));
  372. application->quitLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, quitItem->getIndex()));
  373. application->experimentMenu = new Menu();
  374. MenuItem* loadItem = application->experimentMenu->addItem();
  375. loadItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->loadLabel, application->selectedColor));
  376. loadItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->loadLabel, application->deselectedColor));
  377. loadItem->setActivatedCallback(std::bind(std::printf, "0\n"));
  378. application->loadLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, loadItem->getIndex()));
  379. application->loadLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, loadItem->getIndex()));
  380. application->loadLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, loadItem->getIndex()));
  381. MenuItem* newItem = application->experimentMenu->addItem();
  382. newItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->newLabel, application->selectedColor));
  383. newItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->newLabel, application->deselectedColor));
  384. newItem->setActivatedCallback(std::bind(Application::changeState, application, application->experimentState));
  385. application->newLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, newItem->getIndex()));
  386. application->newLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, newItem->getIndex()));
  387. application->newLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, newItem->getIndex()));
  388. MenuItem* experimentBackItem = application->experimentMenu->addItem();
  389. experimentBackItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->experimentBackLabel, application->selectedColor));
  390. experimentBackItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->experimentBackLabel, application->deselectedColor));
  391. experimentBackItem->setActivatedCallback(std::bind(Application::enterMenu, application, 0));
  392. application->experimentBackLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, experimentBackItem->getIndex()));
  393. application->experimentBackLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, experimentBackItem->getIndex()));
  394. application->experimentBackLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, experimentBackItem->getIndex()));
  395. application->settingsMenu = new Menu();
  396. MenuItem* videoItem = application->settingsMenu->addItem();
  397. videoItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->videoLabel, application->selectedColor));
  398. videoItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->videoLabel, application->deselectedColor));
  399. videoItem->setActivatedCallback(std::bind(std::printf, "0\n"));
  400. application->videoLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, videoItem->getIndex()));
  401. application->videoLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, videoItem->getIndex()));
  402. application->videoLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, videoItem->getIndex()));
  403. MenuItem* audioItem = application->settingsMenu->addItem();
  404. audioItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->audioLabel, application->selectedColor));
  405. audioItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->audioLabel, application->deselectedColor));
  406. audioItem->setActivatedCallback(std::bind(std::printf, "1\n"));
  407. application->audioLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, audioItem->getIndex()));
  408. application->audioLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, audioItem->getIndex()));
  409. application->audioLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, audioItem->getIndex()));
  410. MenuItem* controlsItem = application->settingsMenu->addItem();
  411. controlsItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->controlsLabel, application->selectedColor));
  412. controlsItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->controlsLabel, application->deselectedColor));
  413. controlsItem->setActivatedCallback(std::bind(std::printf, "2\n"));
  414. application->controlsLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, controlsItem->getIndex()));
  415. application->controlsLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, controlsItem->getIndex()));
  416. application->controlsLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, controlsItem->getIndex()));
  417. MenuItem* gameItem = application->settingsMenu->addItem();
  418. gameItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->gameLabel, application->selectedColor));
  419. gameItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->gameLabel, application->deselectedColor));
  420. gameItem->setActivatedCallback(std::bind(std::printf, "3\n"));
  421. application->gameLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, gameItem->getIndex()));
  422. application->gameLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, gameItem->getIndex()));
  423. application->gameLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, gameItem->getIndex()));
  424. MenuItem* settingsBackItem = application->settingsMenu->addItem();
  425. settingsBackItem->setSelectedCallback(std::bind(UIElement::setTintColor, application->settingsBackLabel, application->selectedColor));
  426. settingsBackItem->setDeselectedCallback(std::bind(UIElement::setTintColor, application->settingsBackLabel, application->deselectedColor));
  427. settingsBackItem->setActivatedCallback(std::bind(Application::enterMenu, application, 0));
  428. application->settingsBackLabel->setMouseOverCallback(std::bind(Application::selectMenuItem, application, settingsBackItem->getIndex()));
  429. application->settingsBackLabel->setMouseMovedCallback(std::bind(Application::selectMenuItem, application, settingsBackItem->getIndex()));
  430. application->settingsBackLabel->setMousePressedCallback(std::bind(Application::activateMenuItem, application, settingsBackItem->getIndex()));
  431. application->menuCount = 4;
  432. application->menus = new Menu*[application->menuCount];
  433. application->menus[0] = application->mainMenu;
  434. application->menus[1] = application->challengeMenu;
  435. application->menus[2] = application->experimentMenu;
  436. application->menus[3] = application->settingsMenu;
  437. application->menuContainers = new UIContainer*[application->menuCount];
  438. application->menuContainers[0] = application->mainMenuContainer;
  439. application->menuContainers[1] = application->challengeMenuContainer;
  440. application->menuContainers[2] = application->experimentMenuContainer;
  441. application->menuContainers[3] = application->settingsMenuContainer;
  442. application->currentMenu = application->mainMenu;
  443. application->currentMenuIndex = 0;
  444. application->selectedMenuItemIndex = 0;
  445. application->selectMenuItem(application->selectedMenuItemIndex);
  446. // Models
  447. application->displayModel = application->modelLoader->load("data/models/icosphere.mdl");
  448. application->antModel = application->modelLoader->load("data/models/agent.mdl");
  449. // Model instances
  450. application->displayModelInstance = new ModelInstance();
  451. application->antModelInstance = new ModelInstance();
  452. // Allocate game variables
  453. application->surfaceCam = new SurfaceCameraController();
  454. application->tunnelCam = new TunnelCameraController();
  455. // Setup screen fade-in transition
  456. fadeIn = false;
  457. fadeOut = false;
  458. // Check for splash screen skip setting
  459. skip = false;
  460. application->settings.get("skip_splash", &skip);
  461. // Add window observer and set layout
  462. application->inputManager->addWindowObserver(this);
  463. windowResized(application->width, application->height);
  464. // Start timer
  465. stateTime = 0.0f;
  466. application->frameTimer.reset();
  467. application->frameTimer.start();
  468. }
  469. void SplashState::execute()
  470. {
  471. // Calculate delta time (in seconds)
  472. float dt = static_cast<float>(application->frameTimer.microseconds().count()) / 1000000.0f;
  473. application->frameTimer.reset();
  474. // Add dt to state time
  475. stateTime += dt;
  476. // Listen for splash screen skip
  477. InputEvent event;
  478. application->inputManager->listen(&event);
  479. if (skip || event.type != InputEvent::Type::NONE)
  480. {
  481. application->menuControlProfile->update();
  482. application->inputManager->update();
  483. // Check if application was closed
  484. if (application->escape.isTriggered())
  485. {
  486. application->close(EXIT_SUCCESS);
  487. return;
  488. }
  489. // Check if fullscreen was toggled
  490. else if (application->toggleFullscreen.isTriggered() && !application->toggleFullscreen.wasTriggered())
  491. {
  492. application->changeFullscreen();
  493. }
  494. else
  495. {
  496. // Clear screen
  497. glClear(GL_COLOR_BUFFER_BIT);
  498. SDL_GL_SwapWindow(application->window);
  499. // Change to title state
  500. application->changeState(application->titleState);
  501. return;
  502. }
  503. }
  504. // Start fade-in
  505. if (!fadeIn && stateTime >= blankDuration)
  506. {
  507. // Begin fade-in
  508. fadeIn = true;
  509. application->splashImage->setVisible(true);
  510. application->splashFadeInTween->start();
  511. }
  512. // Begin fade-out
  513. if (!fadeOut && stateTime >= blankDuration + application->splashFadeInTween->getDuration() + hangDuration)
  514. {
  515. fadeOut = true;
  516. application->splashFadeOutTween->start();
  517. }
  518. // Next state
  519. if (fadeOut && application->splashFadeOutTween->isStopped())
  520. {
  521. application->splashImage->setVisible(false);
  522. application->changeState(application->titleState);
  523. return;
  524. }
  525. // Update input
  526. application->inputManager->update();
  527. // Update menu controls
  528. application->menuControlProfile->update();
  529. // Check if application was closed
  530. if (application->inputManager->wasClosed() || application->escape.isTriggered())
  531. {
  532. application->close(EXIT_SUCCESS);
  533. return;
  534. }
  535. // Perform tweening
  536. application->tweener->update(dt);
  537. // Update UI
  538. application->uiRootElement->update();
  539. // Clear to black
  540. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  541. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  542. // Form billboard batch for UI then render UI scene
  543. application->uiBatcher->batch(application->uiBatch, application->uiRootElement);
  544. application->renderer.render(application->uiScene);
  545. // Swap buffers
  546. SDL_GL_SwapWindow(application->window);
  547. }
  548. void SplashState::exit()
  549. {
  550. std::cout << "Exiting SplashState..." << std::endl;
  551. // Hide splash screen
  552. application->splashImage->setVisible(false);
  553. application->inputManager->removeWindowObserver(this);
  554. }
  555. void SplashState::windowClosed()
  556. {
  557. application->close(EXIT_SUCCESS);
  558. }
  559. void SplashState::windowResized(int width, int height)
  560. {
  561. // Update application dimensions
  562. application->width = width;
  563. application->height = height;
  564. if (application->fullscreen)
  565. {
  566. application->fullscreenWidth = width;
  567. application->fullscreenHeight = height;
  568. }
  569. else
  570. {
  571. application->windowedWidth = width;
  572. application->windowedHeight = height;
  573. }
  574. // Setup default render target
  575. application->defaultRenderTarget.width = application->width;
  576. application->defaultRenderTarget.height = application->height;
  577. // Resize UI
  578. application->resizeUI();
  579. }