very early prototype of a spaceshooter in SFML
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.

mainmenu.cpp 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/System.hpp>
  3. #include <SFML/Window.hpp>
  4. #include "main.hpp"
  5. #include "mainmenu.hpp"
  6. namespace Proto4 {
  7. // IDEA: implement Menu superclass, which has a sf::Text title,
  8. // vector<MenuAction> actions, and implements updating and drawing?
  9. // eg. struct MenuAction { sf::Keyboard key, String text, AppState
  10. // transition }
  11. bool MainMenu::init(sf::Font &font) {
  12. font = font;
  13. view.setCenter(middleX, middleY);
  14. view.setSize(xResolution, yResolution);
  15. view.setViewport(sf::FloatRect(0, 0, 1, 1));
  16. mainLine.setString("Proto 4");
  17. entry1.setString("[S] Start New Game");
  18. entry2.setString("[Q] Quit Game");
  19. mainLine.setFont(font);
  20. entry1.setFont(font);
  21. entry2.setFont(font);
  22. mainLine.setPosition(32.f, 32.f);
  23. entry1.setPosition(32.f, 96.f);
  24. entry2.setPosition(32.f, 160.f);
  25. return true;
  26. }
  27. void MainMenu::resize(sf::Event &resizeEvent) {
  28. view.setSize(resizeEvent.size.width, resizeEvent.size.height);
  29. view.setCenter(resizeEvent.size.width / 2, resizeEvent.size.height / 2);
  30. }
  31. // Unlike in game, this gets only called on keypresses.
  32. AppState MainMenu::handleKey(const sf::Event &keyEvent) {
  33. if (keyEvent.key.code == sf::Keyboard::Q)
  34. return AppState::Exit;
  35. if (keyEvent.key.code == sf::Keyboard::S)
  36. return AppState::Game;
  37. // to satisfy the analyzer
  38. return AppState::MainMenu;
  39. }
  40. void MainMenu::draw(sf::RenderTarget &target,
  41. sf::RenderStates states) const {
  42. states.transform *= getTransform();
  43. target.setView(view);
  44. target.draw(mainLine, states);
  45. target.draw(entry1, states);
  46. target.draw(entry2, states);
  47. }
  48. }