Add Nintendo Switch initial support

This commit is contained in:
Adrian Graber 2021-07-04 18:52:48 +02:00
parent f50026828a
commit 28f266302f
13 changed files with 273 additions and 12 deletions

28
.github/workflows/build-switch.yml vendored Normal file
View File

@ -0,0 +1,28 @@
name: re3 cmake devkitA64 (Nintendo Switch)
on:
pull_request:
push:
release:
types: published
jobs:
build-nintendo-switch:
runs-on: ubuntu-latest
container: devkitpro/devkita64:latest
steps:
- uses: actions/checkout@v2
with:
submodules: 'true'
- name: "Build files"
run: |
/opt/devkitpro/portlibs/switch/bin/aarch64-none-elf-cmake -S. -Bbuild -DRE3_AUDIO=OAL -DLIBRW_PLATFORM=GL3 -DLIBRW_GL3_GFXLIB=GLFW -DRE3_WITH_OPUS=False -DRE3_VENDORED_LIBRW=True -DRE3_INSTALL=True
cmake --build build --parallel
- name: "Create binary package (cpack)"
working-directory: ./build
run: |
cpack
- name: "Archive binary package (github artifacts)"
uses: actions/upload-artifact@v2
with:
name: "switch-gl3"
path: build/*.zip
if-no-files-found: error

View File

@ -26,6 +26,25 @@
"compilerArgs": ["-ggdb"], "compilerArgs": ["-ggdb"],
"cStandard": "gnu11", "cStandard": "gnu11",
"cppStandard": "gnu++14" "cppStandard": "gnu++14"
},
{
"name": "devkitPro aarch64 (Nintendo Switch)",
"compilerPath": "${env:DEVKITPRO}/devkitA64/bin/aarch64-none-elf-g++",
"includePath": [
"${default}",
"${env:DEVKITPRO}/portlibs/switch/include",
"${env:DEVKITPRO}/libnx/include"
],
"intelliSenseMode": "gcc-arm64",
"cStandard": "gnu11",
"cppStandard": "gnu++11",
"defines": [
"__SWITCH__",
"GTA_SWITCH",
"LIBRW",
"RW_GL3",
"AUDIO_OAL"
]
} }
], ],
"version": 4 "version": 4

10
.vscode/settings.json vendored
View File

@ -2,8 +2,13 @@
"C_Cpp.default.cStandard": "gnu11", "C_Cpp.default.cStandard": "gnu11",
"C_Cpp.default.cppStandard": "gnu++14", "C_Cpp.default.cppStandard": "gnu++14",
"C_Cpp.default.includePath": [ "C_Cpp.default.includePath": [
"src",
"src/animation", "src/animation",
"src/audio", "src/audio",
"src/audio/eax",
"src/audio/oal",
"src/buildings",
"src/collision",
"src/control", "src/control",
"src/core", "src/core",
"src/entities", "src/entities",
@ -15,8 +20,9 @@
"src/peds", "src/peds",
"src/render", "src/render",
"src/rw", "src/rw",
"src/save", "src/save/",
"src/skel", "src/skel/",
"src/skel/glfw",
"src/text", "src/text",
"src/vehicles", "src/vehicles",
"src/weapons", "src/weapons",

View File

@ -12,6 +12,10 @@ message(STATUS "Building ${CMAKE_PROJECT_NAME} GIT SHA1: ${GIT_SHA1}")
if(WIN32) if(WIN32)
set(${PROJECT}_AUDIOS "OAL" "MSS") set(${PROJECT}_AUDIOS "OAL" "MSS")
elseif(NINTENDO_SWITCH)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/nx")
include(NXFunctions)
set(${PROJECT}_AUDIOS "OAL")
else() else()
set(${PROJECT}_AUDIOS "OAL") set(${PROJECT}_AUDIOS "OAL")
endif() endif()
@ -66,6 +70,8 @@ if(${PROJECT}_INSTALL)
set(os "-apple") set(os "-apple")
elseif(UNIX) elseif(UNIX)
set(os "-linux") set(os "-linux")
elseif(NINTENDO_SWITCH)
set(os "-switch")
else() else()
set(compiler "-UNK") set(compiler "-UNK")
message(WARNING "Unknown os. Created cpack package will be wrong. (override using cpack -P)") message(WARNING "Unknown os. Created cpack package will be wrong. (override using cpack -P)")

View File

@ -0,0 +1,7 @@
if(NOT COMMAND nx_generate_nacp)
message(FATAL_ERROR "The `nx_generate_nacp` cmake command is not available. Please use an appropriate Nintendo Switch toolchain.")
endif()
if(NOT COMMAND nx_create_nro)
message(FATAL_ERROR "The `nx_create_nro` cmake command is not available. Please use an appropriate Nintendo Switch toolchain.")
endif()

BIN
logo_switch.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -120,13 +120,19 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
) )
endif() endif()
if(NINTENDO_SWITCH)
set(${PROJECT}_C_CXX_EXTENSIONS ON)
else()
set(${PROJECT}_C_CXX_EXTENSIONS OFF)
endif()
set_target_properties(${EXECUTABLE} set_target_properties(${EXECUTABLE}
PROPERTIES PROPERTIES
C_STANDARD 11 C_STANDARD 11
C_EXTENSIONS OFF C_EXTENSIONS ${${PROJECT}_C_CXX_EXTENSIONS}
C_STANDARD_REQUIRED ON C_STANDARD_REQUIRED ON
CXX_STANDARD 11 CXX_STANDARD 11
CXX_EXTENSIONS OFF CXX_EXTENSIONS ${${PROJECT}_C_CXX_EXTENSIONS}
CXX_STANDARD_REQUIRED ON CXX_STANDARD_REQUIRED ON
) )
@ -140,3 +146,33 @@ if(${PROJECT}_INSTALL)
install(FILES $<TARGET_PDB_FILE:${EXECUTABLE}> DESTINATION "." OPTIONAL) install(FILES $<TARGET_PDB_FILE:${EXECUTABLE}> DESTINATION "." OPTIONAL)
endif() endif()
endif() endif()
# Build Nintendo Switch binaries
if(NINTENDO_SWITCH)
target_compile_definitions(${EXECUTABLE} PRIVATE
GTA_SWITCH
)
# Needed for OpenAL-Soft
target_link_libraries(${EXECUTABLE} PRIVATE
openal # HACK - something broke with latest cmake
SDL2
)
nx_generate_nacp (${EXECUTABLE}.nacp
NAME "${EXECUTABLE}"
AUTHOR "${EXECUTABLE} Team"
VERSION "1.0.0-${GIT_SHA1}"
)
nx_create_nro(${EXECUTABLE}
NACP ${EXECUTABLE}.nacp
ICON "${PROJECT_SOURCE_DIR}/logo_switch.jpg"
)
if(${PROJECT}_INSTALL)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE}.nro"
DESTINATION "."
)
endif()
endif()

View File

@ -11,6 +11,10 @@
#define __STDC_LIMIT_MACROS // so we get UINT32_MAX etc #define __STDC_LIMIT_MACROS // so we get UINT32_MAX etc
#endif #endif
#ifdef GTA_SWITCH
#include <switch.h>
#endif
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>

View File

@ -319,7 +319,7 @@ enum Config {
#if !defined(RW_GL3) && defined(_WIN32) #if !defined(RW_GL3) && defined(_WIN32)
#define XINPUT #define XINPUT
#endif #endif
#if defined XINPUT || (defined RW_GL3 && !defined LIBRW_SDL2 && !defined __SWITCH__) #if defined XINPUT || (defined RW_GL3 && !defined LIBRW_SDL2 && !defined GTA_SWITCH)
#define DETECT_JOYSTICK_MENU // Then we'll expect user to enter Controller->Detect joysticks if his joystick isn't detected at the start. #define DETECT_JOYSTICK_MENU // Then we'll expect user to enter Controller->Detect joysticks if his joystick isn't detected at the start.
#endif #endif
#define DETECT_PAD_INPUT_SWITCH // Adds automatic switch of pad related stuff between controller and kb/m #define DETECT_PAD_INPUT_SWITCH // Adds automatic switch of pad related stuff between controller and kb/m
@ -439,7 +439,7 @@ enum Config {
#endif #endif
// Streaming // Streaming
#if !defined(_WIN32) && !defined(__SWITCH__) #if !defined(_WIN32) && !defined(GTA_SWITCH)
//#define ONE_THREAD_PER_CHANNEL // Don't use if you're not on SSD/Flash - also not utilized too much right now(see commented LoadAllRequestedModels in Streaming.cpp) //#define ONE_THREAD_PER_CHANNEL // Don't use if you're not on SSD/Flash - also not utilized too much right now(see commented LoadAllRequestedModels in Streaming.cpp)
#define FLUSHABLE_STREAMING // Make it possible to interrupt reading when processing file isn't needed anymore. #define FLUSHABLE_STREAMING // Make it possible to interrupt reading when processing file isn't needed anymore.
#endif #endif
@ -461,4 +461,9 @@ enum Config {
#undef PEDS_REPORT_CRIMES_ON_PHONE #undef PEDS_REPORT_CRIMES_ON_PHONE
#endif #endif
#ifdef GTA_SWITCH
#define IGNORE_MOUSE_KEYBOARD // ignore mouse & keyboard input
#define USE_UNNAMED_SEM // named semaphores are unsupported on the switch
#endif
#endif // VANILLA_DEFINES #endif // VANILLA_DEFINES

View File

@ -45,9 +45,7 @@
#include "Population.h" #include "Population.h"
#include "IniFile.h" #include "IniFile.h"
#ifdef DETECT_JOYSTICK_MENU
#include "crossplatform.h" #include "crossplatform.h"
#endif
#ifndef _WIN32 #ifndef _WIN32
#include "assert.h" #include "assert.h"

View File

@ -198,6 +198,20 @@ char* casepath(char const* path, bool checkPathFirst)
size_t rl = 0; size_t rl = 0;
DIR* d; DIR* d;
char* c;
#if defined(GTA_SWITCH) || defined(GTA_VITA)
if( (c = strstr(p, ":/")) != NULL) // scheme used by some environments, eg. switch, vita
{
size_t deviceNameOffset = c - p + 3;
char* deviceNamePath = (char*)alloca(deviceNameOffset + 1);
strlcpy(deviceNamePath, p, deviceNameOffset);
deviceNamePath[deviceNameOffset] = 0;
d = opendir(deviceNamePath);
p = c + 1;
}
else
#endif
if (p[0] == '/' || p[0] == '\\') if (p[0] == '/' || p[0] == '\\')
{ {
d = opendir("/"); d = opendir("/");
@ -212,7 +226,7 @@ char* casepath(char const* path, bool checkPathFirst)
bool cantProceed = false; // just convert slashes in what's left in string, don't correct case of letters(because we can't) bool cantProceed = false; // just convert slashes in what's left in string, don't correct case of letters(because we can't)
bool mayBeTrailingSlash = false; bool mayBeTrailingSlash = false;
char* c;
while (c = strsep(&p, "/\\")) while (c = strsep(&p, "/\\"))
{ {
// May be trailing slash(allow), slash at the start(avoid), or multiple slashes(avoid) // May be trailing slash(allow), slash at the start(avoid), or multiple slashes(avoid)
@ -279,3 +293,133 @@ char* casepath(char const* path, bool checkPathFirst)
return out; return out;
} }
#endif #endif
#ifdef GTA_SWITCH
/* Taken from glibc */
char *realpath(const char *name, char *resolved)
{
char *rpath, *dest = NULL;
const char *start, *end, *rpath_limit;
long int path_max;
/* As per Single Unix Specification V2 we must return an error if
either parameter is a null pointer. We extend this to allow
the RESOLVED parameter to be NULL in case the we are expected to
allocate the room for the return value. */
if (!name)
return NULL;
/* As per Single Unix Specification V2 we must return an error if
the name argument points to an empty string. */
if (name[0] == '\0')
return NULL;
#ifdef PATH_MAX
path_max = PATH_MAX;
#else
path_max = pathconf(name, _PC_PATH_MAX);
if (path_max <= 0)
path_max = 1024;
#endif
if (!resolved)
{
rpath = (char*)malloc(path_max);
if (!rpath)
return NULL;
}
else
rpath = resolved;
rpath_limit = rpath + path_max;
if (name[0] != '/')
{
if (!getcwd(rpath, path_max))
{
rpath[0] = '\0';
goto error;
}
dest = (char*)memchr(rpath, '\0', path_max);
}
else
{
rpath[0] = '/';
dest = rpath + 1;
}
for (start = end = name; *start; start = end)
{
/* Skip sequence of multiple path-separators. */
while (*start == '/')
++start;
/* Find end of path component. */
for (end = start; *end && *end != '/'; ++end)
/* Nothing. */;
if (end - start == 0)
break;
else if (end - start == 1 && start[0] == '.')
/* nothing */;
else if (end - start == 2 && start[0] == '.' && start[1] == '.')
{
/* Back up to previous component, ignore if at root already. */
if (dest > rpath + 1)
while ((--dest)[-1] != '/')
;
}
else
{
size_t new_size;
if (dest[-1] != '/')
*dest++ = '/';
if (dest + (end - start) >= rpath_limit)
{
ptrdiff_t dest_offset = dest - rpath;
char *new_rpath;
if (resolved)
{
if (dest > rpath + 1)
dest--;
*dest = '\0';
goto error;
}
new_size = rpath_limit - rpath;
if (end - start + 1 > path_max)
new_size += end - start + 1;
else
new_size += path_max;
new_rpath = (char *)realloc(rpath, new_size);
if (!new_rpath)
goto error;
rpath = new_rpath;
rpath_limit = rpath + new_size;
dest = rpath + dest_offset;
}
dest = (char*)memcpy(dest, start, end - start);
*dest = '\0';
}
}
if (dest > rpath + 1 && dest[-1] == '/')
--dest;
*dest = '\0';
return rpath;
error:
if (!resolved)
free(rpath);
return NULL;
}
ssize_t readlink (const char * __path, char * __buf, size_t __buflen)
{
errno = ENOSYS;
return -1;
}
#endif

View File

@ -821,7 +821,9 @@ PadHandler(RsEvent event, void *param)
RwBool RwBool
AttachInputDevices(void) AttachInputDevices(void)
{ {
#ifndef IGNORE_MOUSE_KEYBOARD
RsInputDeviceAttach(rsKEYBOARD, KeyboardHandler); RsInputDeviceAttach(rsKEYBOARD, KeyboardHandler);
#endif
RsInputDeviceAttach(rsPAD, PadHandler); RsInputDeviceAttach(rsPAD, PadHandler);

View File

@ -12,12 +12,14 @@ DWORD _dwOperatingSystemVersion;
#include "resource.h" #include "resource.h"
#else #else
long _dwOperatingSystemVersion; long _dwOperatingSystemVersion;
#ifndef GTA_SWITCH
#ifndef __APPLE__ #ifndef __APPLE__
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#else #else
#include <mach/mach_host.h> #include <mach/mach_host.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
#endif
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
#include <signal.h> #include <signal.h>
@ -51,7 +53,7 @@ long _dwOperatingSystemVersion;
#include "MemoryMgr.h" #include "MemoryMgr.h"
// We found out that GLFW's keyboard input handling is still pretty delayed/not stable, so now we fetch input from X11 directly on Linux. // We found out that GLFW's keyboard input handling is still pretty delayed/not stable, so now we fetch input from X11 directly on Linux.
#if !defined _WIN32 && !defined __APPLE__ && !defined __SWITCH__ // && !defined WAYLAND #if !defined _WIN32 && !defined __APPLE__ && !defined GTA_SWITCH // && !defined WAYLAND
#define GET_KEYBOARD_INPUT_FROM_X11 #define GET_KEYBOARD_INPUT_FROM_X11
#endif #endif
@ -485,11 +487,13 @@ psInitialize(void)
debug("Physical memory size %llu\n", _dwMemAvailPhys); debug("Physical memory size %llu\n", _dwMemAvailPhys);
debug("Available physical memory %llu\n", size); debug("Available physical memory %llu\n", size);
#else #else
#ifndef GTA_SWITCH
struct sysinfo systemInfo; struct sysinfo systemInfo;
sysinfo(&systemInfo); sysinfo(&systemInfo);
_dwMemAvailPhys = systemInfo.freeram; _dwMemAvailPhys = systemInfo.freeram;
debug("Physical memory size %u\n", systemInfo.totalram); debug("Physical memory size %u\n", systemInfo.totalram);
debug("Available physical memory %u\n", systemInfo.freeram); debug("Available physical memory %u\n", systemInfo.freeram);
#endif
#endif #endif
TheText.Unload(); TheText.Unload();
@ -949,13 +953,15 @@ void psPostRWinit(void)
RwVideoMode vm; RwVideoMode vm;
RwEngineGetVideoModeInfo(&vm, GcurSelVM); RwEngineGetVideoModeInfo(&vm, GcurSelVM);
glfwSetFramebufferSizeCallback(PSGLOBAL(window), resizeCB);
#ifndef IGNORE_MOUSE_KEYBOARD
#ifndef GET_KEYBOARD_INPUT_FROM_X11 #ifndef GET_KEYBOARD_INPUT_FROM_X11
glfwSetKeyCallback(PSGLOBAL(window), keypressCB); glfwSetKeyCallback(PSGLOBAL(window), keypressCB);
#endif #endif
glfwSetFramebufferSizeCallback(PSGLOBAL(window), resizeCB);
glfwSetScrollCallback(PSGLOBAL(window), scrollCB); glfwSetScrollCallback(PSGLOBAL(window), scrollCB);
glfwSetCursorPosCallback(PSGLOBAL(window), cursorCB); glfwSetCursorPosCallback(PSGLOBAL(window), cursorCB);
glfwSetCursorEnterCallback(PSGLOBAL(window), cursorEnterCB); glfwSetCursorEnterCallback(PSGLOBAL(window), cursorEnterCB);
#endif
glfwSetWindowIconifyCallback(PSGLOBAL(window), windowIconifyCB); glfwSetWindowIconifyCallback(PSGLOBAL(window), windowIconifyCB);
glfwSetWindowFocusCallback(PSGLOBAL(window), windowFocusCB); glfwSetWindowFocusCallback(PSGLOBAL(window), windowFocusCB);
glfwSetJoystickCallback(joysChangeCB); glfwSetJoystickCallback(joysChangeCB);
@ -1791,7 +1797,7 @@ main(int argc, char *argv[])
InitMemoryMgr(); InitMemoryMgr();
#endif #endif
#ifndef _WIN32 #if !defined(_WIN32) && !defined(GTA_SWITCH)
struct sigaction act; struct sigaction act;
act.sa_sigaction = terminateHandler; act.sa_sigaction = terminateHandler;
act.sa_flags = SA_SIGINFO; act.sa_flags = SA_SIGINFO;