src/gba/rr/rr.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "rr.h"
7
8#include "util/vfs.h"
9
10void GBARRInitRecord(struct GBA* gba) {
11 if (!gba || !gba->rr) {
12 return;
13 }
14
15 if (gba->rr->initFrom & INIT_FROM_SAVEGAME) {
16 if (gba->rr->savedata) {
17 gba->rr->savedata->close(gba->rr->savedata);
18 }
19 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_TRUNC | O_CREAT | O_WRONLY);
20 GBASavedataClone(&gba->memory.savedata, gba->rr->savedata);
21 gba->rr->savedata->close(gba->rr->savedata);
22 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_RDONLY);
23 GBASavedataMask(&gba->memory.savedata, gba->rr->savedata);
24 } else {
25 GBASavedataMask(&gba->memory.savedata, 0);
26 }
27
28 if (gba->rr->initFrom & INIT_FROM_SAVESTATE) {
29 struct VFile* vf = gba->rr->openSavestate(gba->rr, O_TRUNC | O_CREAT | O_RDWR);
30 GBASaveStateNamed(gba, vf, 0);
31 vf->close(vf);
32 } else {
33 ARMReset(gba->cpu);
34 }
35}
36
37void GBARRInitPlay(struct GBA* gba) {
38 if (!gba || !gba->rr) {
39 return;
40 }
41
42 if (gba->rr->initFrom & INIT_FROM_SAVEGAME) {
43 if (gba->rr->savedata) {
44 gba->rr->savedata->close(gba->rr->savedata);
45 }
46 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_RDONLY);
47 GBASavedataMask(&gba->memory.savedata, gba->rr->savedata);
48 } else {
49 GBASavedataMask(&gba->memory.savedata, 0);
50 }
51
52 if (gba->rr->initFrom & INIT_FROM_SAVESTATE) {
53 struct VFile* vf = gba->rr->openSavestate(gba->rr, O_RDONLY);
54 GBALoadStateNamed(gba, vf, SAVESTATE_SCREENSHOT);
55 vf->close(vf);
56 } else {
57 ARMReset(gba->cpu);
58 }
59}
60
61void GBARRDestroy(struct GBARRContext* rr) {
62 if (rr->isPlaying(rr)) {
63 rr->stopPlaying(rr);
64 }
65 if (rr->isRecording(rr)) {
66 rr->stopRecording(rr);
67 }
68 if (rr->savedata) {
69 rr->savedata->close(rr->savedata);
70 rr->savedata = 0;
71 }
72 rr->destroy(rr);
73}