all repos — mgba @ ff637c84d1f229b0891e0d7f9a40ad5c7a8b12f0

mGBA Game Boy Advance Emulator

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