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 <mgba/internal/gba/rr/rr.h>
7
8#include <mgba/core/log.h>
9#include <mgba/core/serialize.h>
10#include <mgba-util/vfs.h>
11
12mLOG_DEFINE_CATEGORY(GBA_RR, "GBA RR");
13
14void GBARRInitRecord(struct GBA* gba) {
15 if (!gba || !gba->rr) {
16 return;
17 }
18
19 if (gba->rr->initFrom & INIT_FROM_SAVEGAME) {
20 if (gba->rr->savedata) {
21 gba->rr->savedata->close(gba->rr->savedata);
22 }
23 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_TRUNC | O_CREAT | O_WRONLY);
24 GBASavedataClone(&gba->memory.savedata, gba->rr->savedata);
25 gba->rr->savedata->close(gba->rr->savedata);
26 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_RDONLY);
27 GBASavedataMask(&gba->memory.savedata, gba->rr->savedata, false);
28 } else {
29 GBASavedataMask(&gba->memory.savedata, 0, false);
30 }
31
32 if (gba->rr->initFrom & INIT_FROM_SAVESTATE) {
33 struct VFile* vf = gba->rr->openSavestate(gba->rr, O_TRUNC | O_CREAT | O_RDWR);
34 //GBASaveStateNamed(gba, vf, SAVESTATE_SAVEDATA);
35 vf->close(vf);
36 } else {
37 ARMReset(gba->cpu);
38 }
39}
40
41void GBARRInitPlay(struct GBA* gba) {
42 if (!gba || !gba->rr) {
43 return;
44 }
45
46 if (gba->rr->initFrom & INIT_FROM_SAVEGAME) {
47 if (gba->rr->savedata) {
48 gba->rr->savedata->close(gba->rr->savedata);
49 }
50 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_RDONLY);
51 GBASavedataMask(&gba->memory.savedata, gba->rr->savedata, false);
52 } else {
53 GBASavedataMask(&gba->memory.savedata, 0, false);
54 }
55
56 if (gba->rr->initFrom & INIT_FROM_SAVESTATE) {
57 struct VFile* vf = gba->rr->openSavestate(gba->rr, O_RDONLY);
58 //GBALoadStateNamed(gba, vf, SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA);
59 vf->close(vf);
60 } else {
61 ARMReset(gba->cpu);
62 }
63}
64
65void GBARRDestroy(struct GBARRContext* rr) {
66 if (rr->isPlaying(rr)) {
67 rr->stopPlaying(rr);
68 }
69 if (rr->isRecording(rr)) {
70 rr->stopRecording(rr);
71 }
72 if (rr->savedata) {
73 rr->savedata->close(rr->savedata);
74 rr->savedata = 0;
75 }
76 rr->destroy(rr);
77}