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 3cb3482
Show file tree
Hide file tree
Showing 17 changed files with 1,845 additions and 9 deletions.
1,324 changes: 1,324 additions & 0 deletions extern/ImGui/backends/imgui_impl_glfw.cpp

Large diffs are not rendered by default.

140 changes: 140 additions & 0 deletions extern/ImGui/imconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//-----------------------------------------------------------------------------
// DEAR IMGUI COMPILE-TIME OPTIONS
// Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure.
// You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions.
//-----------------------------------------------------------------------------
// A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/rebased branch with your modifications to it)
// B) or '#define IMGUI_USER_CONFIG "my_imgui_config.h"' in your project and then add directives in your own file without touching this template.
//-----------------------------------------------------------------------------
// You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp
// files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures.
// Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts.
// Call IMGUI_CHECKVERSION() from your .cpp file to verify that the data structures your files are using are matching the ones imgui.cpp is using.
//-----------------------------------------------------------------------------

#pragma once

//---- Define assertion handler. Defaults to calling assert().
// If your macro uses multiple statements, make sure is enclosed in a 'do { .. } while (0)' block so it can be used as a single statement.
//#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
//#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts

//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows
// Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility.
// DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions()
// for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details.
//#define IMGUI_API __declspec( dllexport )
//#define IMGUI_API __declspec( dllimport )

//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to clean your code of obsolete function/names.
//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
//#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87+ disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This is automatically done by IMGUI_DISABLE_OBSOLETE_FUNCTIONS.

//---- Disable all of Dear ImGui or don't implement standard windows/tools.
// It is very strongly recommended to NOT disable the demo windows and debug tool during development. They are extremely useful in day to day work. Please read comments in imgui_demo.cpp.
//#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty.
//#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty.
//#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowIDStackToolWindow() will be empty.

//---- Don't implement some functions to reduce linkage requirements.
//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a)
//#define IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with Visual Studio] Implement default IME handler (require imm32.lib/.a, auto-link for Visual Studio, -limm32 on command-line for MinGW)
//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a)
//#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, IME).
//#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default).
//#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf)
//#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself.
//#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies)
//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function.
//#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions().
//#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available

//---- Include imgui_user.h at the end of imgui.h as a convenience
//#define IMGUI_INCLUDE_IMGUI_USER_H

//---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another)
//#define IMGUI_USE_BGRA_PACKED_COLOR

//---- Use 32-bit for ImWchar (default is 16-bit) to support unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...)
//#define IMGUI_USE_WCHAR32

//---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version
// By default the embedded implementations are declared static and not available outside of Dear ImGui sources files.
//#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h"
//#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h"
//#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if IMGUI_USE_STB_SPRINTF is defined.
//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
//#define IMGUI_DISABLE_STB_SPRINTF_IMPLEMENTATION // only disabled if IMGUI_USE_STB_SPRINTF is defined.

//---- Use stb_sprintf.h for a faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined)
// Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by stb_sprintf.h.
//#define IMGUI_USE_STB_SPRINTF

//---- Use FreeType to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui)
// Requires FreeType headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the FreeType library (not provided).
// On Windows you may use vcpkg with 'vcpkg install freetype --triplet=x64-windows' + 'vcpkg integrate install'.
//#define IMGUI_ENABLE_FREETYPE

//---- Use FreeType+lunasvg library to render OpenType SVG fonts (SVGinOT)
// Requires lunasvg headers to be available in the include path + program to be linked with the lunasvg library (not provided).
// Only works in combination with IMGUI_ENABLE_FREETYPE.
// (implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement)
//#define IMGUI_ENABLE_FREETYPE_LUNASVG

//---- Use stb_truetype to build and rasterize the font atlas (default)
// The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend.
//#define IMGUI_ENABLE_STB_TRUETYPE

//---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.
// This will be inlined as part of ImVec2 and ImVec4 class declarations.
/*
#define IM_VEC2_CLASS_EXTRA \
constexpr ImVec2(const MyVec2& f) : x(f.x), y(f.y) {} \
operator MyVec2() const { return MyVec2(x,y); }
#define IM_VEC4_CLASS_EXTRA \
constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \
operator MyVec4() const { return MyVec4(x,y,z,w); }
*/
//---- ...Or use Dear ImGui's own very basic math operators.
//#define IMGUI_DEFINE_MATH_OPERATORS

//---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices.
// Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices).
// Another way to allow large meshes while keeping 16-bit indices is to handle ImDrawCmd::VtxOffset in your renderer.
// Read about ImGuiBackendFlags_RendererHasVtxOffset for details.
//#define ImDrawIdx unsigned int

//---- Override ImDrawCallback signature (will need to modify renderer backends accordingly)
//struct ImDrawList;
//struct ImDrawCmd;
//typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data);
//#define ImDrawCallback MyImDrawCallback

//---- Debug Tools: Macro to break in Debugger (we provide a default implementation of this in the codebase)
// (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.)
//#define IM_DEBUG_BREAK IM_ASSERT(0)
//#define IM_DEBUG_BREAK __debugbreak()

//---- Debug Tools: Enable slower asserts
//#define IMGUI_DEBUG_PARANOID

//---- Tip: You can add extra functions within the ImGui:: namespace from anywhere (e.g. your own sources/header files)
/*
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
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 3cb3482

Please sign in to comment.