3DS: launcher now renders

This commit is contained in:
UnknownShadow200 2023-04-08 20:43:06 +10:00
parent 421bfcabcd
commit 574790d360
3 changed files with 658 additions and 0 deletions

View File

@ -246,6 +246,12 @@ typedef cc_uint8 cc_bool;
#define CC_BUILD_PSP
#undef CC_BUILD_FREETYPE
#undef EXTENDED_BLOCKS
#elif defined __3DS__
#define CC_BUILD_CURL
#define CC_BUILD_OPENAL
#define CC_BUILD_3DS
#undef CC_BUILD_FREETYPE
#undef EXTENDED_BLOCKS
#endif
#endif

524
src/Platform_3DS.c Normal file
View File

@ -0,0 +1,524 @@
#include "Core.h"
#if defined CC_BUILD_3DS
#include "_PlatformBase.h"
#include "Stream.h"
#include "ExtMath.h"
#include "Drawer2D.h"
#include "Funcs.h"
#include "Window.h"
#include "Utils.h"
#include "Errors.h"
#include "PackedCol.h"
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <poll.h>
#include <stdio.h>
#include <netdb.h>
#include <3ds.h>
#define US_PER_SEC 1000000LL
#define NS_PER_MS 1000000LL
const cc_result ReturnCode_FileShareViolation = 1000000000; /* TODO: not used apparently */
const cc_result ReturnCode_FileNotFound = ENOENT;
const cc_result ReturnCode_SocketInProgess = EINPROGRESS;
const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK;
const cc_result ReturnCode_DirectoryExists = EEXIST;
/*########################################################################################################################*
*---------------------------------------------------------Memory----------------------------------------------------------*
*#########################################################################################################################*/
void Mem_Set(void* dst, cc_uint8 value, cc_uint32 numBytes) { memset(dst, value, numBytes); }
void Mem_Copy(void* dst, const void* src, cc_uint32 numBytes) { memcpy(dst, src, numBytes); }
void* Mem_TryAlloc(cc_uint32 numElems, cc_uint32 elemsSize) {
cc_uint32 size = CalcMemSize(numElems, elemsSize);
return size ? malloc(size) : NULL;
}
void* Mem_TryAllocCleared(cc_uint32 numElems, cc_uint32 elemsSize) {
return calloc(numElems, elemsSize);
}
void* Mem_TryRealloc(void* mem, cc_uint32 numElems, cc_uint32 elemsSize) {
cc_uint32 size = CalcMemSize(numElems, elemsSize);
return size ? realloc(mem, size) : NULL;
}
void Mem_Free(void* mem) {
if (mem) free(mem);
}
/*########################################################################################################################*
*------------------------------------------------------Logging/Time-------------------------------------------------------*
*#########################################################################################################################*/
void Platform_Log(const char* msg, int len) {
//cc_file fd = -1;
// CITRA LOGGING
//cc_string path = String_Init(msg, len, len);
//File_Open(&fd, &path);
//if (fd > 0) File_Close(fd);
write(STDOUT_FILENO, msg, len);
write(STDOUT_FILENO, "\n", 1);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
}
void DateTime_CurrentLocal(struct DateTime* t) {
struct timeval cur;
struct tm loc_time;
gettimeofday(&cur, NULL);
localtime_r(&cur.tv_sec, &loc_time);
t->year = loc_time.tm_year + 1900;
t->month = loc_time.tm_mon + 1;
t->day = loc_time.tm_mday;
t->hour = loc_time.tm_hour;
t->minute = loc_time.tm_min;
t->second = loc_time.tm_sec;
}
cc_uint64 Stopwatch_Measure(void) {
return svcGetSystemTick();
}
cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) {
if (end < beg) return 0;
return (end - beg) * US_PER_SEC / SYSCLOCK_ARM11;
}
/*########################################################################################################################*
*-----------------------------------------------------Directory/File------------------------------------------------------*
*#########################################################################################################################*/
void Directory_GetCachePath(cc_string* path) { }
cc_result Directory_Create(const cc_string* path) {
char str[NATIVE_STR_LEN];
String_EncodeUtf8(str, path);
/* read/write/search permissions for owner and group, and with read/search permissions for others. */
/* TODO: Is the default mode in all cases */
return mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1 ? errno : 0;
}
int File_Exists(const cc_string* path) {
cc_uint64 beg = Stopwatch_Measure();
Thread_Sleep(10);
cc_uint64 end = Stopwatch_Measure();
int delta = Stopwatch_ElapsedMicroseconds(beg, end);
Platform_Log1("DELTA: %i", &delta);
char str[NATIVE_STR_LEN];
struct stat sb;
String_EncodeUtf8(str, path);
return stat(str, &sb) == 0 && S_ISREG(sb.st_mode);
}
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
cc_string path; char pathBuffer[FILENAME_SIZE];
char str[NATIVE_STR_LEN];
DIR* dirPtr;
struct dirent* entry;
char* src;
int len, res, is_dir;
String_EncodeUtf8(str, dirPath);
dirPtr = opendir(str);
if (!dirPtr) return errno;
/* POSIX docs: "When the end of the directory is encountered, a null pointer is returned and errno is not changed." */
/* errno is sometimes leftover from previous calls, so always reset it before readdir gets called */
errno = 0;
String_InitArray(path, pathBuffer);
while ((entry = readdir(dirPtr))) {
path.length = 0;
String_Format1(&path, "%s/", dirPath);
/* ignore . and .. entry */
src = entry->d_name;
if (src[0] == '.' && src[1] == '\0') continue;
if (src[0] == '.' && src[1] == '.' && src[2] == '\0') continue;
len = String_Length(src);
String_AppendUtf8(&path, src, len);
is_dir = entry->d_type == DT_DIR;
/* TODO: fallback to stat when this fails */
if (is_dir) {
res = Directory_Enum(&path, obj, callback);
if (res) { closedir(dirPtr); return res; }
} else {
callback(&path, obj);
}
errno = 0;
}
res = errno; /* return code from readdir */
closedir(dirPtr);
return res;
}
static cc_result File_Do(cc_file* file, const cc_string* path, int mode) {
char str[NATIVE_STR_LEN];
String_EncodeUtf8(str, path);
*file = open(str, mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
return *file == -1 ? errno : 0;
}
cc_result File_Open(cc_file* file, const cc_string* path) {
return File_Do(file, path, O_RDONLY);
}
cc_result File_Create(cc_file* file, const cc_string* path) {
return File_Do(file, path, O_RDWR | O_CREAT | O_TRUNC);
}
cc_result File_OpenOrCreate(cc_file* file, const cc_string* path) {
return File_Do(file, path, O_RDWR | O_CREAT);
}
cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) {
*bytesRead = read(file, data, count);
return *bytesRead == -1 ? errno : 0;
}
cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) {
*bytesWrote = write(file, data, count);
return *bytesWrote == -1 ? errno : 0;
}
cc_result File_Close(cc_file file) {
return close(file) == -1 ? errno : 0;
}
cc_result File_Seek(cc_file file, int offset, int seekType) {
static cc_uint8 modes[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
return lseek(file, offset, modes[seekType]) == -1 ? errno : 0;
}
cc_result File_Position(cc_file file, cc_uint32* pos) {
*pos = lseek(file, 0, SEEK_CUR);
return *pos == -1 ? errno : 0;
}
cc_result File_Length(cc_file file, cc_uint32* len) {
struct stat st;
if (fstat(file, &st) == -1) { *len = -1; return errno; }
*len = st.st_size; return 0;
}
/*########################################################################################################################*
*--------------------------------------------------------Threading--------------------------------------------------------*
*#########################################################################################################################*/
void Thread_Sleep(cc_uint32 milliseconds) {
svcSleepThread(milliseconds * NS_PER_MS);
}
static void Exec3DSThread(void* param) {
((Thread_StartFunc)param)();
}
void* Thread_Create(Thread_StartFunc func) {
return NULL;
// TODO: Not quite correct, but eh
//return threadCreate(Exec3DSThread, (void*)func, 512 * 1024, 0x3f, -2, false);
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
}
void Thread_Detach(void* handle) {
//Thread thread = (Thread)handle;
//threadDetach(thread);
}
void Thread_Join(void* handle) {
//Thread thread = (Thread)handle;
//threadJoin(thread, U64_MAX);
//threadFree(thread);
}
void* Mutex_Create(void) {
LightLock* lock = (LightLock*)Mem_Alloc(1, sizeof(LightLock), "mutex");
LightLock_Init(lock);
return lock;
}
void Mutex_Free(void* handle) {
Mem_Free(handle);
}
void Mutex_Lock(void* handle) {
LightLock_Lock((LightLock*)handle);
}
void Mutex_Unlock(void* handle) {
LightLock_Unlock((LightLock*)handle);
}
void* Waitable_Create(void) {
LightEvent* event = (LightEvent*)Mem_Alloc(1, sizeof(LightEvent), "waitable");
LightEvent_Init(event, RESET_ONESHOT);
return RESET_ONESHOT;
}
void Waitable_Free(void* handle) {
}
void Waitable_Signal(void* handle) {
LightEvent_Signal((LightEvent*)handle);
}
void Waitable_Wait(void* handle) {
LightEvent_Wait((LightEvent*)handle);
}
void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
s64 timeout_ns = milliseconds * (1000 * 1000); // milliseconds to nanoseconds
int res = LightEvent_WaitTimeout((LightEvent*)handle, timeout_ns);
if (res) Logger_Abort2(res, "Waiting timed event");
}
/*########################################################################################################################*
*--------------------------------------------------------Font/Text--------------------------------------------------------*
*#########################################################################################################################*/
void Platform_LoadSysFonts(void) {
}
/*########################################################################################################################*
*---------------------------------------------------------Socket----------------------------------------------------------*
*#########################################################################################################################*/
union SocketAddress {
struct sockaddr raw;
struct sockaddr_in v4;
};
static int ParseHost(union SocketAddress* addr, const char* host) {
struct addrinfo hints = { 0 };
struct addrinfo* result;
struct addrinfo* cur;
int family = 0, res;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
res = getaddrinfo(host, NULL, &hints, &result);
if (res) return 0;
for (cur = result; cur; cur = cur->ai_next) {
if (cur->ai_family != AF_INET) continue;
family = AF_INET;
Mem_Copy(addr, cur->ai_addr, cur->ai_addrlen);
break;
}
freeaddrinfo(result);
return family;
}
static int ParseAddress(union SocketAddress* addr, const cc_string* address) {
char str[NATIVE_STR_LEN];
String_EncodeUtf8(str, address);
if (inet_pton(AF_INET, str, &addr->v4.sin_addr) > 0) return AF_INET;
return ParseHost(addr, str);
}
int Socket_ValidAddress(const cc_string* address) {
union SocketAddress addr;
return ParseAddress(&addr, address);
}
cc_result Socket_Connect(cc_socket* s, const cc_string* address, int port) {
int family, addrSize = 0, blocking_raw = -1; /* non-blocking mode */
union SocketAddress addr;
cc_result res;
*s = -1;
if (!(family = ParseAddress(&addr, address)))
return ERR_INVALID_ARGUMENT;
*s = socket(family, SOCK_STREAM, IPPROTO_TCP);
if (*s == -1) return errno;
ioctl(*s, FIONBIO, &blocking_raw);
if (family == AF_INET) {
addr.v4.sin_family = AF_INET;
addr.v4.sin_port = htons(port);
addrSize = sizeof(addr.v4);
}
res = connect(*s, &addr.raw, addrSize);
return res == -1 ? errno : 0;
}
cc_result Socket_Read(cc_socket s, cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
int recvCount = recv(s, data, count, 0);
if (recvCount != -1) { *modified = recvCount; return 0; }
*modified = 0; return errno;
}
cc_result Socket_Write(cc_socket s, const cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
int sentCount = send(s, data, count, 0);
if (sentCount != -1) { *modified = sentCount; return 0; }
*modified = 0; return errno;
}
void Socket_Close(cc_socket s) {
shutdown(s, SHUT_RDWR);
close(s);
}
static cc_result Socket_Poll(cc_socket s, int mode, cc_bool* success) {
struct pollfd pfd;
int flags;
pfd.fd = s;
pfd.events = mode == SOCKET_POLL_READ ? POLLIN : POLLOUT;
if (poll(&pfd, 1, 0) == -1) { *success = false; return errno; }
/* to match select, closed socket still counts as readable */
flags = mode == SOCKET_POLL_READ ? (POLLIN | POLLHUP) : POLLOUT;
*success = (pfd.revents & flags) != 0;
return 0;
}
cc_result Socket_CheckReadable(cc_socket s, cc_bool* readable) {
return Socket_Poll(s, SOCKET_POLL_READ, readable);
}
cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) {
socklen_t resultSize;
cc_result res = Socket_Poll(s, SOCKET_POLL_WRITE, writable);
if (res || *writable) return res;
/* https://stackoverflow.com/questions/29479953/so-error-value-after-successful-socket-operation */
getsockopt(s, SOL_SOCKET, SO_ERROR, &res, &resultSize);
return res;
}
/*########################################################################################################################*
*-----------------------------------------------------Process/Module------------------------------------------------------*
*#########################################################################################################################*/
cc_result Process_StartGame2(const cc_string* args, int numArgs) {
return ERR_NOT_SUPPORTED;
}
void Process_Exit(cc_result code) { exit(code); }
cc_result Process_StartOpen(const cc_string* args) {
return ERR_NOT_SUPPORTED;
}
/*########################################################################################################################*
*--------------------------------------------------------Updater----------------------------------------------------------*
*#########################################################################################################################*/
const char* const Updater_D3D9 = NULL;
cc_bool Updater_Clean(void) { return true; }
const struct UpdaterInfo Updater_Info = { "&eCompile latest source code to update", 0 };
cc_result Updater_Start(const char** action) { *action = "Starting"; return ERR_NOT_SUPPORTED; }
cc_result Updater_GetBuildTime(cc_uint64* timestamp) { return ERR_NOT_SUPPORTED; }
cc_result Updater_MarkExecutable(void) { return ERR_NOT_SUPPORTED; }
cc_result Updater_SetNewBuildTime(cc_uint64 timestamp) { return ERR_NOT_SUPPORTED; }
/*########################################################################################################################*
*-------------------------------------------------------Dynamic lib-------------------------------------------------------*
*#########################################################################################################################*/
const cc_string DynamicLib_Ext = String_FromConst(".so");
void* DynamicLib_Load2(const cc_string* path) { return NULL; }
void* DynamicLib_Get2(void* lib, const char* name) { return NULL; }
cc_bool DynamicLib_DescribeError(cc_string* dst) {
String_AppendConst(dst, "Dynamic linking unsupported");
return true;
}
/*########################################################################################################################*
*--------------------------------------------------------Platform---------------------------------------------------------*
*#########################################################################################################################*/
void Platform_Init(void) { }
void Platform_Free(void) { }
cc_bool Platform_DescribeError(cc_result res, cc_string* dst) {
char chars[NATIVE_STR_LEN];
int len;
/* For unrecognised error codes, strerror_r might return messages */
/* such as 'No error information', which is not very useful */
/* (could check errno here but quicker just to skip entirely) */
if (res >= 1000) return false;
len = strerror_r(res, chars, NATIVE_STR_LEN);
if (len == -1) return false;
len = String_CalcLen(chars, NATIVE_STR_LEN);
String_AppendUtf8(dst, chars, len);
return true;
}
/*########################################################################################################################*
*-------------------------------------------------------Encryption--------------------------------------------------------*
*#########################################################################################################################*/
cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) {
return ERR_NOT_SUPPORTED;
}
cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) {
return ERR_NOT_SUPPORTED;
}
/*########################################################################################################################*
*-----------------------------------------------------Configuration-------------------------------------------------------*
*#########################################################################################################################*/
int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, cc_string* args) {
int i, count;
// Unlike other plaforms, 3DS doesn't use argv[0] for program name
count = min(argc, GAME_MAX_CMDARGS);
for (i = 0; i < count; i++) {
args[i] = String_FromReadonly(argv[i]);
}
return count;
}
cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) {
return 0;
}
#endif

128
src/Window_3DS.c Normal file
View File

@ -0,0 +1,128 @@
#include "Core.h"
#if defined CC_BUILD_3DS
#include "Graphics.h"
#include "String.h"
#include "Funcs.h"
#include "Bitmap.h"
#include "Errors.h"
#include "Window.h"
#include <3ds.h>
struct _DisplayData DisplayInfo;
struct _WinData WindowInfo;
// Note from https://github.com/devkitPro/libctru/blob/master/libctru/include/3ds/gfx.h
// * Please note that the 3DS uses *portrait* screens rotated 90 degrees counterclockwise.
// * Width/height refer to the physical dimensions of the screen; that is, the top screen
// * is 240 pixels wide and 400 pixels tall; while the bottom screen is 240x320.
void Window_Init(void) {
//gfxInit(GSP_BGR8_OES,GSP_BGR8_OES,false);
//gfxInit(GSP_BGR8_OES,GSP_RGBA8_OES,false);
//gfxInit(GSP_RGBA8_OES, GSP_BGR8_OES, false);
gfxInit(GSP_BGR8_OES,GSP_BGR8_OES,false);
//gfxInit(GSP_RGBA8_OES,GSP_RGBA8_OES,false);
consoleInit(GFX_BOTTOM, NULL);
u16 width, height;
u8* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &width, &height);
DisplayInfo.Width = height; // deliberately swapped
DisplayInfo.Height = width; // deliberately swapped
DisplayInfo.Depth = 4; // 32 bit
DisplayInfo.ScaleX = 1;
DisplayInfo.ScaleY = 1;
WindowInfo.Width = height; // deliberately swapped
WindowInfo.Height = width; // deliberately swapped
}
static void DoCreateWindow(int _3d) {
WindowInfo.Exists = true;
}
void Window_Create2D(int width, int height) { DoCreateWindow(0); }
void Window_Create3D(int width, int height) { DoCreateWindow(1); }
void Window_SetTitle(const cc_string* title) { }
void Clipboard_GetText(cc_string* value) { }
void Clipboard_SetText(const cc_string* value) { }
int Window_GetWindowState(void) { return WINDOW_STATE_FULLSCREEN; }
cc_result Window_EnterFullscreen(void) { return 0; }
cc_result Window_ExitFullscreen(void) { return 0; }
int Window_IsObscured(void) { return 0; }
void Window_Show(void) { }
void Window_SetSize(int width, int height) { }
void Window_Close(void) {
/* TODO implement */
}
void Window_ProcessEvents(void) {
/* TODO implement */
}
void Cursor_SetPosition(int x, int y) {
/* TODO implement */
}
void Window_ShowDialog(const char* title, const char* msg) {
/* TODO implement */
}
cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) {
return ERR_NOT_SUPPORTED;
}
cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) {
return ERR_NOT_SUPPORTED;
}
static struct Bitmap fb_bmp;
void Window_AllocFramebuffer(struct Bitmap* bmp) {
bmp->scan0 = (BitmapCol*)Mem_Alloc(bmp->width * bmp->height, 4, "window pixels");
fb_bmp = *bmp;
}
void Window_DrawFramebuffer(Rect2D r) {
u16 width, height;
u8* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &width, &height);
//Platform_Log4("SRC: %i,%i (%i,%i)", &r.X, &r.Y, &r.Width, &r.Height);
int width_ = width, height_ = height;
//Platform_Log2("DST: %i, %i", &width_, &height_);
// SRC y = 0 to 240
// SRC x = 0 to 400
// DST X = 0 to 240
// DST Y = 0 to 400
for (int y = r.Y; y < r.Y + r.Height; y++)
for (int x = r.X; x < r.X + r.Width; x++)
{
BitmapCol color = Bitmap_GetPixel(&fb_bmp, x, y);
int addr = (width - 1 - y + x * width) * 3; // TODO -1 or not
fb[addr+0] = BitmapCol_B(color);
fb[addr+1] = BitmapCol_G(color);
fb[addr+2] = BitmapCol_R(color);
}
/* TODO implement */
Platform_LogConst("DRAW FB!!!");
gfxFlushBuffers();
gfxSwapBuffers();
}
void Window_FreeFramebuffer(struct Bitmap* bmp) {
/* TODO implement */
}
void Window_OpenKeyboard(struct OpenKeyboardArgs* args) { /* TODO implement */ }
void Window_SetKeyboardText(const cc_string* text) { }
void Window_CloseKeyboard(void) { /* TODO implement */ }
void Window_EnableRawMouse(void) { }
void Window_UpdateRawMouse(void) { }
void Window_DisableRawMouse(void) { }
#endif