MenuState.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Triangles
  3. * Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <string>
  18. #include <fstream>
  19. #include <sstream>
  20. #include "MenuState.hpp"
  21. #include "Triangles.hpp"
  22. MenuState::MenuState()
  23. {
  24. }
  25. void MenuState::init()
  26. {
  27. for(unsigned int i = 0; i < 30; ++i)
  28. {
  29. //_crazyTriangles[i].setColor(50, rand() % 50 + 125, rand() % 50 + 200, rand() % 150 + 105);
  30. _crazyColors[i][3] = rand() % 150 + 105;
  31. _crazyTriangles[i].setSize(rand() % 384 + (rand() % 20 == 0 ? 900 : 300));
  32. _crazyTriangles[i].setRotation(rand() % 360);
  33. }
  34. std::ifstream ifs("data/credits.txt");
  35. std::ostringstream oss;
  36. oss << ifs.rdbuf();
  37. _credits.setString(sf::String(oss.str()));
  38. _credits.setFont(_context->assets->loadFont("data/ttf/canonical/Ubuntu-L.ttf"));
  39. _credits.setColor(sf::Color::Black);
  40. _music.openFromFile("data/audio/Past the Edge.ogg");
  41. _music.play();
  42. _waterSine.loadFromFile("data/shaders/water.vs", sf::Shader::Fragment);
  43. _waterMap.create(1920, 1080);
  44. _waterFrag.loadFromFile("data/shaders/water.frag", sf::Shader::Fragment);
  45. _waterFrag.setParameter("heightmap", _waterMap.getTexture());
  46. _subStatus = Main;
  47. _logo.bind([this]()
  48. {
  49. _subStatus = Credits;
  50. _quit.setTexture(_context->assets->loadTexture("data/menu/Back.png"));
  51. });
  52. _logo.setRotation(97.5f);
  53. _logo.setTexture(_context->assets->loadTexture("data/menu/Logo.png"));
  54. _play.bind([this]()
  55. {
  56. _status = State::Ingame;
  57. });
  58. _play.setRotation(80.f);
  59. _play.setTexture(_context->assets->loadTexture("data/menu/Play.png"));
  60. _settings.bind([this]()
  61. {
  62. _subStatus = Settings;
  63. _quit.setTexture(_context->assets->loadTexture("data/menu/Back.png"));
  64. });
  65. _settings.setRotation(260.f);
  66. _settings.setTexture(_context->assets->loadTexture("data/menu/Settings.png"));
  67. _quit.bind([this]()
  68. {
  69. switch(_subStatus)
  70. {
  71. case Main:
  72. _context->running = false;
  73. break;
  74. case Settings:
  75. _context->window->recreate();
  76. // move on
  77. default:
  78. _quit.setTexture(_context->assets->loadTexture("data/menu/Quit.png"));
  79. _subStatus = Main;
  80. }
  81. });
  82. _quit.setRotation(100.f);
  83. _quit.setTexture(_context->assets->loadTexture("data/menu/Quit.png"));
  84. _status = State::Ongoing;
  85. }
  86. void MenuState::refresh()
  87. {
  88. _crazy.create(_context->window->getSize().x, _context->window->getSize().y);
  89. for(unsigned int i = 0; i < 30; ++i)
  90. {
  91. for(unsigned int j = 0; j < 3; ++j)
  92. _crazyColors[i][j] = rand() % 50 - 25;
  93. _crazyTriangles[i].setPosition(rand() % _context->window->getSize().x, rand() % _context->window->getSize().y);
  94. }
  95. float u = 1.f; // GUI size essentially; halves when screen size drops below 1024x768.
  96. if(_context->window->getSize().x < 1024)
  97. u = 0.5f;
  98. /*
  99. * SETTINGS
  100. */
  101. _shaders.create(_context->assets->loadFont("data/ttf/canonical/Ubuntu-L.ttf"), "Shaders", &_context->shaders);
  102. _shaders.setSize(250.f * u);
  103. _shaders.setPosition(_context->window->getSize().x / 2.f + 250.f * u,
  104. _context->window->getSize().y / 2.f - 200.f * u);
  105. _shaders.setCharacterSize(40 * u);
  106. _fullscreen.create(_context->assets->loadFont("data/ttf/canonical/Ubuntu-L.ttf"), "Fullscreen", &_context->fullscreen);
  107. _fullscreen.flip(true);
  108. _fullscreen.setSize(250.f * u);
  109. _fullscreen.setPosition(_context->window->getSize().x / 2.f - 250.f * u,
  110. _context->window->getSize().y / 2.f - 200.f * u);
  111. _fullscreen.setCharacterSize(40 * u);
  112. /*
  113. * CREDITS
  114. */
  115. _credits.setCharacterSize(32.f * u);
  116. _credits.setPosition(_context->window->getSize().x / 2.f - _credits.getLocalBounds().width / 2.f, _context->window->getSize().y / 2.f - _credits.getLocalBounds().height / 2.f);
  117. /*
  118. * MAIN MENU
  119. */
  120. _logo.setSize(325.f * u);
  121. _play.setSize(225.f * u);
  122. _settings.setSize(225.f * u);
  123. _quit.setSize(175.f * u);
  124. _logo.setPosition(_context->window->getSize().x / 2.f - 110.f * u, _context->window->getSize().y / 2.f - 200.f * u);
  125. _play.setPosition(_context->window->getSize().x / 2.f + 260.f * u, _context->window->getSize().y / 2.f - 60.f * u);
  126. _settings.setPosition(_context->window->getSize().x / 2.f - 260.f * u, _context->window->getSize().y / 2.f + 30.f * u);
  127. _quit.setPosition(_context->window->getSize().x / 2.f + 260.f * u, _context->window->getSize().y / 2.f + 260.f * u);
  128. }
  129. void MenuState::coreThink(const sf::Event& event)
  130. {
  131. if(event.type == sf::Event::MouseButtonPressed and event.mouseButton.button == sf::Mouse::Left)
  132. {
  133. sf::Vector2i xy = _context->window->getMousePosition();
  134. switch(_subStatus)
  135. {
  136. case Main:
  137. _logo.click(xy);
  138. _play.click(xy);
  139. _settings.click(xy);
  140. break;
  141. case Settings:
  142. _shaders.click(xy);
  143. _fullscreen.click(xy);
  144. break;
  145. case Credits:
  146. break;
  147. }
  148. _quit.click(xy);
  149. }
  150. if(event.type == sf::Event::Closed or (event.type == sf::Event::KeyPressed and event.key.code == sf::Keyboard::Escape))
  151. _context->running = false;
  152. }
  153. void MenuState::coreInput()
  154. {
  155. }
  156. void MenuState::coreUpdate(sf::Time delta)
  157. {
  158. float seconds = delta.asSeconds();
  159. {
  160. static float r = rand() % 256, g = rand() % 256, b = rand() % 256;
  161. static int rInc = 1, gInc = 1, bInc = 1;
  162. r += (rand() % 40) * seconds * rInc;
  163. g += (rand() % 30) * seconds * gInc;
  164. b += (rand() % 20) * seconds * bInc;
  165. if(r >= 225.f)
  166. {
  167. r = 225.f;
  168. rInc = -1;
  169. }
  170. if(g >= 225.f)
  171. {
  172. g = 225.f;
  173. gInc = -1;
  174. }
  175. if(b >= 225.f)
  176. {
  177. b = 225.f;
  178. bInc = -1;
  179. }
  180. if(r <= 60)
  181. {
  182. r = 60;
  183. rInc = 1;
  184. }
  185. if(g <= 60)
  186. {
  187. g = 60;
  188. gInc = 1;
  189. }
  190. if(b <= 60)
  191. {
  192. b = 60;
  193. bInc = 1;
  194. }
  195. _color = sf::Color(r, g, b);
  196. _logo.setColor(_color);
  197. _quit.setColor(_color);
  198. _settings.setColor(_color);
  199. _play.setColor(_color);
  200. _shaders.setColor(_color);
  201. _fullscreen.setColor(_color);
  202. for(unsigned i = 0; i < 30; ++i)
  203. _crazyTriangles[i].setColor(sf::Color(r + _crazyColors[i][0] , g + _crazyColors[i][1], b + _crazyColors[i][2], _crazyColors[i][3]));
  204. }
  205. static bool ongoing = false;
  206. static int k = -1;
  207. static float d = 0;
  208. static float direction = 0.f;
  209. static float speed = 0.f;
  210. static float r = 0;
  211. if(!ongoing)
  212. {
  213. r = rand() % 2 + 1.f;
  214. k = rand() % 30;
  215. ongoing = true;
  216. rand() % 2 == 0 ? direction = 1.f : direction = -1.f;
  217. speed = 0.1f;
  218. }
  219. if(k != -1)
  220. {
  221. float x = delta.asSeconds() * 60.f * speed * direction;
  222. _crazyTriangles[k].rotate(x);
  223. d += delta.asSeconds();
  224. 2 * d < r ? speed += delta.asSeconds() * 3.5f : speed -= delta.asSeconds() * 3.5f;
  225. if(d > r)
  226. {
  227. k = -1;
  228. ongoing = false;
  229. d = 0;
  230. }
  231. }
  232. static sf::Clock clock;
  233. _waterSine.setParameter("time", clock.getElapsedTime().asSeconds());
  234. _logo.update(_context->window->getMousePosition(), delta);
  235. _play.update(_context->window->getMousePosition(), delta);
  236. _settings.update(_context->window->getMousePosition(), delta);
  237. _quit.update(_context->window->getMousePosition(), delta);
  238. }
  239. void MenuState::coreRender(const bool shaders)
  240. {
  241. //_crazy.clear(sf::Color(100, 150, 220));
  242. _crazy.clear(_color);
  243. for(unsigned int i = 0; i < 30; ++i)
  244. _crazy.draw(_crazyTriangles[i]);
  245. sf::Sprite crazy(_crazy.getTexture());
  246. _waterMap.clear();
  247. sf::RectangleShape rect(sf::Vector2f(1920.f, 1080.f));
  248. if(shaders)
  249. _waterMap.draw(rect, &_waterSine);
  250. else
  251. _waterMap.draw(rect);
  252. _waterMap.display();
  253. if(shaders)
  254. _context->window->draw(crazy, &_waterFrag);
  255. else
  256. _context->window->draw(crazy);
  257. switch(_subStatus)
  258. {
  259. case Main:
  260. _context->window->draw(_logo);
  261. _context->window->draw(_play);
  262. _context->window->draw(_settings);
  263. break;
  264. case Settings:
  265. _context->window->draw(_shaders);
  266. _context->window->draw(_fullscreen);
  267. break;
  268. case Credits:
  269. _context->window->draw(_credits);
  270. break;
  271. }
  272. _context->window->draw(_quit);
  273. }