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
72 sdmcArchive = (FS_archive) {
73 ARCH_SDMC,
74 (FS_path) { PATH_EMPTY, 1, (const u8*)"" },
75 0, 0
76 };
77 FSUSER_OpenArchive(0, &sdmcArchive);
78 FSUSER_OpenFile(0, &logFile, sdmcArchive, FS_makePath(PATH_CHAR, "/mgba.log"), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
79
80 struct GUIFont* font = GUIFontCreate();
81
82 GBAContextInit(&context, 0);
83 struct GBAOptions opts = {
84 .useBios = true,
85 .logLevel = 0,
86 .idleOptimization = IDLE_LOOP_REMOVE
87 };
88 GBAConfigLoadDefaults(&context.config, &opts);
89 context.gba->logHandler = GBA3DSLog;
90 context.gba->logLevel = 0;
91
92 struct GBAVideoSoftwareRenderer renderer;
93 GBAVideoSoftwareRendererCreate(&renderer);
94 renderer.outputBuffer = anonymousMemoryMap(256 * VIDEO_VERTICAL_PIXELS * 2);
95 renderer.outputBufferStride = 256;
96 GBAVideoAssociateRenderer(&context.gba->video, &renderer.d);
97
98 if (!font) {
99 goto cleanup;
100 }
101
102 struct GUIParams params = {
103 320, 240,
104 font, _drawStart, _drawEnd, _pollInput
105 };
106 _drawStart();
107 GUIFontPrintf(font, 0, GUIFontHeight(font), GUI_TEXT_LEFT, 0xFFFFFFFF, "Loading...");
108 _drawEnd();
109 char path[256] = "/rom.gba";
110 if (!selectFile(¶ms, "/", path, sizeof(path), "gba") || !GBAContextLoadROM(&context, path, true)) {
111 goto cleanup;
112 }
113 GBAContextStart(&context);
114
115 while (aptMainLoop()) {
116 hidScanInput();
117 int activeKeys = hidKeysHeld() & 0x3FF;
118 if (hidKeysDown() & KEY_X) {
119 break;
120 }
121 GBAContextFrame(&context, activeKeys);
122 uint32_t* texdest = (uint32_t*) tex->data;
123 uint32_t* texsrc = (uint32_t*) renderer.outputBuffer;
124 int x, y;
125 for (y = 0; y < VIDEO_VERTICAL_PIXELS; y += 8) {
126 for (x = 0; x < 16; ++x) {
127 texdest[ 0 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 0) * 128];
128 texdest[ 2 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 0) * 128];
129 texdest[ 8 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 0) * 128];
130 texdest[10 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 0) * 128];
131 texdest[32 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 0) * 128];
132 texdest[34 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 0) * 128];
133 texdest[40 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 0) * 128];
134 texdest[42 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 0) * 128];
135
136 texdest[ 1 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 1) * 128];
137 texdest[ 3 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 1) * 128];
138 texdest[ 9 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 1) * 128];
139 texdest[11 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 1) * 128];
140 texdest[33 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 1) * 128];
141 texdest[35 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 1) * 128];
142 texdest[41 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 1) * 128];
143 texdest[43 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 1) * 128];
144
145 texdest[ 4 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 2) * 128];
146 texdest[ 6 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 2) * 128];
147 texdest[12 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 2) * 128];
148 texdest[14 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 2) * 128];
149 texdest[36 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 2) * 128];
150 texdest[38 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 2) * 128];
151 texdest[44 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 2) * 128];
152 texdest[46 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 2) * 128];
153
154 texdest[ 5 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 3) * 128];
155 texdest[ 7 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 3) * 128];
156 texdest[13 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 3) * 128];
157 texdest[15 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 3) * 128];
158 texdest[37 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 3) * 128];
159 texdest[39 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 3) * 128];
160 texdest[45 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 3) * 128];
161 texdest[47 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 3) * 128];
162
163 texdest[16 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 4) * 128];
164 texdest[18 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 4) * 128];
165 texdest[24 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 4) * 128];
166 texdest[26 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 4) * 128];
167 texdest[48 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 4) * 128];
168 texdest[50 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 4) * 128];
169 texdest[56 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 4) * 128];
170 texdest[58 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 4) * 128];
171
172 texdest[17 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 5) * 128];
173 texdest[19 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 5) * 128];
174 texdest[25 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 5) * 128];
175 texdest[27 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 5) * 128];
176 texdest[49 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 5) * 128];
177 texdest[51 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 5) * 128];
178 texdest[57 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 5) * 128];
179 texdest[59 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 5) * 128];
180
181 texdest[20 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 6) * 128];
182 texdest[22 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 6) * 128];
183 texdest[28 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 6) * 128];
184 texdest[30 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 6) * 128];
185 texdest[52 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 6) * 128];
186 texdest[54 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 6) * 128];
187 texdest[60 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 6) * 128];
188 texdest[62 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 6) * 128];
189
190 texdest[21 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 7) * 128];
191 texdest[23 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 7) * 128];
192 texdest[29 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 7) * 128];
193 texdest[31 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 7) * 128];
194 texdest[53 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 7) * 128];
195 texdest[55 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 7) * 128];
196 texdest[61 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 7) * 128];
197 texdest[63 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 7) * 128];
198 }
199 }
200 _drawStart();
201 sf2d_draw_texture_scale(tex, 40, 300, 1, -1);
202 _drawEnd();
203 }
204
205 GBAContextStop(&context);
206 GBAContextDeinit(&context);
207
208cleanup:
209 mappedMemoryFree(renderer.outputBuffer, 0);
210
211 FSFILE_Close(logFile);
212
213 sf2d_free_texture(tex);
214 sf2d_fini();
215
216 fsExit();
217 gfxExit();
218 hidExit();
219 aptExit();
220 srvExit();
221 return 0;
222}
223
224static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
225 UNUSED(thread);
226 UNUSED(level);
227 char out[256];
228 u64 size;
229 u32 written;
230 size_t len = vsnprintf(out, sizeof(out), format, args);
231 if (len >= 256) {
232 len = 255;
233 }
234 out[len] = '\n';
235 FSFILE_GetSize(logFile, &size);
236 FSFILE_Write(logFile, &written, size, out, len + 1, FS_WRITE_FLUSH);
237}