-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
184 lines (153 loc) · 6.76 KB
/
Parser.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#define _USE_MATH_DEFINES
#include "Parser.h"
#include <sstream>
#include <algorithm>
#include <iostream>
#include <cmath>
namespace animation {
Parser::Parser(std::string filePath) : _config{filePath}, _input{filePath} {
std::string line;
std::getline(_input, line);
std::istringstream iss {line};
((iss >> _config._width).ignore(256, ' ') >> _config._height).ignore(256, ' ') >> _config._framesAmount;
}
bool Parser::loadNextFrame() {
std::string line, instruction;
if (!std::getline(_input, line))
return false;
std::istringstream iss {line};
unsigned frameNumber;
unsigned timeInterval;
(iss >> frameNumber).ignore(256, ' ') >> timeInterval;
sf::Texture img_tex;
sf::RenderTexture renderTexture;
renderTexture.create(_config._width, _config._height);
renderTexture.clear(_colors._backgroundColor);
while(true) {
if (!std::getline(_input, line))
return false;
std::istringstream ss {line};
ss >> instruction;
std::transform(instruction.begin(), instruction.end(), instruction.begin(), ::tolower); // to lower case
if (instruction == "stop" || instruction == "st") {
break;
}
if (instruction == "prostokat" || instruction == "pr") {
float x1, x2, y1, y2;
int flag;
(((ss.ignore(256, ' ') >> x1).ignore(256, ' ') >> y1).ignore(256, ' ') >> x2).ignore(256, ' ') >> y2;
ss.ignore(256, ' ') >> flag;
sf::RectangleShape rectangle(sf::Vector2f(x2 - x1, y2 - y1)) ;
rectangle.setPosition(x1, y1);
rectangle.setOutlineColor(_colors._outlineColor);
rectangle.setOutlineThickness(_colors._outlineThickness);
if (flag)
rectangle.setFillColor(_colors._fillColor);
renderTexture.draw(rectangle);
}
if (instruction == "kolor_piora" || instruction == "kp" || instruction == "kolor_wypelnienia" || instruction == "kw") {
int r, g, b;
((ss.ignore(256, ' ') >> r).ignore(256, ' ') >> g).ignore(256, ' ') >> b;
sf::Color newColor(r, g, b);
if (instruction == "kolor_wypelnienia" || instruction == "kw")
_colors._fillColor = newColor;
else
_colors._outlineColor = newColor;
}
if (instruction == "punkt" || instruction == "pt") {
float x, y;
(ss.ignore(256, ' ') >> x).ignore(256, ' ') >> y;
sf::Vertex point[] = {sf::Vertex(sf::Vector2f(x, y), _colors._outlineColor)};
renderTexture.draw(point, 1, sf::Points);
}
if (instruction== "elipsa" || instruction== "el") {
float x1, x2, y1, y2;
int flag;
(((ss.ignore(256, ' ') >> x1).ignore(256, ' ') >> y1).ignore(256, ' ') >> x2).ignore(256, ' ') >> y2;
ss.ignore(256, ' ') >> flag;
float radius_x = (x2 - x1)/2;
float radius_y = (y2 - y1)/2;
unsigned short quality = 70;
sf::ConvexShape ellipse;
ellipse.setPointCount(quality);
for(unsigned i=0; i<quality; ++i){
float rad = (360/quality*i)/(360/M_PI/2);
float x = cos(rad)*radius_x;
float y = sin(rad)*radius_y;
ellipse.setPoint(i,sf::Vector2f(x,y));
}
ellipse.setOutlineColor(_colors._outlineColor);
if(flag)
ellipse.setFillColor(_colors._fillColor);
ellipse.setPosition(x1 + radius_x, y1 + radius_y);
renderTexture.draw(ellipse);
}
if (instruction== "linia" || instruction == "ln") {
float x1, x2, y1, y2;
(((ss.ignore(256, ' ') >> x1).ignore(256, ' ') >> y1).ignore(256, ' ') >> x2).ignore(256, ' ') >> y2;
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(x1, y1), _colors._outlineColor),
sf::Vertex(sf::Vector2f(x2, y2), _colors._outlineColor)
};
renderTexture.draw(line, 2, sf::Lines);
}
if (instruction== "trojkat" || instruction == "tr") {
float x1, x2, x3, y1, y2, y3;
int f;
(((((ss.ignore(256, ' ') >> x1).ignore(256, ' ') >> y1).ignore(256, ' ') >> x2).ignore(256, ' ') >> y2).ignore(256, ' ') >> x3).ignore(256, ' ') >> y3;
sf::VertexArray triangle(sf::Triangles, 3);
triangle[0].position = sf::Vector2f(x1, y1);
triangle[1].position = sf::Vector2f(x2, y2);
triangle[2].position = sf::Vector2f(x3, y3);
triangle[0].color = _colors._fillColor;
triangle[1].color = _colors._fillColor;
triangle[2].color = _colors._fillColor;
renderTexture.draw(triangle);
}
if (instruction == "rozmiar_piora" || instruction == "rp" ) {
float s;
ss.ignore(256, ' ') >> s;
_colors._outlineThickness = s;
}
if (instruction == "kolo" || instruction == "kl" ) {
float x, y, radius;
int flag;
((ss.ignore(256, ' ') >> x).ignore(256, ' ') >> y).ignore(256, ' ') >> radius;
ss.ignore(256, ' ') >> flag;
sf::CircleShape circle (radius);
circle.setPosition(x, y);
circle.setOutlineColor(_colors._outlineColor);
circle.setOutlineThickness(_colors._outlineThickness);
if (flag)
circle.setFillColor(_colors._fillColor);
renderTexture.draw(circle);
}
if (instruction== "obraz" || instruction == "ob") {
std::string path;
float x, y;
((ss.ignore(256, ' ') >> x).ignore(256, ' ') >> y).ignore(256, ' ') >> path;
if (img_tex.loadFromFile(path)){
sf::Sprite sprite (img_tex);
sprite.setPosition(sf::Vector2f(x, y));
renderTexture.draw(sprite);
}
}
if (instruction == "luk" || instruction == "lk" ) {
float x, y, radius, start_angle, end_angle;
((((ss.ignore(256, ' ') >> x).ignore(256, ' ') >> y).ignore(256, ' ') >> radius).ignore(256, ' ') >> start_angle).ignore(256, ' ') >> end_angle;
sf::VertexArray arc(sf::LinesStrip);
start_angle = start_angle * M_PI/180.;
end_angle = end_angle * M_PI/180.;
float angle = start_angle;
while (angle <= end_angle){
arc.append( sf::Vector2f(x + radius*cos(angle), y + radius*sin(angle)) );
angle += 0.001;
}
renderTexture.draw(arc);
}
}
renderTexture.display();
_frames.emplace_back(renderTexture.getTexture().copyToImage(), timeInterval);
return true;
}
}