Skip to content

Commit

Permalink
Quit button for PWA, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Dec 3, 2024
1 parent d355ba7 commit b01f3e0
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 112 deletions.
2 changes: 2 additions & 0 deletions Sources/Jazz2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@
<ClInclude Include="$(ExtensionLibraryPath)\IO\FileAccess.h" />
<ClInclude Include="$(ExtensionLibraryPath)\Base\IDisposable.h" />
<ClInclude Include="$(ExtensionLibraryPath)\Base\StackAlloc.h" />
<ClInclude Include="$(ExtensionLibraryPath)\IO\BoundedFileStream.h" />
<ClInclude Include="simdjson\simdjson.h" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -1038,6 +1039,7 @@
<ClCompile Include="$(ExtensionLibraryPath)\Containers\DateTime.cpp" />
<ClCompile Include="$(ExtensionLibraryPath)\IO\DeflateStream.cpp" />
<ClCompile Include="$(ExtensionLibraryPath)\Containers\StringUtils.cpp" />
<ClCompile Include="$(ExtensionLibraryPath)\IO\BoundedFileStream.cpp" />
<ClCompile Include="simdjson\simdjson.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions Sources/Jazz2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1287,9 +1287,6 @@
<ClInclude Include="simdjson\simdjson.h">
<Filter>Header Files\simdjson</Filter>
</ClInclude>
<ClInclude Include="nCine\Threading\Atomic.h">
<Filter>Header Files\nCine\Threading</Filter>
</ClInclude>
<ClInclude Include="$(ExtensionLibraryPath)\IO\HttpRequest.h">
<Filter>Header Files\Shared\IO</Filter>
</ClInclude>
Expand Down Expand Up @@ -1539,6 +1536,9 @@
<ClInclude Include="Jazz2\UI\Menu\HighscoresSection.h">
<Filter>Header Files\Jazz2\UI\Menu</Filter>
</ClInclude>
<ClInclude Include="$(ExtensionLibraryPath)\IO\BoundedFileStream.h">
<Filter>Header Files\Shared\IO</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down Expand Up @@ -2384,9 +2384,6 @@
<ClCompile Include="simdjson\simdjson.cpp">
<Filter>Source Files\simdjson</Filter>
</ClCompile>
<ClCompile Include="nCine\Threading\WindowsAtomic.cpp">
<Filter>Source Files\nCine\Threading</Filter>
</ClCompile>
<ClCompile Include="nCine\Graphics\BinaryShaderCache.cpp">
<Filter>Source Files\nCine\Graphics</Filter>
</ClCompile>
Expand Down Expand Up @@ -2525,6 +2522,9 @@
<ClCompile Include="Jazz2\UI\Menu\HighscoresSection.cpp">
<Filter>Source Files\Jazz2\UI\Menu</Filter>
</ClCompile>
<ClCompile Include="$(ExtensionLibraryPath)\IO\BoundedFileStream.cpp">
<Filter>Source Files\Shared\IO</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resources.rc">
Expand Down
8 changes: 8 additions & 0 deletions Sources/Jazz2/PreferencesCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ using namespace nCine;
namespace Jazz2
{
bool PreferencesCache::FirstRun = false;
#if defined(DEATH_TARGET_EMSCRIPTEN)
bool PreferencesCache::IsStandalone = false;
#endif
#if defined(WITH_MULTIPLAYER)
String PreferencesCache::InitialState;
#endif
Expand Down Expand Up @@ -393,6 +396,11 @@ namespace Jazz2
} else if (arg == "/reset-controls"_s) {
UI::ControlScheme::Reset();
}
# if defined(DEATH_TARGET_EMSCRIPTEN)
else if (arg == "/standalone"_s) {
IsStandalone = true;
}
# endif
# if defined(WITH_MULTIPLAYER)
else if (InitialState.empty() && (arg == "/server"_s || arg.hasPrefix("/connect:"_s))) {
InitialState = arg;
Expand Down
3 changes: 3 additions & 0 deletions Sources/Jazz2/PreferencesCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ namespace Jazz2
static constexpr std::int32_t UseVsync = -1;

static bool FirstRun;
#if defined(DEATH_TARGET_EMSCRIPTEN)
static bool IsStandalone;
#endif
#if defined(WITH_MULTIPLAYER)
static String InitialState;
#endif
Expand Down
8 changes: 6 additions & 2 deletions Sources/Jazz2/UI/Menu/BeginSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ namespace Jazz2::UI::Menu
_items.emplace_back(ItemData { Item::Options, _("Options") });
// TRANSLATORS: Menu item in main menu
_items.emplace_back(ItemData { Item::About, _("About") });
#if !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_IOS) && !defined(DEATH_TARGET_SWITCH)
#if !defined(DEATH_TARGET_IOS) && !defined(DEATH_TARGET_SWITCH)
# if defined(DEATH_TARGET_EMSCRIPTEN)
// Show quit button only in PWA/standalone environment
if (PreferencesCache::IsStandalone)
# endif
// TRANSLATORS: Menu item in main menu
_items.emplace_back(ItemData { Item::Quit, _("Quit") });
#endif
Expand Down Expand Up @@ -407,7 +411,7 @@ namespace Jazz2::UI::Menu
_root->PlaySfx("MenuSelect"_s, 0.6f);
_root->SwitchToSection<AboutSection>();
break;
#if !defined(DEATH_TARGET_EMSCRIPTEN) && !defined(DEATH_TARGET_IOS) && !defined(DEATH_TARGET_SWITCH)
#if !defined(DEATH_TARGET_IOS) && !defined(DEATH_TARGET_SWITCH)
case Item::Quit:
theApplication().Quit();
break;
Expand Down
86 changes: 86 additions & 0 deletions Sources/Shared/IO/BoundedFileStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "BoundedFileStream.h"

namespace Death { namespace IO {
//###==##====#=====--==~--~=~- --- -- - - - -

BoundedFileStream::BoundedFileStream(const Containers::StringView path, std::uint64_t offset, std::uint32_t size)
: BoundedFileStream(Containers::String{path}, offset, size)
{
}

BoundedFileStream::BoundedFileStream(Containers::String&& path, std::uint64_t offset, std::uint32_t size)
: _underlyingStream(path, FileAccess::Read), _offset(offset), _size(size)
{
_underlyingStream.Seek(static_cast<std::int64_t>(offset), SeekOrigin::Begin);
}

void BoundedFileStream::Dispose()
{
_underlyingStream.Dispose();
}

std::int64_t BoundedFileStream::Seek(std::int64_t offset, SeekOrigin origin)
{
std::int64_t newPos;
switch (origin) {
case SeekOrigin::Begin: newPos = _offset + offset; break;
case SeekOrigin::Current: newPos = _underlyingStream.GetPosition() + offset; break;
case SeekOrigin::End: newPos = _offset + _size + offset; break;
default: return Stream::OutOfRange;
}

if (newPos < static_cast<std::int64_t>(_offset) || newPos > static_cast<std::int64_t>(_offset + _size)) {
newPos = Stream::OutOfRange;
} else {
newPos = _underlyingStream.Seek(newPos, SeekOrigin::Begin);
if (newPos >= static_cast<std::int64_t>(_offset)) {
newPos -= _offset;
}
}
return newPos;
}

std::int64_t BoundedFileStream::GetPosition() const
{
return _underlyingStream.GetPosition() - static_cast<std::int64_t>(_offset);
}

std::int64_t BoundedFileStream::Read(void* destination, std::int64_t bytesToRead)
{
if (bytesToRead <= 0) {
return 0;
}

DEATH_ASSERT(destination != nullptr, "destination is null", 0);

std::int64_t pos = _underlyingStream.GetPosition() - _offset;
if (bytesToRead > _size - pos) {
bytesToRead = _size - pos;
}

return _underlyingStream.Read(destination, bytesToRead);
}

std::int64_t BoundedFileStream::Write(const void* source, std::int64_t bytesToWrite)
{
// Not supported
return Stream::Invalid;
}

bool BoundedFileStream::Flush()
{
// Not supported
return true;
}

bool BoundedFileStream::IsValid()
{
return _underlyingStream.IsValid();
}

std::int64_t BoundedFileStream::GetSize() const
{
return _size;
}

}}
32 changes: 32 additions & 0 deletions Sources/Shared/IO/BoundedFileStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "FileStream.h"

namespace Death { namespace IO {
//###==##====#=====--==~--~=~- --- -- - - - -

class BoundedFileStream : public Stream
{
public:
BoundedFileStream(const Containers::StringView path, std::uint64_t offset, std::uint32_t size);
BoundedFileStream(Containers::String&& path, std::uint64_t offset, std::uint32_t size);

BoundedFileStream(const BoundedFileStream&) = delete;
BoundedFileStream& operator=(const BoundedFileStream&) = delete;

void Dispose() override;
std::int64_t Seek(std::int64_t offset, SeekOrigin origin) override;
std::int64_t GetPosition() const override;
std::int64_t Read(void* destination, std::int64_t bytesToRead) override;
std::int64_t Write(const void* source, std::int64_t bytesToWrite) override;
bool Flush() override;
bool IsValid() override;
std::int64_t GetSize() const override;

private:
FileStream _underlyingStream;
std::uint64_t _offset;
std::uint64_t _size;
};

}}
103 changes: 3 additions & 100 deletions Sources/Shared/IO/PakFile.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "PakFile.h"
#include "BoundedFileStream.h"
#include "DeflateStream.h"
#include "FileSystem.h"
#include "../Containers/GrowableArray.h"
Expand All @@ -11,104 +12,6 @@ using namespace Death::Containers;
namespace Death { namespace IO {
//###==##====#=====--==~--~=~- --- -- - - - -

class BoundedStream : public Stream
{
public:
BoundedStream(const String& path, std::uint64_t offset, std::uint32_t size);

BoundedStream(const BoundedStream&) = delete;
BoundedStream& operator=(const BoundedStream&) = delete;

void Dispose() override;
std::int64_t Seek(std::int64_t offset, SeekOrigin origin) override;
std::int64_t GetPosition() const override;
std::int64_t Read(void* destination, std::int64_t bytesToRead) override;
std::int64_t Write(const void* source, std::int64_t bytesToWrite) override;
bool Flush() override;
bool IsValid() override;
std::int64_t GetSize() const override;

private:
FileStream _underlyingStream;
std::uint64_t _offset;
std::uint64_t _size;
};

BoundedStream::BoundedStream(const String& path, std::uint64_t offset, std::uint32_t size)
: _underlyingStream(path, FileAccess::Read), _offset(offset), _size(size)
{
_underlyingStream.Seek(static_cast<std::int64_t>(offset), SeekOrigin::Begin);
}

void BoundedStream::Dispose()
{
_underlyingStream.Dispose();
}

std::int64_t BoundedStream::Seek(std::int64_t offset, SeekOrigin origin)
{
std::int64_t newPos;
switch (origin) {
case SeekOrigin::Begin: newPos = _offset + offset; break;
case SeekOrigin::Current: newPos = _underlyingStream.GetPosition() + offset; break;
case SeekOrigin::End: newPos = _offset + _size + offset; break;
default: return Stream::OutOfRange;
}

if (newPos < static_cast<std::int64_t>(_offset) || newPos > static_cast<std::int64_t>(_offset + _size)) {
newPos = Stream::OutOfRange;
} else {
newPos = _underlyingStream.Seek(newPos, SeekOrigin::Begin);
if (newPos >= static_cast<std::int64_t>(_offset)) {
newPos -= _offset;
}
}
return newPos;
}

std::int64_t BoundedStream::GetPosition() const
{
return _underlyingStream.GetPosition() - static_cast<std::int64_t>(_offset);
}

std::int64_t BoundedStream::Read(void* destination, std::int64_t bytesToRead)
{
if (bytesToRead <= 0) {
return 0;
}

DEATH_ASSERT(destination != nullptr, "destination is null", 0);

std::int64_t pos = _underlyingStream.GetPosition() - _offset;
if (bytesToRead > _size - pos) {
bytesToRead = _size - pos;
}

return _underlyingStream.Read(destination, bytesToRead);
}

std::int64_t BoundedStream::Write(const void* source, std::int64_t bytesToWrite)
{
// Not supported
return Stream::Invalid;
}

bool BoundedStream::Flush()
{
// Not supported
return true;
}

bool BoundedStream::IsValid()
{
return _underlyingStream.IsValid();
}

std::int64_t BoundedStream::GetSize() const
{
return _size;
}

#if defined(WITH_ZLIB) || defined(WITH_MINIZ)

class ZlibCompressedBoundedStream : public Stream
Expand All @@ -129,7 +32,7 @@ namespace Death { namespace IO {
std::int64_t GetSize() const override;

private:
BoundedStream _underlyingStream;
BoundedFileStream _underlyingStream;
DeflateStream _deflateStream;
std::int64_t _uncompressedSize;
};
Expand Down Expand Up @@ -311,7 +214,7 @@ namespace Death { namespace IO {
#endif
}

return std::make_unique<BoundedStream>(_path, foundItem->Offset, foundItem->UncompressedSize);
return std::make_unique<BoundedFileStream>(_path, foundItem->Offset, foundItem->UncompressedSize);
}

PakFile::Item* PakFile::FindItem(StringView path)
Expand Down
5 changes: 5 additions & 0 deletions Sources/nCine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ namespace nCine
return ((!hasFocus_ && autoSuspension_) || isSuspended_);
}

void Application::Quit()
{
shouldQuit_ = true;
}

void Application::PreInitCommon(std::unique_ptr<IAppEventHandler> appEventHandler)
{
#if defined(DEATH_TRACE)
Expand Down
5 changes: 2 additions & 3 deletions Sources/nCine/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ namespace nCine
}

/** @brief Raises the quit flag */
inline void Quit() {
shouldQuit_ = true;
}
virtual void Quit();

/** @brief Returns the quit flag value */
inline bool ShouldQuit() const {
return shouldQuit_;
Expand Down
2 changes: 1 addition & 1 deletion Sources/nCine/Graphics/ImGuiDebugOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ namespace nCine
theApplication().SetAutoSuspension(!disableAutoSuspension);
/*ImGui::SameLine();
if (ImGui::Button("Quit")) {
theApplication().quit();
theApplication().Quit();
}*/

guiConfigureGui();
Expand Down
Loading

0 comments on commit b01f3e0

Please sign in to comment.