Debian Codegraph @

src/jj1/scene/jj1scene.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file jj1scene.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created scene.c
10 * - 3rd February 2009: Created scene.h from parts of scene.c
11 * - 3rd February 2009: Renamed scene.c to scene.cpp
12 * - 27th March 2010: Created sceneload.cpp from parts of scene.cpp
13 * - 1st August 2012: Renamed scene.cpp to jj1scene.cpp
14 *
15 * @par Licence:
16 * Copyright (c) 2005-2017 AJ Thomson
17 * Copyright (c) 2015-2024 Carsten Teibes
18 *
19 * OpenJazz is distributed under the terms of
20 * the GNU General Public License, version 2.0
21 *
22 * @par Description:
23 * Deals with the displaying and freeing of the cutscenes.
24 *
25 */
26
27
28 #include "jj1scene.h"
29
30 #include "io/controls.h"
31 #include "io/gfx/font.h"
32 #include "io/gfx/paletteeffects.h"
33 #include "io/gfx/video.h"
34 #include "io/sound.h"
35 #include "loop.h"
36 #include "util.h"
37 #include "io/log.h"
38
39 #include <string.h>
40 #include <algorithm> // std::find_if
41
42
43 /**
44 * Create a JJ1 cutscne frame.
45 *
46 * @param newFrameType The type of the frame
47 * @param newFrameData The frame's data buffer
48 * @param newFrameSize The size of the frame's data buffer
49 */
50 JJ1SceneFrame::JJ1SceneFrame(int newFrameType, unsigned char* newFrameData, int newFrameSize) :
51 next(nullptr), prev(nullptr), frameData(newFrameData),
52 frameSize(newFrameSize), frameType(newFrameType), soundId(SE::NONE) {
53
54 }
55
56
57 /**
58 * Delete the JJ1 cutscene frame.
59 */
60 JJ1SceneFrame::~JJ1SceneFrame() {
61 delete[] frameData;
62 }
63
64
65 /**
66 * Add a frame to the JJ1 cutscene animation.
67 *
68 * @param frameType The type of the frame
69 * @param frameData The frame's data buffer
70 * @param frameSize The size of the frame's data buffer
71 */
72 void JJ1SceneAnimation::addFrame(int frameType, unsigned char* frameData, int frameSize) {
73
74 JJ1SceneFrame* frame = new JJ1SceneFrame(frameType, frameData, frameSize);
75
76 if(sceneFrames == NULL) {
77
78 sceneFrames = frame;
79
80 } else {
81
82 frame->prev = lastFrame;
83 lastFrame->next = frame;
84
85 }
86
87 lastFrame = frame;
88 frames++;
89
90 }
91
92
93 /**
94 * Create a JJ1 cutscene animation.
95 */
96 JJ1SceneAnimation::JJ1SceneAnimation (int id) :
97 sceneFrames(nullptr), lastFrame(nullptr), background(nullptr),
98 id(id), frames(0), reverseAnimation(0) {
99
100 scratch = video.createSurface(nullptr, SW, SH);
101 }
102
103
104 /**
105 * Delete the JJ1 cutscene animation.
106 */
107 JJ1SceneAnimation::~JJ1SceneAnimation () {
108 if(sceneFrames) {
109 JJ1SceneFrame* frame = sceneFrames;
110 JJ1SceneFrame* nextFrame = NULL;
111 while(frame) {
112 nextFrame = frame->next;
113 delete frame;
114 frame = NULL;
115 frame = nextFrame;
116 }
117 }
118
119 video.destroySurface(background);
120 video.destroySurface(scratch);
121 }
122
123
124 /**
125 * Create a JJ1 cutscene image.
126 */
127 JJ1SceneImage::JJ1SceneImage (int id) :
128 image(nullptr), id(id) {
129
130 }
131
132
133 /**
134 * Delete the JJ1 cutscene image.
135 */
136 JJ1SceneImage::~JJ1SceneImage () {
137 video.destroySurface(image);
138 }
139
140
141 /**
142 * Create a JJ1 cutscene palette.
143 */
144 JJ1ScenePalette::JJ1ScenePalette(int id) :
145 id(id) {
146
147 }
148
149 /**
150 * Create a JJ1 cutscene font.
151 */
152 JJ1SceneFont::JJ1SceneFont(int id) :
153 id(id) {
154
155 }
156
157 /**
158 * Create a JJ1 cutscene text object.
159 */
160 JJ1SceneText::JJ1SceneText() :
161 text(nullptr), alignment(0), fontId(-1), x(-1), y(-1),
162 textRect({-1, -1, 0, 0}), extraLineHeight(-1),
163 textColour(0), shadowColour(-1) {
164
165 }
166
167
168 /**
169 * Delete the JJ1 cutscene text object.
170 */
171 JJ1SceneText::~JJ1SceneText() {
172 if (text) {
173 delete[] text;
174 text = nullptr;
175 }
176 }
177
178
179 /**
180 * Create a JJ1 cutscene page.
181 */
182 JJ1ScenePage::JJ1ScenePage() :
183 backgrounds(0), animLoops(0), animSpeed(0), animIndex(-1), // no anim
184 nextPageAfterAnim(0), pageTime(0), musicFile(nullptr), paletteIndex(0),
185 askForYesNo(false), stopMusic(false), backgroundFade(255), transitionType(0)
186 {
187
188 }
189
190
191 /**
192 * Delete the JJ1 cutscene page.
193 */
194 JJ1ScenePage::~JJ1ScenePage() {
195 if (musicFile) {
196 delete[] musicFile;
197 musicFile = nullptr;
198 }
199 }
200
201
202 /**
203 * Create a JJ1 cutscene.
204 *
205 * @param fileName Name of the file containing the cutscene data
206 */
207 JJ1Scene::JJ1Scene (const char * fileName) {
208
209 FilePtr file;
210 LOG_TRACE("Scene: %s", fileName);
211
212 try {
213
214 file = std::make_unique<File>(fileName, PATH_TYPE_GAME);
215
216 } catch (int e) {
217
218 throw;
219
220 }
221
222 // Checking scene file
223 char *identifier1 = file->loadString(18);
224 char identifier2 = file->loadChar();
225 if (strncmp(identifier1, "Digital Dimensions", 18) != 0 || identifier2 != 0x1A) {
226 LOG_ERROR("Scene not valid!");
227 delete[] identifier1;
228 return;
229 }
230 delete[] identifier1;
231
232 signed long int dataOffset = file->loadInt(); // Get offset pointer to first data block
233
234 scriptItems = file->loadShort(); // Get number of script items
235 scriptStarts.resize(scriptItems);
236 pages.resize(scriptItems);
237 LOG_TRACE("Scene: Script items: %d", scriptItems);
238 for (int i = 0; i < scriptItems; i++) {
239 scriptStarts[i] = file->loadInt(); // Load offset to script
240 LOG_MAX("scriptStart: %ld", scriptStarts[i]);
241 }
242
243 // Seek to datastart now
244 file->seek(dataOffset, true); // Seek to data offsets
245 dataItems = file->loadShort() + 1; // Get number of data items
246 LOG_TRACE("Scene: Data items %d", dataItems);
247 dataOffsets.resize(dataItems);
248 for (int i = 0; i < dataItems; i++) {
249 dataOffsets[i] = file->loadInt(); // Load offset to script
250 LOG_MAX("dataOffsets: %ld", dataOffsets[i]);
251 }
252
253 loadData(file.get());
254 loadScripts(file.get());
255 }
256
257
258 /**
259 * Play the JJ1 cutscene.
260 *
261 * @return Error code
262 */
263 int JJ1Scene::play () {
264
265 SDL_Rect dst;
266 unsigned int sceneIndex = 0;
267 JJ1SceneFrame* currentFrame = NULL;
268 auto sceneAnimation = animations.end();
269 PaletteEffect* paletteEffect = NULL;
270 int frameDelay = 0;
271 int prevFrame = 0;
272 int continueToNextPage = 0;
273
274 unsigned int pageTime = pages[sceneIndex].pageTime;
275 unsigned int lastTicks = globalTicks;
276 int newpage = true;
277 SDL_Rect textRect = {0, 0, SW, SH};
278
279 video.clearScreen(0);
280 video.moviePlayback(true);
281
282 while (true) {
283
284 bool upOrLeft = false;
285 bool downOrRight = false;
286 int x, y;
287
288 if (loop(NORMAL_LOOP, paletteEffect) == E_QUIT) {
289
290 if (paletteEffect) delete paletteEffect;
291 video.moviePlayback(false);
292
293 return E_QUIT;
294
295 }
296
297 controls.getCursor(x, y);
298
299 x -= (canvasW - SW) >> 1;
300 y -= (canvasH - SH) >> 1;
301
302 downOrRight = controls.wasCursorReleased();
303
304 if (controls.release(C_ESCAPE) ||
305 (controls.release(C_NO) && pages[sceneIndex].askForYesNo) ||
306 (downOrRight && (x >= 0) && (x < 100) && (y >= SH - 12) && (y < SH))) {
307
308 if (paletteEffect) delete paletteEffect;
309 video.moviePlayback(false);
310
311 return E_NONE;
312
313 }
314
315 SDL_Delay(T_MENU_FRAME);
316
317
318 if(pages[sceneIndex].askForYesNo) {
319 downOrRight |= controls.release(C_ENTER) || controls.release(C_YES);
320 } else {
321 upOrLeft = (controls.release(C_UP) || controls.release(C_LEFT));
322 downOrRight |= (controls.release(C_RIGHT) || controls.release(C_DOWN) || controls.release(C_ENTER));
323 }
324
325 if ((sceneIndex > 0 && upOrLeft) ||
326 downOrRight || continueToNextPage ||
327 ((globalTicks-lastTicks) >= pageTime * 1000 && pageTime != 256 && pageTime != 0)) {
328
329 if(pages[sceneIndex].stopMusic) {
330 stopMusic();
331 }
332
333 if (upOrLeft) sceneIndex--;
334 else sceneIndex++;
335
336 if (sceneIndex == scriptItems) {
337
338 if (paletteEffect) delete paletteEffect;
339 video.moviePlayback(false);
340
341 return E_NONE;
342
343 }
344
345 lastTicks = globalTicks;
346 // Get bg for this page
347 newpage = true;
348
349 pageTime = pages[sceneIndex].pageTime;
350 continueToNextPage = 0;
351 }
352
353 if (newpage) {
354
355 // Always fade out to black
356 if(sceneIndex > 0 && pages[sceneIndex].backgroundFade > 0) {
357 if (paletteEffect) delete paletteEffect;
358 paletteEffect = new FadeOutPaletteEffect(pages[sceneIndex].backgroundFade, NULL);
359 }
360
361 textRect.x = 0;
362 textRect.y = 0;
363 textRect.w = SW;
364 textRect.h = SH;
365
366 auto scenePalette = std::find_if(palettes.begin(), palettes.end(),
367 [&](const auto& pal) { return pal.id == pages[sceneIndex].paletteIndex; });
368 if (scenePalette != palettes.end()) {
369
370 video.setPalette(scenePalette->palette);
371
372 // setup transition animation
373 switch(pages[sceneIndex].transitionType) {
374 case 0:
375 // Fade in from black
376 if(pages[sceneIndex].backgroundFade > 0) {
377 if (paletteEffect) delete paletteEffect;
378 paletteEffect = new FadeInPaletteEffect(pages[sceneIndex].backgroundFade, NULL);
379 }
380 break;
381
382 case 1:
383 // Speckle
384 LOG_TRACE("Scene: Speckle transition unimplemented.");
385 break;
386
387 case 2:
388 // Circle
389 LOG_TRACE("Scene: Circle transition unimplemented.");
390 break;
391 }
392 }
393
394 if(pages[sceneIndex].musicFile) {
395 playMusic(pages[sceneIndex].musicFile);
396 }
397
398 newpage = false;
399
400 }
401
402 // First draw the backgrounds associated with this page
403 if (pages[sceneIndex].backgrounds > 0) {
404
405 for (int bg = 0; bg < pages[sceneIndex].backgrounds; bg++) {
406
407 const auto sceneImage = std::find_if(images.begin(), images.end(),
408 [&](const auto& img) { return img.id == pages[sceneIndex].bgIndex[bg]; });
409 if (sceneImage != images.end()) {
410
411 dst.x = pages[sceneIndex].bgX[bg] + ((canvasW - SW) >> 1);
412 dst.y = pages[sceneIndex].bgY[bg] + ((canvasH - SH) >> 1);
413 SDL_BlitSurface(sceneImage->image, nullptr, canvas, &dst);
414
415 }
416
417 }
418
419 } else if (pages[sceneIndex].animIndex != -1) {
420
421 if (currentFrame == NULL) {
422
423 sceneAnimation = std::find_if(animations.begin(), animations.end(),
424 [&](const auto& anim) { return anim.id == pages[sceneIndex].animIndex; });
425 if (sceneAnimation != animations.end() && sceneAnimation->background) {
426
427 dst.x = (canvasW - SW) >> 1;
428 dst.y = (canvasH - SH) >> 1;
429 frameDelay = 1000 / (pages[sceneIndex].animSpeed >> 8);
430 SDL_BlitSurface(sceneAnimation->background, NULL, canvas, &dst);
431 SDL_BlitSurface(sceneAnimation->background, NULL, sceneAnimation->scratch, NULL);
432 currentFrame = sceneAnimation->sceneFrames;
433 SDL_Delay(frameDelay);
434
435 }
436
437 } else {
438
439 // Upload pixel data to the surface
440 if (SDL_MUSTLOCK(sceneAnimation->scratch)) SDL_LockSurface(sceneAnimation->scratch);
441
442 switch (currentFrame->frameType) {
443
444 case ECompactedAniHeader:
445
446 loadCompactedMem(currentFrame->frameSize, currentFrame->frameData, static_cast<unsigned char*>(sceneAnimation->scratch->pixels));
447
448 break;
449
450 case EFullFrameAniHeader:
451
452 loadFullFrameMem(currentFrame->frameSize, currentFrame->frameData, static_cast<unsigned char*>(sceneAnimation->scratch->pixels));
453
454 break;
455
456 default:
457
458 LOG_DEBUG("Scene::Play unknown type: %d", currentFrame->frameType);
459
460 break;
461
462 }
463
464 if (SDL_MUSTLOCK(sceneAnimation->scratch)) SDL_UnlockSurface(sceneAnimation->scratch);
465
466 dst.x = (canvasW - SW) >> 1;
467 dst.y = (canvasH - SH) >> 1;
468 SDL_BlitSurface(sceneAnimation->scratch, NULL, canvas, &dst);
469
470 playSound(currentFrame->soundId);
471
472 if (prevFrame) currentFrame = currentFrame->prev;
473 else currentFrame = currentFrame->next;
474
475 SDL_Delay(frameDelay);
476
477 if (currentFrame == NULL && sceneAnimation->reverseAnimation) {
478
479 //prevFrame = 1 - prevFrame;
480
481 /*if(prevFrame) currentFrame = lastFrame->prev;
482 else currentFrame = lastFrame->next;*/
483 currentFrame = NULL;//sceneAnimation->sceneFrames;
484
485 } else if (currentFrame == NULL && !pageTime && !pages[sceneIndex].askForYesNo && pages[sceneIndex].nextPageAfterAnim) {
486
487 continueToNextPage = 1;
488
489 }
490
491 }
492
493 } else video.clearScreen(0);
494
495
496 // Draw the texts associated with this page
497 x = 0;
498 y = 0;
499 int extraLineHeight = 0;
500
501 for (const auto& sceneText : pages[sceneIndex].texts) {
502
503 const auto sceneFont = std::find_if(fonts.begin(), fonts.end(),
504 [&](const auto& fnt) { return fnt.id == sceneText.fontId; });
505 if (sceneFont != fonts.end()) {
506 Font* font = sceneFont->font;
507
508 if (sceneText.x != -1) {
509
510 x = sceneText.x;
511 y = sceneText.y;
512
513 }
514
515 if (sceneText.textRect.x != -1) {
516
517 textRect = sceneText.textRect;
518 x = 0;
519 y = 0;
520
521 }
522
523 if (sceneText.extraLineHeight != -1) {
524
525 extraLineHeight = sceneText.extraLineHeight;
526
527 }
528
529 // Actual drawing is only needed when there is text provided
530 if(sceneText.text) {
531 int xOffset = ((canvasW - SW) >> 1) + textRect.x + x;
532 int yOffset = ((canvasH - SH) >> 1) + textRect.y + y;
533
534 switch (sceneText.alignment) {
535
536 case 0: // left
537
538 break;
539
540 case 1: // right
541
542 xOffset += textRect.w - font->getSceneStringWidth(sceneText.text);
543
544 break;
545
546 case 2: // center
547
548 xOffset += (textRect.w - font->getSceneStringWidth(sceneText.text)) >> 1;
549
550 break;
551
552 }
553
554 // Drop shadow
555 if(sceneText.shadowColour != -1) {
556 font->mapPalette(0, MAX_PALETTE_COLORS, 0, 1);
557 font->showSceneString(sceneText.text, xOffset - 1, yOffset + 2);
558 font->restorePalette();
559 }
560
561 // Text itself
562 if(sceneText.textColour) {
563 font->mapPalette(0, MAX_PALETTE_COLORS, sceneText.textColour, MAX_PALETTE_COLORS);
564 }
565 font->showSceneString(sceneText.text, xOffset, yOffset);
566 if(sceneText.textColour) {
567 font->restorePalette();
568 }
569 }
570
571 y += extraLineHeight + font->getHeight();
572 }
573 }
574
575 }
576
577 video.moviePlayback(false);
578
579 return E_NONE;
580
581 }