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.
  • Loading branch information
Craftyawesome committed Aug 19, 2024
1 parent 070eb35 commit c01e424
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 9 deletions.
3 changes: 3 additions & 0 deletions include/libultraship/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -106,7 +106,13 @@ target_sources(libultraship PRIVATE ${Source_Files__Utils})

#=================== 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
Expand Down Expand Up @@ -162,7 +168,7 @@ target_include_directories(libultraship

#=================== Linking ===================
if(NOT EXCLUDE_MPQ_SUPPORT)
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)
Expand Down Expand Up @@ -191,7 +197,7 @@ target_link_libraries(libultraship PUBLIC tinyxml2::tinyxml2)
find_package(spdlog REQUIRED)
target_link_libraries(libultraship PUBLIC spdlog::spdlog)

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)
Expand Down
9 changes: 9 additions & 0 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

namespace Ship {
Expand Down Expand Up @@ -200,15 +202,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,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() {
Expand Down
7 changes: 5 additions & 2 deletions src/graphic/Fast3D/gfx_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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>
Expand Down Expand Up @@ -768,7 +771,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

Expand Down Expand Up @@ -876,7 +879,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

Expand Down
52 changes: 52 additions & 0 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#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__)

#ifdef __MINGW32__
Expand All @@ -19,6 +27,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
Expand Down Expand Up @@ -324,6 +337,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
Expand All @@ -338,6 +355,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
Expand Down Expand Up @@ -369,14 +393,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);

Expand Down Expand Up @@ -439,6 +471,22 @@ static void gfx_sdl_set_keyboard_callbacks(bool (*on_key_down)(int scancode), bo
on_all_keys_up_callback = on_all_keys_up;
}

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();
}

static void gfx_sdl_get_dimensions(uint32_t* width, uint32_t* height, int32_t* posX, int32_t* posY) {
SDL_GL_GetDrawableSize(wnd, static_cast<int*>((void*)width), static_cast<int*>((void*)height));
SDL_GetWindowPosition(wnd, static_cast<int*>(posX), static_cast<int*>(posY));
Expand Down Expand Up @@ -491,7 +539,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.
Expand Down
Loading

0 comments on commit c01e424

Please sign in to comment.