all repos — mgba @ 41b1eecb8ccf1afcdb2fccfc34a7796d08a74a5c

mGBA Game Boy Advance Emulator

src/platform/3ds/main.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
  7#include "gba/renderers/video-software.h"
  8#include "gba/context/context.h"
  9#include "gba/video.h"
 10#include "util/gui.h"
 11#include "util/gui/file-select.h"
 12#include "util/gui/font.h"
 13#include "util/memory.h"
 14
 15#include "3ds-vfs.h"
 16
 17#include <3ds.h>
 18#include <sf2d.h>
 19
 20FS_archive sdmcArchive;
 21
 22extern bool allocateRomBuffer(void);
 23static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
 24static struct VFile* logFile;
 25
 26static void _drawStart(void) {
 27	sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
 28}
 29static void _drawEnd(void) {
 30	sf2d_end_frame();
 31	sf2d_swapbuffers();
 32}
 33
 34static int _pollInput(void) {
 35	hidScanInput();
 36	int keys = 0;
 37	int activeKeys = hidKeysHeld();
 38	if (activeKeys & KEY_X) {
 39		keys |= 1 << GUI_INPUT_CANCEL;
 40	}
 41	if (activeKeys & KEY_B) {
 42		keys |= 1 << GUI_INPUT_BACK;
 43	}
 44	if (activeKeys & KEY_A) {
 45		keys |= 1 << GUI_INPUT_SELECT;
 46	}
 47	if (activeKeys & KEY_LEFT) {
 48		keys |= 1 << GUI_INPUT_LEFT;
 49	}
 50	if (activeKeys & KEY_RIGHT) {
 51		keys |= 1 << GUI_INPUT_RIGHT;
 52	}
 53	if (activeKeys & KEY_UP) {
 54		keys |= 1 << GUI_INPUT_UP;
 55	}
 56	if (activeKeys & KEY_DOWN) {
 57		keys |= 1 << GUI_INPUT_DOWN;
 58	}
 59	return keys;
 60}
 61
 62int main() {
 63	struct GBAContext context;
 64
 65	if (!allocateRomBuffer()) {
 66		return 1;
 67	}
 68
 69	sf2d_init();
 70	sf2d_set_clear_color(0);
 71	sf2d_texture* tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
 72	memset(tex->data, 0, 256 * 256 * 2);
 73
 74	sdmcArchive = (FS_archive) {
 75		ARCH_SDMC,
 76		(FS_path) { PATH_EMPTY, 1, (const u8*)"" },
 77		0, 0
 78	};
 79	FSUSER_OpenArchive(0, &sdmcArchive);
 80
 81	logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
 82	struct GUIFont* font = GUIFontCreate();
 83
 84	GBAContextInit(&context, 0);
 85	struct GBAOptions opts = {
 86		.useBios = true,
 87		.logLevel = 0,
 88		.idleOptimization = IDLE_LOOP_DETECT
 89	};
 90	GBAConfigLoadDefaults(&context.config, &opts);
 91	context.gba->logHandler = GBA3DSLog;
 92
 93	struct GBAVideoSoftwareRenderer renderer;
 94	GBAVideoSoftwareRendererCreate(&renderer);
 95	renderer.outputBuffer = anonymousMemoryMap(256 * VIDEO_VERTICAL_PIXELS * 2);
 96	renderer.outputBufferStride = 256;
 97	context.renderer = &renderer.d;
 98
 99	if (!font) {
100		goto cleanup;
101	}
102
103	struct GUIParams params = {
104		320, 240,
105		font, _drawStart, _drawEnd, _pollInput
106	};
107
108	char currentPath[256] = "";
109	while (aptMainLoop()) {
110		char path[256];
111		if (!selectFile(&params, "/", path, currentPath, sizeof(path), GBAIsROM)) {
112			break;
113		}
114		_drawStart();
115		GUIFontPrintf(font, 160, (GUIFontHeight(font) + 240) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
116		_drawEnd();
117		if (!GBAContextLoadROM(&context, path, true)) {
118			continue;
119		}
120		if (!GBAContextStart(&context)) {
121			continue;
122		}
123
124#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
125		blip_set_rates(context.gba->audio.left,  GBA_ARM7TDMI_FREQUENCY, 48000);
126		blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000);
127#endif
128
129		while (aptMainLoop()) {
130			hidScanInput();
131			int activeKeys = hidKeysHeld() & 0x3FF;
132			if (hidKeysDown() & KEY_X) {
133				break;
134			}
135			GBAContextFrame(&context, activeKeys);
136			GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
137			GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
138#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
139			blip_clear(context.gba->audio.left);
140			blip_clear(context.gba->audio.right);
141#endif
142			gspWaitForPPF();
143			_drawStart();
144			sf2d_draw_texture_scale(tex, 40, 296, 1, -1);
145			_drawEnd();
146		}
147		GBAContextStop(&context);
148	}
149	GBAContextDeinit(&context);
150
151cleanup:
152	mappedMemoryFree(renderer.outputBuffer, 0);
153
154	if (logFile) {
155		logFile->close(logFile);
156	}
157
158	sf2d_free_texture(tex);
159	sf2d_fini();
160	return 0;
161}
162
163static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
164	UNUSED(thread);
165	UNUSED(level);
166	if (!logFile) {
167		return;
168	}
169	char out[256];
170	size_t len = vsnprintf(out, sizeof(out), format, args);
171	if (len >= sizeof(out)) {
172		len = sizeof(out) - 1;
173	}
174	out[len] = '\n';
175	logFile->write(logFile, out, len + 1);
176}