-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
153 lines (120 loc) · 5.04 KB
/
Animation.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
#include "Animation.h"
#include <experimental/filesystem>
#include "Parser.h"
#include <iostream>
#include "Font.h"
namespace animation {
namespace fs = std::experimental::filesystem;
Animation::Animation() : _ctrl(new Controller) {
if (_font.loadFromMemory(&__04B_20__, __04B_20__len))
{
_text.setFont(_font);
_text.setString("PRESS SPACE \n\tTO PLAY\n\nPRESS S TO \n\tSAVE");
_text.setCharacterSize(45);
_text.setFillColor(sf::Color::White);
_text.setStyle(sf::Text::Bold);
_window.setVerticalSyncEnabled(false);
}
}
void Animation::run() {
Parser parser {"mySample.txt"};
parser._config._width > parser._config._height ? _text.setCharacterSize(0.07 * parser._config._height) : _text.setCharacterSize(0.05 * parser._config._width);
//while(parser.loadNextFrame()){}
_window.create(sf::VideoMode(parser._config._width, parser._config._height),"Animation",sf::Style::Titlebar | sf::Style::Close);
sf::FloatRect textRect = _text.getLocalBounds();
_text.setPosition(parser._config._width/2 - textRect.width/2., parser._config._height/2 - textRect.height/2.);
sf::Event event;
sf::Clock clock;
auto it = parser._frames.begin();
bool isLoaded {};
unsigned loadedFrames {1};
while(_window.isOpen()) {
while (_window.pollEvent(event)) {
if(event.type == sf::Event::Closed)
_window.close();
if (event.type == sf::Event::KeyPressed) {
if(isLoaded && event.key.code == sf::Keyboard::Space) {
_ctrl->isPlaying() = true;
clock.restart();
}
if (event.key.code == sf::Keyboard::Escape)
_window.close();
if (isLoaded && event.key.code == sf::Keyboard::Left)
_ctrl->speedDown();
if (isLoaded && event.key.code == sf::Keyboard::Right)
_ctrl->speedUp();
if(isLoaded && event.key.code == sf::Keyboard::S) {
if (!fs::is_directory("animation_output") || !fs::exists("animation_output")) // Check if src folder exists
fs::create_directory("animation_output"); // create src folder
int i {};
for (const auto & frame : parser._frames) {
frame._image.saveToFile("animation_output/" + std::to_string(i) + ".bmp");
process("SAVING", i, parser._config._framesAmount);
++i;
}
_window.display();
}
}
}
if (isLoaded && _ctrl->isPlaying()) {
if (it == parser._frames.end()) {
it = parser._frames.begin();
_ctrl->isPlaying() = false;
_ctrl->resetMultiplier();
clock.restart();
}
else {
_window.clear(sf::Color::Black);
sf::Texture text;
text.create(parser._config._width, parser._config._height);
text.update(it->_image);
_window.draw(sf::Sprite(text));
_window.display();
if (clock.getElapsedTime().asMilliseconds() >= _ctrl->getRealTimeInterval(it->_timeInterval)) {
++it;
clock.restart();
}
}
}
else if (!isLoaded) {
if (parser.loadNextFrame())
++loadedFrames;
else {
isLoaded = true;
it = parser._frames.begin();
}
process("LOADING", loadedFrames, parser._config._framesAmount);
}
else if (isLoaded && !_ctrl->isPlaying()) {
_window.clear(sf::Color::Blue);
_window.draw(_text);
_window.display();
}
}
}
void Animation::process (std::string name, unsigned how_many, unsigned how_many_all){
sf::Text text;
text.setFont(_font);
text.setCharacterSize(45);
int w = _window.getSize().x, h = _window.getSize().y;
w > h ? text.setCharacterSize(0.07 * h) : text.setCharacterSize(0.05 * w);
sf::FloatRect textRect = _text.getLocalBounds();
text.setPosition(w/2 - textRect.width/2., h/2 - textRect.height/2.);
text.setFillColor(sf::Color::White);
text.setStyle(sf::Text::Bold);
text.setString(name);
sf::RectangleShape strip;
sf::RectangleShape loading_strip;
strip.setPosition(w*0.1, h*0.7);
strip.setFillColor(sf::Color(15, 16, 17));
strip.setSize(sf::Vector2f(w*0.8, 50));
loading_strip.setPosition(w*0.1 + 2, h*0.7 + 2);
loading_strip.setFillColor(sf::Color(90, 255, 20));
_window.clear(sf::Color::Blue);
_window.draw(text);
_window.draw(strip);
loading_strip.setSize(sf::Vector2f(w*0.79 * how_many/how_many_all, 46));
_window.draw(loading_strip);
_window.display();
}
}