add video rendering in Screen

master
Vectozavr 2021-11-12 02:47:00 +07:00
parent 85228279ba
commit 70c4993537
4 changed files with 47 additions and 2 deletions

View File

@ -156,6 +156,13 @@ void Shooter::update() {
setDebugInfo(!showDebugInfo()); setDebugInfo(!showDebugInfo());
} }
if (keyboard->isKeyTapped(sf::Keyboard::P)) {
screen->startRender();
}
if (keyboard->isKeyTapped(sf::Keyboard::L)) {
screen->stopRender();
}
if (inGame) { if (inGame) {
screen->setTitle(ShooterConsts::PROJECT_NAME); screen->setTitle(ShooterConsts::PROJECT_NAME);
playerController->update(); playerController->update();

View File

@ -10,8 +10,8 @@ using namespace std;
int main() { int main() {
Shooter game; Shooter game;
game.create(1280, 720, ShooterConsts::PROJECT_NAME, true); //game.create(1280, 720, ShooterConsts::PROJECT_NAME, true);
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::None);
//game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false); //game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false);
//game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); //game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);

View File

@ -36,9 +36,40 @@ void Screen::display() {
std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)"; std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)";
_window->setTitle(title); _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(); _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() { void Screen::clear() {
// Clear the depth buffer // Clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

View File

@ -19,6 +19,10 @@
class Screen final { class Screen final {
private: private:
int _scene = 0;
bool _renderVideo = false;
std::vector<sf::Texture> _renderSequence;
std::string _title; std::string _title;
sf::Color _background; sf::Color _background;
@ -68,6 +72,9 @@ public:
void pushGLStates() { _window->pushGLStates(); }; void pushGLStates() { _window->pushGLStates(); };
void popGLStates() { _window->popGLStates(); }; void popGLStates() { _window->popGLStates(); };
void startRender();
void stopRender();
}; };