/
cutscene.cpp
390 lines (373 loc) · 14.9 KB
/
cutscene.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "cutscene.h" //Cutscene direction data (*.n00)
#include "lpfcut.h" //Cutscene graphics data (*.n[01][^0])
#include "audio/vocfile.h" //Digital audio
#include "UwText.h" //Text strings
#include "uwfont.h" //Underworld font importer
#include "util.h"
#include<iostream>
#include<fstream>
cutscene::cutscene(bool subs/*=true*/, bool audio/*=true*/): cur_n0x(1), cur_frame(0), fade_level(0), fade_target(0), fade_speed(0), with_subs(subs), with_vocs(audio) {
}
bool cutscene::load(const std::string& data_dir, int cut_num) {
base_dir = data_dir;
this->cut_num = cut_num;
std::string n00_file(data_dir+"/cuts/cs"+to_octal3(cut_num)+".n00");
std::string n01_file(data_dir+"/cuts/cs"+to_octal3(cut_num)+".n01");
std::string font_file(data_dir+"/data/fontbig.sys");
std::string strings_file(data_dir+"/data/strings.pak");
if(!load_font(font_file)) return false;
if(!load_n00(n00_file)) return false;
if(!load_strings(strings_file, cut_num)) return false;
//File may or may not exist; things like the "dream" cutscenes use .n01's that don't match the .n00, but they load them immediately with a command in the n00.
load_lpf(n01_file);
return true;
}
bool cutscene::load_n00(std::string& filename) {
cmd.resize(0);
std::ifstream infile(filename.c_str());
if (!infile.is_open()) {
std::cerr << filename << " didn't open. Fix it." << std::endl;
return false;
}
int filesize = 0;
infile.seekg(0, std::ios::end);
filesize = infile.tellg();
infile.seekg(0, std::ios::beg);
std::cout << "Filesize: " << filesize << std::endl;
if (filesize % 2 != 0 || filesize > 2048) {
std::cerr << "Not likely to be a cutscene description. Closing." << std::endl;
infile.close();
return false;
}
while(infile.tellg() < filesize) {
cut_cmd cmd_data;
cmd_data.frame = read16(infile);
cmd_data.cmd_num = read16(infile);
for(int i=0;i<cut_cmd::arg_counts[cmd_data.cmd_num];++i) {
cmd_data.args[i] = read16(infile);
}
cmd.push_back(cmd_data);
}
return true;
}
bool cutscene::load_lpf(std::string& filename) {
frame.resize(0);
cur_frame = 1;
Lpfcut lpf;
if(!lpf.load(filename)) {
std::cerr<<filename<<" didn't open. Fix it." <<std::endl;
return false;
}
lpf.repeat = false;
sf::Uint8 * frame_data = (sf::Uint8 *)lpf.getNextFrame();
while(frame_data) {
sf::Texture f;
f.create(lpf.width, lpf.height);
f.update(frame_data);
frame.push_back(f);
frame_data = (sf::Uint8 *)lpf.getNextFrame();
}
for(int i=0;i<256;++i) {
color temp=lpf.getPalEntry(i);
cur_lpf_pal[i] = sf::Color(temp.r,temp.g,temp.b);
//std::cout<<"Palette "<<i<<": ("<<std::hex<<cur_lpf_pal[i].toInteger()<<") from ("<<temp.r<<temp.g<<temp.b<<temp.a<<")"<<std::endl;
}
if(frame.size() + 1 == lpf.rec_count)
return true;
else {
std::cerr<<"Loaded "<<frame.size()<<" frames, but the lpf reports that "<<lpf.rec_count<<" should be available!"<<std::endl;
}
}
bool cutscene::load_font(std::string& filename) {
uwfont f;
bool ret = f.load(filename);
if(!ret) {
std::cerr<<"Couldn't load "<<filename<<std::endl;
return ret;
}
std::string font_repr = f.to_bdf();
ret = cs_font.loadFromMemory(font_repr.data(), font_repr.length());
if(!ret) {
std::cerr<<"Couldn't load the font into SFML"<<std::endl;
return ret;
}
return true;
}
bool cutscene::load_strings(std::string& filename, int cut_num) {
strings.resize(0);
UwText text;
if(!text.load(filename)) {
std::cerr<<"Couldn't load "<<filename<<". Fix it."<<std::endl;
return false;
}
int blocknum = 0;
for(int i=0;i<text.get_block_count();++i) {
if(text.get_block_num(i) == 0xc00 + cut_num) {
blocknum = i;
break;
}
}
for(int s=0;s<text.get_string_count(blocknum);++s) {
strings.push_back(text.get_string(blocknum, s));
}
return true;
}
void cutscene::play_voc(int number) {
while(cur_sound.getStatus() == sf::SoundSource::Playing) {
sf::sleep(sf::milliseconds(100));
}
cur_sound.stop();
std::string numstr = std::to_string(number);
if(number < 10) {
numstr = std::string("0")+numstr;
}
std::vector<int16_t> snd_dat = vocfile::get_file_dat(std::string(base_dir+"/sound/"+numstr+".voc"));
//cur_sb.loadFromFile(std::string(base_dir+"/sound/"+std::to_string(number)+".voc"));
cur_sb.loadFromSamples(&snd_dat[0], snd_dat.size(),1,12048);
cur_sound.setBuffer(cur_sb);
cur_sound.play();
}
std::string cutscene::format_string(std::string& input) {
std::string output(input);
sf::Text txt = sf::Text(output, cs_font, 10);
int width = txt.getLocalBounds().width;
//300, because I'd rather err toward too many lines than too few, so I'll pretend the screen is a little smaller.
int line_breaks = width / 320;
//std::cout<<"Width: "<<width<<" Breaks: "<<line_breaks;
if(line_breaks == 0) return output;
//int spaces = std::count(output.begin(), output.end(), ' ');
//size_t space_skip = spaces/(line_breaks+1) + 1;
size_t char_skip = output.size() / (line_breaks + 1) + 1;
//std::cout<<"Char skip: "<<char_skip<<std::endl;
//size_t pos = 0;
size_t split_loc = char_skip;
for(size_t i = 0; i < line_breaks; i++) {
size_t posr = output.find(' ', split_loc);
size_t posl = output.substr(split_loc).rfind(' ');
size_t pos = 0;
assert((posl > 0 && posl < output.size())||posl == std::string::npos);
assert((posr > 0 && posr < output.size())||posr == std::string::npos);
if(((split_loc - posl) < (posr - split_loc) || posr == std::string::npos) && posl != std::string::npos ) {
output = output.replace(posl, 1, 1, '\n');
pos = posl;
}
else if(posr != std::string::npos) {
output = output.replace(posr, 1, 1, '\n');
pos = posr;
}
else {
break;
//std::cout<<"Flummoxed by the string: \""<<output<<"\""<<std::endl;
}
split_loc += char_skip;
}
}
void cutscene::play(sf::RenderWindow& screen) {
sf::Sprite spr;
spr.scale(1,1.2);
sf::Text txt("", cs_font, 10);
int prev_frame = 1;
int fps = 5;
for(int i=0; i<cmd.size();i++) {
screen.setFramerateLimit(5);
fps = 5;
cur_frame = cmd[i].frame;
std::cout<<cmd[i].tostring()<<std::endl;
if(prev_frame < cur_frame) {
std::cout<<"Playing from frame "<<prev_frame<<" to frame "<<cur_frame<<std::endl;
for(int f = prev_frame;f<cur_frame;++f) {
//assert(f > 0);
//assert(frame[f-1].getSize() == sf::Vector2u(320,200));
if(f > 0) {
screen.clear();
spr.setTexture(frame[f - 1]);
screen.draw(spr);
screen.draw(txt);
screen.display();
}
}
}
if(cmd[i].cmd_num != 1) {
prev_frame = cur_frame;
}
switch(cmd[i].cmd_num) {
case 0x00: //Display text arg[1] with color arg[0]
{
assert(cmd[i].args[1] < strings.size());
std::string temp_string = format_string(strings[cmd[i].args[1]]);
txt = sf::Text(temp_string, cs_font, 10);
//txt.setColor(cur_lpf_pal[cmd[i].args[0]]);
txt.setFillColor(cur_lpf_pal[cmd[i].args[0]]);
int txtw = (320 - txt.getLocalBounds().width)/2;
int txth = 240 - txt.getLocalBounds().height - 10;
txt.setPosition(txtw,txth);
}
break;
case 0x03: //Pause for arg[0]/2 seconds
if(cur_frame > 0) {
screen.clear();
spr.setTexture(frame[cur_frame - 1]);
screen.draw(spr);
screen.draw(txt);
screen.display();
}
sf::sleep(sf::milliseconds(cmd[i].args[0]*500));
break;
case 0x04: //Play up to frame arg[0] (at rate arg[1]? Doesn't seem necessary)
for(;cur_frame<cmd[i].args[0]-1;++cur_frame) {
if(cur_frame > 0 && cur_frame<= frame.size()) {
spr.setTexture(frame[cur_frame-1]);
}
screen.clear();
screen.draw(spr);
screen.draw(txt);
screen.display();
}
prev_frame = cur_frame;
break;
case 0x05: //Probably related to "static" cutscene settings
break;
case 0x06: //End of cutscene
return;
case 0x07: //Repeat a section of animation arg[0] times
for(int rep=0;rep<cmd[i].args[0];++rep) {
for(int rep_frame=0;rep_frame < cmd[i].frame-1;++rep_frame) {
if(cur_frame - 1 >= 0 && cur_frame - 1 < frame.size()) {
spr.setTexture(frame[rep_frame]);
}
screen.draw(spr);
screen.draw(txt);
screen.display();
}
}
break;
case 0x08: //Switch to cs{args[0]}.n{args[1]} for playback
{
std::string n0x_file(base_dir+"/cuts/cs"+to_octal3(cmd[i].args[0])+".n"+to_octal2(cmd[i].args[1]));
if(!load_lpf(n0x_file)) {
std::cerr<<"Couldn't load "<<n0x_file<<", even though I was explicitly told to."<<std::endl;
}
cut_num = cmd[i].args[0];
cur_n0x = cmd[i].args[1];
cur_frame = 1;
spr.setTexture(frame[cur_frame - 1]);
txt = sf::Text("", cs_font, 10);
}
break;
case 0x09: //Fade out
{
screen.setFramerateLimit(45);
sf::RectangleShape fade(sf::Vector2f(screen.getSize()));
if(cur_frame - 1 >= 0 && cur_frame - 1 < frame.size()) {
spr.setTexture(frame[cur_frame-1]);
}
int rate = 256/(16*cmd[i].args[0]+1);
for(int opacity=0;opacity < 255; opacity+=rate) {
fade.setFillColor(sf::Color(0,0,0,opacity));
screen.draw(spr);
screen.draw(txt);
screen.draw(fade);
screen.display();
}
txt = sf::Text("", cs_font, 10);
screen.setFramerateLimit(fps);
}
break;
case 0x0a: //Fade in
{
screen.setFramerateLimit(45);
sf::RectangleShape fade(sf::Vector2f(screen.getSize()));
if(cur_frame - 1 >= 0 && cur_frame - 1 < frame.size()) {
spr.setTexture(frame[cur_frame-1]);
}
int rate = 256/(16*(cmd[i].args[0]+1));
for(int opacity=255;opacity >= 0; opacity-=rate) {
fade.setFillColor(sf::Color(0,0,0,opacity));
screen.draw(spr);
screen.draw(txt);
screen.draw(fade);
screen.display();
}
screen.setFramerateLimit(fps);
}
break;
case 0x0d: //Display text arg[1] with color arg[0] and play sound arg[2]
if(with_subs) {
assert(cmd[i].args[1] < strings.size());
std::string temp_string = format_string(strings[cmd[i].args[1]]);
txt = sf::Text(temp_string, cs_font, 10);
txt.setFillColor(cur_lpf_pal[cmd[i].args[0]]);
//txt.setColor(cur_lpf_pal[cmd[i].args[0]]);
int txtw = (320 - txt.getLocalBounds().width)/2;
int txth = 240 - txt.getLocalBounds().height - 10;
txt.setPosition(txtw,txth);
std::cout<<"Set text position to "<<txtw<<", "<<txth<<std::endl;
}
if(with_vocs) {
play_voc(cmd[i].args[2]);
}
break;
case 0x0e: //Pause for arg[0] if audio is on, arg[1] if audio is off
if(with_vocs) {
sf::sleep(sf::milliseconds(cmd[i].args[0]*500));
}
else {
sf::sleep(sf::milliseconds(cmd[i].args[1]*1000));
}
break;
default:
std::cout<<"Not implemented: "<<cmd[i].tostring()<<std::endl;
break;
}
}
}
std::string cutscene::to_octal2(int cut_num) {
std::string base("");
char dig_1, dig_2;
dig_2 = cut_num % 8 + '0';
cut_num /= 8;
dig_1 = cut_num + '0';
return base+dig_1+dig_2;
}
std::string cutscene::to_octal3(int cut_num) {
std::string base("");
char dig_1, dig_2, dig_3;
dig_3 = cut_num % 8 + '0';
cut_num /= 8;
dig_2 = cut_num % 8 + '0';
cut_num /= 8;
dig_1 = cut_num + '0';
return base+dig_1+dig_2+dig_3;
}
const int cutscene::cut_cmd::arg_counts[32] = { 2, 0, 2, 1,
2, 1, 0, 1,
2, 1, 1, 1,
1, 3, 2, 0, //UW1 functions end at 0xf
0, 0, 0, 0,
0, 0, 0, 0,
1, 1, 0, 1,
0, 0, 0, 0 };
std::string cutscene::cut_cmd::tostring() {
if(cmd_num >=0 && cmd_num < 16) {
std::string retval(std::string("Frame: ") + std::to_string(frame) + " Cmd: " + std::to_string(cmd_num));
if(arg_counts[cmd_num] > 0) retval += " Args: ";
for(int i=0;i<arg_counts[cmd_num];++i) {
retval+=" "+std::to_string(args[i]);
}
return retval;
}
else if(cmd_num >=16 && cmd_num < 32) {
return std::string("Probably a command from a UW2 cutscene. Parsing past this point is broken :-)");
}
}
#ifdef STAND_ALONE_CS
int main(int argc, char *argv[]) {
//2 args: directory containing game data (cuts+sound), and a cutscene number.
sf::RenderWindow window(sf::VideoMode(320, 240), "SFML window");
cutscene c;
if(!c.load(argv[1], std::stoi(argv[2]))) {
std::cerr<<"Failed to load files using path "<<argv[1]<<std::endl;
}
c.play(window);
}
#endif