From 70c49935374db6051059232cb95d2357143758c3 Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Fri, 12 Nov 2021 02:47:00 +0700 Subject: [PATCH] add video rendering in Screen --- Shooter.cpp | 7 +++++++ Source.cpp | 4 ++-- engine/Screen.cpp | 31 +++++++++++++++++++++++++++++++ engine/Screen.h | 7 +++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Shooter.cpp b/Shooter.cpp index e5c481c..8c78b63 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -156,6 +156,13 @@ void Shooter::update() { setDebugInfo(!showDebugInfo()); } + if (keyboard->isKeyTapped(sf::Keyboard::P)) { + screen->startRender(); + } + if (keyboard->isKeyTapped(sf::Keyboard::L)) { + screen->stopRender(); + } + if (inGame) { screen->setTitle(ShooterConsts::PROJECT_NAME); playerController->update(); diff --git a/Source.cpp b/Source.cpp index 1737a7f..fc008f3 100644 --- a/Source.cpp +++ b/Source.cpp @@ -10,8 +10,8 @@ using namespace std; int main() { Shooter game; - game.create(1280, 720, ShooterConsts::PROJECT_NAME, true); - //game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); + //game.create(1280, 720, ShooterConsts::PROJECT_NAME, true); + game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::None); //game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false); //game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); diff --git a/engine/Screen.cpp b/engine/Screen.cpp index 27192fb..2f45374 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -36,9 +36,40 @@ void Screen::display() { std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)"; _window->setTitle(title); + if(_renderVideo) { + sf::Texture copyTexture; + copyTexture.create(_window->getSize().x, _window->getSize().y); + copyTexture.update(*_window); + // most of the time of video rendering is wasting on .png sequence saving + // that's why we will save all images in the end + // TODO: sometimes we have a huge time delay here for no obvious reason + _renderSequence.push_back(copyTexture); + } + _window->display(); } +void Screen::startRender() { + stopRender(); + _renderVideo = true; +} + +void Screen::stopRender() { + if(_renderVideo) { + + int i = 0; + for(; i < _renderSequence.size(); i++) { + _renderSequence[i].copyToImage().saveToFile("film/png/" + std::to_string(i) + ".png"); + } + _renderSequence.clear(); + + std::string c = "ffmpeg -stats -r 60 -i film/png/%d.png -vcodec libx264 -crf 1 -pix_fmt yuv420p -frames " + std::to_string(i) + " film/mp4/" + std::to_string(_scene) + "_" + _title + ".mp4"; + popen(c.c_str(), "w"); + _scene++; + _renderVideo = false; + } +} + void Screen::clear() { // Clear the depth buffer glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); diff --git a/engine/Screen.h b/engine/Screen.h index ad5fb69..b215971 100644 --- a/engine/Screen.h +++ b/engine/Screen.h @@ -19,6 +19,10 @@ class Screen final { private: + int _scene = 0; + bool _renderVideo = false; + std::vector _renderSequence; + std::string _title; sf::Color _background; @@ -68,6 +72,9 @@ public: void pushGLStates() { _window->pushGLStates(); }; void popGLStates() { _window->popGLStates(); }; + + void startRender(); + void stopRender(); };