MenuState.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include "Utility.hpp"
  23. MenuState::MenuState(): State(), _padSelection(0) {}
  24. void MenuState::init()
  25. {
  26. for(unsigned int i = 0; i < 30; ++i)
  27. {
  28. //_crazyTriangles[i].setColor(50, rand() % 50 + 125, rand() % 50 + 200, rand() % 150 + 105);
  29. _crazyColors[i][3] = rand() % 150 + 105;
  30. _crazyTriangles[i].setSize(rand() % 384 + (rand() % 20 == 0 ? 900 : 300));
  31. _crazyTriangles[i].setRotation(rand() % 360);
  32. }
  33. std::ifstream ifs("data/credits.txt");
  34. std::ostringstream oss;
  35. oss << ifs.rdbuf();
  36. _credits.setString(sf::String(oss.str()));
  37. _credits.setFont(_context->assets->loadFont("data/ttf/canonical/Ubuntu-L.ttf"));
  38. _credits.setColor(sf::Color::Black);
  39. _music.openFromFile("data/audio/Past the Edge.ogg");
  40. _music.play();
  41. _waterSine.loadFromFile("data/shaders/water.vs", sf::Shader::Fragment);
  42. _waterMap.create(1920, 1080);
  43. _waterFrag.loadFromFile("data/shaders/water.frag", sf::Shader::Fragment);
  44. _waterFrag.setParameter("heightmap", _waterMap.getTexture());
  45. _subStatus = Main;
  46. _logo.bind([this]()
  47. {
  48. _subStatus = Credits;
  49. _quit.setTexture(_context->assets->loadTexture("data/menu/Back.png"));
  50. });
  51. _logo.setRotation(97.5f);
  52. _logo.setTexture(_context->assets->loadTexture("data/menu/Logo.png"));
  53. _play.bind([this]()
  54. {
  55. _status = State::Ingame;
  56. });
  57. _play.setRotation(80.f);
  58. _play.setTexture(_context->assets->loadTexture("data/menu/Play.png"));
  59. _settings.bind([this]()
  60. {
  61. _subStatus = Settings;
  62. _quit.setTexture(_context->assets->loadTexture("data/menu/Back.png"));
  63. });
  64. _settings.setRotation(260.f);
  65. _settings.setTexture(_context->assets->loadTexture("data/menu/Settings.png"));
  66. _quit.bind([this]()
  67. {
  68. switch(_subStatus)
  69. {
  70. case Main:
  71. _context->running = false;
  72. break;
  73. case Settings:
  74. _context->window->recreate();
  75. // move on
  76. default:
  77. _quit.setTexture(_context->assets->loadTexture("data/menu/Quit.png"));
  78. _subStatus = Main;
  79. }
  80. });
  81. _quit.setRotation(100.f);
  82. _quit.setTexture(_context->assets->loadTexture("data/menu/Quit.png"));
  83. _status = State::Ongoing;
  84. }
  85. void MenuState::refresh()
  86. {
  87. _crazy.create(_context->window->getSize().x, _context->window->getSize().y);
  88. for(unsigned int i = 0; i < 30; ++i)
  89. {
  90. for(unsigned int j = 0; j < 3; ++j)
  91. _crazyColors[i][j] = rand() % 50 - 25;
  92. _crazyTriangles[i].setPosition(rand() % _context->window->getSize().x, rand() % _context->window->getSize().y);
  93. }
  94. float u = 1.f; // GUI size essentially; halves when screen size drops below 1024x768.
  95. if(_context->window->getSize().x < 1024)
  96. u = 0.5f;
  97. /*
  98. * SETTINGS
  99. */
  100. _shaders.create(_context->assets->loadFont("data/ttf/canonical/Ubuntu-L.ttf"), "Shaders", &_context->shaders);
  101. _shaders.setSize(250.f * u);
  102. _shaders.setPosition(_context->window->getSize().x / 2.f + 250.f * u,
  103. _context->window->getSize().y / 2.f - 200.f * u);
  104. _shaders.setCharacterSize(40 * u);
  105. _fullscreen.create(_context->assets->loadFont("data/ttf/canonical/Ubuntu-L.ttf"), "Fullscreen", &_context->fullscreen);
  106. _fullscreen.flip(true);
  107. _fullscreen.setSize(250.f * u);
  108. _fullscreen.setPosition(_context->window->getSize().x / 2.f - 250.f * u,
  109. _context->window->getSize().y / 2.f - 200.f * u);
  110. _fullscreen.setCharacterSize(40 * u);
  111. /*
  112. * CREDITS
  113. */
  114. _credits.setCharacterSize(32.f * u);
  115. _credits.setPosition(_context->window->getSize().x / 2.f - _credits.getLocalBounds().width / 2.f, _context->window->getSize().y / 2.f - _credits.getLocalBounds().height / 2.f);
  116. /*
  117. * MAIN MENU
  118. */
  119. _logo.setSize(325.f * u);
  120. _play.setSize(225.f * u);
  121. _settings.setSize(225.f * u);
  122. _quit.setSize(175.f * u);
  123. _logo.setPosition(_context->window->getSize().x / 2.f - 110.f * u, _context->window->getSize().y / 2.f - 200.f * u);
  124. _play.setPosition(_context->window->getSize().x / 2.f + 260.f * u, _context->window->getSize().y / 2.f - 60.f * u);
  125. _settings.setPosition(_context->window->getSize().x / 2.f - 260.f * u, _context->window->getSize().y / 2.f + 30.f * u);
  126. _quit.setPosition(_context->window->getSize().x / 2.f + 260.f * u, _context->window->getSize().y / 2.f + 260.f * u);
  127. }
  128. void MenuState::coreThink(const sf::Event& event)
  129. {
  130. if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
  131. {
  132. sf::Vector2i xy = _context->window->getMousePosition();
  133. switch(_subStatus)
  134. {
  135. case Main:
  136. _logo.click(xy);
  137. _play.click(xy);
  138. _settings.click(xy);
  139. break;
  140. case Settings:
  141. _shaders.click(xy);
  142. _fullscreen.click(xy);
  143. break;
  144. case Credits:
  145. break;
  146. }
  147. _quit.click(xy);
  148. }
  149. if(event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed and event.key.code == sf::Keyboard::Escape))
  150. _context->running = false;
  151. if(event.type == sf::Event::JoystickMoved && event.joystickMove.axis == sf::Joystick::Axis::PovX)
  152. {
  153. if(event.joystickMove.position == -100)
  154. switch(_padSelection)
  155. {
  156. case 1: _padSelection = 2; break;
  157. default: break;
  158. }
  159. if(event.joystickMove.position == 100)
  160. switch(_padSelection)
  161. {
  162. case 0: _padSelection = 1; break;
  163. case 2: _padSelection = 1; break;
  164. default: break;
  165. }
  166. }
  167. if(event.type == sf::Event::JoystickMoved && event.joystickMove.axis == sf::Joystick::Axis::PovY)
  168. {
  169. if(event.joystickMove.position == -100)
  170. switch(_padSelection)
  171. {
  172. case 1: _padSelection = 0; break;
  173. case 2: _padSelection = 0; break;
  174. case 3: _padSelection = 1; break;
  175. default: break;
  176. }
  177. if(event.joystickMove.position == 100)
  178. switch(_padSelection)
  179. {
  180. case 0: _padSelection = 2; break;
  181. case 1: _padSelection = 3; break;
  182. case 2: _padSelection = 3; break;
  183. default: break;
  184. }
  185. }
  186. }
  187. void MenuState::coreInput()
  188. {
  189. }
  190. void MenuState::coreUpdate(sf::Time delta)
  191. {
  192. float seconds = delta.asSeconds();
  193. {
  194. static float r = rand() % 256, g = rand() % 256, b = rand() % 256;
  195. static int rInc = 1, gInc = 1, bInc = 1;
  196. r += (rand() % 40) * seconds * rInc;
  197. g += (rand() % 30) * seconds * gInc;
  198. b += (rand() % 20) * seconds * bInc;
  199. if(r >= 225.f)
  200. {
  201. r = 225.f;
  202. rInc = -1;
  203. }
  204. if(g >= 225.f)
  205. {
  206. g = 225.f;
  207. gInc = -1;
  208. }
  209. if(b >= 225.f)
  210. {
  211. b = 225.f;
  212. bInc = -1;
  213. }
  214. if(r <= 60)
  215. {
  216. r = 60;
  217. rInc = 1;
  218. }
  219. if(g <= 60)
  220. {
  221. g = 60;
  222. gInc = 1;
  223. }
  224. if(b <= 60)
  225. {
  226. b = 60;
  227. bInc = 1;
  228. }
  229. _color = sf::Color(r, g, b);
  230. _logo.setColor(_color);
  231. _quit.setColor(_color);
  232. _settings.setColor(_color);
  233. _play.setColor(_color);
  234. _shaders.setColor(_color);
  235. _fullscreen.setColor(_color);
  236. for(unsigned i = 0; i < 30; ++i)
  237. _crazyTriangles[i].setColor(sf::Color(r + _crazyColors[i][0] , g + _crazyColors[i][1], b + _crazyColors[i][2], _crazyColors[i][3]));
  238. }
  239. static bool ongoing = false;
  240. static int k = -1;
  241. static float d = 0;
  242. static float direction = 0.f;
  243. static float speed = 0.f;
  244. static float r = 0;
  245. if(!ongoing)
  246. {
  247. r = rand() % 2 + 1.f;
  248. k = rand() % 30;
  249. ongoing = true;
  250. rand() % 2 == 0 ? direction = 1.f : direction = -1.f;
  251. speed = 0.1f;
  252. }
  253. if(k != -1)
  254. {
  255. float x = delta.asSeconds() * 60.f * speed * direction;
  256. _crazyTriangles[k].rotate(x);
  257. d += delta.asSeconds();
  258. 2 * d < r ? speed += delta.asSeconds() * 3.5f : speed -= delta.asSeconds() * 3.5f;
  259. if(d > r)
  260. {
  261. k = -1;
  262. ongoing = false;
  263. d = 0;
  264. }
  265. }
  266. static sf::Clock clock;
  267. _waterSine.setParameter("time", clock.getElapsedTime().asSeconds());
  268. if(sf::Joystick::isConnected(0))
  269. {
  270. sf::Vector2f v;
  271. switch(_padSelection)
  272. {
  273. case 0:
  274. v = _logo.getPosition();
  275. break;
  276. case 1:
  277. v = _play.getPosition();
  278. break;
  279. case 2:
  280. v = _settings.getPosition();
  281. break;
  282. case 3:
  283. v = _quit.getPosition();
  284. break;
  285. }
  286. _context->window->lockMouse(sf::Vector2i(v));
  287. }
  288. _logo.update(_context->window->getMousePosition(), delta);
  289. _play.update(_context->window->getMousePosition(), delta);
  290. _settings.update(_context->window->getMousePosition(), delta);
  291. _quit.update(_context->window->getMousePosition(), delta);
  292. }
  293. void MenuState::coreRender(const bool shaders)
  294. {
  295. //_crazy.clear(sf::Color(100, 150, 220));
  296. _crazy.clear(_color);
  297. for(unsigned int i = 0; i < 30; ++i)
  298. _crazy.draw(_crazyTriangles[i]);
  299. sf::Sprite crazy(_crazy.getTexture());
  300. _waterMap.clear();
  301. sf::RectangleShape rect(sf::Vector2f(1920.f, 1080.f));
  302. if(shaders)
  303. _waterMap.draw(rect, &_waterSine);
  304. else
  305. _waterMap.draw(rect);
  306. _waterMap.display();
  307. if(shaders)
  308. _context->window->draw(crazy, &_waterFrag);
  309. else
  310. _context->window->draw(crazy);
  311. switch(_subStatus)
  312. {
  313. case Main:
  314. _context->window->draw(_logo);
  315. _context->window->draw(_play);
  316. _context->window->draw(_settings);
  317. break;
  318. case Settings:
  319. _context->window->draw(_shaders);
  320. _context->window->draw(_fullscreen);
  321. break;
  322. case Credits:
  323. _context->window->draw(_credits);
  324. break;
  325. }
  326. _context->window->draw(_quit);
  327. }