-
Notifications
You must be signed in to change notification settings - Fork 0
/
onstage.cpp
114 lines (94 loc) · 3.61 KB
/
onstage.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
#include "stage_command.hpp"
#include "thestage.hpp"
#include "picojson.h"
#include "cmdline.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cinttypes>
#include <ctime>
#include <cairo.h>
#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>
#include "stage_viewer.hpp"
int main(int argc, char** argv)
{
TheStage stage;
cmdline::parser opt;
opt.add<std::string>("file", 'f', "Script file.");
opt.add("interactive", 'i', "Run in interactive mode.");
opt.add("quiet", 'q', "Quiet mode; does not emit recorded script.");
opt.add<std::string>("directory", 'd', "Recording directory.", false, ".");
opt.add("list-commands", 'l', "List all the avaiable commands.");
opt.add<int>("start", 's', "Start at.", false, 0);
opt.parse_check(argc, argv);
if (opt.exist("list-commands")) {
std::cout << "Commands supported:" << std::endl;
exit(0);
}
bool isInteractive = opt.exist("interactive");
bool isQuiet = opt.exist("quiet");
std::string recordingDirectory = opt.get<std::string>("directory");
// TODO: Do these things inside the Stage
std::string scriptFileName = opt.get<std::string>("file");
std::ifstream scriptFile(scriptFileName);
if (!scriptFile.is_open()) {
std::cout << "No such file: " << scriptFileName << std::endl;
exit(1);
}
picojson::value json;
scriptFile >> json;
scriptFile.close();
picojson::object& obj = json.get<picojson::object>();
picojson::array& commands = obj["commands"].get<picojson::array>();
// TODO: Make VideoConfig and use it to deal with these.
picojson::object& videoSetting = obj["video"].get<picojson::object>();
std::string outputDirectory = videoSetting["output"].get<std::string>();
stage.SetFps((int)videoSetting["fps"].get<double>());
stage.SetDuration((int)videoSetting["duration"].get<double>());
stage.SetResolution((int)videoSetting["width"].get<double>(), (int)videoSetting["height"].get<double>());
stage.SetOutputDirectory(videoSetting["output"].get<std::string>());
stage.SetResourcesDirectory(videoSetting["resources"].get<std::string>());
stage.ShowVideoSetting();
rsvg_init(); // TODO: GCC tells this is deprecated. See the detail. Or about librsvgmm.
for(picojson::array::iterator it = commands.begin(); it != commands.end(); ++it) {
stage.storedCommands.push_back(StageCommandFactory::Create(*it));
}
stage.Start();
int startAt = opt.get<int>("start");
if (isInteractive) {
StageViewer viewer(&stage);
viewer.Run();
} else {
for(int i = 0; i < stage.GetDuration(); i++) {
stage.ExecuteCommandsUntilCurrentFrame();
if (i < startAt) {
stage.Skip();
std::cout << stage.GetCurrentFrame() << "th frame skipped." << std::endl;
} else {
cairo_surface_t* surface = stage.Render();
std::stringstream filename;
filename << outputDirectory << "f" << std::setw(3) << std::setfill('0') << i + 1 << ".png";
cairo_surface_write_to_png(surface, filename.str().c_str());
cairo_surface_destroy(surface);
std::cout << stage.GetCurrentFrame() << "th frame rendered." << std::endl;
}
}
}
stage.End();
rsvg_term(); // TODO: GCC tells this is deprecated. See the detail.
if (!isQuiet) {
time_t t = time(0);
std::stringstream ss;
// TODO: Trim the trailing slash
// TODO: Make sure the existance of the directory you're about to write into.
ss << recordingDirectory << "/recorded-" << t << ".json";
std::string recordedScriptFileName = ss.str();
std::ofstream recordedScriptFile(recordedScriptFileName);
recordedScriptFile << stage.GetRecordedScript();
recordedScriptFile.close();
}
return 0;
}