Skip to content

Commit

Permalink
Add Nintendo Switch support
Browse files Browse the repository at this point in the history
  • Loading branch information
usineur committed Dec 15, 2019
1 parent 191b5d8 commit 4a1d159
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 14 deletions.
16 changes: 16 additions & 0 deletions Code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ if(GLES)
add_definitions(-DHAVE_GLES)
endif(GLES)

if(SWITCH_LIBNX)
set(CMAKE_CXX_FLAGS "-O3 -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE -ffunction-sections -fdata-sections -ftls-model=local-exec")
include_directories($ENV{DEVKITPRO}/libnx/include $ENV{DEVKITPRO}/portlibs/switch/include)
add_definitions(-D__SWITCH__)
add_custom_target(${CMAKE_PROJECT_NAME}.nro
DEPENDS ${CMAKE_PROJECT_NAME}
COMMAND nacptool --create "Eldritch" "usineur" "406" ${CMAKE_PROJECT_NAME}.nacp
COMMAND elf2nro Projects/Eld/${CMAKE_PROJECT_NAME} ${CMAKE_PROJECT_NAME}.nro
--icon=${CMAKE_SOURCE_DIR}/icon.jpg --nacp=${CMAKE_PROJECT_NAME}.nacp
)
add_custom_target(nxlink
DEPENDS ${CMAKE_PROJECT_NAME}.nro
COMMAND nxlink -a $(SWITCHIP) ${CMAKE_PROJECT_NAME}.nro -s -p ${CMAKE_PROJECT_NAME}/${CMAKE_PROJECT_NAME}.nro
)
endif(SWITCH_LIBNX)

add_subdirectory(Libraries/Core/)
add_subdirectory(Libraries/Math/)
add_subdirectory(Libraries/3D/)
Expand Down
2 changes: 1 addition & 1 deletion Code/Libraries/3D/src/GL2/gl2renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ bool GL2Renderer::Reset()

/*virtual*/ void GL2Renderer::Refresh()
{
#if BUILD_SDL
#if BUILD_SDL && !BUILD_SWITCH
// On Linux, at least, SDL needs to be set as the current context *after* the
// GL window is shown, so this allows be to do it there.
if( m_Window && m_Window->GetSDLWindow() && m_RenderContext )
Expand Down
4 changes: 4 additions & 0 deletions Code/Libraries/Core/src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include <csignal>
#endif

#if BUILD_SWITCH
#include <switch.h>
#endif

// This should be the first file included in every .cpp (even before that
// file's matching header).
// This should be kept a very lightweight include that can go anywhere.
Expand Down
6 changes: 5 additions & 1 deletion Code/Libraries/Core/src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ Display::Display()
m_AspectRatio = static_cast<float>( m_Width ) / static_cast<float>( m_Height );

STATICHASH( Fullscreen );
#if BUILD_SWITCH
m_Fullscreen = true;
#else
m_Fullscreen = ConfigManager::GetBool( sFullscreen );
#endif

#if BUILD_LINUX
// On Linux, changing the screen resolution isn't really supported, so always upscale.
Expand Down Expand Up @@ -358,4 +362,4 @@ void Display::GetCurrentDisplayMode( int& Width, int& Height, int& RefreshRate )

PRINTF( "Using display mode %dx%d\n", BestDisplayMode.Width, BestDisplayMode.Height );
return BestDisplayMode;
}
}
6 changes: 3 additions & 3 deletions Code/Libraries/Core/src/fileutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <shlobj.h>
#endif

#if BUILD_LINUX || BUILD_MAC
#if BUILD_LINUX || BUILD_MAC || BUILD_SWITCH
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -383,7 +383,7 @@ void GetFilesInFolderInternal( const SimpleString& Path, const SimpleString& Pre

FindClose( FileHandle );
}
#elif BUILD_LINUX || BUILD_MAC
#elif BUILD_LINUX || BUILD_MAC || BUILD_SWITCH
DIR* pDirectory = opendir( Path.CStr() );
if( pDirectory )
{
Expand Down Expand Up @@ -465,7 +465,7 @@ void GetFoldersInFolderInternal( const SimpleString& Path, const SimpleString& P

FindClose( FileHandle );
}
#elif BUILD_LINUX || BUILD_MAC
#elif BUILD_LINUX || BUILD_MAC || BUILD_SWITCH
DIR* pDirectory = opendir( Path.CStr() );
if( pDirectory )
{
Expand Down
6 changes: 5 additions & 1 deletion Code/Libraries/Core/src/inputsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,11 @@ float InputSystem::GetVelocity( const HashedString& Axis ) const
void InputSystem::SetMouseScale( const HashedString& Axis, const float Scale )
{
DEBUGASSERT( m_MouseAdjustments.Search( Axis ).IsValid() );
#if BUILD_SWITCH
m_MouseAdjustments[ Axis ].m_ScaleFactor = 0;
#else
m_MouseAdjustments[ Axis ].m_ScaleFactor = Scale;
#endif
}

bool InputSystem::GetMouseInvert( const HashedString& Axis )
Expand Down Expand Up @@ -1788,4 +1792,4 @@ void InputSystem::PublishDisplayInputString( const SDisplayInput& DisplayInput )
MAKEHASH( m_DefinitionName );
MAKEHASHFROM( InputName, DisplayInput.m_Name );
ConfigManager::SetString( sInputName, InputString.CStr(), sm_DefinitionName );
}
}
2 changes: 1 addition & 1 deletion Code/Libraries/Core/src/timedate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ timedate_tm_t TimeDate::GetLocalTime()
_tzset(); // Apply environment variables so that _localtime_s will convert properly
_time64( &Time );
_localtime64_s( &TimeStruct, &Time );
#elif BUILD_LINUX || BUILD_MAC
#elif BUILD_LINUX || BUILD_MAC || BUILD_SWITCH
tzset();
time( &Time );
localtime_r( &Time, &TimeStruct );
Expand Down
2 changes: 1 addition & 1 deletion Code/Libraries/Core/src/timedate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <time.h>
typedef __time64_t timedate_time_t;
typedef struct tm timedate_tm_t;
#elif BUILD_LINUX || BUILD_MAC
#elif BUILD_LINUX || BUILD_MAC || BUILD_SWITCH
#include <time.h>
typedef time_t timedate_time_t;
typedef struct tm timedate_tm_t;
Expand Down
10 changes: 8 additions & 2 deletions Code/Libraries/Core/src/versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
#define BUILD_LINUX 0
#endif

#ifdef __SWITCH__
#define BUILD_SWITCH 1
#else
#define BUILD_SWITCH 0
#endif

#define BUILD_STEAM 0
#define BUILD_SDL ( 0 || BUILD_MAC || BUILD_LINUX )
#define BUILD_SDL ( 0 || BUILD_MAC || BUILD_LINUX || BUILD_SWITCH )
#define BUILD_WINDOWS_NO_SDL ( BUILD_WINDOWS && ( !BUILD_SDL ) )

#define REPORT_BUILD 0
Expand Down Expand Up @@ -50,4 +56,4 @@
#endif
#endif // REPORT_BUILD

#endif // VERSIONS_H
#endif // VERSIONS_H
9 changes: 8 additions & 1 deletion Code/Libraries/Core/src/xinputcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,17 @@ void XInputController::Tick( const float DeltaTime )
if( SDL_GameControllerGetAttached( m_Controller ) )
{
#define SET_BUTTON( EB, SDLB ) m_CurrentState.m_Buttons[ EB ] = SDL_GameControllerGetButton( m_Controller, SDLB ) != 0
#if BUILD_SWITCH
SET_BUTTON( EB_A, SDL_CONTROLLER_BUTTON_B );
SET_BUTTON( EB_B, SDL_CONTROLLER_BUTTON_A );
SET_BUTTON( EB_X, SDL_CONTROLLER_BUTTON_Y );
SET_BUTTON( EB_Y, SDL_CONTROLLER_BUTTON_X );
#else
SET_BUTTON( EB_A, SDL_CONTROLLER_BUTTON_A );
SET_BUTTON( EB_B, SDL_CONTROLLER_BUTTON_B );
SET_BUTTON( EB_X, SDL_CONTROLLER_BUTTON_X );
SET_BUTTON( EB_Y, SDL_CONTROLLER_BUTTON_Y );
#endif
SET_BUTTON( EB_Up, SDL_CONTROLLER_BUTTON_DPAD_UP );
SET_BUTTON( EB_Down, SDL_CONTROLLER_BUTTON_DPAD_DOWN );
SET_BUTTON( EB_Left, SDL_CONTROLLER_BUTTON_DPAD_LEFT );
Expand Down Expand Up @@ -483,4 +490,4 @@ void XInputController::SetFeedback( float Low, float High )
Low = Saturate( Low );
High = Saturate( High );
SetVibration( ( c_uint16 )( Low * 65535.0f ), ( c_uint16 )( High * 65535.0f ) );
}
}
3 changes: 3 additions & 0 deletions Code/Libraries/Framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ add_library(Framework ${framework_src})
target_link_libraries(Framework Core)
target_link_libraries(Framework 3D)
target_link_libraries(Framework UI)
if(SWITCH_LIBNX)
target_link_libraries(Framework vorbis ogg EGL drm_nouveau glapi GLESv2 nx)
endif(SWITCH_LIBNX)
17 changes: 15 additions & 2 deletions Code/Libraries/Framework/src/framework3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ Framework3D::Framework3D()
: m_EventManager( NULL )
, m_Display( NULL )
, m_Window( NULL )
#if !BUILD_SWITCH
, m_SplashWindow( NULL )
#endif
, m_Keyboard( NULL )
, m_Mouse( NULL )
, m_Clock( NULL )
Expand Down Expand Up @@ -158,6 +160,11 @@ void Framework3D::Main()
}

SDL_DisableScreenSaver();
#if BUILD_SWITCH
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
#endif

STATICHASH( Framework );
Expand Down Expand Up @@ -310,7 +317,9 @@ void Framework3D::Main()

if( ShowWindowASAP() )
{
#if !BUILD_SWITCH
SafeDelete( m_SplashWindow );
#endif
#if BUILD_WINDOWS_NO_SDL
m_Window->Show( m_CmdShow );
#elif BUILD_SDL
Expand Down Expand Up @@ -340,6 +349,7 @@ void Framework3D::CreateSplashWindow( const uint WindowIcon, const char* const T

STATICHASH( Framework );
STATICHASH( SplashImage );
#if !BUILD_SWITCH
const char* const pSplashImage = ConfigManager::GetString( sSplashImage, NULL, sFramework );
if( !pSplashImage )
{
Expand Down Expand Up @@ -385,6 +395,7 @@ void Framework3D::CreateSplashWindow( const uint WindowIcon, const char* const T
{
}
#endif
#endif
}

/*virtual*/ void Framework3D::ShutDown()
Expand All @@ -409,7 +420,9 @@ void Framework3D::CreateSplashWindow( const uint WindowIcon, const char* const T
#endif

SafeDelete( m_Display );
#if !BUILD_SWITCH
SafeDelete( m_SplashWindow );
#endif
SafeDelete( m_Clock );
SafeDelete( m_Keyboard );
SafeDelete( m_Mouse );
Expand All @@ -429,7 +442,7 @@ void Framework3D::CreateSplashWindow( const uint WindowIcon, const char* const T
Profiler::DeleteInstance();
#endif

#if BUILD_SDL
#if BUILD_SDL && !BUILD_SWITCH
SDL_Quit();
#endif

Expand Down Expand Up @@ -1090,4 +1103,4 @@ LRESULT CALLBACK Framework3D::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, L

return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
#endif // BUILD_WINDOWS_NO_SDL
#endif // BUILD_WINDOWS_NO_SDL
4 changes: 3 additions & 1 deletion Code/Libraries/Framework/src/framework3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ class Framework3D : public IWBEventObserver
WBEventManager* m_EventManager; // Event manager for framework only, not for game.
Display* m_Display;
Window* m_Window;
#if !BUILD_SWITCH
Window* m_SplashWindow;
#endif
Keyboard* m_Keyboard;
Mouse* m_Mouse;
Clock* m_Clock;
Expand Down Expand Up @@ -158,4 +160,4 @@ class Framework3D : public IWBEventObserver
bool m_IsShuttingDown;
};

#endif
#endif
4 changes: 4 additions & 0 deletions Code/Projects/Eld/src/eldframework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,9 @@ ENotificationPosition GetSteamNoticePos( const HashedString& SteamNoticePosName
}

// All done, show the window finally.
#if !BUILD_SWITCH
SafeDelete( m_SplashWindow );
#endif
#if BUILD_WINDOWS_NO_SDL
m_Window->Show( m_CmdShow );
#elif BUILD_SDL
Expand Down Expand Up @@ -1809,7 +1811,9 @@ void EldFramework::PrepareForLoad()
{
PRINTF( "EldFramework::ToggleFullscreen\n" );

#if !BUILD_SWITCH
Framework3D::ToggleFullscreen();
#endif

// For fullscreen upscaling, we may need a new m_UpscaleView
UpdateViews();
Expand Down
9 changes: 9 additions & 0 deletions Code/Projects/Eld/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin
extern "C" int main( int argc, char* argv[] )
#endif
{
#if BUILD_SWITCH
socketInitializeDefault();
nxlinkStdio();
#endif
#if BUILD_WINDOWS_NO_SDL
Unused( hPrevInstance );
Unused( lpCmdLine );
Expand All @@ -154,5 +158,10 @@ extern "C" int main( int argc, char* argv[] )

ShutDown();

#if BUILD_SWITCH
SDL_Quit();
socketExit();
#endif

return 0;
}
Binary file added Code/icon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4a1d159

Please sign in to comment.