📄 movie.cpp
字号:
/******************************************************************************* Snes9x - Portable Super Nintendo Entertainment System (TM) emulator. (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and Jerremy Koot (jkoot@snes9x.com) (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net) (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net), funkyass (funkyass@spam.shaw.ca), Joel Yliluoma (http://iki.fi/bisqwit/) Kris Bleakley (codeviolation@hotmail.com), Matthew Kendora, Nach (n-a-c-h@users.sourceforge.net), Peter Bortas (peter@bortas.org) and zones (kasumitokoduck@yahoo.com) C4 x86 assembler and some C emulation code (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com), _Demo_ (_demo_@zsnes.com), and Nach C4 C++ code (c) Copyright 2003 Brad Jorsch DSP-1 emulator code (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson, John Weidman, neviksti (neviksti@hotmail.com), Kris Bleakley, Andreas Naive DSP-2 emulator code (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and Lord Nightmare (lord_nightmare@users.sourceforge.net OBC1 emulator code (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and Kris Bleakley Ported from x86 assembler to C by sanmaiwashi SPC7110 and RTC C++ emulator code (c) Copyright 2002 Matthew Kendora with research by zsKnight, John Weidman, and Dark Force S-DD1 C emulator code (c) Copyright 2003 Brad Jorsch with research by Andreas Naive and John Weidman S-RTC C emulator code (c) Copyright 2001 John Weidman ST010 C++ emulator code (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora Super FX x86 assembler emulator code (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault Super FX C emulator code (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman SH assembler code partly based on x86 assembler code (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) Input recording/playback code (c) Copyright 2004 blip Specific ports contains the works of other authors. See headers in individual files. Snes9x homepage: http://www.snes9x.com Permission to use, copy, modify and distribute Snes9x in both binary and source form, for non-commercial purposes, is hereby granted without fee, providing that this license information and copyright notice appear with all copies and any derived work. This software is provided 'as-is', without any express or implied warranty. In no event shall the authors be held liable for any damages arising from the use of this software. Snes9x is freeware for PERSONAL USE only. Commercial users should seek permission of the copyright holders first. Commercial use includes charging money for Snes9x or software derived from Snes9x. The copyright holders request that bug fixes and improvements to the code should be forwarded to them so everyone can benefit from the modifications in future versions. Super NES and Super Nintendo Entertainment System are trademarks of Nintendo Co., Limited and its subsidiary companies.*******************************************************************************/#include <string.h>#ifdef HAVE_STRINGS_H#include <strings.h>#endif#include <ctype.h>#include <stdlib.h>#if defined(__unix) || defined(__linux) || defined(__sun) || defined(__DJGPP) || defined(__MACOSX__)#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#endif#include <time.h>#ifdef __WIN32__#include <io.h>#ifndef W_OK#define W_OK 2#endif#endif#include "movie.h"#include "snes9x.h"#include "cpuexec.h"#include "snapshot.h"#define SMV_MAGIC 0x1a564d53 // SMV0x1a#define SMV_VERSION 1#define SMV_HEADER_SIZE 32#define CONTROLLER_DATA_SIZE 2#define BUFFER_GROWTH_SIZE 4096enum MovieState{ MOVIE_STATE_NONE=0, MOVIE_STATE_PLAY, MOVIE_STATE_RECORD};static struct SMovie{ enum MovieState State; char Filename [_MAX_PATH]; FILE* File; uint32 SaveStateOffset; uint32 ControllerDataOffset; uint32 MovieId; uint32 CurrentFrame; uint32 MaxFrame; uint32 RerecordCount; uint8 ControllersMask; uint8 Opts; bool8 ReadOnly; uint32 BytesPerFrame; uint8* InputBuffer; uint32 InputBufferSize; uint8* InputBufferPtr; bool8 FrameDisplay; char FrameDisplayString[256];} Movie;/* For illustration:struct MovieFileHeader{ uint32 magic; // SMV0x1a uint32 version; uint32 uid; // used to match savestates to a particular movie uint32 rerecord_count; uint32 length_frames; uint8 flags[4]; uint32 offset_to_savestate; // smvs have an embedded savestate uint32 offset_to_controller_data; // after the header comes extra metadata // sizeof(metadata) = offset_to_savestate - sizeof(MovieFileHeader)};*/static int bytes_per_frame(){ int i; int num_controllers; num_controllers=0; for(i=0; i<5; ++i) { if(Movie.ControllersMask & (1<<i)) { ++num_controllers; } } return CONTROLLER_DATA_SIZE*num_controllers;}static inline uint32 Read32(const uint8*& ptr){ uint32 v=(ptr[0] | (ptr[1]<<8) | (ptr[2]<<16) | (ptr[3]<<24)); ptr += 4; return v;}static inline uint16 Read16(const uint8*& ptr) /* const version */{ uint16 v=(ptr[0] | (ptr[1]<<8)); ptr += 2; return v;}static inline uint16 Read16(uint8*& ptr) /* non-const version */{ uint16 v=(ptr[0] | (ptr[1]<<8)); ptr += 2; return v;}static void Write32(uint32 v, uint8*& ptr){ ptr[0]=(uint8)(v&0xff); ptr[1]=(uint8)((v>>8)&0xff); ptr[2]=(uint8)((v>>16)&0xff); ptr[3]=(uint8)((v>>24)&0xff); ptr += 4;}static void Write16(uint16 v, uint8*& ptr){ ptr[0]=(uint8)(v&0xff); ptr[1]=(uint8)((v>>8)&0xff); ptr += 2;}static int read_movie_header(FILE* fd, SMovie* movie){ uint8 header[SMV_HEADER_SIZE]; if(fread(header, 1, SMV_HEADER_SIZE, fd) != SMV_HEADER_SIZE) return WRONG_FORMAT; const uint8* ptr=header; uint32 magic=Read32(ptr); if(magic!=SMV_MAGIC) return WRONG_FORMAT; uint32 version=Read32(ptr); if(version!=SMV_VERSION) return WRONG_VERSION; movie->MovieId=Read32(ptr); movie->RerecordCount=Read32(ptr); movie->MaxFrame=Read32(ptr); movie->ControllersMask=*ptr++; movie->Opts=*ptr++; ptr += 2; movie->SaveStateOffset=Read32(ptr); movie->ControllerDataOffset=Read32(ptr); return SUCCESS;}static void write_movie_header(FILE* fd, const SMovie* movie){ uint8 header[SMV_HEADER_SIZE]; uint8* ptr=header; Write32(SMV_MAGIC, ptr); Write32(SMV_VERSION, ptr); Write32(movie->MovieId, ptr); Write32(movie->RerecordCount, ptr); Write32(movie->MaxFrame, ptr); *ptr++=movie->ControllersMask; *ptr++=movie->Opts; *ptr++=0; *ptr++=0; Write32(movie->SaveStateOffset, ptr); Write32(movie->ControllerDataOffset, ptr); fwrite(header, 1, SMV_HEADER_SIZE, fd);}static void flush_movie(){ fseek(Movie.File, 0, SEEK_SET); write_movie_header(Movie.File, &Movie); fseek(Movie.File, Movie.ControllerDataOffset, SEEK_SET); fwrite(Movie.InputBuffer, 1, Movie.BytesPerFrame*(Movie.MaxFrame+1), Movie.File);}static void change_state(MovieState new_state){ if(new_state==Movie.State) return; if(Movie.State==MOVIE_STATE_RECORD) { flush_movie(); } Movie.State=new_state; if(new_state==MOVIE_STATE_NONE) { fclose(Movie.File); Movie.File=NULL; // FIXME: truncate movie to MaxFrame length /* truncate() could be used, if it's certain * that the savestate block is never after * the controller data block. It is not guaranteed * by the format. */ }}static void reserve_buffer_space(uint32 space_needed){ if(space_needed > Movie.InputBufferSize) { uint32 ptr_offset = Movie.InputBufferPtr - Movie.InputBuffer; uint32 alloc_chunks = space_needed / BUFFER_GROWTH_SIZE; Movie.InputBufferSize = BUFFER_GROWTH_SIZE * (alloc_chunks+1); Movie.InputBuffer = (uint8*)realloc(Movie.InputBuffer, Movie.InputBufferSize); Movie.InputBufferPtr = Movie.InputBuffer + ptr_offset; }}static void read_frame_controller_data(){ int i; for(i=0; i<5; ++i) { if(Movie.ControllersMask & (1<<i)) { IPPU.Joypads[i]=(uint32)(Read16(Movie.InputBufferPtr)) | 0x80000000L; } else { IPPU.Joypads[i]=0; // pretend the controller is disconnected } }}static void write_frame_controller_data(){ reserve_buffer_space((uint32)((Movie.InputBufferPtr+Movie.BytesPerFrame)-Movie.InputBuffer)); int i; for(i=0; i<5; ++i) { if(Movie.ControllersMask & (1<<i)) { Write16((uint16)(IPPU.Joypads[i] & 0xffff), Movie.InputBufferPtr); } else { IPPU.Joypads[i]=0; // pretend the controller is disconnected } }}void S9xMovieInit (){ memset(&Movie, 0, sizeof(Movie)); Movie.State = MOVIE_STATE_NONE;}int S9xMovieOpen (const char* filename, bool8 read_only){ FILE* fd; STREAM stream; int result; int fn; if(!(fd=fopen(filename, read_only ? "rb" : "rb+"))) return FILE_NOT_FOUND; // stop current movie before opening change_state(MOVIE_STATE_NONE); // read header if((result=read_movie_header(fd, &Movie))!=SUCCESS) { fclose(fd); return result; } fn=dup(fileno(fd)); fclose(fd); // apparently this lseek is necessary lseek(fn, Movie.SaveStateOffset, SEEK_SET); if(!(stream=REOPEN_STREAM(fn, "rb"))) return FILE_NOT_FOUND; if(Movie.Opts & MOVIE_OPT_FROM_RESET)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -