/
PlayerEngine.cpp
128 lines (85 loc) · 2.71 KB
/
PlayerEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Created by romain on 18/04/2020.
//
#include "PlayerEngine.h"
void PlayerEngine::loadFile(const std::string& fileName, const sf::Font& font) {
std::string text, line;
std::ifstream in(fileName);
while(std::getline(in, line)) {
text += line;
}
std::vector<std::string> symbols;
if(doc.Parse(text.c_str()).HasParseError()) {
throw std::runtime_error("Error while parsing JSON provided");
}
const rj::Value &allSlides = doc["slides"];
assert(allSlides.IsArray());
assert(doc["width"].IsInt());
windowX = static_cast<float>(doc["width"].GetInt());
assert(doc["height"].IsInt());
windowY = static_cast<float>(doc["height"].GetInt());
//Itérer à travers tous les slides
for(rj::SizeType i = 0; i < allSlides.Size(); i++) {
Slide slide;
slide.setId(allSlides[i]["id"].GetInt());
const rj::Value &elements = allSlides[i]["elements"];
assert(allSlides[i]["duration"].IsInt());
slide.setDuration(allSlides[i]["duration"].GetInt());
//On itère à travers les éléments
for(rj::SizeType j = 0; j < elements.Size(); j++) {
auto position = elements[j]["position"].GetArray();
int x, y;
for(const auto & it : position) {
auto pos = it.GetObject();
x = pos["x"].GetInt();
y = pos["y"].GetInt();
}
sf::Vector2f pos(static_cast<float>(x), static_cast<float>(y));
switch(elements[j]["type"].GetInt()) { //Gestion du type
case Element::Texte:
slide.addText(getText(elements[j]["text"].GetString(), elements[j]["size"].GetInt(), pos, font));
break;
case Element::Image:
slide.addSprite(getSprite(elements[j]["image"].GetString(), pos));
break;
}
}
addSlide(slide);
}
//On trie les slides par leur id
m_slides.sort();
}
sf::Vector2f PlayerEngine::getWindowDimensions() const {
return sf::Vector2f(windowX, windowY);
}
void PlayerEngine::addSlide(const Slide& slide) {
m_slides.push_back(slide);
}
void PlayerEngine::Run(sf::RenderWindow &window) {
sf::Event e{};
while(window.isOpen()) {
for(Slide s : m_slides) {
while(window.pollEvent(e)) {
if (e.type == sf::Event::Closed)
window.close();
}
s.Run(window);
}
}
}
sf::Text PlayerEngine::getText(const std::string& text, int characterSize, sf::Vector2f pos, const sf::Font& font) {
sf::Text temp;
temp.setFont(font);
temp.setString(sf::String(text));
temp.setCharacterSize(characterSize);
temp.setPosition(pos);
return temp;
}
sf::Sprite PlayerEngine::getSprite(const std::string &file, sf::Vector2f pos) {
sf::Sprite temp;
sf::Texture t;
t.loadFromFile(file);
temp.setTexture(t);
temp.setPosition(pos);
return temp;
}