@@ -6,45 +6,36 @@ File under GNU GPL v3.0 license
66*/
77#include " ./Gameloop.hpp"
88#include " ../controller/MainMenuCtrl.hpp"
9- #include " ../model/storage/UiData.hpp"
109#include " ../model/sysObjects/Player.hpp"
11- #include " ../view/Window.hpp"
1210#include < SFML/Window/Event.hpp>
1311
1412namespace OpMon {
1513
16- GameLoop::GameLoop () {}
17-
18- GameLoop::~GameLoop () {
19- while (!_gameScreens.empty ()) {
20- delete (_gameScreens.top ());
21- _gameScreens.pop ();
22- }
14+ GameLoop::GameLoop ()
15+ : uidata(std::make_unique<Model::UiData>()) {
16+ std::unique_ptr<Controller::AGameScreen> firstCtrl = std::make_unique<Controller::MainMenuCtrl>(uidata.get ());
17+ _gameScreens.push (std::move (firstCtrl));
2318 }
2419
2520 GameStatus GameLoop::operator ()() {
2621
27- window.open ();
28-
29- std::unique_ptr<Model::UiData> uidata = std::make_unique<Model::UiData>();
30-
31- // TODO: add first item outside of the Gameloop.
32- Controller::AGameScreen *first_ctrl = new Controller::MainMenuCtrl (uidata.get ());
33-
34- _gameScreens.push (first_ctrl);
22+ std::unique_ptr<View::Window, std::function<void (View::Window*)>> window (new View::Window (), [](View::Window* w) {
23+ w->close ();
24+ });
25+ window->open ();
3526
36- GameStatus status = GameStatus::CONTINUE;
27+ GameStatus status{ GameStatus::CONTINUE} ;
3728
3829 while (status != GameStatus::STOP) {
3930 status = GameStatus::CONTINUE;
4031
4132 // Gets the current game screen's controller
42- auto ctrl = _gameScreens.top ();
33+ auto * ctrl = _gameScreens.top (). get ();
4334 sf::Event event;
4435
4536 // process all pending SFML events
4637 while (status == GameStatus::CONTINUE) {
47- bool isEvent = window. getWindow ().pollEvent (event);
38+ bool isEvent = window-> getWindow ().pollEvent (event);
4839 if (isEvent == false )
4940 event.type = sf::Event::SensorChanged;
5041 status = _checkQuit (event);
@@ -58,41 +49,32 @@ namespace OpMon {
5849
5950 if (status == GameStatus::CONTINUE) {
6051 // frame update & draw
61- status = ctrl->update (window. getFrame ());
52+ status = ctrl->update (window-> getFrame ());
6253 }
6354
6455 switch (status) {
6556 case GameStatus::NEXT: // Pauses the current screen and passes to the next
6657 ctrl->suspend ();
67- _gameScreens.push (ctrl->getNextGameScreen ());
58+ _gameScreens.push (std::move ( ctrl->getNextGameScreen () ));
6859 break ;
6960 case GameStatus::PREVIOUS: // Deletes the current screen and returns to the previous one
70- delete (ctrl);
7161 _gameScreens.pop ();
7262 _gameScreens.top ()->resume ();
7363 break ;
7464 case GameStatus::CONTINUE:
75- window. refresh ();
65+ window-> refresh ();
7666 break ;
7767 default :
7868 break ;
7969 }
8070 }
8171
82- window.close ();
83-
8472 return GameStatus::STOP;
8573 }
8674
8775 GameStatus GameLoop::_checkQuit (const sf::Event &event) {
88- switch (event.type ) {
89- case sf::Event::Closed:
90- return GameStatus::STOP;
91- default :
92- break ;
93- }
94-
95- if (sf::Keyboard::isKeyPressed (sf::Keyboard::Escape)) {
76+ if (event.type == sf::Event::Closed
77+ || sf::Keyboard::isKeyPressed (sf::Keyboard::Escape)) {
9678 return GameStatus::STOP;
9779 }
9880
0 commit comments