diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/src/main.cpp b/src/main.cpp index 5876efc..5f31427 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,7 @@ #include <SFML/Graphics.hpp> +#include <thread> +#include <mutex> +#include <deque> int main() { sf::Texture texture; @@ -32,9 +35,33 @@ int main() { int flag_width = texture.getSize().x*0.6; int flag_height = texture.getSize().y*0.6; + system("rm -rf frames/*; mkdir frames"); + + std::deque<sf::Image> pending_frames; + std::mutex images_lock; + + bool running = true; + std::thread work_thread([&images_lock, &running, &pending_frames]() mutable { + size_t frame_index = 0; + while(true) { + images_lock.lock(); + if(!pending_frames.empty()) { + sf::Image image = std::move(pending_frames.front()); + pending_frames.pop_front(); + images_lock.unlock(); + image.saveToFile("frames/" + std::to_string(frame_index) + ".png"); + ++frame_index; + } else { + bool is_kill = !running; + images_lock.unlock(); + if(is_kill) + break; + } + } + }); + sf::Image output_image; output_image.create(flag_width, flag_height, sf::Color::Transparent); - std::vector<sf::Image> frames; sf::Clock timer; while (window.isOpen()) { @@ -51,14 +78,14 @@ int main() { texture_output.update(window); sf::Image image = texture_output.copyToImage(); + output_image.copy(image, 0, 0, sf::IntRect(flag_start_x, flag_start_y, flag_width, flag_height), false); - frames.push_back(std::move(output_image)); + std::lock_guard<std::mutex> lock(images_lock); + pending_frames.push_back(output_image); } - system("rm -rf frames/*; mkdir frames"); - for(size_t i = 0; i < frames.size(); ++i) { - frames[i].saveToFile("frames/" + std::to_string(i) + ".png"); - } + running = false; + work_thread.join(); system("ffmpeg -i \"frames/%d.png\" -framerate 24 -filter_complex \"fps=24,split=2[palette_in][gif];[palette_in]palettegen[palette_out];[gif]fifo[gif_fifo]; [gif_fifo][palette_out]paletteuse\" -y frames/output.gif"); return 0; |