Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. #ifndef GAME_H
  2. #define GAME_H
  3.  
  4. #include <SFML/Graphics/Drawable.hpp>
  5. #include <SFML/Graphics/RenderWindow.hpp>
  6. #include <string>
  7. #include <vector>
  8. #include <map>
  9.  
  10. #include "../game/Entity.h"
  11. #include "../game/FpsCounter.h"
  12. #include "../game/EScene.h"
  13. #include "../game/Scene.h"
  14. #include "../game/InputHandler.h"
  15. #include "../game/WindowStyle.h"
  16. #include "../menu/VBox.h"
  17. #include "../menu/HBox.h"
  18. #include "../menu/ButtonMenu.h"
  19. #include "../animation/TranslateAnimation.h"
  20.  
  21. namespace ProjectSpace
  22. {
  23. /**
  24. * @brief Core class of the Game. Contains main Game-Loop and Event-Loop.
  25. */
  26. class Game
  27. {
  28. public:
  29.  
  30. /**
  31. * @brief Constructs a Game detecting the monitor's resolution automatically.
  32. *
  33. * @param[in] windowTitle Title of the Game's window.
  34. */
  35. Game(std::string windowTitle, WindowStyle style = DEFAULT);
  36.  
  37. /**
  38. * @brief Constructs a Game.
  39. *
  40. * @param[in] screenWidth Width of the Game's window.
  41. * @param[in] screenHeight Height of the Game's window.
  42. * @param[in] windowTitle Title of the Game's window.
  43. */
  44. Game(unsigned int screenWidth, unsigned int screenHeight, std::string windowTitle, WindowStyle style = DEFAULT);
  45.  
  46. /**
  47. * @brief Destroys every Scene of the Game.
  48. */
  49. ~Game();
  50.  
  51. /**
  52. * @brief Starts the Game/Game-Loop.
  53. */
  54. void start();
  55.  
  56. /**
  57. * @brief Sets the current scene.
  58. *
  59. * @param[in] scene The scene
  60. */
  61. void setCurrentScene(EScene scene);
  62.  
  63. private:
  64. /**
  65. * @brief Helper function for initializing everything before starting the Game. Creates all Sprites and stuff...
  66. */
  67. void init();
  68.  
  69. sf::RenderWindow window; // Window of the Game.
  70. FpsCounter fpsCounter; // Fps Counter of the Game.
  71. InputHandler inputHandler; // InputHandler for input Actions that are always available in the game (Not Scene dependant).
  72. VBox menu;
  73. std::map<EScene, Scene*> scenes; // All Scenes of the Game.
  74. Scene* currentScene; // Scene that is currently active (is drawn and updated).
  75.  
  76. TranslateAnimation* menuForward;
  77. TranslateAnimation* menuBackward;
  78. ButtonMenu* buttonMenu;
  79.  
  80. float windowWidth;
  81. float windowHeight;
  82. };
  83. }
  84.  
  85. #endif
  86.  
  87. #include "Game.h"
  88.  
  89. #include <SFML/System/Clock.hpp>
  90. #include <SFML/Window/Event.hpp>
  91. #include <SFML/Window/VideoMode.hpp>
  92.  
  93. #include "../menu/TextBox.h"
  94. #include "../animation/FadeAnimation.h"
  95. #include "../menu/Button.h"
  96. #include "../menu/ButtonMenu.h"
  97. #include "InputHandler.h"
  98. #include "Scene.h"
  99. #include "Factory.h"
  100.  
  101. namespace ProjectSpace
  102. {
  103. Game::Game(std::string windowTitle, WindowStyle style)
  104. : window{sf::VideoMode{sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height}, windowTitle, style},
  105. fpsCounter{"rsrc/fonts/arial.ttf"}, currentScene{nullptr}, windowWidth{(float)sf::VideoMode::getDesktopMode().width},
  106. windowHeight{(float)sf::VideoMode::getDesktopMode().height}
  107. {
  108. window.setFramerateLimit(60);
  109.  
  110. init();
  111. }
  112.  
  113. Game::Game(unsigned int screenWidth, unsigned int screenHeight, std::string windowTitle, WindowStyle style)
  114. : window{sf::VideoMode{screenWidth, screenHeight}, windowTitle, style}, fpsCounter{"rsrc/fonts/arial.ttf"},
  115. currentScene{nullptr}
  116. {
  117. // Makes sure that not more than 60 Frames per Second are calculated. Save Ressources.
  118. window.setFramerateLimit(60);
  119.  
  120. init();
  121. }
  122.  
  123. Game::~Game()
  124. {
  125. for (auto const& p : scenes)
  126. {
  127. delete p.second;
  128. }
  129. }
  130.  
  131. void Game::start()
  132. {
  133. sf::Clock clock{};
  134.  
  135. // This is main Game-Loop
  136. while (window.isOpen())
  137. {
  138. // At the start of each iteration "clock.restart()" will return the time that the last iteration took.
  139. // This is so that all objects of the current iteration know how much time has passed since the last frame.
  140. sf::Time time = clock.restart();
  141. float frameTime = time.asSeconds();
  142.  
  143. // std::cout << "frameTime: " << frameTime << std::endl;
  144.  
  145. // This is the Event-Loop (Hier muessen wir nochmal gucken, was wir damit anstellen koennen. Ist wohl ziemlich wichtig in SFML.)
  146. sf::Event event;
  147. while (window.pollEvent(event))
  148. {
  149. // For example by pressing the Close-Button on a Window, a Closed Event is created.
  150. if (event.type == sf::Event::Closed)
  151. {
  152. window.close();
  153. }
  154. }
  155.  
  156. currentScene->update(time);
  157.  
  158. inputHandler.update(time);
  159. fpsCounter.update(time);
  160. menu.update(time);
  161. menuForward->update(time);
  162. menuBackward->update(time);
  163. buttonMenu->update(time);
  164.  
  165. // Default way of drawing stuff in SFML. Clear the window, draw you'r stuff and then display it.
  166. window.clear();
  167.  
  168. window.draw(*currentScene);
  169. window.draw(fpsCounter);
  170. window.draw(menu);
  171.  
  172. window.display();
  173. }
  174. }
  175.  
  176. void Game::setCurrentScene(EScene scene)
  177. {
  178. if (scenes.count(scene) == 0)
  179. {
  180. std::cout << "@Game::setCurrentScene(): Given scene is not known to Game." << std::endl;
  181. return;
  182. }
  183.  
  184. currentScene = scenes[scene];
  185. }
  186.  
  187. void Game::init()
  188. {
  189. // Creating Levels
  190. scenes[EScene::DEBUG] = Factory::CREATE_DEBUG_SCENE(window);
  191. scenes[EScene::LEVEL_ONE] = Factory::CREATE_DEBUG_SCENE_2(window);
  192. currentScene = scenes[EScene::DEBUG];
  193.  
  194. // Creating Menu
  195. Button* btn = new Button{[this]()
  196. {
  197. window.close();
  198. }, window, "EXIT"};
  199.  
  200. Button* btn2 = new Button{[this]()
  201. {
  202. window.close();
  203. }, window, "EXIT"};
  204.  
  205. Button* btn3 = new Button{[this]()
  206. {
  207. setCurrentScene(EScene::DEBUG);
  208. }, window, "Debug Level"};
  209.  
  210. Button* btn4 = new Button{[this]()
  211. {
  212. setCurrentScene(EScene::LEVEL_ONE);
  213. }, window, "Level 1"};
  214.  
  215. menu.addMenuElements({btn, btn2, btn3, btn4});
  216. menu.setPosition(-10, 50);
  217. menu.setSpacing(5);
  218.  
  219. buttonMenu = new ButtonMenu{{btn, btn2, btn3, btn4}, &inputHandler};
  220.  
  221. menuForward = new TranslateAnimation{&menu, sf::Vector2f{-250, 50}, sf::Vector2f{-10, 50}, 0.5f};
  222. menuBackward = new TranslateAnimation{&menu, sf::Vector2f{-10, 50}, sf::Vector2f{-250, 50}, 0.5f};
  223.  
  224. // Setting up input handler
  225. inputHandler.add([this]()
  226. {
  227. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  228. {
  229. window.close();
  230. }
  231.  
  232. if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
  233. {
  234. menuBackward->stop();
  235. menuForward->start();
  236. }
  237.  
  238. if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
  239. {
  240. menuForward->stop();
  241. menuBackward->start();
  242. }
  243. });
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement