view src/common/movie.cpp @ 562:114f58b5b6d0

new glyph rendering skeleton complete.
author Robert McIntyre <rlm@mit.edu>
date Fri, 31 Aug 2012 09:22:52 -0500
parents 3e36553d0cbf
children
line wrap: on
line source
1 #include <cstdio>
2 #include <cctype>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cassert>
6 #include <algorithm>
8 using namespace std;
10 #ifdef HAVE_STRINGS_H
11 # include <strings.h>
12 #endif
14 #if defined(__unix) || defined(__linux) || defined(__sun) || defined(__DJGPP)
15 # include <unistd.h>
16 # include <sys/types.h>
17 # include <sys/stat.h>
18 # include <climits>
19 # define stricmp strcasecmp
20 // FIXME: this is wrong, but we don't want buffer overflow
21 # if defined _MAX_PATH
22 # undef _MAX_PATH
23 //# define _MAX_PATH 128
24 # define _MAX_PATH 260
25 # endif
26 #endif
28 #ifdef WIN32
29 # include <io.h>
30 # ifndef W_OK
31 # define W_OK 2
32 # endif
33 # define ftruncate chsize
34 #endif
36 #include "movie.h"
37 #include "System.h"
38 #include "../gba/GBA.h"
39 #include "../gba/GBAGlobals.h"
40 #include "../gba/RTC.h"
41 #include "../gb/GB.h"
42 #include "../gb/gbGlobals.h"
43 #include "inputGlobal.h"
44 #include "unzip.h"
45 #include "Util.h"
47 #include "vbalua.h"
49 #if (defined(WIN32) && !defined(SDL))
50 # include "../win32/stdafx.h"
51 # include "../win32/MainWnd.h"
52 # include "../win32/VBA.h"
53 # include "../win32/WinMiscUtil.h"
54 #endif
56 extern int emulating; // from system.cpp
57 extern u16 currentButtons[4]; // from SDL.cpp
58 extern u16 lastKeys;
60 SMovie Movie;
61 bool loadingMovie = false;
63 // probably bad idea to have so many global variables, but I hate to recompile almost everything after editing VBA.h
64 bool autoConvertMovieWhenPlaying = false;
66 static u16 initialInputs[4] = { 0, 0, 0, 0 };
68 static bool resetSignaled = false;
69 static bool resetSignaledLast = false;
71 static int prevEmulatorType, prevBorder, prevWinBorder, prevBorderAuto;
73 // little-endian integer pop/push functions:
74 static inline uint32 Pop32(const uint8 * &ptr)
75 {
76 uint32 v = (ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24));
77 ptr += 4;
78 return v;
79 }
81 static inline uint16 Pop16(const uint8 * &ptr) /* const version */
82 {
83 uint16 v = (ptr[0] | (ptr[1] << 8));
84 ptr += 2;
85 return v;
86 }
88 static inline uint16 Pop16(uint8 * &ptr) /* non-const version */
89 {
90 uint16 v = (ptr[0] | (ptr[1] << 8));
91 ptr += 2;
92 return v;
93 }
95 static inline uint8 Pop8(const uint8 * &ptr)
96 {
97 return *(ptr)++;
98 }
100 static inline void Push32(uint32 v, uint8 * &ptr)
101 {
102 ptr[0] = (uint8)(v & 0xff);
103 ptr[1] = (uint8)((v >> 8) & 0xff);
104 ptr[2] = (uint8)((v >> 16) & 0xff);
105 ptr[3] = (uint8)((v >> 24) & 0xff);
106 ptr += 4;
107 }
109 static inline void Push16(uint16 v, uint8 * &ptr)
110 {
111 ptr[0] = (uint8)(v & 0xff);
112 ptr[1] = (uint8)((v >> 8) & 0xff);
113 ptr += 2;
114 }
116 static inline void Push8(uint8 v, uint8 * &ptr)
117 {
118 *ptr++ = v;
119 }
121 // little-endian integer read/write functions:
122 static inline uint16 Read16(const uint8 *ptr)
123 {
124 return ptr[0] | (ptr[1] << 8);
125 }
127 static inline void Write16(uint16 v, uint8 *ptr)
128 {
129 ptr[0] = uint8(v & 0xff);
130 ptr[1] = uint8((v >> 8) & 0xff);
131 }
133 static long file_length(FILE *fp)
134 {
135 long cur_pos = ftell(fp);
136 fseek(fp, 0, SEEK_END);
137 long length = ftell(fp);
138 fseek(fp, cur_pos, SEEK_SET);
139 return length;
140 }
142 static int bytes_per_frame(SMovie &mov)
143 {
144 int num_controllers = 0;
146 for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i)
147 if (mov.header.controllerFlags & MOVIE_CONTROLLER(i))
148 ++num_controllers;
150 return CONTROLLER_DATA_SIZE * num_controllers;
151 }
153 static void reserve_buffer_space(uint32 space_needed)
154 {
155 if (space_needed > Movie.inputBufferSize)
156 {
157 uint32 ptr_offset = Movie.inputBufferPtr - Movie.inputBuffer;
158 uint32 alloc_chunks = (space_needed - 1) / BUFFER_GROWTH_SIZE + 1;
159 uint32 old_size = Movie.inputBufferSize;
160 Movie.inputBufferSize = BUFFER_GROWTH_SIZE * alloc_chunks;
161 Movie.inputBuffer = (uint8 *)realloc(Movie.inputBuffer, Movie.inputBufferSize);
162 // FIXME: this only fixes the random input problem during dma-frame-skip, but not the skip
163 memset(Movie.inputBuffer + old_size, 0, Movie.inputBufferSize - old_size);
164 Movie.inputBufferPtr = Movie.inputBuffer + ptr_offset;
165 }
166 }
168 static int read_movie_header(FILE *file, SMovie &movie)
169 {
170 assert(file != NULL);
171 assert(VBM_HEADER_SIZE == sizeof(SMovieFileHeader)); // sanity check on the header type definition
173 uint8 headerData [VBM_HEADER_SIZE];
175 if (fread(headerData, 1, VBM_HEADER_SIZE, file) != VBM_HEADER_SIZE)
176 return MOVIE_WRONG_FORMAT; // if we failed to read in all VBM_HEADER_SIZE bytes of the header
178 const uint8 * ptr = headerData;
179 SMovieFileHeader &header = movie.header;
181 header.magic = Pop32(ptr);
182 if (header.magic != VBM_MAGIC)
183 return MOVIE_WRONG_FORMAT;
185 header.version = Pop32(ptr);
186 if (header.version != VBM_VERSION)
187 return MOVIE_WRONG_VERSION;
189 header.uid = Pop32(ptr);
190 header.length_frames = Pop32(ptr) + 1; // HACK: add 1 to the length for compatibility
191 header.rerecord_count = Pop32(ptr);
193 header.startFlags = Pop8(ptr);
194 header.controllerFlags = Pop8(ptr);
195 header.typeFlags = Pop8(ptr);
196 header.optionFlags = Pop8(ptr);
198 header.saveType = Pop32(ptr);
199 header.flashSize = Pop32(ptr);
200 header.gbEmulatorType = Pop32(ptr);
202 for (int i = 0; i < 12; i++)
203 header.romTitle[i] = Pop8(ptr);
205 header.minorVersion = Pop8(ptr);
207 header.romCRC = Pop8(ptr);
208 header.romOrBiosChecksum = Pop16(ptr);
209 header.romGameCode = Pop32(ptr);
211 header.offset_to_savestate = Pop32(ptr);
212 header.offset_to_controller_data = Pop32(ptr);
214 return MOVIE_SUCCESS;
215 }
217 static void write_movie_header(FILE *file, const SMovie &movie)
218 {
219 assert(ftell(file) == 0); // we assume file points to beginning of movie file
221 uint8 headerData [VBM_HEADER_SIZE];
222 uint8 *ptr = headerData;
223 const SMovieFileHeader &header = movie.header;
225 Push32(header.magic, ptr);
226 Push32(header.version, ptr);
228 Push32(header.uid, ptr);
229 Push32(header.length_frames - 1, ptr); // HACK: reduce the length by 1 for compatibility with certain faulty old tools
230 // like TME
231 Push32(header.rerecord_count, ptr);
233 Push8(header.startFlags, ptr);
234 Push8(header.controllerFlags, ptr);
235 Push8(header.typeFlags, ptr);
236 Push8(header.optionFlags, ptr);
238 Push32(header.saveType, ptr);
239 Push32(header.flashSize, ptr);
240 Push32(header.gbEmulatorType, ptr);
242 for (int i = 0; i < 12; ++i)
243 Push8(header.romTitle[i], ptr);
245 Push8(header.minorVersion, ptr);
247 Push8(header.romCRC, ptr);
248 Push16(header.romOrBiosChecksum, ptr);
249 Push32(header.romGameCode, ptr);
251 Push32(header.offset_to_savestate, ptr);
252 Push32(header.offset_to_controller_data, ptr);
254 fwrite(headerData, 1, VBM_HEADER_SIZE, file);
255 }
257 static void flush_movie_header()
258 {
259 assert(Movie.file != 0 && "logical error!");
260 if (!Movie.file)
261 return;
263 long originalPos = ftell(Movie.file);
265 // (over-)write the header
266 fseek(Movie.file, 0, SEEK_SET);
267 write_movie_header(Movie.file, Movie);
269 fflush(Movie.file);
271 fseek(Movie.file, originalPos, SEEK_SET);
272 }
274 static void flush_movie_frames()
275 {
276 assert(Movie.file && "logical error!");
277 if (!Movie.file)
278 return;
280 long originalPos = ftell(Movie.file);
282 // overwrite the controller data
283 fseek(Movie.file, Movie.header.offset_to_controller_data, SEEK_SET);
284 fwrite(Movie.inputBuffer, 1, Movie.bytesPerFrame * Movie.header.length_frames, Movie.file);
286 fflush(Movie.file);
288 fseek(Movie.file, originalPos, SEEK_SET);
289 }
291 static void truncate_movie(long length)
292 {
293 // truncate movie to length
294 // NOTE: it's certain that the savestate block is never after the
295 // controller data block, because the VBM format decrees it.
297 assert(Movie.file && length >= 0);
298 if (!Movie.file || length < 0)
299 return;
301 assert(Movie.header.offset_to_savestate <= Movie.header.offset_to_controller_data);
302 if (Movie.header.offset_to_savestate > Movie.header.offset_to_controller_data)
303 return;
305 Movie.header.length_frames = length;
306 flush_movie_header();
307 const long truncLen = long(Movie.header.offset_to_controller_data + Movie.bytesPerFrame * length);
308 if (file_length(Movie.file) != truncLen)
309 {
310 ftruncate(fileno(Movie.file), truncLen);
311 }
312 }
314 static void remember_input_state()
315 {
316 for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i)
317 {
318 if (systemCartridgeType == 0)
319 {
320 initialInputs[i] = u16(~P1 & 0x03FF);
321 }
322 else
323 {
324 extern int32 gbJoymask[4];
325 for (int i = 0; i < 4; ++i)
326 initialInputs[i] = u16(gbJoymask[i] & 0xFFFF);
327 }
328 }
329 }
331 static void change_state(MovieState new_state)
332 {
333 #if (defined(WIN32) && !defined(SDL))
334 theApp.frameSearching = false;
335 theApp.frameSearchSkipping = false;
336 #endif
338 if (new_state == MOVIE_STATE_NONE)
339 {
340 Movie.pauseFrame = -1;
342 if (Movie.state == MOVIE_STATE_NONE)
343 return;
345 truncate_movie(Movie.header.length_frames);
347 fclose(Movie.file);
348 Movie.file = NULL;
349 Movie.currentFrame = 0;
350 #if (defined(WIN32) && !defined(SDL))
351 // undo changes to border settings
352 {
353 gbBorderOn = prevBorder;
354 theApp.winGbBorderOn = prevWinBorder;
355 gbBorderAutomatic = prevBorderAuto;
356 systemGbBorderOn();
357 }
358 #endif
359 gbEmulatorType = prevEmulatorType;
361 extern int32 gbDMASpeedVersion;
362 gbDMASpeedVersion = 1;
364 extern int32 gbEchoRAMFixOn;
365 gbEchoRAMFixOn = 1;
367 gbNullInputHackTempEnabled = gbNullInputHackEnabled;
369 if (Movie.inputBuffer)
370 {
371 free(Movie.inputBuffer);
372 Movie.inputBuffer = NULL;
373 }
375 }
376 else if (new_state == MOVIE_STATE_PLAY)
377 {
378 assert(Movie.file);
380 // this would cause problems if not dealt with
381 if (Movie.currentFrame >= Movie.header.length_frames)
382 {
383 new_state = MOVIE_STATE_END;
384 Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * Movie.header.length_frames;
385 }
386 }
387 else if (new_state == MOVIE_STATE_RECORD)
388 {
389 assert(Movie.file);
391 // this would cause problems if not dealt with
392 if (Movie.currentFrame > Movie.header.length_frames)
393 {
394 new_state = MOVIE_STATE_END;
395 Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * Movie.header.length_frames;
396 }
398 fseek(Movie.file, Movie.header.offset_to_controller_data + Movie.bytesPerFrame * Movie.currentFrame, SEEK_SET);
399 }
401 if (new_state == MOVIE_STATE_END && Movie.state != MOVIE_STATE_END)
402 {
403 #if defined(SDL)
404 systemClearJoypads();
405 #endif
406 systemScreenMessage("Movie end");
407 }
409 Movie.state = new_state;
411 // checking for movie end
412 bool willPause = false;
414 // if the movie's been set to pause at a certain frame
415 if (Movie.state != MOVIE_STATE_NONE && Movie.pauseFrame >= 0 && Movie.currentFrame == (uint32)Movie.pauseFrame)
416 {
417 Movie.pauseFrame = -1;
418 willPause = true;
419 }
421 if (Movie.state == MOVIE_STATE_END)
422 {
423 if (Movie.currentFrame == Movie.header.length_frames)
424 {
425 #if (defined(WIN32) && !defined(SDL))
426 if (theApp.movieOnEndPause)
427 {
428 willPause = true;
429 }
430 #else
431 // SDL FIXME
432 #endif
434 #if (defined(WIN32) && !defined(SDL))
435 switch (theApp.movieOnEndBehavior)
436 {
437 case 1:
438 // the old behavior
439 //VBAMovieRestart();
440 break;
441 case 2:
442 #else
443 // SDL FIXME
444 #endif
445 if (Movie.RecordedThisSession)
446 {
447 // if user has been recording this movie since the last time it started playing,
448 // they probably don't want the movie to end now during playback,
449 // so switch back to recording when it reaches the end
450 VBAMovieSwitchToRecording();
451 systemScreenMessage("Recording resumed");
452 willPause = true;
453 }
454 #if (defined(WIN32) && !defined(SDL))
455 break;
456 case 3:
457 // keep open
458 break;
459 case 0:
460 // fall through
461 default:
462 // close movie
463 //VBAMovieStop(false);
464 break;
465 }
466 #else
467 // SDL FIXME
468 #endif
469 }
470 #if 1
471 else if (Movie.currentFrame > Movie.header.length_frames)
472 {
473 #if (defined(WIN32) && !defined(SDL))
474 switch (theApp.movieOnEndBehavior)
475 {
476 case 1:
477 // FIXME: this should be delayed till the current frame ends
478 VBAMovieRestart();
479 break;
480 case 2:
481 // nothing
482 break;
483 case 3:
484 // keep open
485 break;
486 case 0:
487 // fall through
488 default:
489 // close movie
490 VBAMovieStop(false);
491 break;
492 }
493 #else
494 // SDLFIXME
495 #endif
496 }
497 #endif
498 } // end if (Movie.state == MOVIE_STATE_END)
500 if (willPause)
501 {
502 systemSetPause(true);
503 }
504 }
506 void VBAMovieInit()
507 {
508 memset(&Movie, 0, sizeof(Movie));
509 Movie.state = MOVIE_STATE_NONE;
510 Movie.pauseFrame = -1;
512 resetSignaled = false;
513 resetSignaledLast = false;
515 // RLM: should probably add inputBuffer initialization here.
516 reserve_buffer_space(90001);
517 }
519 void VBAMovieGetRomInfo(const SMovie &movieInfo, char romTitle [12], uint32 &romGameCode, uint16 &checksum, uint8 &crc)
520 {
521 if (systemCartridgeType == 0) // GBA
522 {
523 extern u8 *bios, *rom;
524 memcpy(romTitle, &rom[0xa0], 12); // GBA TITLE
525 memcpy(&romGameCode, &rom[0xac], 4); // GBA ROM GAME CODE
526 if ((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0)
527 checksum = utilCalcBIOSChecksum(bios, 4); // GBA BIOS CHECKSUM
528 else
529 checksum = 0;
530 crc = rom[0xbd]; // GBA ROM CRC
531 }
532 else // non-GBA
533 {
534 extern u8 *gbRom;
535 memcpy(romTitle, &gbRom[0x134], 12); // GB TITLE (note this can be 15 but is truncated to 12)
536 romGameCode = (uint32)gbRom[0x146]; // GB ROM UNIT CODE
538 checksum = (gbRom[0x14e] << 8) | gbRom[0x14f]; // GB ROM CHECKSUM, read from big-endian
539 crc = gbRom[0x14d]; // GB ROM CRC
540 }
541 }
543 #ifdef SDL
544 static void GetBatterySaveName(char *buffer)
545 {
546 extern char batteryDir[2048], filename[2048]; // from SDL.cpp
547 extern char *sdlGetFilename(char *name); // from SDL.cpp
548 if (batteryDir[0])
549 sprintf(buffer, "%s/%s.sav", batteryDir, sdlGetFilename(filename));
550 else
551 sprintf(buffer, "%s.sav", filename);
552 }
554 #endif
556 static void SetPlayEmuSettings()
557 {
558 prevEmulatorType = gbEmulatorType;
559 gbEmulatorType = Movie.header.gbEmulatorType;
561 #if (defined(WIN32) && !defined(SDL))
562 // theApp.removeIntros = false;
563 theApp.skipBiosFile = (Movie.header.optionFlags & MOVIE_SETTING_SKIPBIOSFILE) != 0;
564 theApp.useBiosFile = (Movie.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0;
565 #else
566 extern int saveType, sdlRtcEnable, sdlFlashSize; // from SDL.cpp
567 extern bool8 useBios, skipBios, removeIntros; // from SDL.cpp
568 useBios = (Movie.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0;
569 skipBios = (Movie.header.optionFlags & MOVIE_SETTING_SKIPBIOSFILE) != 0;
570 removeIntros = false /*(Movie.header.optionFlags & MOVIE_SETTING_REMOVEINTROS) != 0*/;
571 #endif
573 extern void SetPrefetchHack(bool);
574 if (systemCartridgeType == 0) // lag disablement applies only to GBA
575 SetPrefetchHack((Movie.header.optionFlags & MOVIE_SETTING_LAGHACK) != 0);
577 gbNullInputHackTempEnabled = ((Movie.header.optionFlags & MOVIE_SETTING_GBINPUTHACK) != 0);
579 // some GB/GBC games depend on the sound rate, so just use the highest one
580 systemSoundSetQuality(1);
581 useOldFrameTiming = false;
583 extern int32 gbDMASpeedVersion;
584 if ((Movie.header.optionFlags & MOVIE_SETTING_GBCFF55FIX) != 0)
585 gbDMASpeedVersion = 1;
586 else
587 gbDMASpeedVersion = 0; // old CGB HDMA5 timing was used
589 extern int32 gbEchoRAMFixOn;
590 if ((Movie.header.optionFlags & MOVIE_SETTING_GBECHORAMFIX) != 0)
591 gbEchoRAMFixOn = 1;
592 else
593 gbEchoRAMFixOn = 0;
595 #if (defined(WIN32) && !defined(SDL))
596 rtcEnable((Movie.header.optionFlags & MOVIE_SETTING_RTCENABLE) != 0);
597 theApp.winSaveType = Movie.header.saveType;
598 theApp.winFlashSize = Movie.header.flashSize;
600 prevBorder = gbBorderOn;
601 prevWinBorder = theApp.winGbBorderOn;
602 prevBorderAuto = gbBorderAutomatic;
603 if ((gbEmulatorType == 2 || gbEmulatorType == 5)
604 && !theApp.hideMovieBorder) // games played in SGB mode can have a border
605 {
606 gbBorderOn = true;
607 theApp.winGbBorderOn = true;
608 gbBorderAutomatic = false;
609 }
610 else
611 {
612 gbBorderOn = false;
613 theApp.winGbBorderOn = false;
614 gbBorderAutomatic = false;
615 if (theApp.hideMovieBorder)
616 {
617 theApp.hideMovieBorder = false;
618 prevBorder = false; // it might be expected behaviour that it stays hidden after the movie
619 }
620 }
621 systemGbBorderOn();
622 #else
623 sdlRtcEnable = (Movie.header.optionFlags & MOVIE_SETTING_RTCENABLE) != 0;
624 saveType = Movie.header.saveType;
625 sdlFlashSize = Movie.header.flashSize;
626 #endif
627 }
629 static void HardResetAndSRAMClear()
630 {
631 #if (defined(WIN32) && !defined(SDL))
632 winEraseBatteryFile(); // delete the damn SRAM file and keep it from being resurrected from RAM
633 MainWnd *temp = ((MainWnd *)theApp.m_pMainWnd);
634 if (!temp->winFileRun(true)) // restart running the game
635 {
636 temp->winFileClose();
637 }
638 #else
639 char fname [1024];
640 GetBatterySaveName(fname);
641 remove(fname); // delete the damn SRAM file
643 // Henceforth, emuCleanUp means "clear out SRAM"
644 //theEmulator.emuCleanUp(); // keep it from being resurrected from RAM <--This is wrong, it'll deallocate all variables --Felipe
646 /// FIXME the correct SDL code to call for a full restart isn't in a function yet
647 theEmulator.emuReset(false);
648 #endif
649 }
651 int VBAMovieOpen(const char *filename, bool8 read_only)
652 {
653 loadingMovie = true;
654 uint8 movieReadOnly = read_only ? 1 : 0;
656 FILE * file;
657 STREAM stream;
658 int result;
659 int fn;
661 char movie_filename[_MAX_PATH];
662 #ifdef WIN32
663 _fullpath(movie_filename, filename, _MAX_PATH);
664 #else
665 // SDL FIXME: convert to fullpath
666 strncpy(movie_filename, filename, _MAX_PATH);
667 movie_filename[_MAX_PATH - 1] = '\0';
668 #endif
670 if (movie_filename[0] == '\0')
671 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
673 if (!emulating)
674 { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; }
676 // bool alreadyOpen = (Movie.file != NULL && _stricmp(movie_filename, Movie.filename) == 0);
678 // if (alreadyOpen)
679 change_state(MOVIE_STATE_NONE); // have to stop current movie before trying to re-open it
681 if (!(file = fopen(movie_filename, "rb+")))
682 if (!(file = fopen(movie_filename, "rb")))
683 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
684 //else
685 // movieReadOnly = 2; // we have to open the movie twice, no need to do this both times
687 // if (!alreadyOpen)
688 // change_state(MOVIE_STATE_NONE); // stop current movie when we're able to open the other one
689 //
690 // if (!(file = fopen(movie_filename, "rb+")))
691 // if(!(file = fopen(movie_filename, "rb")))
692 // {loadingMovie = false; return MOVIE_FILE_NOT_FOUND;}
693 // else
694 // movieReadOnly = 2;
696 // clear out the current movie
697 VBAMovieInit();
699 // read header
700 if ((result = read_movie_header(file, Movie)) != MOVIE_SUCCESS)
701 {
702 fclose(file);
703 { loadingMovie = false; return result; }
704 }
706 // set emulator settings that make the movie more likely to stay synchronized
707 SetPlayEmuSettings();
709 // extern bool systemLoadBIOS();
710 // if (!systemLoadBIOS())
711 // { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; }
713 // read the metadata / author info from file
714 fread(Movie.authorInfo, 1, MOVIE_METADATA_SIZE, file);
715 fn = dup(fileno(file)); // XXX: why does this fail?? it returns -1 but errno == 0
716 fclose(file);
718 // apparently this lseek is necessary
719 lseek(fn, Movie.header.offset_to_savestate, SEEK_SET);
720 if (!(stream = utilGzReopen(fn, "rb")))
721 if (!(stream = utilGzOpen(movie_filename, "rb")))
722 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
723 else
724 fn = dup(fileno(file));
725 // in case the above dup failed but opening the file normally doesn't fail
727 if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT)
728 {
729 // load the snapshot
730 result = theEmulator.emuReadStateFromStream(stream) ? MOVIE_SUCCESS : MOVIE_WRONG_FORMAT;
732 // FIXME: Kludge for conversion
733 remember_input_state();
734 }
735 else if (Movie.header.startFlags & MOVIE_START_FROM_SRAM)
736 {
737 // 'soft' reset:
738 theEmulator.emuReset(false);
740 // load the SRAM
741 result = theEmulator.emuReadBatteryFromStream(stream) ? MOVIE_SUCCESS : MOVIE_WRONG_FORMAT;
742 }
743 else
744 {
745 HardResetAndSRAMClear();
746 }
748 utilGzClose(stream);
750 if (result != MOVIE_SUCCESS)
751 { loadingMovie = false; return result; }
753 // if (!(file = fopen(movie_filename, /*read_only ? "rb" :*/ "rb+"))) // want to be able to switch out of read-only later
754 // {
755 // if(!Movie.readOnly || !(file = fopen(movie_filename, "rb"))) // try read-only if failed
756 // return MOVIE_FILE_NOT_FOUND;
757 // }
758 if (!(file = fopen(movie_filename, "rb+")))
759 if (!(file = fopen(movie_filename, "rb")))
760 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
761 else
762 movieReadOnly = 2;
764 // recalculate length of movie from the file size
765 Movie.bytesPerFrame = bytes_per_frame(Movie);
766 fseek(file, 0, SEEK_END);
767 long fileSize = ftell(file);
768 Movie.header.length_frames = (fileSize - Movie.header.offset_to_controller_data) / Movie.bytesPerFrame;
770 if (fseek(file, Movie.header.offset_to_controller_data, SEEK_SET))
771 { fclose(file); loadingMovie = false; return MOVIE_WRONG_FORMAT; }
773 strcpy(Movie.filename, movie_filename);
774 Movie.file = file;
775 Movie.inputBufferPtr = Movie.inputBuffer;
776 Movie.currentFrame = 0;
777 Movie.readOnly = movieReadOnly;
778 Movie.RecordedThisSession = false;
780 // read controller data
781 uint32 to_read = Movie.bytesPerFrame * Movie.header.length_frames;
782 reserve_buffer_space(to_read);
783 fread(Movie.inputBuffer, 1, to_read, file);
785 change_state(MOVIE_STATE_PLAY);
787 char messageString[64] = "Movie ";
788 bool converted = false;
789 if (autoConvertMovieWhenPlaying)
790 {
791 int result = VBAMovieConvertCurrent();
792 if (result == MOVIE_SUCCESS)
793 strcat(messageString, "converted and ");
794 else if (result == MOVIE_WRONG_VERSION)
795 strcat(messageString, "higher revision ");
796 }
798 if (Movie.state == MOVIE_STATE_PLAY)
799 strcat(messageString, "replaying ");
800 else
801 strcat(messageString, "finished ");
802 if (Movie.readOnly)
803 strcat(messageString, "(read)");
804 else
805 strcat(messageString, "(edit)");
806 systemScreenMessage(messageString);
808 VBAUpdateButtonPressDisplay();
809 VBAUpdateFrameCountDisplay();
810 systemRefreshScreen();
812 { loadingMovie = false; return MOVIE_SUCCESS; }
813 }
815 static void SetRecordEmuSettings()
816 {
817 Movie.header.optionFlags = 0;
818 #if (defined(WIN32) && !defined(SDL))
819 if (theApp.useBiosFile)
820 Movie.header.optionFlags |= MOVIE_SETTING_USEBIOSFILE;
821 if (theApp.skipBiosFile)
822 Movie.header.optionFlags |= MOVIE_SETTING_SKIPBIOSFILE;
823 if (rtcIsEnabled())
824 Movie.header.optionFlags |= MOVIE_SETTING_RTCENABLE;
825 Movie.header.saveType = theApp.winSaveType;
826 Movie.header.flashSize = theApp.winFlashSize;
827 #else
828 extern int saveType, sdlRtcEnable, sdlFlashSize; // from SDL.cpp
829 extern bool8 useBios, skipBios; // from SDL.cpp
830 if (useBios)
831 Movie.header.optionFlags |= MOVIE_SETTING_USEBIOSFILE;
832 if (skipBios)
833 Movie.header.optionFlags |= MOVIE_SETTING_SKIPBIOSFILE;
834 if (sdlRtcEnable)
835 Movie.header.optionFlags |= MOVIE_SETTING_RTCENABLE;
836 Movie.header.saveType = saveType;
837 Movie.header.flashSize = sdlFlashSize;
838 #endif
839 prevEmulatorType = Movie.header.gbEmulatorType = gbEmulatorType;
841 if (!memLagTempEnabled)
842 Movie.header.optionFlags |= MOVIE_SETTING_LAGHACK;
844 if (gbNullInputHackTempEnabled)
845 Movie.header.optionFlags |= MOVIE_SETTING_GBINPUTHACK;
847 Movie.header.optionFlags |= MOVIE_SETTING_GBCFF55FIX;
848 extern int32 gbDMASpeedVersion;
849 gbDMASpeedVersion = 1;
851 Movie.header.optionFlags |= MOVIE_SETTING_GBECHORAMFIX;
852 extern int32 gbEchoRAMFixOn;
853 gbEchoRAMFixOn = 1;
855 // some GB/GBC games depend on the sound rate, so just use the highest one
856 systemSoundSetQuality(1);
858 useOldFrameTiming = false;
860 #if (defined(WIN32) && !defined(SDL))
861 // theApp.removeIntros = false;
863 prevBorder = gbBorderOn;
864 prevWinBorder = theApp.winGbBorderOn;
865 prevBorderAuto = gbBorderAutomatic;
866 if (gbEmulatorType == 2 || gbEmulatorType == 5) // only games played in SGB mode will have a border
867 {
868 gbBorderOn = true;
869 theApp.winGbBorderOn = true;
870 gbBorderAutomatic = false;
871 }
872 else
873 {
874 gbBorderOn = false;
875 theApp.winGbBorderOn = false;
876 gbBorderAutomatic = false;
877 }
878 systemGbBorderOn();
879 #else
880 /// SDLFIXME
881 #endif
882 }
884 uint16 VBAMovieGetCurrentInputOf(int controllerNum, bool normalOnly)
885 {
886 if (controllerNum < 0 || controllerNum >= MOVIE_NUM_OF_POSSIBLE_CONTROLLERS)
887 return 0;
889 return normalOnly ? (currentButtons[controllerNum] & BUTTON_REGULAR_MASK) : currentButtons[controllerNum];
890 }
892 int VBAMovieCreate(const char *filename, const char *authorInfo, uint8 startFlags, uint8 controllerFlags, uint8 typeFlags)
893 {
894 // make sure at least one controller is enabled
895 if ((controllerFlags & MOVIE_CONTROLLERS_ANY_MASK) == 0)
896 return MOVIE_WRONG_FORMAT;
898 if (!emulating)
899 return MOVIE_UNKNOWN_ERROR;
901 loadingMovie = true;
903 FILE * file;
904 STREAM stream;
905 int fn;
907 char movie_filename [_MAX_PATH];
908 #ifdef WIN32
909 _fullpath(movie_filename, filename, _MAX_PATH);
910 #else
911 // FIXME: convert to fullpath
912 strncpy(movie_filename, filename, _MAX_PATH);
913 movie_filename[_MAX_PATH - 1] = '\0';
914 #endif
916 bool alreadyOpen = (Movie.file != NULL && stricmp(movie_filename, Movie.filename) == 0);
918 if (alreadyOpen)
919 change_state(MOVIE_STATE_NONE); // have to stop current movie before trying to re-open it
921 if (movie_filename[0] == '\0')
922 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
924 if (!(file = fopen(movie_filename, "wb")))
925 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
927 if (!alreadyOpen)
928 change_state(MOVIE_STATE_NONE); // stop current movie when we're able to open the other one
930 // clear out the current movie
931 //printf("RLM: movie init\n");
933 VBAMovieInit();
935 // fill in the movie's header
936 Movie.header.uid = (uint32)time(NULL);
937 Movie.header.magic = VBM_MAGIC;
938 Movie.header.version = VBM_VERSION;
939 Movie.header.rerecord_count = 0;
940 Movie.header.length_frames = 0;
941 Movie.header.startFlags = startFlags;
942 Movie.header.controllerFlags = controllerFlags;
943 Movie.header.typeFlags = typeFlags;
944 Movie.header.minorVersion = VBM_REVISION;
946 // set emulator settings that make the movie more likely to stay synchronized when it's later played back
947 SetRecordEmuSettings();
949 // set ROM and BIOS checksums and stuff
950 VBAMovieGetRomInfo(Movie, Movie.header.romTitle, Movie.header.romGameCode, Movie.header.romOrBiosChecksum, Movie.header.romCRC);
952 //printf("RLM: Writing movie header\n");
953 // write the header to file
954 write_movie_header(file, Movie);
956 //printf("RLM: setting metadata\n");
958 // copy over the metadata / author info
959 VBAMovieSetMetadata("________________Robert McIntyre______________________________________________________________________________________________________________________________________________________________________________________________________________________");
961 //printf("RLM: writing metadata\n");
963 // write the metadata / author info to file
966 fwrite(Movie.authorInfo, 1, sizeof(char) * MOVIE_METADATA_SIZE, file);
968 // write snapshot or SRAM if applicable
969 if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT
970 || Movie.header.startFlags & MOVIE_START_FROM_SRAM)
971 {
972 Movie.header.offset_to_savestate = (uint32)ftell(file);
974 // close the file and reopen it as a stream:
976 fn = dup(fileno(file));
977 fclose(file);
979 if (!(stream = utilGzReopen(fn, "ab"))) // append mode to start at end, no seek necessary
980 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
982 // write the save data:
983 if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT)
984 {
985 // save snapshot
986 if (!theEmulator.emuWriteStateToStream(stream))
987 {
988 utilGzClose(stream);
989 { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; }
990 }
991 }
992 else if (Movie.header.startFlags & MOVIE_START_FROM_SRAM)
993 {
994 // save SRAM
995 if (!theEmulator.emuWriteBatteryToStream(stream))
996 {
997 utilGzClose(stream);
998 { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; }
999 }
1001 // 'soft' reset:
1002 theEmulator.emuReset(false);
1005 utilGzClose(stream);
1007 // reopen the file and seek back to the end
1009 if (!(file = fopen(movie_filename, "rb+")))
1010 { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; }
1012 fseek(file, 0, SEEK_END);
1014 else // no snapshot or SRAM
1016 HardResetAndSRAMClear();
1019 Movie.header.offset_to_controller_data = (uint32)ftell(file);
1021 strcpy(Movie.filename, movie_filename);
1022 Movie.file = file;
1023 Movie.bytesPerFrame = bytes_per_frame(Movie);
1024 Movie.inputBufferPtr = Movie.inputBuffer;
1025 Movie.currentFrame = 0;
1026 Movie.readOnly = false;
1027 Movie.RecordedThisSession = true;
1029 change_state(MOVIE_STATE_RECORD);
1031 systemScreenMessage("Recording movie...");
1032 { loadingMovie = false; return MOVIE_SUCCESS; }
1035 void VBAUpdateButtonPressDisplay()
1037 uint32 keys = currentButtons[0] & BUTTON_REGULAR_RECORDING_MASK;
1039 const static char KeyMap[] = { 'A', 'B', 's', 'S', '>', '<', '^', 'v', 'R', 'L', '!', '?', '{', '}', 'v', '^' };
1040 const static int KeyOrder[] = { 5, 6, 4, 7, 0, 1, 9, 8, 3, 2, 12, 15, 13, 14, 11, 10 }; // < ^ > v A B L R S s { = } _
1041 // ? !
1042 char buffer[256];
1043 sprintf(buffer, " ");
1045 #ifndef WIN32
1046 // don't bother color-coding autofire and such
1047 int i;
1048 for (i = 0; i < 15; i++)
1050 int j = KeyOrder[i];
1051 int mask = (1 << (j));
1052 buffer[strlen(" ") + i] = ((keys & mask) != 0) ? KeyMap[j] : ' ';
1055 systemScreenMessage(buffer, 2, -1);
1056 #else
1057 const bool eraseAll = !theApp.inputDisplay;
1058 uint32 autoHeldKeys = eraseAll ? 0 : theApp.autoHold & BUTTON_REGULAR_RECORDING_MASK;
1059 uint32 autoFireKeys = eraseAll ? 0 : (theApp.autoFire | theApp.autoFire2) & BUTTON_REGULAR_RECORDING_MASK;
1060 uint32 pressedKeys = eraseAll ? 0 : keys;
1062 char colorList[64];
1063 memset(colorList, 1, strlen(buffer));
1065 if (!eraseAll)
1067 for (int i = 0; i < 15; i++)
1069 const int j = KeyOrder[i];
1070 const int mask = (1 << (j));
1071 bool pressed = (pressedKeys & mask) != 0;
1072 const bool autoHeld = (autoHeldKeys & mask) != 0;
1073 const bool autoFired = (autoFireKeys & mask) != 0;
1074 const bool erased = (lastKeys & mask) != 0 && (!pressed && !autoHeld && !autoFired);
1075 extern int textMethod;
1076 if (textMethod != 2 && (autoHeld || (autoFired && !pressed) || erased))
1078 int colorNum = 1; // default is white
1079 if (autoHeld)
1080 colorNum += (pressed ? 2 : 1); // yellow if pressed, red if not
1081 else if (autoFired)
1082 colorNum += 5; // blue if autofired and not currently pressed
1083 else if (erased)
1084 colorNum += 8; // black on black
1086 colorList[strlen(" ") + i] = colorNum;
1087 pressed = true;
1089 buffer[strlen(" ") + i] = pressed ? KeyMap[j] : ' ';
1093 lastKeys = currentButtons[0];
1094 lastKeys |= theApp.autoHold & BUTTON_REGULAR_RECORDING_MASK;
1095 lastKeys |= (theApp.autoFire | theApp.autoFire2) & BUTTON_REGULAR_RECORDING_MASK;
1097 systemScreenMessage(buffer, 2, -1, colorList);
1098 #endif
1101 void VBAUpdateFrameCountDisplay()
1103 const int MAGICAL_NUMBER = 64; // FIXME: this won't do any better, but only to remind you of sz issues
1104 char frameDisplayString[MAGICAL_NUMBER];
1105 char lagFrameDisplayString[MAGICAL_NUMBER];
1106 char extraCountDisplayString[MAGICAL_NUMBER];
1108 #if (defined(WIN32) && !defined(SDL))
1109 if (theApp.frameCounter)
1110 #else
1111 /// SDL FIXME
1112 #endif
1114 switch (Movie.state)
1116 case MOVIE_STATE_PLAY:
1117 case MOVIE_STATE_END:
1119 sprintf(frameDisplayString, "%d / %d", Movie.currentFrame, Movie.header.length_frames);
1120 if (!Movie.readOnly)
1121 strcat(frameDisplayString, " (edit)");
1122 break;
1124 case MOVIE_STATE_RECORD:
1126 sprintf(frameDisplayString, "%d (record)", Movie.currentFrame);
1127 break;
1129 default:
1131 sprintf(frameDisplayString, "%d (no movie)", systemCounters.frameCount);
1132 break;
1136 #if (defined(WIN32) && !defined(SDL))
1137 if (theApp.lagCounter)
1138 #else
1139 /// SDL FIXME
1140 #endif
1142 // sprintf(lagFrameDisplayString, " %c %d", systemCounters.laggedLast ? '*' : '|', systemCounters.lagCount);
1143 sprintf(lagFrameDisplayString, " | %d%s", systemCounters.lagCount, systemCounters.laggedLast ? " *" : "");
1144 strcat(frameDisplayString, lagFrameDisplayString);
1147 #if (defined(WIN32) && !defined(SDL))
1148 if (theApp.extraCounter)
1149 #else
1150 /// SDL FIXME
1151 #endif
1153 sprintf(extraCountDisplayString, " | %d", systemCounters.frameCount - systemCounters.extraCount);
1154 strcat(frameDisplayString, extraCountDisplayString);
1157 #if (defined(WIN32) && !defined(SDL))
1158 else
1160 frameDisplayString[0] = '\0';
1162 #else
1163 /// SDL FIXME
1164 #endif
1165 systemScreenMessage(frameDisplayString, 1, -1);
1168 // this function should only be called once every frame
1169 void VBAMovieUpdateState()
1171 ++Movie.currentFrame;
1172 //printf("RLM: inside updateState\n");
1173 if (Movie.state == MOVIE_STATE_PLAY)
1175 Movie.inputBufferPtr += Movie.bytesPerFrame;
1176 if (Movie.currentFrame >= Movie.header.length_frames)
1178 // the movie ends anyway; what to do next depends on the settings
1179 change_state(MOVIE_STATE_END);
1182 else if (Movie.state == MOVIE_STATE_RECORD)
1184 //printf("RLM: Movie_STATE_RECORD\n");
1185 VBAMovieWrite(0,true);
1186 // use first fseek?
1187 //TODO: THis is the problem.
1188 if (Movie.inputBuffer){
1189 fwrite(Movie.inputBufferPtr, 1, Movie.bytesPerFrame, Movie.file);
1191 //printf("RLM: write successful.\n");
1192 Movie.header.length_frames = Movie.currentFrame;
1193 Movie.inputBufferPtr += Movie.bytesPerFrame;
1194 Movie.RecordedThisSession = true;
1195 flush_movie_header();
1197 else if (Movie.state == MOVIE_STATE_END)
1199 change_state(MOVIE_STATE_END);
1203 void VBAMovieRead(int i, bool /*sensor*/)
1205 if (Movie.state != MOVIE_STATE_PLAY)
1206 return;
1208 if (i < 0 || i >= MOVIE_NUM_OF_POSSIBLE_CONTROLLERS)
1209 return; // not a controller we're recognizing
1211 if (Movie.header.controllerFlags & MOVIE_CONTROLLER(i))
1213 currentButtons[i] = Read16(Movie.inputBufferPtr + CONTROLLER_DATA_SIZE * i);
1215 else
1217 currentButtons[i] = 0; // pretend the controller is disconnected
1220 printf("RLM: button %d\n",currentButtons[i]);
1221 if ((currentButtons[i] & BUTTON_MASK_NEW_RESET) != 0){
1222 printf("RLM: reset signaled\n");
1223 resetSignaled = true;
1227 void VBAMovieWrite(int i, bool /*sensor*/)
1229 if (Movie.state != MOVIE_STATE_RECORD)
1230 return;
1232 if (i < 0 || i >= MOVIE_NUM_OF_POSSIBLE_CONTROLLERS)
1233 return; // not a controller we're recognizing
1235 reserve_buffer_space((uint32)((Movie.inputBufferPtr - Movie.inputBuffer) + Movie.bytesPerFrame));
1237 if (Movie.header.controllerFlags)
1239 // get the current controller data
1240 uint16 buttonData = currentButtons[i];
1241 //printf("RLM: currentButtons %i\n", currentButtons[0]);
1243 // mask away the irrelevent bits
1244 buttonData &= BUTTON_REGULAR_MASK | BUTTON_MOTION_MASK;
1246 // soft-reset "button" for 1 frame if the game is reset while recording
1247 if (resetSignaled)
1249 buttonData |= BUTTON_MASK_NEW_RESET;
1252 // backward compatibility kludge
1253 if (resetSignaledLast)
1255 buttonData |= BUTTON_MASK_OLD_RESET;
1258 Write16(buttonData, Movie.inputBufferPtr + CONTROLLER_DATA_SIZE * i);
1260 // and for display
1261 currentButtons[i] = buttonData;
1263 else
1265 // pretend the controller is disconnected (otherwise input it gives could cause desync since we're not writing it to the
1266 // movie)
1267 currentButtons[i] = 0;
1271 void VBAMovieStop(bool8 suppress_message)
1273 if (Movie.state != MOVIE_STATE_NONE)
1275 change_state(MOVIE_STATE_NONE);
1276 if (!suppress_message)
1277 systemScreenMessage("Movie stop");
1281 int VBAMovieGetInfo(const char *filename, SMovie *info)
1283 assert(info != NULL);
1284 if (info == NULL)
1285 return -1;
1287 FILE * file;
1288 int result;
1289 SMovie &local_movie = *info;
1291 memset(info, 0, sizeof(*info));
1292 if (filename[0] == '\0')
1293 return MOVIE_FILE_NOT_FOUND;
1294 if (!(file = fopen(filename, "rb")))
1295 return MOVIE_FILE_NOT_FOUND;
1297 // read header
1298 if ((result = (read_movie_header(file, local_movie))) != MOVIE_SUCCESS)
1300 fclose(file);
1301 return result;
1304 // read the metadata / author info from file
1305 fread(local_movie.authorInfo, 1, sizeof(char) * MOVIE_METADATA_SIZE, file);
1307 strncpy(local_movie.filename, filename, _MAX_PATH);
1308 local_movie.filename[_MAX_PATH - 1] = '\0';
1310 if (Movie.file != NULL && stricmp(local_movie.filename, Movie.filename) == 0) // alreadyOpen
1312 local_movie.bytesPerFrame = Movie.bytesPerFrame;
1313 local_movie.header.length_frames = Movie.header.length_frames;
1315 else
1317 // recalculate length of movie from the file size
1318 local_movie.bytesPerFrame = bytes_per_frame(local_movie);
1319 fseek(file, 0, SEEK_END);
1320 int fileSize = ftell(file);
1321 local_movie.header.length_frames =
1322 (fileSize - local_movie.header.offset_to_controller_data) / local_movie.bytesPerFrame;
1325 fclose(file);
1327 if (access(filename, W_OK))
1328 info->readOnly = true;
1330 return MOVIE_SUCCESS;
1333 bool8 VBAMovieActive()
1335 return (Movie.state != MOVIE_STATE_NONE);
1338 bool8 VBAMovieLoading()
1340 return loadingMovie;
1343 bool8 VBAMoviePlaying()
1345 return (Movie.state == MOVIE_STATE_PLAY);
1348 bool8 VBAMovieRecording()
1350 return (Movie.state == MOVIE_STATE_RECORD);
1353 bool8 VBAMovieReadOnly()
1355 if (!VBAMovieActive())
1356 return false;
1358 return Movie.readOnly;
1361 void VBAMovieToggleReadOnly()
1363 if (!VBAMovieActive())
1364 return;
1366 if (Movie.readOnly != 2)
1368 Movie.readOnly = !Movie.readOnly;
1370 systemScreenMessage(Movie.readOnly ? "Movie now read-only" : "Movie now editable");
1372 else
1374 systemScreenMessage("Can't toggle read-only movie");
1378 uint32 VBAMovieGetVersion()
1380 if (!VBAMovieActive())
1381 return 0;
1383 return Movie.header.version;
1386 uint32 VBAMovieGetMinorVersion()
1388 if (!VBAMovieActive())
1389 return 0;
1391 return Movie.header.minorVersion;
1394 uint32 VBAMovieGetId()
1396 if (!VBAMovieActive())
1397 return 0;
1399 return Movie.header.uid;
1402 uint32 VBAMovieGetLength()
1404 if (!VBAMovieActive())
1405 return 0;
1407 return Movie.header.length_frames;
1410 uint32 VBAMovieGetFrameCounter()
1412 if (!VBAMovieActive())
1413 return 0;
1415 return Movie.currentFrame;
1418 uint32 VBAMovieGetRerecordCount()
1420 if (!VBAMovieActive())
1421 return 0;
1423 return Movie.header.rerecord_count;
1426 uint32 VBAMovieSetRerecordCount(uint32 newRerecordCount)
1428 uint32 oldRerecordCount = 0;
1429 if (!VBAMovieActive())
1430 return 0;
1432 oldRerecordCount = Movie.header.rerecord_count;
1433 Movie.header.rerecord_count = newRerecordCount;
1434 return oldRerecordCount;
1437 std::string VBAMovieGetAuthorInfo()
1439 if (!VBAMovieActive())
1440 return "";
1442 return Movie.authorInfo;
1445 std::string VBAMovieGetFilename()
1447 if (!VBAMovieActive())
1448 return "";
1450 return Movie.filename;
1453 void VBAMovieFreeze(uint8 * *buf, uint32 *size)
1455 // sanity check
1456 if (!VBAMovieActive())
1458 return;
1461 *buf = NULL;
1462 *size = 0;
1464 // compute size needed for the buffer
1465 // room for header.uid, currentFrame, and header.length_frames
1466 uint32 size_needed = sizeof(Movie.header.uid) + sizeof(Movie.currentFrame) + sizeof(Movie.header.length_frames);
1467 size_needed += (uint32)(Movie.bytesPerFrame * Movie.header.length_frames);
1468 *buf = new uint8[size_needed];
1469 *size = size_needed;
1471 uint8 *ptr = *buf;
1472 if (!ptr)
1474 return;
1477 Push32(Movie.header.uid, ptr);
1478 Push32(Movie.currentFrame, ptr);
1479 Push32(Movie.header.length_frames - 1, ptr); // HACK: shorten the length by 1 for backward compatibility
1481 memcpy(ptr, Movie.inputBuffer, Movie.bytesPerFrame * Movie.header.length_frames);
1484 int VBAMovieUnfreeze(const uint8 *buf, uint32 size)
1486 // sanity check
1487 if (!VBAMovieActive())
1489 return MOVIE_NOT_FROM_A_MOVIE;
1492 const uint8 *ptr = buf;
1493 if (size < sizeof(Movie.header.uid) + sizeof(Movie.currentFrame) + sizeof(Movie.header.length_frames))
1495 return MOVIE_WRONG_FORMAT;
1498 uint32 movie_id = Pop32(ptr);
1499 uint32 current_frame = Pop32(ptr);
1500 uint32 end_frame = Pop32(ptr) + 1; // HACK: restore the length for backward compatibility
1501 uint32 space_needed = Movie.bytesPerFrame * end_frame;
1503 if (movie_id != Movie.header.uid)
1504 return MOVIE_NOT_FROM_THIS_MOVIE;
1506 if (space_needed > size)
1507 return MOVIE_WRONG_FORMAT;
1509 if (Movie.readOnly)
1511 // here, we are going to keep the input data from the movie file
1512 // and simply rewind to the currentFrame pointer
1513 // this will cause a desync if the savestate is not in sync // <-- NOT ANYMORE
1514 // with the on-disk recording data, but it's easily solved
1515 // by loading another savestate or playing the movie from the beginning
1517 // don't allow loading a state inconsistent with the current movie
1518 uint32 length_history = min(current_frame, Movie.header.length_frames);
1519 if (end_frame < length_history)
1520 return MOVIE_SNAPSHOT_INCONSISTENT;
1522 uint32 space_shared = Movie.bytesPerFrame * length_history;
1523 if (memcmp(Movie.inputBuffer, ptr, space_shared))
1524 return MOVIE_SNAPSHOT_INCONSISTENT;
1526 Movie.currentFrame = current_frame;
1527 Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * min(current_frame, Movie.header.length_frames);
1529 else
1531 // here, we are going to take the input data from the savestate
1532 // and make it the input data for the current movie, then continue
1533 // writing new input data at the currentFrame pointer
1534 Movie.currentFrame = current_frame;
1535 Movie.header.length_frames = end_frame;
1536 if (!VBALuaRerecordCountSkip())
1537 ++Movie.header.rerecord_count;
1539 Movie.RecordedThisSession = true;
1541 // do this before calling reserve_buffer_space()
1542 Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * min(current_frame, Movie.header.length_frames);
1543 reserve_buffer_space(space_needed);
1544 memcpy(Movie.inputBuffer, ptr, space_needed);
1546 // for consistency, no auto movie conversion here since we don't auto convert the corresponding savestate
1547 flush_movie_header();
1548 flush_movie_frames();
1551 change_state(MOVIE_STATE_PLAY); // check for movie end
1553 // necessary!
1554 resetSignaled = false;
1555 resetSignaledLast = false;
1557 // necessary to check if there's a reset signal at the previous frame
1558 if (current_frame > 0)
1560 const u8 NEW_RESET = u8(BUTTON_MASK_NEW_RESET >> 8);
1561 for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i)
1563 if ((Movie.header.controllerFlags & MOVIE_CONTROLLER(i)) && (*(Movie.inputBufferPtr+1- Movie.bytesPerFrame) & NEW_RESET))
1565 resetSignaledLast = true;
1566 break;
1571 return MOVIE_SUCCESS;
1574 bool VBAMovieEnded()
1576 return (Movie.state == MOVIE_STATE_END);
1577 // return (Movie.state != MOVIE_STATE_NONE && Movie.currentFrame >= Movie.header.length_frames);
1580 bool VBAMovieAllowsRerecording()
1582 bool allows = (Movie.state != MOVIE_STATE_NONE) && (Movie.currentFrame <= Movie.header.length_frames);
1583 return /*!VBAMovieReadOnly() &&*/ allows;
1586 bool VBAMovieSwitchToPlaying()
1588 if (!VBAMovieActive())
1589 return false;
1591 if (!Movie.readOnly)
1593 VBAMovieToggleReadOnly();
1596 change_state(MOVIE_STATE_PLAY);
1597 if (Movie.state == MOVIE_STATE_PLAY)
1598 systemScreenMessage("Movie replay (continue)");
1599 else
1600 systemScreenMessage("Movie end");
1602 return true;
1605 bool VBAMovieSwitchToRecording()
1607 if (!VBAMovieAllowsRerecording())
1608 return false;
1610 if (Movie.readOnly)
1612 VBAMovieToggleReadOnly();
1615 if (!VBALuaRerecordCountSkip())
1616 ++Movie.header.rerecord_count;
1618 change_state(MOVIE_STATE_RECORD);
1619 systemScreenMessage("Movie re-record");
1621 //truncate_movie(Movie.currentFrame);
1623 return true;
1626 uint32 VBAMovieGetState()
1628 // ?
1629 if (!VBAMovieActive())
1630 return MOVIE_STATE_NONE;
1632 return Movie.state;
1635 void VBAMovieSignalReset()
1637 if (VBAMovieActive())
1638 resetSignaled = true;
1641 void VBAMovieResetIfRequested()
1643 if (resetSignaled)
1645 theEmulator.emuReset(false);
1646 resetSignaled = false;
1647 resetSignaledLast = true;
1649 else
1651 resetSignaledLast = false;
1655 void VBAMovieSetMetadata(const char *info)
1657 if (!memcmp(Movie.authorInfo, info, MOVIE_METADATA_SIZE))
1658 return;
1660 memcpy(Movie.authorInfo, info, MOVIE_METADATA_SIZE); // strncpy would omit post-0 bytes
1661 Movie.authorInfo[MOVIE_METADATA_SIZE - 1] = '\0';
1663 if (Movie.file)
1665 // (over-)write the header
1666 fseek(Movie.file, 0, SEEK_SET);
1668 write_movie_header(Movie.file, Movie);
1670 // write the metadata / author info to file
1671 fwrite(Movie.authorInfo, 1, sizeof(char) * MOVIE_METADATA_SIZE, Movie.file);
1673 fflush(Movie.file);
1675 //printf("RLM: setMetadata called\n");
1679 void VBAMovieRestart()
1681 if (VBAMovieActive())
1683 systemSoundClearBuffer();
1685 bool8 modified = Movie.RecordedThisSession;
1687 VBAMovieStop(true);
1689 char movieName [_MAX_PATH];
1690 strncpy(movieName, Movie.filename, _MAX_PATH);
1691 movieName[_MAX_PATH - 1] = '\0';
1692 VBAMovieOpen(movieName, Movie.readOnly); // can't just pass in Movie.filename, since VBAMovieOpen clears out Movie's
1693 // variables
1695 Movie.RecordedThisSession = modified;
1697 systemScreenMessage("Movie replay (restart)");
1701 int VBAMovieGetPauseAt()
1703 return Movie.pauseFrame;
1706 void VBAMovieSetPauseAt(int at)
1708 Movie.pauseFrame = at;
1711 ///////////////////////
1712 // movie tools
1714 // FIXME: is it safe to convert/flush a movie while recording it (considering fseek() problem)?
1715 int VBAMovieConvertCurrent()
1717 if (!VBAMovieActive())
1719 return MOVIE_NOTHING;
1722 if (Movie.header.minorVersion > VBM_REVISION)
1724 return MOVIE_WRONG_VERSION;
1727 if (Movie.header.minorVersion == VBM_REVISION)
1729 return MOVIE_NOTHING;
1732 Movie.header.minorVersion = VBM_REVISION;
1734 if (Movie.header.length_frames == 0) // this could happen
1736 truncate_movie(0);
1737 return MOVIE_SUCCESS;
1740 // fix movies recorded from snapshots
1741 if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT)
1743 uint8 *firstFramePtr = Movie.inputBuffer;
1744 for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i)
1746 if (Movie.header.controllerFlags & MOVIE_CONTROLLER(i))
1748 Push16(initialInputs[i], firstFramePtr);
1749 // note: this is correct since Push16 advances the dest pointer by sizeof u16
1754 // convert old resets to new ones
1755 const u8 OLD_RESET = u8(BUTTON_MASK_OLD_RESET >> 8);
1756 const u8 NEW_RESET = u8(BUTTON_MASK_NEW_RESET >> 8);
1757 for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i)
1759 if (Movie.header.controllerFlags & MOVIE_CONTROLLER(i))
1761 uint8 *startPtr = Movie.inputBuffer + sizeof(u16) * i + 1;
1762 uint8 *endPtr = Movie.inputBuffer + Movie.bytesPerFrame * (Movie.header.length_frames - 1);
1763 for (; startPtr < endPtr; startPtr += Movie.bytesPerFrame)
1765 if (startPtr[Movie.bytesPerFrame] & OLD_RESET)
1767 startPtr[0] |= NEW_RESET;
1773 flush_movie_header();
1774 flush_movie_frames();
1775 return MOVIE_SUCCESS;
1778 bool VBAMovieTuncateAtCurrentFrame()
1780 if (!VBAMovieActive())
1781 return false;
1783 truncate_movie(Movie.currentFrame);
1784 change_state(MOVIE_STATE_END);
1785 systemScreenMessage("Movie truncated");
1787 return true;
1790 bool VBAMovieFixHeader()
1792 if (!VBAMovieActive())
1793 return false;
1795 flush_movie_header();
1796 systemScreenMessage("Movie header fixed");
1797 return true;