-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_runner.cpp
56 lines (47 loc) · 1.16 KB
/
main_runner.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
#include "runtime/ink_story.h"
#include "ink_compiler.h"
#include <iostream>
#if __has_include(<print>)
#include <print>
using std::print;
#else
#include <format>
#define print(fmt, ...) std::cout << std::format(fmt __VA_OPT__(,) __VA_ARGS__)
#endif
int main(int argc, char* argv[]) {
if (argc != 2) {
print("Error: No ink file specified\n");
return 1;
}
std::string infile = argv[1];
InkStory story;
if (infile.ends_with(".inkb")) {
story = InkStory(infile);
} else {
InkCompiler compiler;
story = compiler.compile_file(infile);
}
while (true) {
while (story.can_continue()) {
std::string result = story.continue_story();
if (!result.empty()) {
print("{}\n\n", result);
}
}
const std::vector<std::string>& current_choices = story.get_current_choices();
if (!current_choices.empty()) {
for (std::size_t i = 0; i < current_choices.size(); ++i) {
print("{}: {}\n", i + 1, current_choices[i]);
}
print("\n");
std::size_t choice = SIZE_MAX;
while (choice > current_choices.size()) {
print("> ");
std::cin >> choice;
}
story.choose_choice_index(static_cast<std::size_t>(choice - 1));
} else {
break;
}
}
}