Skip to content

Commit

Permalink
Fixed Android mappings and major crash, updated pdqsort.h
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Jul 17, 2024
1 parent f5e283b commit 62d7fb6
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Sources/Jazz2/LevelHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ namespace Jazz2
#endif

LevelHandler::LevelHandler(IRootController* root)
: _root(root), _eventSpawner(this), _difficulty(GameDifficulty::Default), _isReforged(false), _cheatsUsed(false), _checkpointCreated(false),
: _root(root), _lightingShader(nullptr), _blurShader(nullptr), _downsampleShader(nullptr), _combineShader(nullptr), _combineWithWaterShader(nullptr),
_eventSpawner(this), _difficulty(GameDifficulty::Default), _isReforged(false), _cheatsUsed(false), _checkpointCreated(false),
_cheatsBufferLength(0), _nextLevelType(ExitType::None), _nextLevelTime(0.0f), _elapsedFrames(0.0f), _checkpointFrames(0.0f),
_waterLevel(FLT_MAX), _weatherType(WeatherType::None), _pressedKeys(ValueInit, (std::size_t)KeySym::COUNT), _overrideActions(0)
{
Expand Down
13 changes: 0 additions & 13 deletions Sources/Jazz2/PlayerViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ namespace Jazz2
_target->setMagFiltering(SamplerFilter::Linear);

Shader* shader = _downsampleOnly ? _owner->_levelHandler->_downsampleShader : _owner->_levelHandler->_blurShader;
if (shader == nullptr) {
if (_downsampleOnly) {
LOGE("PrecompiledShader::Downsample is not assigned");
} else {
LOGE("PrecompiledShader::Blur is not assigned");
}
}

// Prepare render command
_renderCommand.material().setShader(shader);
Expand Down Expand Up @@ -278,17 +271,11 @@ namespace Jazz2
_lightingBuffer->setMagFiltering(SamplerFilter::Nearest);
_lightingBuffer->setWrap(SamplerWrapping::ClampToEdge);

LOGW("Initializing blur pass 1");
_downsamplePass.Initialize(_viewTexture.get(), w / 2, h / 2, Vector2f(0.0f, 0.0f));
LOGW("Initializing blur pass 2");
_blurPass1.Initialize(_downsamplePass.GetTarget(), w / 2, h / 2, Vector2f(1.0f, 0.0f));
LOGW("Initializing blur pass 3");
_blurPass2.Initialize(_blurPass1.GetTarget(), w / 2, h / 2, Vector2f(0.0f, 1.0f));
LOGW("Initializing blur pass 4");
_blurPass3.Initialize(_blurPass2.GetTarget(), w / 4, h / 4, Vector2f(1.0f, 0.0f));
LOGW("Initializing blur pass 5");
_blurPass4.Initialize(_blurPass3.GetTarget(), w / 4, h / 4, Vector2f(0.0f, 1.0f));
LOGW("Initializing blur pass 6");

if (notInitialized) {
_combineRenderer = std::make_unique<CombineRenderer>(this);
Expand Down
4 changes: 2 additions & 2 deletions Sources/nCine/Backends/Android/AndroidInputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ namespace nCine
for (unsigned int i = 0; i < MaxNumJoysticks; i++) {
const int deviceId = joystickStates_[i].deviceId_;
if (deviceId > -1 && !isDeviceConnected(deviceId)) {
LOGI("Joystick %d (device %d) \"%s\" has been disconnected", i, deviceId, joystickStates_[i].name_);
LOGI("Gamepad %d (device %d) \"%s\" has been disconnected", i, deviceId, joystickStates_[i].name_);
joystickStates_[i].deviceId_ = -1;

if (inputEventHandler_ != nullptr && joystickStates_[i].guid_.isValid()) {
Expand Down Expand Up @@ -670,7 +670,7 @@ namespace nCine
deviceInfo(deviceId, joyId);

const uint8_t* g = joystickStates_[joyId].guid_.data;
LOGI("Device %d, \"%s\" (%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x), has been connected as joystick %d - %d axes, %d buttons",
LOGI("Device %d, \"%s\" (%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x), has been connected as gamepad %d - %d axes, %d buttons",
deviceId, joystickStates_[joyId].name_, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], joyId, joystickStates_[joyId].numAxes_, joystickStates_[joyId].numButtons_);

joystickStates_[joyId].deviceId_ = deviceId;
Expand Down
4 changes: 2 additions & 2 deletions Sources/nCine/Backends/GlfwInputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ namespace nCine
const char* guid = nullptr;
# endif
glfwGetJoystickAxes(joy, &numAxes);
LOGI("Joystick %d \"%s\" (%s) has been connected - %d axes, %d buttons, %d hats",
LOGI("Gamepad %d \"%s\" (%s) has been connected - %d axes, %d buttons, %d hats",
joyId, glfwGetJoystickName(joy), guid, numAxes, numButtons, numHats);
#endif
updateJoystickStates();
Expand All @@ -395,7 +395,7 @@ namespace nCine
}
} else if (event == GLFW_DISCONNECTED) {
joyEventsSimulator_.resetJoystickState(joyId);
LOGI("Joystick %d has been disconnected", joyId);
LOGI("Gamepad %d has been disconnected", joyId);
if (inputEventHandler_ != nullptr) {
inputEventHandler_->OnJoyDisconnected(joyConnectionEvent_);
joyMapping_.OnJoyDisconnected(joyConnectionEvent_);
Expand Down
6 changes: 3 additions & 3 deletions Sources/nCine/Backends/SdlInputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace nCine
sdlJoysticks_[i] = SDL_JoystickOpen(i);
if (sdlJoysticks_[i]) {
SDL_Joystick* sdlJoy = sdlJoysticks_[i];
LOGI("Joystick %d \"%s\" - %d hats, %d axes, %d buttons, %d balls",
LOGI("Gamepad %d \"%s\" - %d hats, %d axes, %d buttons, %d balls",
i, SDL_JoystickName(sdlJoy), SDL_JoystickNumHats(sdlJoy), SDL_JoystickNumAxes(sdlJoy),
SDL_JoystickNumButtons(sdlJoy), SDL_JoystickNumBalls(sdlJoy));
}
Expand Down Expand Up @@ -381,7 +381,7 @@ namespace nCine
SDL_Joystick* joy = sdlJoysticks_[deviceIndex];
const SDL_JoystickGUID joystickGuid = SDL_JoystickGetGUID(joy);
SDL_JoystickGetGUIDString(joystickGuid, joyGuidString_, 33);
LOGI("Joystick %d \"%s\" (%s) has been connected - %d hats, %d axes, %d buttons, %d balls",
LOGI("Gamepad %d \"%s\" (%s) has been connected - %d hats, %d axes, %d buttons, %d balls",
deviceIndex, SDL_JoystickName(joy), joyGuidString_, SDL_JoystickNumHats(joy), SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumBalls(joy));
#endif
joyMapping_.OnJoyConnected(joyConnectionEvent_);
Expand All @@ -402,7 +402,7 @@ namespace nCine
}
sdlJoysticks_[MaxNumJoysticks - 1] = nullptr;

LOGI("Joystick %d has been disconnected", deviceIndex);
LOGI("Gamepad %d has been disconnected", deviceIndex);
inputEventHandler_->OnJoyDisconnected(joyConnectionEvent_);
joyMapping_.OnJoyDisconnected(joyConnectionEvent_);
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/nCine/Base/Algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ namespace nCine

template <std::size_t size>
inline std::int32_t copyStringFirst(char(&dest)[size], Containers::StringView source) {
return copyStringFirst(dest, size, source.data(), source.size());
return copyStringFirst(dest, size, source.data(), (std::int32_t)source.size());
}

int formatString(char* buffer, std::size_t maxLen, const char* format, ...);
Expand Down
56 changes: 48 additions & 8 deletions Sources/nCine/Base/pdqsort/pdqsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,36 @@ namespace pdqsort_detail
// Returns floor(log2(n)), assumes n > 0.
template<class T>
inline int log2(T n) {
#if DEATH_CXX_STANDARD >= 201103L
typename std::make_unsigned<T>::type u = n;
#else
if (n < 0) {
return sizeof(T) * CHAR_BIT - 1;
}
T u = n;
#endif
#if defined(__has_builtin)
# if __has_builtin(__builtin_clz)
if (sizeof(T) <= sizeof(int)) {
return sizeof(int) * CHAR_BIT - __builtin_clz(u) - 1;
}
# endif
# if __has_builtin(__builtin_clzl)
if (sizeof(T) <= sizeof(int)) {
return sizeof(long) * CHAR_BIT - __builtin_clzl(u) - 1;
}
# endif
# if __has_builtin(__builtin_clzll) && __cplusplus >= 201103L
if (sizeof(T) <= sizeof(long long)) {
return sizeof(long long) * CHAR_BIT - __builtin_clzll(u) - 1;
}
# endif
#endif
int log = 0;
while (n >>= 1) ++log;
int cnt = sizeof(T) * CHAR_BIT;
while (cnt >>= 1) {
log += (u >> (log + cnt)) ? cnt : 0;
}
return log;
}

Expand Down Expand Up @@ -227,10 +255,10 @@ namespace pdqsort_detail
// The following branchless partitioning is derived from "BlockQuicksort: How Branch
// Mispredictions don’t affect Quicksort" by Stefan Edelkamp and Armin Weiss, but
// heavily micro-optimized.
unsigned char offsets_l_storage[block_size + cacheline_size];
unsigned char offsets_r_storage[block_size + cacheline_size];
unsigned char* offsets_l = align_cacheline(offsets_l_storage);
unsigned char* offsets_r = align_cacheline(offsets_r_storage);
unsigned char offsets_l_storage[block_size * 1];
unsigned char offsets_r_storage[block_size * 1];
unsigned char* offsets_l = offsets_l_storage;
unsigned char* offsets_r = offsets_r_storage;

Iter offsets_l_base = first;
Iter offsets_r_base = last;
Expand Down Expand Up @@ -426,6 +454,7 @@ namespace pdqsort_detail
// recurse on the left partition, since it's sorted (all equal).
if (!leftmost && !comp(*(begin - 1), *begin)) {
begin = partition_left(begin, end, comp) + 1;
leftmost = false;
continue;
}

Expand Down Expand Up @@ -476,8 +505,19 @@ namespace pdqsort_detail
} else {
// If we were decently balanced and we tried to sort an already partitioned
// sequence try to use insertion sort.
if (already_partitioned && partial_insertion_sort(begin, pivot_pos, comp)
&& partial_insertion_sort(pivot_pos + 1, end, comp)) return;
if (already_partitioned && partial_insertion_sort(begin, pivot_pos, comp)) {
if(partial_insertion_sort(pivot_pos + 1, end, comp)) return;
begin = pivot_pos + 1;
leftmost = false;
continue;
}

}

if((end - (pivot_pos + 1)) < insertion_sort_threshold) {
unguarded_insertion_sort(pivot_pos + 1, end, comp);
end = pivot_pos;
continue;
}

// Sort the left partition first using recursion and do tail recursion elimination for
Expand Down Expand Up @@ -524,4 +564,4 @@ inline void pdqsort_branchless(Iter begin, Iter end) {
pdqsort_branchless(begin, end, std::less<T>());
}

#undef PDQSORT_PREFER_MOVE
#undef PDQSORT_PREFER_MOVE
14 changes: 7 additions & 7 deletions Sources/nCine/Input/JoyMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ namespace nCine
bool JoyMapping::OnJoyConnected(const JoyConnectionEvent& event)
{
#if defined(NCINE_INPUT_DEBUGGING)
LOGI("Controller connected - joyId: %d", event.joyId);
LOGI("Gamepad connected - joyId: %d", event.joyId);
#endif

const char* joyName = inputManager_->joyName(event.joyId);
Expand All @@ -475,7 +475,7 @@ namespace nCine
mapping.desc = mappings_[index].desc;

const std::uint8_t* g = joyGuid.data;
LOGI("Joystick mapping found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), also known as \"%s\"", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId, mappings_[index].name);
LOGI("Gamepad mapping found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), also known as \"%s\"", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId, mappings_[index].name);
}
}

Expand All @@ -489,7 +489,7 @@ namespace nCine
}

const std::uint8_t * g = joyGuid.data;
LOGI("Joystick mapping not found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), using Android default mapping", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);
LOGI("Gamepad mapping not found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), using Android default mapping", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);

mapping.isValid = true;

Expand Down Expand Up @@ -523,7 +523,7 @@ namespace nCine
mapping.desc = mappings_[index].desc;

const std::uint8_t* g = joyGuid.data;
LOGI("Joystick mapping found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d)", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);
LOGI("Gamepad mapping found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d)", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);
}
}

Expand All @@ -545,12 +545,12 @@ namespace nCine
mapping.desc = mappings_[index].desc;

const uint8_t* g = joyGuid.data;
LOGI("Joystick mapping not found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), using XInput mapping", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);
LOGI("Gamepad mapping not found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), using XInput mapping", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);
}

if (!mapping.isValid) {
const uint8_t* g = joyGuid.data;
LOGI("Joystick mapping not found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), please provide correct mapping in \"gamecontrollerdb.txt\" file, otherwise the joystick will not work properly", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);
LOGI("Gamepad mapping not found for \"%s\" [%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x] (%d), please provide correct mapping in \"gamecontrollerdb.txt\" file, otherwise the joystick will not work properly", joyName, g[0], g[1], g[2], g[3], g[4], g[5], g[6], g[7], g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15], event.joyId);
}
}
#endif
Expand All @@ -561,7 +561,7 @@ namespace nCine
void JoyMapping::OnJoyDisconnected(const JoyConnectionEvent& event)
{
#if defined(NCINE_INPUT_DEBUGGING)
LOGI("Controller disconnected - joyId: %d", event.joyId);
LOGI("Gamepad disconnected - joyId: %d", event.joyId);
#endif

#if defined(WITH_SDL)
Expand Down
4 changes: 2 additions & 2 deletions Sources/nCine/Input/JoyMappingDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <CommonBase.h>

#if defined(DEATH_TARGET_ANDROID)
# define SDL_JOYSTICK_ANDROID (1)
# define SDL_PLATFORM_ANDROID (1)
#elif defined(DEATH_TARGET_EMSCRIPTEN)
# define SDL_JOYSTICK_EMSCRIPTEN (1)
#elif defined(DEATH_TARGET_WINDOWS_RT)
Expand All @@ -22,7 +22,7 @@

static const char* ControllerMappings[] = {
// Additional nCine gamepad mappings
#ifdef SDL_JOYSTICK_ANDROID
#ifdef SDL_PLATFORM_ANDROID
"05000000791d000009000000cf7f3f00,NYKO PLAYPAD PRO,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,leftstick:b6,rightstick:b7,start:b8,back:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2",
"05000000c82d000001610000ffff3f00,8BitDo SN30 Pro,a:b1,b:b0,back:b13,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b10,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b9,rightx:a2,righty:a3,start:b12,x:b4,y:b3",
#endif
Expand Down

0 comments on commit 62d7fb6

Please sign in to comment.