3DS: Start using sf2dlib

This commit is contained in:
Jeffrey Pfau 2015-08-23 09:20:56 -07:00
parent 0affe7c8d7
commit 836d73bc93
2 changed files with 100 additions and 18 deletions

View File

@ -171,7 +171,7 @@ elseif(UNIX)
elseif(3DS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
add_definitions(-DCOLOR_16_BIT -DCOLOR_5_6_5)
list(APPEND OS_LIB ctru)
list(APPEND OS_LIB sf2d ctru)
file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/3ds/3ds-*.c)
source_group("3DS-specific code" FILES ${OS_SRC})
endif()

View File

@ -12,19 +12,32 @@
#include "3ds-vfs.h"
#include <3ds.h>
#include <sf2d.h>
FS_archive sdmcArchive;
static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
static Handle logFile;
static void _drawStart(void) {
sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
}
static void _drawEnd(void) {
sf2d_end_frame();
sf2d_swapbuffers();
}
int main() {
struct GBAContext context;
srvInit();
aptInit();
hidInit(0);
gfxInit(GSP_RGB565_OES, GSP_RGB565_OES, false);
fsInit();
sf2d_init();
sf2d_set_clear_color(0);
sf2d_texture* tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
sdmcArchive = (FS_archive) {
ARCH_SDMC,
(FS_path) { PATH_EMPTY, 1, (const u8*)"" },
@ -44,10 +57,8 @@ int main() {
struct GBAVideoSoftwareRenderer renderer;
GBAVideoSoftwareRendererCreate(&renderer);
size_t stride = VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL;
color_t* videoBuffer = anonymousMemoryMap(stride * VIDEO_VERTICAL_PIXELS);
renderer.outputBuffer = videoBuffer;
renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
renderer.outputBuffer = anonymousMemoryMap(256 * VIDEO_VERTICAL_PIXELS * 2);
renderer.outputBufferStride = 256;
GBAVideoAssociateRenderer(&context.gba->video, &renderer.d);
GBAContextLoadROM(&context, "/rom.gba", true);
@ -60,29 +71,100 @@ int main() {
break;
}
GBAContextFrame(&context, activeKeys);
uint32_t* texdest = (uint32_t*) tex->data;
uint32_t* texsrc = (uint32_t*) renderer.outputBuffer;
int x, y;
for (y = 0; y < VIDEO_VERTICAL_PIXELS; y += 8) {
for (x = 0; x < 16; ++x) {
texdest[ 0 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 0) * 128];
texdest[ 2 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 0) * 128];
texdest[ 8 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 0) * 128];
texdest[10 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 0) * 128];
texdest[32 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 0) * 128];
texdest[34 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 0) * 128];
texdest[40 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 0) * 128];
texdest[42 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 0) * 128];
u16 width, height;
u16* screen = (u16*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &height, &width);
u32 startX = (width - VIDEO_HORIZONTAL_PIXELS) / 2;
u32 startY = (height + VIDEO_VERTICAL_PIXELS) / 2 - 1;
u32 x, y;
for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
screen[startY - y + (startX + x) * height] = videoBuffer[y * VIDEO_HORIZONTAL_PIXELS + x];
texdest[ 1 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 1) * 128];
texdest[ 3 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 1) * 128];
texdest[ 9 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 1) * 128];
texdest[11 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 1) * 128];
texdest[33 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 1) * 128];
texdest[35 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 1) * 128];
texdest[41 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 1) * 128];
texdest[43 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 1) * 128];
texdest[ 4 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 2) * 128];
texdest[ 6 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 2) * 128];
texdest[12 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 2) * 128];
texdest[14 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 2) * 128];
texdest[36 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 2) * 128];
texdest[38 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 2) * 128];
texdest[44 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 2) * 128];
texdest[46 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 2) * 128];
texdest[ 5 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 3) * 128];
texdest[ 7 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 3) * 128];
texdest[13 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 3) * 128];
texdest[15 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 3) * 128];
texdest[37 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 3) * 128];
texdest[39 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 3) * 128];
texdest[45 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 3) * 128];
texdest[47 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 3) * 128];
texdest[16 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 4) * 128];
texdest[18 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 4) * 128];
texdest[24 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 4) * 128];
texdest[26 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 4) * 128];
texdest[48 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 4) * 128];
texdest[50 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 4) * 128];
texdest[56 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 4) * 128];
texdest[58 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 4) * 128];
texdest[17 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 5) * 128];
texdest[19 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 5) * 128];
texdest[25 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 5) * 128];
texdest[27 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 5) * 128];
texdest[49 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 5) * 128];
texdest[51 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 5) * 128];
texdest[57 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 5) * 128];
texdest[59 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 5) * 128];
texdest[20 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 6) * 128];
texdest[22 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 6) * 128];
texdest[28 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 6) * 128];
texdest[30 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 6) * 128];
texdest[52 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 6) * 128];
texdest[54 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 6) * 128];
texdest[60 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 6) * 128];
texdest[62 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 6) * 128];
texdest[21 + x * 64 + y * 128] = texsrc[0 + x * 8 + (y + 7) * 128];
texdest[23 + x * 64 + y * 128] = texsrc[1 + x * 8 + (y + 7) * 128];
texdest[29 + x * 64 + y * 128] = texsrc[2 + x * 8 + (y + 7) * 128];
texdest[31 + x * 64 + y * 128] = texsrc[3 + x * 8 + (y + 7) * 128];
texdest[53 + x * 64 + y * 128] = texsrc[4 + x * 8 + (y + 7) * 128];
texdest[55 + x * 64 + y * 128] = texsrc[5 + x * 8 + (y + 7) * 128];
texdest[61 + x * 64 + y * 128] = texsrc[6 + x * 8 + (y + 7) * 128];
texdest[63 + x * 64 + y * 128] = texsrc[7 + x * 8 + (y + 7) * 128];
}
}
gfxFlushBuffers();
gfxSwapBuffers();
gspWaitForVBlank1();
_drawStart();
sf2d_draw_texture_scale(tex, 40, 300, 1, -1);
_drawEnd();
}
GBAContextStop(&context);
GBAContextDeinit(&context);
mappedMemoryFree(videoBuffer, 0);
cleanup:
mappedMemoryFree(renderer.outputBuffer, 0);
FSFILE_Close(logFile);
sf2d_free_texture(tex);
sf2d_fini();
fsExit();
gfxExit();
hidExit();