Completely unfinished DS port

macOS: Always build cocoa build when using makefile, fixes it trying to compile carbon build on M1 macs
This commit is contained in:
UnknownShadow200 2024-02-05 18:15:02 +11:00
parent 4de0747168
commit e07f1bc4c7
5 changed files with 657 additions and 20 deletions

View file

@ -12,14 +12,6 @@ ifndef $(PLAT)
else
PLAT=$(shell uname -s | tr '[:upper:]' '[:lower:]')
endif
ifeq ($(PLAT),darwin)
ifeq ($(shell uname -m), x86_64)
PLAT=mac_x64
else
PLAT=mac_x32
endif
endif
endif
ifeq ($(PLAT),web)
@ -46,15 +38,9 @@ CFLAGS=-g -pipe -fno-math-errno
LIBS=-lsocket -lX11 -lXi -lGL
endif
ifeq ($(PLAT),mac_x32)
CFLAGS=-g -m32 -pipe -fno-math-errno
LIBS=
LDFLAGS=-rdynamic -framework Carbon -framework AGL -framework OpenGL -framework IOKit
endif
ifeq ($(PLAT),mac_x64)
ifeq ($(PLAT),darwin)
OBJECTS+=src/interop_cocoa.o
CFLAGS=-g -m64 -pipe -fno-math-errno
CFLAGS=-g -pipe -fno-math-errno
LIBS=
LDFLAGS=-rdynamic -framework Cocoa -framework OpenGL -framework IOKit -lobjc
endif
@ -120,10 +106,8 @@ mingw:
$(MAKE) $(ENAME) PLAT=mingw
sunos:
$(MAKE) $(ENAME) PLAT=sunos
mac_x32:
$(MAKE) $(ENAME) PLAT=mac_x32
mac_x64:
$(MAKE) $(ENAME) PLAT=mac_x64
darwin:
$(MAKE) $(ENAME) PLAT=darwin
freebsd:
$(MAKE) $(ENAME) PLAT=freebsd
openbsd:

View file

@ -341,6 +341,15 @@ typedef cc_uint8 cc_bool;
#define CC_BUILD_LOWMEM
#define CC_BUILD_CONSOLE
#undef CC_BUILD_FREETYPE
#elif defined PLAT_NDS
#define CC_BUILD_NDS
#define CC_BUILD_OPENAL
#define CC_BUILD_HTTPCLIENT
#define CC_BUILD_COOPTHREADED
#define CC_BUILD_LOWMEM
#define CC_BUILD_CONSOLE
#undef CC_BUILD_FREETYPE
#undef CC_BUILD_RESOURCES
#endif
#endif

242
src/Graphics_NDS.c Normal file
View file

@ -0,0 +1,242 @@
#include "Core.h"
#ifdef CC_BUILD_NDS
#include "_GraphicsBase.h"
#include "Errors.h"
#include "Logger.h"
#include "Window.h"
/*########################################################################################################################*
*---------------------------------------------------------General---------------------------------------------------------*
*#########################################################################################################################*/
void Gfx_Create(void) {
Gfx_RestoreState();
}
cc_bool Gfx_TryRestoreContext(void) {
return true;
}
void Gfx_Free(void) {
Gfx_FreeState();
}
/*########################################################################################################################*
*-----------------------------------------------------------Misc----------------------------------------------------------*
*#########################################################################################################################*/
cc_result Gfx_TakeScreenshot(struct Stream* output) {
return ERR_NOT_SUPPORTED;
}
void Gfx_GetApiInfo(cc_string* info) {
String_AppendConst(info, "-- Using Nintendo DS --\n");
PrintMaxTextureInfo(info);
}
void Gfx_SetFpsLimit(cc_bool vsync, float minFrameMs) {
gfx_minFrameMs = minFrameMs;
gfx_vsync = vsync;
}
void Gfx_OnWindowResize(void) { }
void Gfx_BeginFrame(void) {
}
void Gfx_Clear(void) {
}
void Gfx_ClearCol(PackedCol color) {
}
void Gfx_EndFrame(void) {
if (gfx_minFrameMs) LimitFPS();
}
/*########################################################################################################################*
*---------------------------------------------------------Textures--------------------------------------------------------*
*#########################################################################################################################*/
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
return NULL;
}
void Gfx_BindTexture(GfxResourceID texId) {
}
void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) {
}
void Gfx_UpdateTexturePart(GfxResourceID texId, int x, int y, struct Bitmap* part, cc_bool mipmaps) {
}
void Gfx_DeleteTexture(GfxResourceID* texId) {
}
void Gfx_EnableMipmaps(void) { }
void Gfx_DisableMipmaps(void) { }
/*########################################################################################################################*
*-----------------------------------------------------State management----------------------------------------------------*
*#########################################################################################################################*/
void Gfx_SetFaceCulling(cc_bool enabled) { }
void Gfx_SetAlphaBlending(cc_bool enabled) { }
void Gfx_SetAlphaArgBlend(cc_bool enabled) { }
void Gfx_SetColWriteMask(cc_bool r, cc_bool g, cc_bool b, cc_bool a) {
}
void Gfx_SetDepthWrite(cc_bool enabled) { }
void Gfx_SetDepthTest(cc_bool enabled) { }
static void Gfx_FreeState(void) { FreeDefaultResources(); }
static void Gfx_RestoreState(void) {
InitDefaultResources();
}
cc_bool Gfx_WarnIfNecessary(void) { return false; }
/*########################################################################################################################*
*---------------------------------------------------------Matrices--------------------------------------------------------*
*#########################################################################################################################*/
void Gfx_CalcOrthoMatrix(struct Matrix* matrix, float width, float height, float zNear, float zFar) {
/* Transposed, source https://learn.microsoft.com/en-us/windows/win32/opengl/glortho */
/* The simplified calculation below uses: L = 0, R = width, T = 0, B = height */
*matrix = Matrix_Identity;
matrix->row1.x = 2.0f / width;
matrix->row2.y = -2.0f / height;
matrix->row3.z = -2.0f / (zFar - zNear);
matrix->row4.x = -1.0f;
matrix->row4.y = 1.0f;
matrix->row4.z = -(zFar + zNear) / (zFar - zNear);
}
static double Cotangent(double x) { return Math_Cos(x) / Math_Sin(x); }
void Gfx_CalcPerspectiveMatrix(struct Matrix* matrix, float fov, float aspect, float zFar) {
float zNear = 0.1f;
float c = (float)Cotangent(0.5f * fov);
/* Transposed, source https://learn.microsoft.com/en-us/windows/win32/opengl/glfrustum */
/* For a FOV based perspective matrix, left/right/top/bottom are calculated as: */
/* left = -c * aspect, right = c * aspect, bottom = -c, top = c */
/* Calculations are simplified because of left/right and top/bottom symmetry */
*matrix = Matrix_Identity;
matrix->row1.x = c / aspect;
matrix->row2.y = c;
matrix->row3.z = -(zFar + zNear) / (zFar - zNear);
matrix->row3.w = -1.0f;
matrix->row4.z = -(2.0f * zFar * zNear) / (zFar - zNear);
matrix->row4.w = 0.0f;
}
/*########################################################################################################################*
*----------------------------------------------------------Buffers--------------------------------------------------------*
*#########################################################################################################################*/
GfxResourceID Gfx_CreateIb2(int count, Gfx_FillIBFunc fillFunc, void* obj) {
return (void*)1;
}
void Gfx_BindIb(GfxResourceID ib) { }
void Gfx_DeleteIb(GfxResourceID* ib) { }
static GfxResourceID Gfx_AllocStaticVb(VertexFormat fmt, int count) {
return NULL;
}
void Gfx_BindVb(GfxResourceID vb) { }
void Gfx_DeleteVb(GfxResourceID* vb) {
}
void* Gfx_LockVb(GfxResourceID vb, VertexFormat fmt, int count) {
return NULL;
}
void Gfx_UnlockVb(GfxResourceID vb) {
}
static GfxResourceID Gfx_AllocDynamicVb(VertexFormat fmt, int maxVertices) {
return NULL;
}
void Gfx_BindDynamicVb(GfxResourceID vb) { Gfx_BindVb(vb); }
void* Gfx_LockDynamicVb(GfxResourceID vb, VertexFormat fmt, int count) {
return NULL;
}
void Gfx_UnlockDynamicVb(GfxResourceID vb) { Gfx_UnlockVb(vb); }
void Gfx_DeleteDynamicVb(GfxResourceID* vb) { Gfx_DeleteVb(vb); }
/*########################################################################################################################*
*-----------------------------------------------------State management----------------------------------------------------*
*#########################################################################################################################*/
void Gfx_SetFog(cc_bool enabled) {
}
void Gfx_SetFogCol(PackedCol color) {
}
void Gfx_SetFogDensity(float value) {
}
void Gfx_SetFogEnd(float value) {
}
void Gfx_SetFogMode(FogFunc func) {
}
void Gfx_SetTexturing(cc_bool enabled) { }
void Gfx_SetAlphaTest(cc_bool enabled) { }
void Gfx_DepthOnlyRendering(cc_bool depthOnly) { }
/*########################################################################################################################*
*---------------------------------------------------------Matrices--------------------------------------------------------*
*#########################################################################################################################*/
void Gfx_LoadMatrix(MatrixType type, const struct Matrix* matrix) {
}
void Gfx_LoadIdentityMatrix(MatrixType type) {
}
void Gfx_EnableTextureOffset(float x, float y) {
}
void Gfx_DisableTextureOffset(void) { }
/*########################################################################################################################*
*--------------------------------------------------------Rendering--------------------------------------------------------*
*#########################################################################################################################*/
void Gfx_SetVertexFormat(VertexFormat fmt) {
}
void Gfx_DrawVb_Lines(int verticesCount) {
}
void Gfx_DrawVb_IndexedTris_Range(int verticesCount, int startVertex) {
}
void Gfx_DrawVb_IndexedTris(int verticesCount) {
}
void Gfx_DrawIndexedTris_T2fC4b(int verticesCount, int startVertex) {
}
#endif

268
src/Platform_NDS.c Normal file
View file

@ -0,0 +1,268 @@
#include "Core.h"
#if defined CC_BUILD_NDS
#include "_PlatformBase.h"
#include "Stream.h"
#include "ExtMath.h"
#include "Funcs.h"
#include "Window.h"
#include "Utils.h"
#include "Errors.h"
#include "Options.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <nds/bios.h>
#include <nds/timers.h>
#include "_PlatformConsole.h"
const cc_result ReturnCode_FileShareViolation = 1000000000; // not used
const cc_result ReturnCode_FileNotFound = ENOENT;
const cc_result ReturnCode_SocketInProgess = EINPROGRESS;
const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK;
const cc_result ReturnCode_DirectoryExists = EEXIST;
const char* Platform_AppNameSuffix = " N64";
/*########################################################################################################################*
*------------------------------------------------------Logging/Time-------------------------------------------------------*
*#########################################################################################################################*/
static u32 rtcSecs;
static void syncRTC(void) { rtcSecs++; }
cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) {
if (end < beg) return 0;
return end - beg;
}
cc_uint64 Stopwatch_Measure(void) {
u16 raw = timerTick(1);
u32 usecs = timerTicks2usec(raw);
return rtcSecs * 1000000ULL + usecs;
}
void Platform_Log(const char* msg, int len) {
char buffer[256];
cc_string str;
String_InitArray(str, buffer);
String_AppendAll(&str, msg, len);
buffer[str.length] = '\0';
printf("%s\n", buffer);
}
#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;
}
/*########################################################################################################################*
*-----------------------------------------------------Directory/File------------------------------------------------------*
*#########################################################################################################################*/
static const cc_string root_path = String_FromConst("/");
static void GetNativePath(char* str, const cc_string* path) {
// TODO temp hack
cc_string path_ = *path;
int idx = String_IndexOf(path, '/');
if (idx >= 0) path_ = String_UNSAFE_SubstringAt(&path_, idx + 1);
Mem_Copy(str, root_path.buffer, root_path.length);
str += root_path.length;
String_EncodeUtf8(str, &path_);
}
cc_result Directory_Create(const cc_string* path) {
return ERR_NOT_SUPPORTED;
}
int File_Exists(const cc_string* path) {
return false;
}
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
return ERR_NOT_SUPPORTED;
}
cc_result File_Open(cc_file* file, const cc_string* path) {
*file = -1;
return ERR_NOT_SUPPORTED;
}
cc_result File_Create(cc_file* file, const cc_string* path) {
*file = -1;
return ERR_NOT_SUPPORTED;
}
cc_result File_OpenOrCreate(cc_file* file, const cc_string* path) {
*file = -1;
return ERR_NOT_SUPPORTED;
}
cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) {
return ERR_NOT_SUPPORTED;
}
cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) {
return ERR_NOT_SUPPORTED;
}
cc_result File_Close(cc_file file) {
if (file < 0) return 0;
return ERR_NOT_SUPPORTED;
}
cc_result File_Seek(cc_file file, int offset, int seekType) {
return ERR_NOT_SUPPORTED;
}
cc_result File_Position(cc_file file, cc_uint32* pos) {
return ERR_NOT_SUPPORTED;
}
cc_result File_Length(cc_file file, cc_uint32* len) {
return ERR_NOT_SUPPORTED;
}
/*########################################################################################################################*
*--------------------------------------------------------Threading--------------------------------------------------------*
*#########################################################################################################################*/
// !!! NOTE: PSP uses cooperative multithreading (not preemptive multithreading) !!!
void Thread_Sleep(cc_uint32 milliseconds) {
swiDelay(8378 * milliseconds); // TODO probably wrong
}
void* Thread_Create(Thread_StartFunc func) {
return NULL;
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
// TODO: actual multithreading ???
}
void Thread_Detach(void* handle) {
}
void Thread_Join(void* handle) {
}
void* Mutex_Create(void) {
return NULL;
}
void Mutex_Free(void* handle) {
}
void Mutex_Lock(void* handle) {
}
void Mutex_Unlock(void* handle) {
}
void* Waitable_Create(void) {
return NULL;
}
void Waitable_Free(void* handle) {
}
void Waitable_Signal(void* handle) {
}
void Waitable_Wait(void* handle) {
}
void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
}
/*########################################################################################################################*
*---------------------------------------------------------Socket----------------------------------------------------------*
*#########################################################################################################################*/
cc_result Socket_ParseAddress(const cc_string* address, int port, cc_sockaddr* addrs, int* numValidAddrs) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_Connect(cc_socket* s, cc_sockaddr* addr, cc_bool nonblocking) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_Read(cc_socket s, cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_Write(cc_socket s, const cc_uint8* data, cc_uint32 count, cc_uint32* modified) {
return ERR_NOT_SUPPORTED;
}
void Socket_Close(cc_socket s) { }
cc_result Socket_CheckReadable(cc_socket s, cc_bool* readable) {
return ERR_NOT_SUPPORTED;
}
cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) {
return ERR_NOT_SUPPORTED;
}
/*########################################################################################################################*
*--------------------------------------------------------Platform---------------------------------------------------------*
*#########################################################################################################################*/
void Platform_Init(void) {
// Setup a timer that triggers an interrupt once per second
timerStart(1, ClockDivider_1024, TIMER_FREQ_1024(1), syncRTC);
// TODO: Redesign Drawer2D to better handle this
Options_SetBool(OPT_USE_CHAT_FONT, true);
}
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--------------------------------------------------------*
*#########################################################################################################################*/
static cc_result GetMachineID(cc_uint32* key) {
return ERR_NOT_SUPPORTED;
}
#endif

134
src/Window_NDS.c Normal file
View file

@ -0,0 +1,134 @@
#include "Core.h"
#if defined CC_BUILD_NDS
#include "Window.h"
#include "Platform.h"
#include "Input.h"
#include "Event.h"
#include "Graphics.h"
#include "String.h"
#include "Funcs.h"
#include "Bitmap.h"
#include "Errors.h"
#include "ExtMath.h"
#include <nds/arm9/background.h>
static cc_bool launcherMode;
static int bg_id;
static u16* bg_ptr;
struct _DisplayData DisplayInfo;
struct _WindowData WindowInfo;
void Window_Init(void) {
DisplayInfo.Width = 256;
DisplayInfo.Height = 192;
DisplayInfo.Depth = 4; // 32 bit
DisplayInfo.ScaleX = 0.5f;
DisplayInfo.ScaleY = 0.5f;
Window_Main.Width = DisplayInfo.Width;
Window_Main.Height = DisplayInfo.Height;
Window_Main.Focused = true;
Window_Main.Exists = true;
Input.Sources = INPUT_SOURCE_GAMEPAD;
consoleDemoInit();
videoSetMode(MODE_5_2D);
vramSetBankA(VRAM_A_MAIN_BG);
bg_id = bgInit(2, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
bg_ptr = bgGetGfxPtr(bg_id);
}
void Window_Free(void) { }
void Window_Create2D(int width, int height) { launcherMode = true; }
void Window_Create3D(int width, int height) { launcherMode = false; }
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_RequestClose(void) {
Event_RaiseVoid(&WindowEvents.Closing);
}
/*########################################################################################################################*
*----------------------------------------------------Input processing-----------------------------------------------------*
*#########################################################################################################################*/
void Window_ProcessEvents(double delta) {
}
void Cursor_SetPosition(int x, int y) { } // Makes no sense for PSP
void Window_EnableRawMouse(void) { Input.RawMode = true; }
void Window_DisableRawMouse(void) { Input.RawMode = false; }
void Window_UpdateRawMouse(void) { }
/*########################################################################################################################*
*------------------------------------------------------Framebuffer--------------------------------------------------------*
*#########################################################################################################################*/
void Window_AllocFramebuffer(struct Bitmap* bmp) {
bmp->scan0 = (BitmapCol*)Mem_Alloc(bmp->width * bmp->height, 4, "window pixels");
}
void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) {
for (int y = r.y; y < r.y + r.Height; y++)
{
BitmapCol* src = Bitmap_GetRow(bmp, y);
uint16_t* dst = bg_ptr + 256 * y;
for (int x = r.x; x < r.x + r.Width; x++)
{
BitmapCol color = src[x];
// 888 to 565 (discard least significant bits)
// quoting libDNS: < Bitmap background with 16 bit color values of the form aBBBBBGGGGGRRRRR (if 'a' is not set, the pixel will be transparent)
dst[x] = 0x8000 | ((BitmapCol_B(color) & 0xF8) << 7) | ((BitmapCol_G(color) & 0xF8) << 2) | (BitmapCol_R(color) >> 3);
}
}
bgShow(bg_id);
bgUpdate();
}
void Window_FreeFramebuffer(struct Bitmap* bmp) {
Mem_Free(bmp->scan0);
}
/*########################################################################################################################*
*------------------------------------------------------Soft keyboard------------------------------------------------------*
*#########################################################################################################################*/
void Window_OpenKeyboard(struct OpenKeyboardArgs* args) { /* TODO implement */ }
void Window_SetKeyboardText(const cc_string* text) { }
void Window_CloseKeyboard(void) { /* TODO implement */ }
/*########################################################################################################################*
*-------------------------------------------------------Misc/Other--------------------------------------------------------*
*#########################################################################################################################*/
void Window_ShowDialog(const char* title, const char* msg) {
/* TODO implement */
Platform_LogConst(title);
Platform_LogConst(msg);
}
cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) {
return ERR_NOT_SUPPORTED;
}
cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) {
return ERR_NOT_SUPPORTED;
}
#endif