Skip to content

Commit

Permalink
Fixed glfwSwapInterval, added /windowed parameter, better Linux pat…
Browse files Browse the repository at this point in the history
…h handling
  • Loading branch information
deathkiller committed Oct 9, 2022
1 parent bc93ebb commit b8b7cc6
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 75 deletions.
60 changes: 40 additions & 20 deletions Jazz2/Jazz2/ContentResolver.cpp
Expand Up @@ -40,6 +40,26 @@ namespace Jazz2
{
std::memset(_palettes, 0, sizeof(_palettes));

#if defined(DEATH_TARGET_UNIX)
_contentPath = "/usr/share/Jazz² Resurrection/Content/"_s;
if (fs::IsDirectory(_contentPath)) {
// Shared Content exists, try to use standard XDG paths
auto localStorage = fs::GetLocalStorage();
if (!localStorage.empty()) {
_cachePath = fs::JoinPath(localStorage, "Jazz² Resurrection/Cache/"_s);
_sourcePath = fs::JoinPath(localStorage, "Jazz² Resurrection/Source/"_s);
} else {
_cachePath = "Cache/"_s;
_sourcePath = "Source/"_s;
}
} else {
// Fallback to relative paths
_contentPath = "Content/"_s;
_cachePath = "Cache/"_s;
_sourcePath = "Source/"_s;
}
#endif

CompileShaders();
}

Expand Down Expand Up @@ -124,7 +144,7 @@ namespace Jazz2
}

// Try to load it
auto s = fs::Open(fs::JoinPath({ "Content"_s, "Metadata"_s, path + ".res"_s }), FileAccessMode::Read);
auto s = fs::Open(fs::JoinPath({ GetContentPath(), "Metadata"_s, path + ".res"_s }), FileAccessMode::Read);
auto fileSize = s->GetSize();
if (fileSize < 4 || fileSize > 64 * 1024 * 1024) {
// 64 MB file size limit
Expand Down Expand Up @@ -271,9 +291,9 @@ namespace Jazz2
continue;
}

String fullPath = fs::JoinPath({ "Content"_s, "Animations"_s, path });
String fullPath = fs::JoinPath({ GetContentPath(), "Animations"_s, path });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::JoinPath({ "Cache"_s, "Animations"_s, path });
fullPath = fs::JoinPath({ GetCachePath(), "Animations"_s, path });
if (!fs::IsReadableFile(fullPath)) {
continue;
}
Expand Down Expand Up @@ -307,7 +327,7 @@ namespace Jazz2
return RequestGraphicsAura(path, paletteOffset);
}

auto s = fs::Open(fs::JoinPath({ "Content"_s, "Animations"_s, path + ".res"_s }), FileAccessMode::Read);
auto s = fs::Open(fs::JoinPath({ GetContentPath(), "Animations"_s, path + ".res"_s }), FileAccessMode::Read);
auto fileSize = s->GetSize();
if (fileSize < 4 || fileSize > 64 * 1024 * 1024) {
// 64 MB file size limit, also if not found try to use cache
Expand All @@ -328,7 +348,7 @@ namespace Jazz2
std::unique_ptr<GenericGraphicResource> graphics = std::make_unique<GenericGraphicResource>();
graphics->Flags |= GenericGraphicResourceFlags::Referenced;

String fullPath = fs::JoinPath({ "Content"_s, "Animations"_s, path });
String fullPath = fs::JoinPath({ GetContentPath(), "Animations"_s, path });
std::unique_ptr<ITextureLoader> texLoader = ITextureLoader::createFromFile(fullPath);
if (texLoader->hasLoaded()) {
auto texFormat = texLoader->texFormat().internalFormat();
Expand Down Expand Up @@ -432,9 +452,9 @@ namespace Jazz2
GenericGraphicResource* ContentResolver::RequestGraphicsAura(const StringView& path, uint16_t paletteOffset)
{
// Try "Content" directory first, then "Cache" directory
String fullPath = fs::JoinPath({ "Content"_s, "Animations"_s, path });
String fullPath = fs::JoinPath({ GetContentPath(), "Animations"_s, path });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::JoinPath({ "Cache"_s, "Animations"_s, path });
fullPath = fs::JoinPath({ GetCachePath(), "Animations"_s, path });
}

auto s = fs::Open(fullPath, FileAccessMode::Read);
Expand Down Expand Up @@ -613,9 +633,9 @@ namespace Jazz2
std::unique_ptr<Tiles::TileSet> ContentResolver::RequestTileSet(const StringView& path, uint16_t captionTileId, bool applyPalette)
{
// Try "Content" directory first, then "Cache" directory
String fullPath = fs::JoinPath({ "Content"_s, "Tilesets"_s, path + ".j2t"_s });
String fullPath = fs::JoinPath({ GetContentPath(), "Tilesets"_s, path + ".j2t"_s });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::JoinPath({ "Cache"_s, "Tilesets"_s, path + ".j2t"_s });
fullPath = fs::JoinPath({ GetCachePath(), "Tilesets"_s, path + ".j2t"_s });
}

auto s = fs::Open(fullPath, FileAccessMode::Read);
Expand Down Expand Up @@ -720,9 +740,9 @@ namespace Jazz2
bool ContentResolver::LoadLevel(LevelHandler* levelHandler, const StringView& path, GameDifficulty difficulty)
{
// Try "Content" directory first, then "Cache" directory
String fullPath = fs::JoinPath({ "Content"_s, "Episodes"_s, path + ".j2l"_s });
String fullPath = fs::JoinPath({ GetContentPath(), "Episodes"_s, path + ".j2l"_s });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::JoinPath({ "Cache"_s, "Episodes"_s, path + ".j2l"_s });
fullPath = fs::JoinPath({ GetCachePath(), "Episodes"_s, path + ".j2l"_s });
}

auto s = fs::Open(fullPath, FileAccessMode::Read);
Expand Down Expand Up @@ -838,9 +858,9 @@ namespace Jazz2

std::optional<Episode> ContentResolver::GetEpisode(const StringView& name)
{
String fullPath = fs::JoinPath({ "Content"_s, "Episodes"_s, name + ".j2e"_s });
String fullPath = fs::JoinPath({ GetContentPath(), "Episodes"_s, name + ".j2e"_s });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::JoinPath({ "Cache"_s, "Episodes"_s, name + ".j2e"_s });
fullPath = fs::JoinPath({ GetCachePath(), "Episodes"_s, name + ".j2e"_s });
}
return GetEpisodeByPath(fullPath);
}
Expand Down Expand Up @@ -886,10 +906,10 @@ namespace Jazz2

std::unique_ptr<AudioStreamPlayer> ContentResolver::GetMusic(const StringView& path)
{
String fullPath = fs::JoinPath({ "Content"_s, "Music"_s, path });
String fullPath = fs::JoinPath({ GetContentPath(), "Music"_s, path });
if (!fs::IsReadableFile(fullPath)) {
// "Source" directory must be case in-sensitive
fullPath = fs::FindPathCaseInsensitive(fs::JoinPath("Source"_s, path));
fullPath = fs::FindPathCaseInsensitive(fs::JoinPath(GetSourcePath(), path));
}
if (fs::IsReadableFile(fullPath)) {
return std::make_unique<AudioStreamPlayer>(fullPath);
Expand All @@ -907,8 +927,8 @@ namespace Jazz2
auto& font = _fonts[(int)fontType];
if (font == nullptr) {
switch (fontType) {
case FontType::Small: font = std::make_unique<UI::Font>(fs::JoinPath({ "Content"_s, "Animations"_s, "UI"_s, "font_small.png"_s }), _palettes); break;
case FontType::Medium: font = std::make_unique<UI::Font>(fs::JoinPath({ "Content"_s, "Animations"_s, "UI"_s, "font_medium.png"_s }), _palettes); break;
case FontType::Small: font = std::make_unique<UI::Font>(fs::JoinPath({ GetContentPath(), "Animations"_s, "UI"_s, "font_small.png"_s }), _palettes); break;
case FontType::Medium: font = std::make_unique<UI::Font>(fs::JoinPath({ GetContentPath(), "Animations"_s, "UI"_s, "font_medium.png"_s }), _palettes); break;
default: return nullptr;
}
}
Expand Down Expand Up @@ -1041,12 +1061,12 @@ namespace Jazz2
#if _DEBUG
void ContentResolver::MigrateGraphics(const StringView& path)
{
String auraPath = fs::JoinPath({ "Content"_s, "Animations"_s, path.exceptSuffix(4) + ".aura"_s });
String auraPath = fs::JoinPath({ GetContentPath(), "Animations"_s, path.exceptSuffix(4) + ".aura"_s });
if (fs::IsFile(auraPath)) {
return;
}

auto s = fs::Open(fs::JoinPath({ "Content"_s, "Animations"_s, path + ".res"_s }), FileAccessMode::Read);
auto s = fs::Open(fs::JoinPath({ GetContentPath(), "Animations"_s, path + ".res"_s }), FileAccessMode::Read);
auto fileSize = s->GetSize();
if (fileSize < 4 || fileSize > 64 * 1024 * 1024) {
// 64 MB file size limit, also if not found try to use cache
Expand All @@ -1063,7 +1083,7 @@ namespace Jazz2
return;
}

String fullPath = fs::JoinPath({ "Content"_s, "Animations"_s, path });
String fullPath = fs::JoinPath({ GetContentPath(), "Animations"_s, path });
std::unique_ptr<ITextureLoader> texLoader = ITextureLoader::createFromFile(fullPath);
if (texLoader->hasLoaded()) {
auto texFormat = texLoader->texFormat().internalFormat();
Expand Down
30 changes: 30 additions & 0 deletions Jazz2/Jazz2/ContentResolver.h
Expand Up @@ -248,6 +248,30 @@ namespace Jazz2
return _palettes;
}

StringView GetContentPath() const {
#if defined(DEATH_TARGET_UNIX)
return _contentPath;
#else
return "Content/"_s;
#endif
}

StringView GetCachePath() const {
#if defined(DEATH_TARGET_UNIX)
return _cachePath;
#else
return "Cache/"_s;
#endif
}

StringView GetSourcePath() const {
#if defined(DEATH_TARGET_UNIX)
return _sourcePath;
#else
return "Source/"_s;
#endif
}

static ContentResolver& Current();

private:
Expand All @@ -271,5 +295,11 @@ namespace Jazz2
HashMap<Pair<String, uint16_t>, std::unique_ptr<GenericGraphicResource>> _cachedGraphics;
std::unique_ptr<UI::Font> _fonts[(int)FontType::Count];
std::unique_ptr<Shader> _precompiledShaders[(int)PrecompiledShader::Count];

#if defined(DEATH_TARGET_UNIX)
String _contentPath;
String _cachePath;
String _sourcePath;
#endif
};
}
2 changes: 0 additions & 2 deletions Jazz2/Jazz2/LevelHandler.cpp
Expand Up @@ -635,7 +635,6 @@ namespace Jazz2
std::shared_ptr<AudioBufferPlayer> LevelHandler::PlaySfx(AudioBuffer* buffer, const Vector3f& pos, bool sourceRelative, float gain, float pitch)
{
auto& player = _playingSounds.emplace_back(std::make_shared<AudioBufferPlayer>(buffer));
//player->setPosition(Vector3f((pos.X - _cameraPos.X) / (DefaultWidth * 3), (pos.Y - _cameraPos.Y) / (DefaultHeight * 3), 0.8f));
player->setPosition(Vector3f(pos.X, pos.Y, 100.0f));
player->setGain(gain * PreferencesCache::MasterVolume * PreferencesCache::SfxVolume);
player->setSourceRelative(sourceRelative);
Expand All @@ -657,7 +656,6 @@ namespace Jazz2
if (it != _commonResources->Sounds.end()) {
int idx = (it->second.Buffers.size() > 1 ? Random().Next(0, (int)it->second.Buffers.size()) : 0);
auto& player = _playingSounds.emplace_back(std::make_shared<AudioBufferPlayer>(it->second.Buffers[idx].get()));
//player->setPosition(Vector3f((pos.X - _cameraPos.X) / (DefaultWidth * 3), (pos.Y - _cameraPos.Y) / (DefaultHeight * 3), 0.8f));
player->setPosition(Vector3f(pos.X, pos.Y, 100.0f));
player->setGain(gain * PreferencesCache::MasterVolume * PreferencesCache::SfxVolume);

Expand Down
2 changes: 2 additions & 0 deletions Jazz2/Jazz2/PreferencesCache.cpp
Expand Up @@ -217,6 +217,8 @@ namespace Jazz2
AllowCheatsWeapons = true;
} else if (arg == "/fullscreen"_s) {
EnableFullscreen = true;
} else if (arg == "/windowed"_s) {
EnableFullscreen = false;
} else if (arg == "/no-vsync"_s) {
// V-Sync can be turned off only with command-line parameter
EnableVsync = false;
Expand Down
5 changes: 3 additions & 2 deletions Jazz2/Jazz2/Scripting/LevelScripts.cpp
Expand Up @@ -509,8 +509,9 @@ enum WeatherType {
const char* src = &path[1];
const char* srcLast = src;

std::memcpy(result, "Content", sizeof("Content") - 1);
char* dst = result + sizeof("Content") - 1;
const StringView& contentPath = ContentResolver::Current().GetContentPath();
std::memcpy(result, contentPath.data(), contentPath.size());
char* dst = result + contentPath.size();
char* dstStart = dst;
char* dstLast = dstStart;

Expand Down
5 changes: 3 additions & 2 deletions Jazz2/Jazz2/UI/Cinematics.cpp
Expand Up @@ -131,9 +131,10 @@ namespace Jazz2::UI
bool Cinematics::LoadFromFile(const String& path)
{
// Try "Content" directory first, then "Source" directory
String fullPath = fs::JoinPath({ "Content"_s, "Cinematics"_s, path + ".j2v" });
auto& resolver = ContentResolver::Current();
String fullPath = fs::JoinPath({ resolver.GetContentPath(), "Cinematics"_s, path + ".j2v" });
if (!fs::IsReadableFile(fullPath)) {
fullPath = fs::FindPathCaseInsensitive(fs::JoinPath("Source"_s, path + ".j2v"));
fullPath = fs::FindPathCaseInsensitive(fs::JoinPath(resolver.GetSourcePath(), path + ".j2v"));
}
if (!fs::IsReadableFile(fullPath)) {
return false;
Expand Down
6 changes: 4 additions & 2 deletions Jazz2/Jazz2/UI/Menu/EpisodeSelectSection.cpp
Expand Up @@ -15,8 +15,10 @@ namespace Jazz2::UI::Menu
_transitionTime(0.0f),
_shouldStart(false)
{
auto& resolver = ContentResolver::Current();

// Search both "Content/Episodes/" and "Cache/Episodes/"
fs::Directory dir(fs::JoinPath("Content"_s, "Episodes"_s), fs::EnumerationOptions::SkipDirectories);
fs::Directory dir(fs::JoinPath(resolver.GetContentPath(), "Episodes"_s), fs::EnumerationOptions::SkipDirectories);
while (true) {
StringView item = dir.GetNext();
if (item == nullptr) {
Expand All @@ -26,7 +28,7 @@ namespace Jazz2::UI::Menu
AddEpisode(item);
}

fs::Directory dirCache(fs::JoinPath("Cache"_s, "Episodes"_s), fs::EnumerationOptions::SkipDirectories);
fs::Directory dirCache(fs::JoinPath(resolver.GetCachePath(), "Episodes"_s), fs::EnumerationOptions::SkipDirectories);
while (true) {
StringView item = dirCache.GetNext();
if (item == nullptr) {
Expand Down
3 changes: 2 additions & 1 deletion Jazz2/Jazz2/UI/Menu/MainMenu.cpp
Expand Up @@ -474,7 +474,8 @@ namespace Jazz2::UI::Menu
}
}

auto s = fs::Open(fs::JoinPath({ "Content"_s, "Animations"_s, "main_menu.layer"_s }), FileAccessMode::Read);
auto& resolver = ContentResolver::Current();
auto s = fs::Open(fs::JoinPath({ resolver.GetContentPath(), "Animations"_s, "main_menu.layer"_s }), FileAccessMode::Read);
if (s->GetSize() < 8) {
return;
}
Expand Down

2 comments on commit b8b7cc6

@myself600
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...just when I was figuring out how to make path finding working :-)

@deathkiller
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux, it tries to find "/usr/share/Jazz² Resurrection/Content/" now, if it exists it will use following paths:

  • Content -> "/usr/share/Jazz² Resurrection/Content/"
  • Cache -> "$XDG_DATA_HOME/Jazz² Resurrection/Cache/" or "~/.local/share/Jazz² Resurrection/Cache/"
  • Source -> "$XDG_DATA_HOME/Jazz² Resurrection/Source/" or "~/.local/share/Jazz² Resurrection/Source/"

Otherwise it will use relative paths to current working directory as before.

Please sign in to comment.