Skip to content
This repository was archived by the owner on Mar 3, 2022. It is now read-only.

Commit 0746377

Browse files
authored
Merge pull request #78 from Bobor-dev/contributors-update
Adding std::unique_ptr and code enhancement
2 parents b246dcf + 098a01b commit 0746377

File tree

12 files changed

+78
-120
lines changed

12 files changed

+78
-120
lines changed

src/opmon/controller/AGameScreen.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ File under GNU GPL v3.0 license
99
#include "../start/GameStatus.hpp"
1010
#include <SFML/Graphics/RenderTexture.hpp>
1111
#include <SFML/Window/Event.hpp>
12+
#include <memory>
1213

1314
namespace OpMon {
1415
namespace Controller {
@@ -25,8 +26,8 @@ namespace OpMon {
2526
*/
2627
class AGameScreen {
2728
public:
28-
AGameScreen()
29-
: _next_gs(nullptr) {}
29+
AGameScreen() = default;
30+
virtual ~AGameScreen() = default;
3031

3132
/**
3233
* process a new SFML Input (keyboard, mouse, ...).
@@ -43,12 +44,10 @@ namespace OpMon {
4344
virtual void suspend(){};
4445
virtual void resume(){};
4546

46-
AGameScreen *getNextGameScreen() { return _next_gs; };
47-
48-
virtual ~AGameScreen() = default;
47+
std::unique_ptr<AGameScreen> getNextGameScreen() { return std::move(_next_gs); };
4948

5049
protected:
51-
AGameScreen *_next_gs;
50+
std::unique_ptr<AGameScreen> _next_gs;
5251
};
5352

5453
} // namespace Controller

src/opmon/controller/BattleCtrl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ namespace OpMon {
1818
}
1919

2020
BattleCtrl::BattleCtrl(OpTeam *one, OpTeam *two, UiData *uidata, Player *player)
21-
: playerTeam(one)
21+
: data(uidata, player)
22+
, playerTeam(one)
2223
, trainerTeam(two)
2324
, atk(one->getOp(0))
2425
, def(two->getOp(0))
25-
, data(uidata, player)
2626
, view(one, two, "beta", "grass", this->data) {
2727
initBattle(0, 0);
2828
}

src/opmon/controller/BattleCtrl.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ namespace OpMon {
2323
class BattleCtrl : public AGameScreen {
2424
private:
2525
Model::BattleData data;
26+
27+
Model::OpTeam *playerTeam;
28+
Model::OpTeam *trainerTeam;
29+
2630
/*The player's current OpMon*/
2731
Model::OpMon *atk;
2832
/*The opposite trainer's current OpMon*/
2933
Model::OpMon *def;
3034

31-
Model::OpTeam *playerTeam;
32-
Model::OpTeam *trainerTeam;
33-
3435
View::Battle view;
3536

3637
Model::Turn atkTurn;

src/opmon/controller/MainMenuCtrl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ namespace OpMon {
2525
case sf::Keyboard::Return:
2626
switch(view.getCursorPosition()) {
2727
case 0:
28-
_next_gs = new StartSceneCtrl(data.getUiDataPtr());
28+
_next_gs = std::make_unique<StartSceneCtrl>(data.getUiDataPtr());
2929
data.getUiDataPtr()->getJukebox().playSound("push");
3030
return GameStatus::NEXT;
3131
case 1:
3232
data.getUiDataPtr()->getJukebox().playSound("nope");
3333
return GameStatus::CONTINUE;
3434
case 2:
35-
_next_gs = new OptionsMenuCtrl(data.getUiDataPtr());
35+
_next_gs = std::make_unique<OptionsMenuCtrl>(data.getUiDataPtr());
3636
data.getUiDataPtr()->getJukebox().playSound("push");
3737
return GameStatus::NEXT;
3838
case 3:

src/opmon/controller/OverworldCtrl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ namespace OpMon {
119119
if(view.getBattleDeclared()->isDefeated()) {
120120
view.endBattle();
121121
} else {
122-
_next_gs = new BattleCtrl(data.getPlayer().getOpTeam(), view.getBattleDeclared(), data.getUiDataPtr(), data.getPlayerPtr());
122+
_next_gs = std::make_unique<BattleCtrl>(data.getPlayer().getOpTeam(), view.getBattleDeclared(), data.getUiDataPtr(), data.getPlayerPtr());
123123
return GameStatus::NEXT;
124124
}
125125
}

src/opmon/controller/StartSceneCtrl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace OpMon {
3838
}
3939
//P is used to skip the introduction, but it must be not working when entering the name
4040
if(event.key.code == sf::Keyboard::P && startscene.getPart() != 1) {
41-
_next_gs = new OverworldCtrl(data.getPlayer(), data.getUiDataPtr());
41+
_next_gs = std::make_unique<OverworldCtrl>(data.getPlayer(), data.getUiDataPtr());
4242
return GameStatus::NEXT;
4343
}
4444
break;
@@ -61,7 +61,7 @@ namespace OpMon {
6161

6262
//If it's the end of the introduction, go to the overworld
6363
if(view.getPart() > 2) {
64-
_next_gs = new OverworldCtrl(data.getPlayer(), data.getUiDataPtr());
64+
_next_gs = std::make_unique<OverworldCtrl>(data.getPlayer(), data.getUiDataPtr());
6565
return GameStatus::NEXT;
6666
}
6767
return GameStatus::CONTINUE;
@@ -72,7 +72,7 @@ namespace OpMon {
7272
if(animNext) {
7373
animNext = false;
7474
view.draw(frame);
75-
_next_gs = new AnimationCtrl(std::make_unique<View::Animations::WinAnim>(frame.getTexture(), false));
75+
_next_gs = std::make_unique<AnimationCtrl>(std::make_unique<View::Animations::WinAnim>(frame.getTexture(), false));
7676
return GameStatus::NEXT;
7777
}
7878
GameStatus toReturn = view();
@@ -83,10 +83,10 @@ namespace OpMon {
8383
if(toReturn == GameStatus::NEXT) {
8484
switch(view.getPart()) {
8585
case 1:
86-
_next_gs = new AnimationCtrl(std::make_unique<View::Animations::WinAnim>(frame.getTexture(), true));
86+
_next_gs = std::make_unique<AnimationCtrl>(std::make_unique<View::Animations::WinAnim>(frame.getTexture(), true));
8787
break;
8888
case 3:
89-
_next_gs = new OverworldCtrl(data.getPlayer(), data.getUiDataPtr());
89+
_next_gs = std::make_unique<OverworldCtrl>(data.getPlayer(), data.getUiDataPtr());
9090
break;
9191
default:
9292
handleError("Internal error, unknown part in StartSceneCtrl::update", true);

src/opmon/model/storage/ResourceLoader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ namespace OpMon {
4040
}
4141
}
4242

43-
sf::Music *ResourceLoader::loadMusic(const char *path) {
44-
auto *music = new sf::Music();
45-
if(!music->openFromFile(getResourcePath() + path)) {
43+
std::unique_ptr<sf::Music> ResourceLoader::loadMusic(const char *path) {
44+
auto music = std::make_unique<sf::Music>();
45+
if(!music->openFromFile(ResourceLoader::getResourcePath() + path)) {
4646
handleError(std::string("Failed to load music: ") + path, false);
4747
}
48-
return music;
48+
return std::move(music);
4949
}
5050

5151
void ResourceLoader::loadKeysFile(const char *path, std::ifstream &file) {

src/opmon/model/storage/ResourceLoader.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ namespace OpMon {
5656
static void
5757
loadTextureArray(std::vector<sf::Texture> &container, const char *path, size_t nb_frame, size_t path_offset = 0);
5858

59-
static sf::Music *loadMusic(const char *path);
59+
static std::unique_ptr<sf::Music> loadMusic(const char *path);
6060

6161
static void loadKeysFile(const char *path, std::ifstream &file);
6262
};
6363

6464
template <typename T>
6565
void ResourceLoader::load(T &resource, const char *path, bool fatal) {
6666

67-
if(!resource.loadFromFile(getResourcePath() + path)) {
67+
if(!resource.loadFromFile(ResourceLoader::getResourcePath() + path)) {
6868
handleError(std::string("Failed to load resource: ") + path, fatal);
6969
}
7070
}

src/opmon/start/Gameloop.cpp

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1412
namespace 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

src/opmon/start/Gameloop.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ File under GNU GPL v3.0
88
#pragma once
99

1010
#include "../controller/AGameScreen.hpp"
11+
#include "../model/storage/UiData.hpp"
1112
#include "../view/Window.hpp"
1213
#include "./GameStatus.hpp"
1314
#include <memory>
@@ -18,21 +19,19 @@ namespace OpMon {
1819
class GameLoop {
1920
public:
2021
GameLoop();
21-
~GameLoop();
22+
~GameLoop() = default;
2223

2324
GameStatus operator()();
2425

2526
/**
26-
* Checks the event to know if the game must be stopped.
27-
* Returns GameStatus::STOP if escape is pressed or if the game is closed.
28-
* Returns GameStatus::CONTINUE if the game must continue.
29-
*/
27+
* Checks the event to know if the game must be stopped.
28+
* Returns GameStatus::STOP if escape is pressed or if the game is closed.
29+
* Returns GameStatus::CONTINUE if the game must continue.
30+
*/
3031
GameStatus _checkQuit(const sf::Event &event);
3132

32-
private:
33-
View::Window window;
34-
35-
std::stack<Controller::AGameScreen *> _gameScreens;
33+
std::unique_ptr<Model::UiData> uidata;
34+
std::stack<std::unique_ptr<Controller::AGameScreen>> _gameScreens;
3635
};
3736

3837
} // namespace OpMon

0 commit comments

Comments
 (0)