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/supervisor/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
22static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
23static Handle logFile;
24
25static void _drawStart(void) {
26 sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
27}
28static void _drawEnd(void) {
29 sf2d_end_frame();
30 sf2d_swapbuffers();
31}
32
33static int _pollInput(void) {
34 hidScanInput();
35 int keys = 0;
36 int activeKeys = hidKeysHeld();
37 if (activeKeys & KEY_X) {
38 keys |= 1 << GUI_INPUT_CANCEL;
39 }
40 if (activeKeys & KEY_B) {
41 keys |= 1 << GUI_INPUT_BACK;
42 }
43 if (activeKeys & KEY_A) {
44 keys |= 1 << GUI_INPUT_SELECT;
45 }
46 if (activeKeys & KEY_LEFT) {
47 keys |= 1 << GUI_INPUT_LEFT;
48 }
49 if (activeKeys & KEY_RIGHT) {
50 keys |= 1 << GUI_INPUT_RIGHT;
51 }
52 if (activeKeys & KEY_UP) {
53 keys |= 1 << GUI_INPUT_UP;
54 }
55 if (activeKeys & KEY_DOWN) {
56 keys |= 1 << GUI_INPUT_DOWN;
57 }
58 return keys;
59}
60
61int main() {
62 struct GBAContext context;
63 srvInit();
64 aptInit();
65 hidInit(0);
66 fsInit();
67 sdmcInit();
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 FSUSER_OpenFile(0, &logFile, sdmcArchive, FS_makePath(PATH_CHAR, "/mgba.log"), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
81
82 struct GUIFont* font = GUIFontCreate();
83
84 GBAContextInit(&context, 0);
85 struct GBAOptions opts = {
86 .useBios = true,
87 .logLevel = 0,
88 .idleOptimization = IDLE_LOOP_REMOVE
89 };
90 GBAConfigLoadDefaults(&context.config, &opts);
91 context.gba->logHandler = GBA3DSLog;
92 context.gba->logLevel = 0;
93
94 struct GBAVideoSoftwareRenderer renderer;
95 GBAVideoSoftwareRendererCreate(&renderer);
96 renderer.outputBuffer = anonymousMemoryMap(256 * VIDEO_VERTICAL_PIXELS * 2);
97 renderer.outputBufferStride = 256;
98 GBAVideoAssociateRenderer(&context.gba->video, &renderer.d);
99
100 if (!font) {
101 goto cleanup;
102 }
103
104 struct GUIParams params = {
105 320, 240,
106 font, _drawStart, _drawEnd, _pollInput
107 };
108
109 while (aptMainLoop()) {
110 char path[256];
111 if (!selectFile(¶ms, "/", path, sizeof(path), "gba")) {
112 break;
113 }
114 _drawStart();
115 GUIFontPrintf(font, 130, (GUIFontHeight(font) + 240) / 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "Loading...");
116 _drawEnd();
117 if (!GBAContextLoadROM(&context, path, true)) {
118 continue;
119 }
120 if (!GBAContextStart(&context)) {
121 continue;
122 }
123 while (aptMainLoop()) {
124 hidScanInput();
125 int activeKeys = hidKeysHeld() & 0x3FF;
126 if (hidKeysDown() & KEY_X) {
127 break;
128 }
129 GBAContextFrame(&context, activeKeys);
130 GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
131 GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
132 gspWaitForPPF();
133 _drawStart();
134 sf2d_draw_texture_scale(tex, 40, 296, 1, -1);
135 _drawEnd();
136 }
137 GBAContextStop(&context);
138 }
139 GBAContextDeinit(&context);
140
141cleanup:
142 mappedMemoryFree(renderer.outputBuffer, 0);
143
144 FSFILE_Close(logFile);
145
146 sf2d_free_texture(tex);
147 sf2d_fini();
148
149 sdmcExit();
150 fsExit();
151 gfxExit();
152 hidExit();
153 aptExit();
154 srvExit();
155 return 0;
156}
157
158static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
159 UNUSED(thread);
160 UNUSED(level);
161 char out[256];
162 u64 size;
163 u32 written;
164 size_t len = vsnprintf(out, sizeof(out), format, args);
165 if (len >= 256) {
166 len = 255;
167 }
168 out[len] = '\n';
169 FSFILE_GetSize(logFile, &size);
170 FSFILE_Write(logFile, &written, size, out, len + 1, FS_WRITE_FLUSH);
171}