Skip to content

Commit

Permalink
Auto-suspend audio device, gamecontrollerdb.txt on Android, improved …
Browse files Browse the repository at this point in the history
…versioning
  • Loading branch information
deathkiller committed Jan 28, 2024
1 parent 47c03f9 commit f9dbcb8
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 18 deletions.
4 changes: 1 addition & 3 deletions Sources/Jazz2/PreferencesCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ namespace Jazz2

BoolOptions boolOptions = (BoolOptions)uc.ReadValue<uint64_t>();

#if !defined(DEATH_TARGET_ANDROID) && !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_IOS) && !defined(DEATH_TARGET_SWITCH)
#if !defined(DEATH_TARGET_EMSCRIPTEN)
EnableFullscreen = ((boolOptions & BoolOptions::EnableFullscreen) == BoolOptions::EnableFullscreen);
#endif
ShowPerformanceMetrics = ((boolOptions & BoolOptions::ShowPerformanceMetrics) == BoolOptions::ShowPerformanceMetrics);
Expand All @@ -160,9 +160,7 @@ namespace Jazz2
WeaponWheel = ((boolOptions & BoolOptions::EnableWeaponWheel) == BoolOptions::EnableWeaponWheel ? WeaponWheelStyle::Enabled : WeaponWheelStyle::Disabled);
EnableRgbLights = ((boolOptions & BoolOptions::EnableRgbLights) == BoolOptions::EnableRgbLights);
AllowUnsignedScripts = ((boolOptions & BoolOptions::AllowUnsignedScripts) == BoolOptions::AllowUnsignedScripts);
#if defined(DEATH_TARGET_ANDROID)
UseNativeBackButton = ((boolOptions & BoolOptions::UseNativeBackButton) == BoolOptions::UseNativeBackButton);
#endif
EnableDiscordIntegration = ((boolOptions & BoolOptions::EnableDiscordIntegration) == BoolOptions::EnableDiscordIntegration);
TutorialCompleted = ((boolOptions & BoolOptions::TutorialCompleted) == BoolOptions::TutorialCompleted);
ResumeOnStart = ((boolOptions & BoolOptions::ResumeOnStart) == BoolOptions::ResumeOnStart);
Expand Down
5 changes: 4 additions & 1 deletion Sources/Jazz2/UI/ControlScheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ namespace Jazz2::UI
normalizedLength = std::clamp(normalizedLength, 0.0f, 1.0f);
result.Movement.Y = std::copysign(normalizedLength, result.Movement.Y);

#if defined(DEATH_TARGET_ANDROID)
// Allow native Android back button as menu key
if (PreferencesCache::UseNativeBackButton && pressedKeys[(uint32_t)KeySym::BACK]) {
result.PressedActions |= (1 << (int32_t)PlayerActions::Menu);
}

#endif
return result;
}

Expand Down Expand Up @@ -214,10 +215,12 @@ namespace Jazz2::UI
if (pressedKeys[(uint32_t)KeySym::RETURN]) {
pressedActions |= (1 << (int32_t)PlayerActions::Fire);
}
#if defined(DEATH_TARGET_ANDROID)
// Allow native Android back button as menu key
if (PreferencesCache::UseNativeBackButton && pressedKeys[(uint32_t)KeySym::BACK]) {
pressedActions |= (1 << (int32_t)PlayerActions::Menu);
}
#endif
// Use ChangeWeapon action as delete key
if (pressedKeys[(uint32_t)KeySym::Delete]) {
pressedActions |= (1 << (int32_t)PlayerActions::ChangeWeapon);
Expand Down
8 changes: 6 additions & 2 deletions Sources/Jazz2/UI/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,14 @@ namespace Jazz2::UI
// Touch Controls
if (_touchButtonsTimer > 0.0f) {
for (auto& button : _touchButtons) {
if (button.Graphics == nullptr || (PreferencesCache::UseNativeBackButton && button.Action == PlayerActions::Menu)) {
if (button.Graphics == nullptr) {
continue;
}

#if defined(DEATH_TARGET_ANDROID)
if (button.Action == PlayerActions::Menu && PreferencesCache::UseNativeBackButton) {
continue;
}
#endif
float x = button.Left;
float y = button.Top;
if ((button.Align & Alignment::Right) == Alignment::Right) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/UI/Menu/ControlsOptionsSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Jazz2::UI::Menu
TouchControls,
InputDiagnostics,
#if defined(DEATH_TARGET_ANDROID)
UseNativeBackButton,
UseNativeBackButton
#endif
};

Expand Down
10 changes: 8 additions & 2 deletions Sources/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,13 @@ void GameEventHandler::OnInit()
theApplication().inputManager().setCursor(IInputManager::Cursor::Hidden);
}

# if !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_WINDOWS_RT)
// Try to load gamepad mappings from `Content` directory
# if !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_SWITCH) && !defined(DEATH_TARGET_WINDOWS_RT)
// Try to load gamepad mappings from `Content` directory (or from parent directory of `Source` on Android)
# if defined(DEATH_TARGET_ANDROID)
String mappingsPath = fs::CombinePath(fs::GetDirectoryName(resolver.GetSourcePath()), "gamecontrollerdb.txt"_s);
# else
String mappingsPath = fs::CombinePath(resolver.GetContentPath(), "gamecontrollerdb.txt"_s);
# endif
if (fs::IsReadableFile(mappingsPath)) {
theApplication().inputManager().addJoyMappingsFromFile(mappingsPath);
}
Expand Down Expand Up @@ -402,7 +406,9 @@ void GameEventHandler::OnResizeWindow(int width, int height)
_currentHandler->OnInitializeViewport(width, height);
}

#if !defined(DEATH_TARGET_ANDROID) && !defined(DEATH_TARGET_IOS) && !defined(DEATH_TARGET_SWITCH)
PreferencesCache::EnableFullscreen = theApplication().gfxDevice().isFullscreen();
#endif

LOGI("Rendering resolution: %ix%i", width, height);
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/nCine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,11 @@ namespace nCine
if (appEventHandler_ != nullptr) {
appEventHandler_->OnSuspend();
}
#if defined(WITH_AUDIO)
IAudioDevice& audioDevice = theServiceLocator().audioDevice();
audioDevice.suspendDevice();
#endif

LOGI("IAppEventHandler::OnSuspend() invoked");
}

Expand All @@ -757,6 +762,11 @@ namespace nCine
if (appEventHandler_ != nullptr) {
appEventHandler_->OnResume();
}
#if defined(WITH_AUDIO)
IAudioDevice& audioDevice = theServiceLocator().audioDevice();
audioDevice.resumeDevice();
#endif

const TimeStamp suspensionDuration = frameTimer_->resume();
LOGD("Suspended for %.3f seconds", suspensionDuration.seconds());
#if defined(NCINE_PROFILING)
Expand Down
18 changes: 18 additions & 0 deletions Sources/nCine/Audio/ALAudioDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ namespace nCine
return nativeFreq_;
}

void ALAudioDevice::suspendDevice()
{
#if defined(ALC_SOFT_pause_device)
if (device_ != nullptr) {
alcDevicePauseSOFT(device_);
}
#endif
}

void ALAudioDevice::resumeDevice()
{
#if defined(ALC_SOFT_pause_device)
if (device_ != nullptr) {
alcDeviceResumeSOFT(device_);
}
#endif
}

#if defined(DEATH_TARGET_WINDOWS) && !defined(DEATH_TARGET_WINDOWS_RT)
void ALAudioDevice::recreateAudioDevice()
{
Expand Down
3 changes: 3 additions & 0 deletions Sources/nCine/Audio/ALAudioDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ namespace nCine

int nativeFrequency() override;

void suspendDevice() override;
void resumeDevice() override;

private:
/// Maximum number of OpenAL sources
#if defined(DEATH_TARGET_ANDROID) || defined(DEATH_TARGET_EMSCRIPTEN) || defined(DEATH_TARGET_IOS)
Expand Down
6 changes: 6 additions & 0 deletions Sources/nCine/Audio/IAudioDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ namespace nCine
virtual void updateListener(const Vector3f& position, const Vector3f& velocity) = 0;

virtual int nativeFrequency() = 0;

virtual void suspendDevice() = 0;
virtual void resumeDevice() = 0;
};

inline IAudioDevice::~IAudioDevice() { }
Expand Down Expand Up @@ -104,5 +107,8 @@ namespace nCine
const Vector3f& getListenerPosition() const override { return Vector3f::Zero; }
void updateListener(const Vector3f& position, const Vector3f& velocity) override { }
int nativeFrequency() override { return 0; }

void suspendDevice() override { }
void resumeDevice() override { }
};
}
6 changes: 4 additions & 2 deletions Sources/nCine/Backends/Android/AndroidApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ namespace nCine
break;
case APP_CMD_PAUSE:
LOGW("APP_CMD_PAUSE event received");
theAndroidApplication().suspend();
isPaused = true;
if (!isPaused) {
theAndroidApplication().suspend();
isPaused = true;
}
break;
case APP_CMD_STOP:
LOGW("APP_CMD_STOP event received (not handled)");
Expand Down
7 changes: 6 additions & 1 deletion Sources/nCine/CommonHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
#endif

#if defined(NCINE_INCLUDE_OPENAL)
# define AL_ALEXT_PROTOTYPES
# if !defined(AL_ALEXT_PROTOTYPES)
# define AL_ALEXT_PROTOTYPES
# endif
# if defined(DEATH_TARGET_APPLE)
# include <OpenAL/al.h>
# elif defined(DEATH_TARGET_EMSCRIPTEN)
Expand All @@ -52,6 +54,9 @@
#endif

#if defined(NCINE_INCLUDE_OPENALC)
# if !defined(AL_ALEXT_PROTOTYPES)
# define AL_ALEXT_PROTOTYPES
# endif
# if defined(DEATH_TARGET_APPLE)
# include <OpenAL/alc.h>
# include <OpenAL/al.h>
Expand Down
8 changes: 6 additions & 2 deletions cmake/ncine_build_android.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ list(APPEND ANDROID_CMAKE_ARGS -DCMAKE_ANDROID_NDK=${NDK_DIR})
set(PACKAGE_VERSION_MAJOR ${NCINE_VERSION_MAJOR})
set(PACKAGE_VERSION_MINOR ${NCINE_VERSION_MINOR})
set(PACKAGE_VERSION_PATCH ${NCINE_VERSION_PATCH})
if (NCINE_VERSION_FROM_GIT AND GIT_NO_TAG)
set(PACKAGE_VERSION_PATCH "0")
if(NCINE_VERSION_FROM_GIT AND GIT_NO_TAG)
if(DEFINED NCINE_VERSION_PATCH_LAST)
set(PACKAGE_VERSION_PATCH ${NCINE_VERSION_PATCH_LAST})
else()
set(PACKAGE_VERSION_PATCH "0")
endif()
endif()

set(GRADLE_BUILDTOOLS_VERSION 33.0.1)
Expand Down
8 changes: 6 additions & 2 deletions cmake/ncine_extra_sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,12 @@ elseif(WINDOWS_PHONE OR WINDOWS_STORE)
set(PACKAGE_VERSION_MAJOR ${NCINE_VERSION_MAJOR})
set(PACKAGE_VERSION_MINOR ${NCINE_VERSION_MINOR})
set(PACKAGE_VERSION_PATCH ${NCINE_VERSION_PATCH})
if (NCINE_VERSION_FROM_GIT AND GIT_NO_TAG)
set(PACKAGE_VERSION_PATCH "0")
if(NCINE_VERSION_FROM_GIT AND GIT_NO_TAG)
if(DEFINED NCINE_VERSION_PATCH_LAST)
set(PACKAGE_VERSION_PATCH ${NCINE_VERSION_PATCH_LAST})
else()
set(PACKAGE_VERSION_PATCH "0")
endif()
endif()
set(PACKAGE_VERSION_REV "0")
if(DEFINED GIT_REV_COUNT)
Expand Down
6 changes: 5 additions & 1 deletion cmake/ncine_generated_sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ if(WIN32)
message(STATUS "Writing a version info resource file")
set(PACKAGE_VERSION_PATCH ${NCINE_VERSION_PATCH})
if(NCINE_VERSION_FROM_GIT AND GIT_NO_TAG)
set(PACKAGE_VERSION_PATCH "0")
if(DEFINED NCINE_VERSION_PATCH_LAST)
set(PACKAGE_VERSION_PATCH ${NCINE_VERSION_PATCH_LAST})
else()
set(PACKAGE_VERSION_PATCH "0")
endif()
endif()
set(PACKAGE_VERSION_REV "0")
if(DEFINED GIT_REV_COUNT)
Expand Down
14 changes: 13 additions & 1 deletion cmake/ncine_get_version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,37 @@ if(NCINE_VERSION_FROM_GIT)
else()
if(GIT_NO_TAG)
if(GIT_NO_LAST_TAG)
# Fallback to last commit date if no tag found
string(REPLACE "-" ";" GIT_LAST_COMMIT_DATE_LIST ${GIT_LAST_COMMIT_DATE})
list(GET GIT_LAST_COMMIT_DATE_LIST 0 NCINE_VERSION_MAJOR)
list(GET GIT_LAST_COMMIT_DATE_LIST 1 NCINE_VERSION_MINOR)
set(NCINE_VERSION_PATCH "r${GIT_REV_COUNT}-${GIT_SHORT_HASH}")
else()
string(REGEX REPLACE "^v" "" GIT_LAST_TAG_NAME ${GIT_LAST_TAG_NAME})
string(REPLACE "." ";" GIT_TAG_NAME_LIST ${GIT_LAST_TAG_NAME})
list(LENGTH GIT_TAG_NAME_LIST GIT_TAG_NAME_LIST_LENGTH)
if(GIT_TAG_NAME_LIST_LENGTH GREATER 0)
# Use the last commit revision and hash for nightly builds,
# but use NCINE_VERSION_PATCH_LAST for internal version numbers
list(GET GIT_TAG_NAME_LIST 0 NCINE_VERSION_MAJOR)
list(GET GIT_TAG_NAME_LIST 1 NCINE_VERSION_MINOR)
set(NCINE_VERSION_PATCH "r${GIT_REV_COUNT}-${GIT_SHORT_HASH}")

set(NCINE_VERSION_PATCH_LAST 0)
if(GIT_TAG_NAME_LIST_LENGTH GREATER 2)
list(GET GIT_TAG_NAME_LIST 2 NCINE_VERSION_PATCH_LAST)
endif()
else()
# Fallback to last commit date if the last tag is not in valid format
string(REPLACE "-" ";" GIT_LAST_COMMIT_DATE_LIST ${GIT_LAST_COMMIT_DATE})
list(GET GIT_LAST_COMMIT_DATE_LIST 0 NCINE_VERSION_MAJOR)
list(GET GIT_LAST_COMMIT_DATE_LIST 1 NCINE_VERSION_MINOR)
set(NCINE_VERSION_PATCH "r${GIT_REV_COUNT}-${GIT_SHORT_HASH}")
endif()
endif()
else()
# The last commit is tagged, use it as version
string(REGEX REPLACE "^v" "" GIT_TAG_NAME ${GIT_TAG_NAME})
string(REPLACE "." ";" GIT_TAG_NAME_LIST ${GIT_TAG_NAME})
list(LENGTH GIT_TAG_NAME_LIST GIT_TAG_NAME_LIST_LENGTH)
if(GIT_TAG_NAME_LIST_LENGTH GREATER 0)
Expand Down Expand Up @@ -129,4 +141,4 @@ else()
endif()
message(STATUS "Game version: ${NCINE_VERSION}")

mark_as_advanced(NCINE_VERSION_MAJOR NCINE_VERSION_MINOR NCINE_VERSION_PATCH NCINE_VERSION)
mark_as_advanced(NCINE_VERSION_MAJOR NCINE_VERSION_MINOR NCINE_VERSION_PATCH NCINE_VERSION_PATCH_LAST NCINE_VERSION)

0 comments on commit f9dbcb8

Please sign in to comment.