Skip to content

Commit

Permalink
Revert "Remove Switch (Kenix3#531)"
Browse files Browse the repository at this point in the history
This reverts commit 3d2e6bc.
briaguya-ai committed Apr 29, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3d2e6bc commit cb52fcf
Showing 18 changed files with 396 additions and 10 deletions.
7 changes: 7 additions & 0 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -25,6 +25,9 @@ if((CMAKE_SYSTEM_NAME MATCHES "Windows") AND ("${CMAKE_CXX_COMPILER_ID}" MATCHES
target_link_libraries(storm ${CLANG_RT_BUILTINS_LIBRARY})
endif()
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch")
target_compile_definitions(storm PRIVATE -D_POSIX_C_SOURCE=200809L)
endif()

#=================== ImGui ===================

@@ -41,6 +44,10 @@ target_sources(ImGui
${IMGUI_DIR}/imgui.cpp
)

if (CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch")
target_include_directories(ImGui PRIVATE ${DEVKITPRO}/portlibs/switch/include/)
endif()

target_sources(ImGui
PRIVATE
${IMGUI_DIR}/backends/imgui_impl_opengl3.cpp
2 changes: 1 addition & 1 deletion extern/ImGui/backends/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@
#define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_COMBINED >= 3300) // 3.3+ GLFW_HOVERED
#define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwSetWindowOpacity
#define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetMonitorContentScale
#if defined(__EMSCRIPTEN__) // no Vulkan support in GLFW for Emscripten
#if defined(__EMSCRIPTEN__) || defined(__SWITCH__) // no Vulkan support in GLFW for Emscripten or homebrew Nintendo Switch
#define GLFW_HAS_VULKAN (0)
#else
#define GLFW_HAS_VULKAN (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwCreateWindowSurface
11 changes: 11 additions & 0 deletions extern/ImGui/imconfig.h
Original file line number Diff line number Diff line change
@@ -127,3 +127,14 @@ namespace ImGui
void MyFunction(const char* name, MyMatrix44* mtx);
}
*/

#ifdef __SWITCH__
#define IMGUI_IMPL_OPENGL_LOADER_CUSTOM
#define GL_GLEXT_PROTOTYPES 1
#include <glad/glad.h>

#define ImGuiKey_NavGamepadActivate ImGuiKey_GamepadFaceRight
#define ImGuiKey_NavGamepadCancel ImGuiKey_GamepadFaceDown
#define ImGuiKey_NavGamepadMenu ImGuiKey_GamepadFaceUp
#define ImGuiKey_NavGamepadInput ImGuiKey_GamepadFaceLeft
#endif
3 changes: 3 additions & 0 deletions include/libultraship/classes.h
Original file line number Diff line number Diff line change
@@ -38,5 +38,8 @@
#ifdef __APPLE__
#include "utils/AppleFolderManager.h"
#endif
#ifdef __SWITCH__
#include "port/switch/SwitchImpl.h"
#endif
#endif
#endif
12 changes: 9 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -111,7 +111,13 @@ target_sources(libultraship PRIVATE ${Source_Files__Utils__BinaryTools})

#=================== Port ===================

if (CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
if (CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch")
set(Source_Files__Port
${CMAKE_CURRENT_SOURCE_DIR}/port/switch/SwitchImpl.h
${CMAKE_CURRENT_SOURCE_DIR}/port/switch/SwitchImpl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/port/switch/SwitchPerformanceProfiles.h
)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
set(Source_Files__Port
${CMAKE_CURRENT_SOURCE_DIR}/port/mobile/MobileImpl.h
${CMAKE_CURRENT_SOURCE_DIR}/port/mobile/MobileImpl.cpp
@@ -173,7 +179,7 @@ target_include_directories(libultraship

#=================== Linking ===================

if (NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
if (NOT CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch" AND NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_link_libraries(libultraship PUBLIC "$<LINK_LIBRARY:WHOLE_ARCHIVE,storm>")
else()
target_link_libraries(libultraship PUBLIC storm)
@@ -195,7 +201,7 @@ target_link_libraries(libultraship PRIVATE libzip::zip)
find_package(nlohmann_json REQUIRED)
target_link_libraries(libultraship PUBLIC nlohmann_json::nlohmann_json)

if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(libultraship PRIVATE Threads::Threads)
9 changes: 9 additions & 0 deletions src/Context.cpp
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@

#ifdef __APPLE__
#include "utils/AppleFolderManager.h"
#elif defined(__SWITCH__)
#include "port/switch/SwitchImpl.h"
#endif

namespace Ship {
@@ -198,15 +200,22 @@ void Context::InitResourceManager(const std::vector<std::string>& otrFiles,
}

if (!GetResourceManager()->DidLoadSuccessfully()) {
#if defined(__SWITCH__)
printf("Main OTR file not found!\n");
#else
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OTR file not found",
"Main OTR file not found. Please generate one", nullptr);
SPDLOG_ERROR("Main OTR file not found!");
#ifdef __IOS__
// We need this exit to close the app when we dismiss the dialog
exit(0);
#endif
#endif
return;
}
#ifdef __SWITCH__
Ship::Switch::Init(PostInitPhase);
#endif
}

void Context::InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) {
Original file line number Diff line number Diff line change
@@ -82,8 +82,11 @@ std::shared_ptr<ControllerRumbleMapping> RumbleMappingFactory::CreateRumbleMappi
}

auto controller = SDL_GameControllerOpen(sdlIndex);
#ifdef __SWITCH__
bool hasRumble = false;
#else
bool hasRumble = SDL_GameControllerHasRumble(controller);

#endif
if (hasRumble) {
sdlControllersWithRumble[lusIndex] = SDL_GameControllerOpen(sdlIndex);
} else {
Original file line number Diff line number Diff line change
@@ -94,8 +94,12 @@ bool SDLMapping::UsesPlaystationLayout() {
}

bool SDLMapping::UsesSwitchLayout() {
#ifdef __SWITCH__
return true;
#else
auto type = GetSDLControllerType();
return type == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO || type == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR;
#endif
}

bool SDLMapping::UsesXboxLayout() {
7 changes: 5 additions & 2 deletions src/graphic/Fast3D/gfx_opengl.cpp
Original file line number Diff line number Diff line change
@@ -31,6 +31,9 @@
#elif __APPLE__
#include <SDL2/SDL.h>
#include <GL/glew.h>
#elif __SWITCH__
#include <SDL2/SDL.h>
#include <glad/glad.h>
#elif USE_OPENGLES
#include <SDL2/SDL.h>
#include <GLES3/gl3.h>
@@ -749,7 +752,7 @@ static void gfx_opengl_upload_texture(const uint8_t* rgba32_buf, uint32_t width,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba32_buf);
}

#ifdef USE_OPENGLES
#if defined(__SWITCH__) || defined(USE_OPENGLES)
#define GL_MIRROR_CLAMP_TO_EDGE 0x8743
#endif

@@ -844,7 +847,7 @@ static void gfx_opengl_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_
}

static void gfx_opengl_init(void) {
#ifndef __linux__
#if !defined(__SWITCH__) && !defined(__linux__)
glewInit();
#endif

41 changes: 41 additions & 0 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include <stdio.h>

#ifndef __SWITCH__
#include "libultraship/libultraship.h"
#else
// including libultraship.h on switch leads to conflicting typedefs for u64 and s64
// so we need to just include classes.h instead here
#include "libultraship/classes.h"
#endif

#if defined(ENABLE_OPENGL) || defined(__APPLE__)

@@ -18,6 +24,11 @@
#elif __APPLE__
#include <SDL.h>
#include "gfx_metal.h"
#elif __SWITCH__
#include <SDL2/SDL.h>
#include <switch.h>
#include <glad/glad.h>
#include "port/switch/SwitchImpl.h"
#else
#include <SDL2/SDL.h>
#define GL_GLEXT_PROTOTYPES 1
@@ -322,6 +333,10 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
#elif defined(__SWITCH__)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif

#ifdef _WIN32
@@ -336,6 +351,13 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s
char title[512];
int len = sprintf(title, "%s (%s)", game_name, gfx_api_name);

#ifdef __SWITCH__
// For Switch we need to set the window width before creating the window
Ship::Switch::GetDisplaySize(&window_width, &window_height);
width = window_width;
height = window_height;
#endif

#ifdef __IOS__
Uint32 flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_SHOWN;
#else
@@ -367,14 +389,22 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s
}

if (use_opengl) {
#ifndef __SWITCH__
SDL_GL_GetDrawableSize(wnd, &window_width, &window_height);

if (start_in_fullscreen) {
set_fullscreen(true, false);
}
#endif

ctx = SDL_GL_CreateContext(wnd);

#ifdef __SWITCH__
if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) {
printf("Failed to initialize glad\n");
}
#endif

SDL_GL_MakeCurrent(wnd, ctx);
SDL_GL_SetSwapInterval(vsync_enabled ? 1 : 0);

@@ -438,9 +468,16 @@ static void gfx_sdl_set_keyboard_callbacks(bool (*on_key_down)(int scancode), bo
}

static void gfx_sdl_main_loop(void (*run_one_game_iter)(void)) {
#ifdef __SWITCH__
while (Ship::Switch::IsRunning()) {
#else
while (is_running) {
#endif
run_one_game_iter();
}
#ifdef __SWITCH__
Ship::Switch::Exit();
#endif

SDL_DestroyRenderer(renderer);
SDL_Quit();
@@ -498,7 +535,11 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) {
#endif
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
#ifdef __SWITCH__
Ship::Switch::GetDisplaySize(&window_width, &window_height);
#else
SDL_GL_GetDrawableSize(wnd, &window_width, &window_height);
#endif
} else if (event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(wnd)) {
// We listen specifically for main window close because closing main window
// on macOS does not trigger SDL_Quit.
Loading

0 comments on commit cb52fcf

Please sign in to comment.