$NetBSD: patch-aj,v 1.3 2004/08/22 11:37:28 kristerw Exp $

--- src/gens/emulator/g_main.c.orig	Tue May 18 22:34:00 2004
+++ src/gens/emulator/g_main.c	Sun Aug 22 13:28:23 2004
@@ -55,6 +55,23 @@
 int SegaCD_Accurate = 0;
 int Kaillera_Client_Running = 0;
 
+int NbRerecord=0;
+char MovieFileName[1024];
+int MoviePlaying=0;
+int FrameCount=0;
+unsigned MovieLastFrame=0;
+
+FILE *MovieFile;
+
+struct type_MovieFrame
+{
+       char P1;
+       char P2;
+       char PX;
+};
+
+struct type_MovieFrame *MovieData = NULL;
+
 static int Gens_Running = 0;
 
 void
@@ -394,6 +411,13 @@
 
 	      break;
 
+	    case SDLK_q:
+	      if (KMOD_CTRL & mod)
+		{
+		  close_gens ();
+		}
+	      break;
+
 	    case SDLK_v:
 	      if (KMOD_CTRL & mod)
 		{
@@ -429,14 +453,14 @@
 	case SDL_JOYAXISMOTION:
 	  if (event.jaxis.axis < 2)
 	    {
-	      if (event.jaxis.value < -500)
+	      if (event.jaxis.value < -10922)
 		{
 		  joystate[0x100 * event.jaxis.which +
 			   ((event.jaxis.axis == 0) ? 0x3 : 0x1)] = 1;
 		  joystate[0x100 * event.jaxis.which +
 			   ((event.jaxis.axis == 0) ? 0x4 : 0x2)] = 0;
 		}
-	      else if (event.jaxis.value > 500)
+	      else if (event.jaxis.value > 10922)
 		{
 		  joystate[0x100 * event.jaxis.which +
 			   ((event.jaxis.axis == 0) ? 0x4 : 0x2)] = 1;
@@ -661,11 +685,11 @@
 int
 main (int argc, char *argv[])
 {
-  Init_Genesis_Bios ();
-
   char sdlbuf[32];
   GtkWidget *widget;
 
+  Init_Genesis_Bios ();
+
   add_pixmap_directory (DATADIR);
   gtk_init (&argc, &argv);
   gens_window = create_gens_window ();
@@ -849,4 +873,84 @@
     }
 
   return (0);
+}
+
+void PlayMovie()
+{
+	if(MoviePlaying) {
+		StopMovie();
+		return;
+	}
+	if(Change_File_L_MV(MovieFileName, State_Dir)==0)
+		return;
+	if (Genesis_Started)
+	{
+		Reset_Genesis();
+	}
+	else if (_32X_Started)
+	{
+		Reset_32X();
+	}
+	FrameCount=0;
+	if(LoadMovieFromFile(MovieFile,MovieFileName)==0)
+		return;
+	MESSAGE_NUM_L("Playing movie from start: %d rerecords","Playing movie from start: %d rerecords",NbRerecord,1500);
+	MoviePlaying=1;
+}
+
+int LoadMovieFromFile(FILE *MovieFile,char* FileName)
+{
+	MovieFile=fopen(FileName,"r+b");
+	if(MovieFile==NULL)
+	{
+		MESSAGE_L("Error loading movie:disk error","Error loading movie:disk error", 2000);
+		return 0;
+	}
+
+	fseek(MovieFile,0,SEEK_END);
+	MovieLastFrame=(ftell(MovieFile)-64)/3;
+	
+	MovieData = malloc(sizeof(struct type_MovieFrame)*MovieLastFrame);
+	if(!MovieData)
+	{
+		MESSAGE_L("Memory error allocating movie", "Memory error allocating movie", 2000);
+		return 0;
+	}
+	fseek(MovieFile,64,SEEK_SET);
+
+	if(fread(MovieData, sizeof(*MovieData), MovieLastFrame, MovieFile) < MovieLastFrame)
+	{
+		MESSAGE_L("Error loading movie:file read","Error loading movie:file read", 2000);
+		return 0;
+	}
+
+	fseek(MovieFile,16,SEEK_SET);
+	fread((char*)&NbRerecord,sizeof(NbRerecord),1,MovieFile);
+
+	fclose(MovieFile);
+	MovieFile=NULL;
+
+	return 1;
+}
+void ReadInMovie(unsigned  frame, char *p1, char *p2, char *px)
+{
+	if(frame>MovieLastFrame)
+	{
+		MESSAGE_NUM_L("Overflow error reading frame : %d","Overflow error reading frame : %d",frame,2000);
+		return;
+	}
+	*p1=MovieData[frame].P1;
+	*p2=MovieData[frame].P2;
+	*px=MovieData[frame].PX;
+}
+
+void
+StopMovie(void)
+{
+  if (!MoviePlaying)
+    return;
+
+  MoviePlaying = 0;
+  free(MovieData);
+  MovieData = NULL;
 }
