spacebox/src/Recorder.hpp

84 lines
2.2 KiB
C++
Raw Normal View History

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#pragma once
2019-05-07 03:33:54 -04:00
#define GLM_ENABLE_EXPERIMENTAL
#include <list>
#include <vector>
2019-05-07 03:33:54 -04:00
#include <sstream>
#include <string>
2019-05-19 05:55:46 -04:00
#include <functional>
2019-05-22 04:27:15 -04:00
#include <cstdlib>
#include <fstream>
#include <thread>
2019-05-07 03:33:54 -04:00
#include "SDL.h"
#include "SDL_mixer.h"
2019-05-07 03:33:54 -04:00
#include "glm/ext.hpp"
#include "json/json.hpp"
#include "filesystem.hpp"
#include "Node.hpp"
2023-06-08 20:04:40 -04:00
#include "Configuration.hpp"
2019-05-16 03:51:36 -04:00
#include "Animation.hpp"
2021-10-02 19:21:07 -04:00
#include "Log.hpp"
2019-05-07 03:33:54 -04:00
struct Stash
{
std::vector<unsigned char*> pixel_buffers;
2019-06-16 03:05:02 -04:00
std::vector<bool> flipped;
2019-06-04 03:20:13 -04:00
std::vector<Uint8*> audio_buffers;
std::vector<int> audio_buffer_lengths;
int frame_offset;
Stash(int frame_offset = 0) : frame_offset(frame_offset) {}
};
class Recorder : public Node
2019-05-07 03:33:54 -04:00
{
private:
2019-05-31 02:54:31 -04:00
Stash current_stash = Stash();
Stash most_recent_stash;
std::list<Stash> in_game_stashes;
std::list<Stash> video_stashes;
2019-06-04 03:20:13 -04:00
fs::path current_video_directory, current_audio_path;
2019-05-31 02:54:31 -04:00
bool is_recording = false, writing_recording = false, writing_most_recent = false;
2019-06-04 03:20:13 -04:00
std::ofstream audio_file;
2019-05-16 03:51:36 -04:00
float frame_length();
static void process_audio(void*, Uint8*, int);
public:
Animation animation = Animation(std::bind(&Recorder::add_frame, this));
2019-05-07 03:33:54 -04:00
Recorder(Node*);
void respond(SDL_Event&);
void capture_screen();
2019-05-31 02:54:31 -04:00
void grab_stash();
void write_most_recent_frames();
2019-05-16 03:51:36 -04:00
void start_recording();
2019-06-04 03:20:13 -04:00
void open_audio_file();
void add_frame();
int get_memory_size();
2019-06-04 03:20:13 -04:00
void make_directory();
void write_stash_frames(Stash*);
void keep_stash();
2019-05-16 03:51:36 -04:00
void end_recording();
void finish_writing_video();
2019-05-31 02:54:31 -04:00
void write_mp4();
2019-06-04 03:20:13 -04:00
void write_audio(Uint8*, int);
void update(float timestamp);
virtual std::string class_name() const { return "Recorder"; }
2019-05-07 03:33:54 -04:00
};