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
68 sf2d_init();
69 sf2d_set_clear_color(0);
70 sf2d_texture* tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
71 memset(tex->data, 0, 256 * 256 * 2);
72
73 sdmcArchive = (FS_archive) {
74 ARCH_SDMC,
75 (FS_path) { PATH_EMPTY, 1, (const u8*)"" },
76 0, 0
77 };
78 FSUSER_OpenArchive(0, &sdmcArchive);
79 FSUSER_OpenFile(0, &logFile, sdmcArchive, FS_makePath(PATH_CHAR, "/mgba.log"), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
80
81 struct GUIFont* font = GUIFontCreate();
82
83 GBAContextInit(&context, 0);
84 struct GBAOptions opts = {
85 .useBios = true,
86 .logLevel = 0,
87 .idleOptimization = IDLE_LOOP_REMOVE
88 };
89 GBAConfigLoadDefaults(&context.config, &opts);
90 context.gba->logHandler = GBA3DSLog;
91 context.gba->logLevel = 0;
92
93 struct GBAVideoSoftwareRenderer renderer;
94 GBAVideoSoftwareRendererCreate(&renderer);
95 renderer.outputBuffer = anonymousMemoryMap(256 * VIDEO_VERTICAL_PIXELS * 2);
96 renderer.outputBufferStride = 256;
97 GBAVideoAssociateRenderer(&context.gba->video, &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 _drawStart();
108 GUIFontPrintf(font, 0, GUIFontHeight(font), GUI_TEXT_LEFT, 0xFFFFFFFF, "Loading...");
109 _drawEnd();
110 char path[256] = "/rom.gba";
111 if (!selectFile(¶ms, "/", path, sizeof(path), "gba") || !GBAContextLoadROM(&context, path, true)) {
112 goto cleanup;
113 }
114 GBAContextStart(&context);
115
116 while (aptMainLoop()) {
117 hidScanInput();
118 int activeKeys = hidKeysHeld() & 0x3FF;
119 if (hidKeysDown() & KEY_X) {
120 break;
121 }
122 GBAContextFrame(&context, activeKeys);
123 GSPGPU_FlushDataCache(0, renderer.outputBuffer, 256 * VIDEO_VERTICAL_PIXELS * 2);
124 GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
125 gspWaitForPPF();
126 _drawStart();
127 sf2d_draw_texture_scale(tex, 40, 296, 1, -1);
128 _drawEnd();
129 }
130
131 GBAContextStop(&context);
132 GBAContextDeinit(&context);
133
134cleanup:
135 mappedMemoryFree(renderer.outputBuffer, 0);
136
137 FSFILE_Close(logFile);
138
139 sf2d_free_texture(tex);
140 sf2d_fini();
141
142 fsExit();
143 gfxExit();
144 hidExit();
145 aptExit();
146 srvExit();
147 return 0;
148}
149
150static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
151 UNUSED(thread);
152 UNUSED(level);
153 char out[256];
154 u64 size;
155 u32 written;
156 size_t len = vsnprintf(out, sizeof(out), format, args);
157 if (len >= 256) {
158 len = 255;
159 }
160 out[len] = '\n';
161 FSFILE_GetSize(logFile, &size);
162 FSFILE_Write(logFile, &written, size, out, len + 1, FS_WRITE_FLUSH);
163}