1/*****************************************************************************\
2 Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3 This file is licensed under the Snes9x License.
4 For further information, consult the LICENSE file in the root directory.
5\*****************************************************************************/
6
7#include "snes9x.h"
8#include "movie.h"
9#include "logger.h"
10
11static int resetno = 0;
12static int framecounter = 0;
13static FILE *video = NULL;
14static FILE *audio = NULL;
15
16
17void S9xResetLogger (void)
18{
19 if (!Settings.DumpStreams)
20 return;
21
22 char buffer[128];
23
24 S9xCloseLogger();
25 framecounter = 0;
26
27 sprintf(buffer, "videostream%d.dat", resetno);
28 video = fopen(buffer, "wb");
29 if (!video)
30 {
31 printf("Opening %s failed. Logging cancelled.\n", buffer);
32 return;
33 }
34
35 sprintf(buffer, "audiostream%d.dat", resetno);
36 audio = fopen(buffer, "wb");
37 if (!audio)
38 {
39 printf("Opening %s failed. Logging cancelled.\n", buffer);
40 fclose(video);
41 return;
42 }
43
44 resetno++;
45}
46
47void S9xCloseLogger (void)
48{
49 if (video)
50 {
51 fclose(video);
52 video = NULL;
53 }
54
55 if (audio)
56 {
57 fclose(audio);
58 audio = NULL;
59 }
60}
61
62void S9xVideoLogger (void *pixels, int width, int height, int depth, int bytes_per_line)
63{
64 int fc = S9xMovieGetFrameCounter();
65 if (fc > 0)
66 framecounter = fc;
67 else
68 framecounter++;
69
70 if (video)
71 {
72 char *data = (char *) pixels;
73
74 for (int i = 0; i < height; i++)
75 {
76 if (!fwrite(data + i * bytes_per_line, depth, width, video))
77 printf ("Error writing video data.\n");
78 }
79 fflush(video);
80 fflush(audio);
81
82 if (Settings.DumpStreamsMaxFrames > 0 && framecounter >= Settings.DumpStreamsMaxFrames)
83 {
84 printf("Logging ended.\n");
85 S9xCloseLogger();
86 }
87
88 }
89}
90
91void S9xAudioLogger (void *samples, int length)
92{
93 if (audio)
94 {
95 if (!fwrite(samples, 1, length, audio))
96 printf ("Error writing audio data.\n");
97 }
98}
99