Skip to content

Commit

Permalink
Fixed RenderBatcher & AudioStream, fixed ImGui `WantCaptureKeyboa…
Browse files Browse the repository at this point in the history
…rd`, set `STACK_SIZE` on Emscripten, adjusted MSVC build
  • Loading branch information
deathkiller committed Nov 16, 2023
1 parent d8bbeb0 commit df175ee
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 125 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Jazz² Resurrection is reimplementation of the game **Jazz Jackrabbit 2** releas

*Cache is recreated during intro cinematics on the first startup, so it can't be skipped.*

Alternatively, you can install it using [![Homebrew](https://img.shields.io/homebrew/cask/v/jazz2-resurrection?logo=homebrew&logoColor=ffffff&label=Homebrew&color=b56b2b)](https://formulae.brew.sh/cask/jazz2-resurrection) `brew install --cask jazz2-resurrection`

### Android
* Download the game
* Install `Jazz2.apk` or `Jazz2_x64.apk` on the device
Expand Down
18 changes: 18 additions & 0 deletions Sources/Shared/CommonBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,24 @@
# endif
#endif

/** @brief Mark an if condition as likely to happen */
#if (defined(DEATH_TARGET_GCC) && !defined(DEATH_TARGET_CLANG) && __GNUC__ >= 10) || (DEATH_CXX_STANDARD > 201703 && ((defined(DEATH_TARGET_CLANG) && !defined(DEATH_TARGET_APPLE_CLANG) && __clang_major__ >= 12) || (defined(DEATH_TARGET_MSVC) && _MSC_VER >= 1926)))
# define DEATH_LIKELY(...) (__VA_ARGS__) [[likely]]
#elif defined(DEATH_TARGET_GCC)
# define DEATH_LIKELY(...) (__builtin_expect((__VA_ARGS__), 1))
#else
# define DEATH_LIKELY(...) (__VA_ARGS__)
#endif

/** @brief Mark an if condition as unlikely to happen */
#if (defined(DEATH_TARGET_GCC) && !defined(DEATH_TARGET_CLANG) && __GNUC__ >= 10) || (DEATH_CXX_STANDARD > 201703 && ((defined(DEATH_TARGET_CLANG) && !defined(DEATH_TARGET_APPLE_CLANG) && __clang_major__ >= 12) || (defined(DEATH_TARGET_MSVC) && _MSC_VER >= 1926)))
# define DEATH_UNLIKELY(...) (__VA_ARGS__) [[unlikely]]
#elif defined(DEATH_TARGET_GCC)
# define DEATH_UNLIKELY(...) (__builtin_expect((__VA_ARGS__), 0))
#else
# define DEATH_UNLIKELY(...) (__VA_ARGS__)
#endif

/** @brief Passthrough */
#define DEATH_PASSTHROUGH(...) __VA_ARGS__
/** @brief No-op */
Expand Down
2 changes: 1 addition & 1 deletion Sources/nCine/Audio/AudioLoaderMpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace nCine
bytesPerSample_ = 2;
numChannels_ = 2;
frequency_ = device.nativeFrequency();
numSamples_ = -1;
numSamples_ = UINT32_MAX;
hasLoaded_ = true;
}

Expand Down
6 changes: 1 addition & 5 deletions Sources/nCine/Audio/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,7 @@ namespace nCine

frequency_ = audioLoader.frequency();
numSamples_ = audioLoader.numSamples();
if (numSamples_ >= 0) {
duration_ = float(numSamples_) / frequency_;
} else {
duration_ = -1.0;
}
duration_ = (numSamples_ == UINT32_MAX ? -1.0f : float(numSamples_) / frequency_);

audioReader_ = audioLoader.createReader();
audioReader_->setLooping(isLooping_);
Expand Down
2 changes: 1 addition & 1 deletion Sources/nCine/Audio/AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace nCine

/// Returns the size of the loaded buffer in bytes
inline unsigned long bufferSize() const {
return numSamples_ * numChannels_ * bytesPerSample_;
return (numSamples_ == UINT32_MAX ? UINT32_MAX : (numSamples_ * numChannels_ * bytesPerSample_));
}

/// Returns the number of samples in the streaming buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "ImGuiAndroidInput.h"
#include "AndroidJniHelper.h"
#include "../Application.h"
#include "../Input/ImGuiJoyMappedInput.h"
#include "../../Application.h"
#include "../../Input/ImGuiJoyMappedInput.h"

#include <imgui.h>

Expand Down
File renamed without changes.
181 changes: 95 additions & 86 deletions Sources/nCine/Graphics/ImGuiDebugOverlay.cpp

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions Sources/nCine/Graphics/ImGuiDebugOverlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ namespace nCine
};
};

bool disableAppInputEvents_;
IInputEventHandler* appInputHandler_;
bool lockOverlayPositions_;
bool showTopLeftOverlay_;
bool showTopRightOverlay_;
Expand Down
35 changes: 31 additions & 4 deletions Sources/nCine/Graphics/ImGuiDrawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "GL/GLDepthTest.h"
#include "GL/GLCullFace.h"
#include "../Application.h"
#include "../Input/IInputManager.h"

#include <imgui.h>

Expand All @@ -23,7 +24,7 @@
#elif defined(WITH_QT5)
# include "../Backends/ImGuiQt5Input.h"
#elif defined(DEATH_TARGET_ANDROID)
# include "../Backends/ImGuiAndroidInput.h"
# include "../Backends/Android/ImGuiAndroidInput.h"
#endif

#if defined(WITH_EMBEDDED_SHADERS)
Expand All @@ -36,10 +37,10 @@ using namespace Death::IO;
namespace nCine
{
ImGuiDrawing::ImGuiDrawing(bool withSceneGraph)
: withSceneGraph_(withSceneGraph), lastFrameWidth_(0), lastFrameHeight_(0), lastLayerValue_(0)
: withSceneGraph_(withSceneGraph), appInputHandler_(nullptr), lastFrameWidth_(0), lastFrameHeight_(0), lastLayerValue_(0)
{
ImGuiIO& io = ImGui::GetIO();
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;

#if defined(WITH_OPENGLES) || defined(DEATH_TARGET_EMSCRIPTEN)
io.BackendRendererName = "nCine_OpenGL_ES";
Expand Down Expand Up @@ -82,9 +83,22 @@ namespace nCine
#if defined(DEATH_TARGET_WINDOWS)
String systemFont = fs::CombinePath(fs::GetWindowsDirectory(), "Fonts\\SegoeUI.ttf");
if (fs::FileExists(systemFont)) {
io.Fonts->AddFontFromFileTTF(systemFont.data(), 17.0f);
// Include the most of european latin characters
static const ImWchar ranges[] = { 0x0020, 0x017E, 0 };
io.Fonts->AddFontFromFileTTF(systemFont.data(), 17.0f, nullptr, ranges);
}
#endif

// TODO: Add these lines to "ImGui::NavUpdateCancelRequest()"
/*
if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) {
if (g.NavWindow->NavLastIds[0] != 0) {
g.NavWindow->NavLastIds[0] = 0;
} else {
FocusWindow(NULL);
}
}
*/
}

ImGuiDrawing::~ImGuiDrawing()
Expand Down Expand Up @@ -144,6 +158,19 @@ namespace nCine
ImGui::EndFrame();
ImGui::Render();

ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureKeyboard) {
if (appInputHandler_ == nullptr) {
appInputHandler_ = IInputManager::handler();
IInputManager::setHandler(nullptr);
}
} else {
if (appInputHandler_ != nullptr) {
IInputManager::setHandler(appInputHandler_);
appInputHandler_ = nullptr;
}
}

draw(renderQueue);
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/nCine/Graphics/ImGuiDrawing.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace nCine
class GLBufferObject;
class RenderCommand;
class RenderQueue;
class IInputEventHandler;

/// The class the handles ImGui drawing
class ImGuiDrawing
Expand Down Expand Up @@ -43,6 +44,7 @@ namespace nCine
static const int UniformsBufferSize = 65;
unsigned char uniformsBuffer_[UniformsBufferSize];
std::unique_ptr<GLShaderUniforms> imguiShaderUniforms_;
IInputEventHandler* appInputHandler_;

int lastFrameWidth_;
int lastFrameHeight_;
Expand Down
8 changes: 4 additions & 4 deletions Sources/nCine/Graphics/RenderBatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace nCine
batchCommand->setType(refCommand->type());
#endif
instancesBlock = batchCommand->material().uniformBlock(Material::InstancesBlockName);
FATAL_ASSERT_MSG(instancesBlock != nullptr, "Batched shader does not have an %s uniform block", Material::InstancesBlockName);
FATAL_ASSERT_MSG(instancesBlock != nullptr, "Batched shader does not have an \"%s\" uniform block", Material::InstancesBlockName);

const unsigned long nonBlockUniformsSize = batchCommand->material().shaderProgram()->uniformsSize();
// Determine how much memory is needed by uniform blocks that are not for instances
Expand Down Expand Up @@ -170,9 +170,9 @@ namespace nCine
batchingWithIndices = true;
}

// Don't request more bytes than a UBO can hold
// Don't request more bytes than an instances block or an UBO can hold (also protects against big `RenderingSettings::maxBatchSize` values)
const unsigned long currentSize = nonBlockUniformsSize + nonInstancesBlocksSize + instancesBlockSize;
if (currentSize + singleInstanceBlockSize > UboMaxSize) {
if (instancesBlockSize + singleInstanceBlockSize > instancesBlock->size() || currentSize + singleInstanceBlockSize > UboMaxSize) {
break;
}

Expand Down Expand Up @@ -342,6 +342,7 @@ namespace nCine

++it;
}
instancesBlock->setUsedSize(instancesBlockOffset);

if (batchedShaderHasAttributes) {
batchCommand->geometry().releaseVertexPointer();
Expand All @@ -356,7 +357,6 @@ namespace nCine
batchCommand->material().setBlendingEnabled(refCommand->material().isBlendingEnabled());
batchCommand->material().setBlendingFactors(refCommand->material().srcBlendingFactor(), refCommand->material().destBlendingFactor());
batchCommand->setBatchSize((int)(nextStart - start));
batchCommand->material().uniformBlock(Material::InstancesBlockName)->setUsedSize(instancesBlockOffset);
batchCommand->setLayer(refCommand->layer());
batchCommand->setVisitOrder(refCommand->visitOrder());

Expand Down
2 changes: 1 addition & 1 deletion Sources/nCine/Graphics/RenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace nCine
{
namespace
{
static const char BatchSizeFormatString[] = "#define BATCH_SIZE (%d)\n#line 0\n";
static const char BatchSizeFormatString[] = "#ifndef BATCH_SIZE\n\t#define BATCH_SIZE (%d)\n#endif\n#line 0\n";

struct ShaderLoad
{
Expand Down
34 changes: 17 additions & 17 deletions cmake/ncine_compiler_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ if(WIN32)
# Link to Windows Sockets 2 library for HTTP requests
target_link_libraries(${NCINE_APP} PRIVATE ws2_32)

# Try to use VC-LTL library
# Try to use VC-LTL library (if not disabled)
if(DEATH_WITH_VC_LTL AND MSVC)
if(NOT VC_LTL_Root)
if(EXISTS "${NCINE_ROOT}/Libs/VC-LTL/_msvcrt.h")
Expand Down Expand Up @@ -140,6 +140,7 @@ if(EMSCRIPTEN)
"SHELL:-s DISABLE_EXCEPTION_CATCHING=1"
"SHELL:-s FORCE_FILESYSTEM=1"
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
"SHELL:-s STACK_SIZE=131072" # 128 Kb
"SHELL:-s MALLOC='emmalloc'"
"SHELL:-s LZ4=1"
"SHELL:--bind")
Expand Down Expand Up @@ -214,15 +215,15 @@ endif()
if(MSVC)
# Build with Multiple Processes and force UTF-8
target_compile_options(${NCINE_APP} PRIVATE /MP /utf-8)
# Always use the non debug version of the runtime library
# Always use the non-debug version of the runtime library
target_compile_options(${NCINE_APP} PUBLIC $<IF:$<BOOL:${VC_LTL_FOUND}>,/MT,/MD>)
# Disabling exceptions
# Disable exceptions
target_compile_definitions(${NCINE_APP} PRIVATE "_HAS_EXCEPTIONS=0")
target_compile_options(${NCINE_APP} PRIVATE /EHsc)
# Extra optimizations in release
# Extra optimizations in Release
target_compile_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:/fp:fast /O2 /Oi /Qpar /Gy>)
# Include PDB debug information in release
target_compile_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:/Zi>)
# Include PDB debug information in Release and enable hot reloading in Debug
target_compile_options(${NCINE_APP} PRIVATE $<IF:$<CONFIG:Debug>,/ZI,/Zi>)
target_link_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:/DEBUG:FULL>)
# Turn off SAFESEH because of OpenAL on x86 and also UAC
target_link_options(${NCINE_APP} PRIVATE /SAFESEH:NO /MANIFESTUAC:NO)
Expand All @@ -233,7 +234,7 @@ if(MSVC)
target_compile_options(${NCINE_APP} PRIVATE /arch:${NCINE_ARCH_EXTENSIONS})
endif()

# Enabling Whole Program Optimization
# Enable Whole Program Optimization
if(NCINE_LINKTIME_OPTIMIZATION)
target_compile_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:/GL>)
target_link_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:/LTCG>)
Expand All @@ -249,9 +250,8 @@ if(MSVC)
# "conversion from 'size_t' to '<smaller int type>', possible loss of data"
target_compile_options(${NCINE_APP} PUBLIC "/wd4244" "/wd4267")

# Disabling incremental linking and manifest generation
target_link_options(${NCINE_APP} PRIVATE $<$<CONFIG:Debug>:/MANIFEST:NO /INCREMENTAL:NO>)
target_link_options(${NCINE_APP} PRIVATE $<$<CONFIG:RelWithDebInfo>:/MANIFEST:NO /INCREMENTAL:NO>)
# Adjust incremental linking
target_link_options(${NCINE_APP} PRIVATE $<IF:$<CONFIG:Debug>,/INCREMENTAL,/INCREMENTAL:NO>)

if(NCINE_WITH_TRACY)
target_link_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:/DEBUG>)
Expand All @@ -272,13 +272,13 @@ else() # GCC and LLVM
target_link_options(${NCINE_APP} PUBLIC -municode)
endif()

# Only in debug - preserve debug information
# Only in Debug - preserve debug information
if(EMSCRIPTEN)
target_compile_options(${NCINE_APP} PUBLIC $<$<CONFIG:Debug>:-g>)
target_link_options(${NCINE_APP} PUBLIC $<$<CONFIG:Debug>:-g>)
endif()

# Only in debug
# Only in Debug
if(NCINE_ADDRESS_SANITIZER)
# Add ASan options as public so that targets linking the library will also use them
if(EMSCRIPTEN)
Expand All @@ -290,7 +290,7 @@ else() # GCC and LLVM
endif()
endif()

# Only in debug
# Only in Debug
if(NCINE_UNDEFINED_SANITIZER)
# Add UBSan options as public so that targets linking the library will also use them
if(EMSCRIPTEN)
Expand All @@ -302,7 +302,7 @@ else() # GCC and LLVM
endif()
endif()

# Only in debug
# Only in Debug
if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") AND NCINE_CODE_COVERAGE)
# Add code coverage options as public so that targets linking the library will also use them
target_compile_options(${NCINE_APP} PUBLIC $<$<CONFIG:Debug>:--coverage>)
Expand All @@ -329,7 +329,7 @@ else() # GCC and LLVM

target_compile_options(${NCINE_APP} PRIVATE $<$<CONFIG:Debug>:-fvar-tracking-assignments>)

# Extra optimizations in release
# Extra optimizations in Release
if(NINTENDO_SWITCH)
# -Ofast is crashing on Nintendo Switch for some reason, use -O2 instead
target_compile_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:-O2 -funsafe-loop-optimizations -ftree-loop-if-convert-stores>)
Expand Down Expand Up @@ -361,11 +361,11 @@ else() # GCC and LLVM
endif()

if(NOT EMSCRIPTEN)
# Extra optimizations in release
# Extra optimizations in Release
target_compile_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:-Ofast>)
endif()

# Enabling ThinLTO of Clang 4
# Enable ThinLTO of Clang 4
if(NCINE_LINKTIME_OPTIMIZATION)
if(EMSCRIPTEN)
target_compile_options(${NCINE_APP} PRIVATE $<$<CONFIG:Release>:-flto>)
Expand Down
4 changes: 2 additions & 2 deletions cmake/ncine_extra_sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ if(NCINE_WITH_IMGUI)
list(APPEND HEADERS ${NCINE_SOURCE_DIR}/nCine/Backends/ImGuiQt5Input.h)
list(APPEND SOURCES ${NCINE_SOURCE_DIR}/nCine/Backends/ImGuiQt5Input.cpp)
elseif(ANDROID)
list(APPEND HEADERS ${NCINE_SOURCE_DIR}/nCine/Backends/ImGuiAndroidInput.h)
list(APPEND SOURCES ${NCINE_SOURCE_DIR}/nCine/Backends/ImGuiAndroidInput.cpp)
list(APPEND HEADERS ${NCINE_SOURCE_DIR}/nCine/Backends/Android/ImGuiAndroidInput.h)
list(APPEND SOURCES ${NCINE_SOURCE_DIR}/nCine/Backends/Android/ImGuiAndroidInput.cpp)
endif()

list(APPEND HEADERS ${NCINE_SOURCE_DIR}/nCine/Graphics/ImGuiDebugOverlay.h)
Expand Down

0 comments on commit df175ee

Please sign in to comment.