diff --git a/Makefile b/Makefile index 8571424..c19d641 100644 --- a/Makefile +++ b/Makefile @@ -7,18 +7,23 @@ CXX = c++ AR = ar ARFLAGS = rcs RM = rm -f -INSTALL = install -PREFIX = /usr/local -BINDIR = $(PREFIX)/bin -DESTDIR = -DATA_DIR = ./data +INSTALL = install +PREFIX = /usr/local +BINDIR = $(PREFIX)/bin +DATADIR = $(PREFIX)/share +PKGDATADIR = $(DATADIR)/$(TARGET) +APPLICATIONSDIR = $(DATADIR)/applications +ICONDIR = $(DATADIR)/icons/hicolor +DESTDIR = +DATA_DIR = ./data STDFLAGS = -std=c++20 -Isrc WARNFLAGS = -Wall -Wextra -Wpedantic -Wno-missing-field-initializers RELEASE_FLAGS = -O2 -DNDEBUG DEBUG_FLAGS = -g -O0 OPTFLAGS = $(RELEASE_FLAGS) -CXXFLAGS = $(STDFLAGS) $(WARNFLAGS) $(OPTFLAGS) +INSTALL_DEFS = -DHIOCTANE_INSTALL_DATA_DIR=\"$(PKGDATADIR)/data\" +CXXFLAGS = $(STDFLAGS) $(WARNFLAGS) $(OPTFLAGS) $(INSTALL_DEFS) LDFLAGS = TEST_DEFS = -DHIOCTANE_DEFAULT_DATA_DIR=\"$(DATA_DIR)\" @@ -170,9 +175,77 @@ smoke: release install: release $(INSTALL) -d "$(DESTDIR)$(BINDIR)" $(INSTALL) -m 755 $(TARGET) "$(DESTDIR)$(BINDIR)/$(TARGET)" + @if [ -d "$(DATA_DIR)" ]; then \ + $(INSTALL) -d "$(DESTDIR)$(PKGDATADIR)/data" && \ + dest_data=$$(cd "$(DESTDIR)$(PKGDATADIR)/data" && pwd) && \ + ( \ + set -e; \ + cd "$(DATA_DIR)"; \ + find . -type d | LC_ALL=C sort | while IFS= read -r d; do \ + rel="$${d#./}"; \ + [ "$$rel" = "." ] && continue; \ + $(INSTALL) -d "$$dest_data/$$rel"; \ + done; \ + find . -type f | LC_ALL=C sort | while IFS= read -r f; do \ + rel="$${f#./}"; \ + $(INSTALL) -m 644 "$$f" "$$dest_data/$$rel"; \ + done; \ + ); \ + fi + $(INSTALL) -d "$(DESTDIR)$(APPLICATIONSDIR)" + @bin_path="$(BINDIR)/$(TARGET)"; \ + esc_bin=$$(printf '%s\n' "$$bin_path" | sed 's/[&|\\]/\\&/g'); \ + case "$$bin_path" in \ + *" "*|*" "*) exec_val="\"$$esc_bin\"" ;; \ + *) exec_val="$$esc_bin" ;; \ + esac; \ + tmp_desktop=$$(mktemp "$${TMPDIR:-/tmp}/$(TARGET).desktop.XXXXXX"); \ + rc=0; \ + sed -e "s|^Exec=.*|Exec=$$exec_val|" -e "s|^TryExec=.*|TryExec=$$esc_bin|" \ + "xdg/$(TARGET).desktop" > "$$tmp_desktop" && \ + $(INSTALL) -m 644 "$$tmp_desktop" "$(DESTDIR)$(APPLICATIONSDIR)/$(TARGET).desktop" || rc=$$?; \ + rm -f "$$tmp_desktop"; \ + exit $$rc + $(INSTALL) -d "$(DESTDIR)$(ICONDIR)/scalable/apps" + $(INSTALL) -m 644 "xdg/icons/scalable/$(TARGET).svg" "$(DESTDIR)$(ICONDIR)/scalable/apps/$(TARGET).svg" + @set -e; for sz in 48x48 64x64 128x128 256x256; do \ + $(INSTALL) -d "$(DESTDIR)$(ICONDIR)/$$sz/apps"; \ + $(INSTALL) -m 644 "xdg/icons/$$sz/$(TARGET).png" "$(DESTDIR)$(ICONDIR)/$$sz/apps/$(TARGET).png"; \ + done + @if [ -z "$(DESTDIR)" ]; then \ + if command -v update-desktop-database >/dev/null 2>&1; then \ + update-desktop-database "$(APPLICATIONSDIR)" >/dev/null 2>&1 || true; \ + fi; \ + if command -v gtk-update-icon-cache >/dev/null 2>&1; then \ + gtk-update-icon-cache -f -t "$(ICONDIR)" >/dev/null 2>&1 || true; \ + fi; \ + fi uninstall: $(RM) "$(DESTDIR)$(BINDIR)/$(TARGET)" + $(RM) "$(DESTDIR)$(APPLICATIONSDIR)/$(TARGET).desktop" + $(RM) "$(DESTDIR)$(ICONDIR)/scalable/apps/$(TARGET).svg" + @for sz in 48x48 64x64 128x128 256x256; do \ + $(RM) "$(DESTDIR)$(ICONDIR)/$$sz/apps/$(TARGET).png"; \ + done + rm -rf "$(DESTDIR)$(PKGDATADIR)/data" + @rmdir "$(DESTDIR)$(PKGDATADIR)" 2>/dev/null || true + @if [ -z "$(DESTDIR)" ]; then \ + if command -v update-desktop-database >/dev/null 2>&1; then \ + update-desktop-database "$(APPLICATIONSDIR)" >/dev/null 2>&1 || true; \ + fi; \ + if command -v gtk-update-icon-cache >/dev/null 2>&1; then \ + gtk-update-icon-cache -f -t "$(ICONDIR)" >/dev/null 2>&1 || true; \ + fi; \ + fi + +install-user: release + @xdg_data="$${XDG_DATA_HOME:-$$HOME/.local/share}"; \ + $(MAKE) DESTDIR="$(DESTDIR)" PREFIX="$$HOME/.local" DATADIR="$$xdg_data" install + +uninstall-user: + @xdg_data="$${XDG_DATA_HOME:-$$HOME/.local/share}"; \ + $(MAKE) DESTDIR="$(DESTDIR)" PREFIX="$$HOME/.local" DATADIR="$$xdg_data" uninstall clean: $(RM) $(ALL_OBJS) $(ALL_LIBS) $(TARGET) $(TEST_BINS) .buildmode diff --git a/README.md b/README.md index 5ad85a5..5109def 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ A modern, cross-platform **SDL2 + OpenGL 3.3 Core** C++20 source port of Bullfro - POSIX `make` - `SDL2` development libraries & headers (`pkg-config` or `sdl2-config`) - OpenGL 3.3+ drivers / system headers -- Essential game assets in `./data` (default lookup; can also be overridden via `--data-dir` or `HIOCTANE_DATA`, with `./HiOctane` supported as a legacy fallback). Optional `DATA/INTRO.DAT` is excluded by default so the game boots cleanly to the title screen. +- Essential game assets in `./data` (default lookup; also searches relocatable `../share/top-octane/data` and XDG `XDG_DATA_HOME`/`XDG_DATA_DIRS` directories when installed, or can be overridden via `--data-dir` or `HIOCTANE_DATA`, with `./HiOctane` supported as a legacy fallback). Optional `DATA/INTRO.DAT` is excluded by default so the game boots cleanly to the title screen. ## Building & running @@ -27,7 +27,8 @@ make smoke # runs `./top-octane --smoke-test 30` Other make targets: - `make debug`: builds `./top-octane` with `-g -O0` (automatically cleans stale release objects). -- `make install` / `make uninstall`: installs/removes `top-octane` in `$(DESTDIR)$(PREFIX)/bin` (default `PREFIX=/usr/local`). +- `make install` / `make uninstall`: installs/removes `top-octane`, game assets (`$(PREFIX)/share/top-octane/data`), XDG desktop shortcut (`$(PREFIX)/share/applications/top-octane.desktop`, **Games** menu group), and hicolor icons (`scalable`, `48x48`–`256x256`) into `$(DESTDIR)$(PREFIX)` (default `PREFIX=/usr/local`). +- `make install-user` / `make uninstall-user`: installs/removes per-user (rootless) into `~/.local/bin` and `${XDG_DATA_HOME:-~/.local/share}` (adds **Top-Octane** to the desktop **Games** menu for the current user). - `make clean`: removes built objects, libraries, binaries, and `.buildmode`. ### Windows (MSYS2 / MinGW) @@ -38,7 +39,7 @@ make test ``` ## CLI options -- `--data-dir `: Path to the read-only assets directory (default: `./data`; also reads `HIOCTANE_DATA` env var). +- `--data-dir `: Path to the read-only assets directory (default: `./data` or installed/XDG data dirs; also reads `HIOCTANE_DATA` env var). - `--track <1..6>`: Jump directly into a race on track 1–6. - `--track-viewer <1..6>`: Open interactive 3D track inspection camera. - `--classic`: Launch in 4:3 classic palette visual mode. diff --git a/src/core/vfs.cpp b/src/core/vfs.cpp index cc96740..926d6b0 100644 --- a/src/core/vfs.cpp +++ b/src/core/vfs.cpp @@ -38,6 +38,20 @@ std::vector split_rel_path(const std::string& rel) { return parts; } +std::filesystem::path strip_trailing_sep(std::filesystem::path p) { + while (p.has_relative_path() && p.filename().empty()) { + p = p.parent_path(); + } + return p; +} + +void append_unique(std::vector& out, const std::filesystem::path& p) { + if (p.empty()) return; + if (std::find(out.begin(), out.end(), p) == out.end()) { + out.push_back(p); + } +} + bool is_valid_hioctane_dir(const std::filesystem::path& p) { std::error_code ec; if (!std::filesystem::is_directory(p, ec)) { @@ -58,18 +72,100 @@ bool is_valid_hioctane_dir(const std::filesystem::path& p) { } // namespace -bool Vfs::init(const std::string& explicit_data_dir) { +std::vector Vfs::build_search_paths( + const std::string& explicit_data_dir, + const std::filesystem::path& cwd, + const std::filesystem::path& base_dir, + const std::filesystem::path& pref_dir) { std::vector candidates; if (!explicit_data_dir.empty()) { - candidates.emplace_back(explicit_data_dir); + append_unique(candidates, std::filesystem::path(explicit_data_dir)); } if (const char* env_dir = std::getenv("HIOCTANE_DATA")) { if (*env_dir) { - candidates.emplace_back(env_dir); + append_unique(candidates, std::filesystem::path(env_dir)); } } + const std::filesystem::path clean_cwd = strip_trailing_sep(cwd); + const std::filesystem::path clean_base = strip_trailing_sep(base_dir); + + auto add_folder_candidates = [&](const char* folder_name) { + if (!clean_cwd.empty()) { + append_unique(candidates, clean_cwd / folder_name); + if (clean_cwd.has_parent_path() && clean_cwd.parent_path() != clean_cwd) { + append_unique(candidates, clean_cwd.parent_path() / folder_name); + } + } + if (!clean_base.empty()) { + append_unique(candidates, clean_base / folder_name); + if (clean_base.has_parent_path() && clean_base.parent_path() != clean_base) { + append_unique(candidates, clean_base.parent_path() / folder_name); + } + } + }; + add_folder_candidates("data"); + add_folder_candidates("HiOctane"); + + // Relocatable installed prefix paths (/bin/top-octane -> /share[/games]/top-octane[/data]) + if (!clean_base.empty() && clean_base.has_parent_path() && clean_base.parent_path() != clean_base) { + const std::filesystem::path prefix = clean_base.parent_path(); + append_unique(candidates, prefix / "share" / "top-octane" / "data"); + append_unique(candidates, prefix / "share" / "top-octane"); + append_unique(candidates, prefix / "share" / "games" / "top-octane" / "data"); + append_unique(candidates, prefix / "share" / "games" / "top-octane"); + } + +#ifndef _WIN32 + // XDG Base Directory Specification asset lookup (XDG_DATA_HOME, then XDG_DATA_DIRS) + std::filesystem::path xdg_data_home; + if (const char* xdh = std::getenv("XDG_DATA_HOME"); xdh && xdh[0] == '/') { + xdg_data_home = strip_trailing_sep(std::filesystem::path(xdh)); + } else if (const char* home = std::getenv("HOME"); home && *home) { + xdg_data_home = strip_trailing_sep(std::filesystem::path(home)) / ".local" / "share"; + } + if (!xdg_data_home.empty()) { + append_unique(candidates, xdg_data_home / "top-octane" / "data"); + append_unique(candidates, xdg_data_home / "top-octane"); + } + + const char* xdd_env = std::getenv("XDG_DATA_DIRS"); + const std::string xdd = (xdd_env && *xdd_env) ? std::string(xdd_env) : std::string("/usr/local/share:/usr/share"); + std::stringstream ss(xdd); + std::string entry; + while (std::getline(ss, entry, ':')) { + if (entry.empty() || entry[0] != '/') continue; + const std::filesystem::path base = strip_trailing_sep(std::filesystem::path(entry)); + append_unique(candidates, base / "top-octane" / "data"); + append_unique(candidates, base / "top-octane"); + append_unique(candidates, base / "games" / "top-octane" / "data"); + append_unique(candidates, base / "games" / "top-octane"); + } +#endif + +#ifdef HIOCTANE_INSTALL_DATA_DIR + if (HIOCTANE_INSTALL_DATA_DIR[0] != '\0') { + append_unique(candidates, std::filesystem::path(HIOCTANE_INSTALL_DATA_DIR)); + } +#endif + + if (!pref_dir.empty()) { + append_unique(candidates, strip_trailing_sep(pref_dir) / "data"); + } + + return candidates; +} + +bool Vfs::init(const std::string& explicit_data_dir) { std::error_code ec; + if (char* pref = SDL_GetPrefPath("top-octane", "TopOctane")) { + pref_dir_ = pref; + SDL_free(pref); + } else { + pref_dir_ = std::filesystem::current_path(ec) / "save"; + } + std::filesystem::create_directories(pref_dir_, ec); + std::filesystem::path cwd = std::filesystem::current_path(ec); std::filesystem::path base_dir; if (char* base_p = SDL_GetBasePath()) { @@ -77,19 +173,7 @@ bool Vfs::init(const std::string& explicit_data_dir) { SDL_free(base_p); } - auto add_folder_candidates = [&](const char* folder_name) { - if (!cwd.empty()) { - candidates.emplace_back(cwd / folder_name); - candidates.emplace_back(cwd.parent_path() / folder_name); - } - if (!base_dir.empty()) { - candidates.emplace_back(base_dir / folder_name); - candidates.emplace_back(base_dir.parent_path() / folder_name); - } - }; - add_folder_candidates("data"); - add_folder_candidates("HiOctane"); - + const auto candidates = build_search_paths(explicit_data_dir, cwd, base_dir, pref_dir_); data_root_.clear(); for (const auto& c : candidates) { if (is_valid_hioctane_dir(c)) { @@ -99,16 +183,8 @@ bool Vfs::init(const std::string& explicit_data_dir) { } } - if (char* pref = SDL_GetPrefPath("top-octane", "TopOctane")) { - pref_dir_ = pref; - SDL_free(pref); - } else { - pref_dir_ = std::filesystem::current_path() / "save"; - } - std::filesystem::create_directories(pref_dir_, ec); - if (data_root_.empty()) { - LOG_ERROR("Could not locate Top-Octane asset directory (expected ./data or --data-dir)"); + LOG_ERROR("Could not locate Top-Octane asset directory (checked --data-dir, HIOCTANE_DATA, ./data, and XDG data dirs)"); return false; } LOG_INFO("Using read-only asset directory: %s", data_root_.string().c_str()); diff --git a/src/core/vfs.hpp b/src/core/vfs.hpp index 7153245..2d4d5f1 100644 --- a/src/core/vfs.hpp +++ b/src/core/vfs.hpp @@ -9,9 +9,16 @@ namespace top_octane { class Vfs { public: - // Initializes read-only asset lookup from explicit path, HIOCTANE_DATA env, ./data (default), or ./HiOctane fallback + // Initializes read-only asset lookup from explicit path, HIOCTANE_DATA env, ./data, ./HiOctane, or installed/XDG data dirs bool init(const std::string& explicit_data_dir = ""); + // Builds prioritized candidate directories to search for Hi-Octane assets (GAME DATA + MAPS). + static std::vector build_search_paths( + const std::string& explicit_data_dir, + const std::filesystem::path& cwd, + const std::filesystem::path& base_dir, + const std::filesystem::path& pref_dir); + const std::filesystem::path& data_root() const { return data_root_; } const std::filesystem::path& pref_dir() const { return pref_dir_; } diff --git a/src/game/game_app.cpp b/src/game/game_app.cpp index f0376b6..704793c 100644 --- a/src/game/game_app.cpp +++ b/src/game/game_app.cpp @@ -207,6 +207,12 @@ bool GameApp::init(const LaunchOptions& opts) { hiscores_.load(vfs_.pref_dir() / "hiscores.dat"); +#ifdef SDL_HINT_APP_NAME + SDL_SetHint(SDL_HINT_APP_NAME, "Top-Octane"); +#endif + SDL_SetHint("SDL_VIDEO_X11_WMCLASS", "top-octane"); + SDL_SetHint("SDL_VIDEO_WAYLAND_WMCLASS", "top-octane"); + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) { LOG_ERROR("SDL_Init failed: %s", SDL_GetError()); return false; diff --git a/tests/test_assets.cpp b/tests/test_assets.cpp index 42748db..aaa6d93 100644 --- a/tests/test_assets.cpp +++ b/tests/test_assets.cpp @@ -10,9 +10,12 @@ #include "core/config.hpp" #include "core/vfs.hpp" +#include #include +#include #include #include +#include using namespace top_octane; using namespace top_octane::assets; @@ -236,5 +239,67 @@ int main() { CHECK(sf_bank.find_preset(0, 0) != nullptr); std::cout << "[PASS] MUSIC.DAT (20 XMI tracks), SAMPLE.OPL (162 timbres), BULLFROG.SBK (17 presets) validated\n"; + // 8. VFS relocatable prefix & XDG Base Directory search path order + { + auto find_idx = [](const std::vector& list, const std::filesystem::path& target) -> ptrdiff_t { + auto it = std::find(list.begin(), list.end(), target); + return (it == list.end()) ? -1 : std::distance(list.begin(), it); + }; + +#ifndef _WIN32 + struct ScopedEnv { + const char* key; + std::optional prev; + ScopedEnv(const char* k, const char* val) : key(k) { + if (const char* old = std::getenv(k)) prev = std::string(old); + if (val) setenv(k, val, 1); + else unsetenv(k); + } + ~ScopedEnv() { + if (prev.has_value()) setenv(key, prev->c_str(), 1); + else unsetenv(key); + } + }; + ScopedEnv env_ho("HIOCTANE_DATA", nullptr); + ScopedEnv env_xdh("XDG_DATA_HOME", "/tmp/top-octane-xdg-home/"); + ScopedEnv env_xdd("XDG_DATA_DIRS", "/tmp/top-octane-xdg1:/tmp/top-octane-xdg2/"); +#endif + + auto paths = Vfs::build_search_paths( + "/explicit/data", + std::filesystem::path("/work/dir/"), + std::filesystem::path("/opt/test-prefix/bin/"), + std::filesystem::path("/tmp/pref/")); + + CHECK(!paths.empty()); + CHECK(paths.front() == std::filesystem::path("/explicit/data")); + + const ptrdiff_t prefix_idx = find_idx(paths, std::filesystem::path("/opt/test-prefix/share/top-octane/data")); + const ptrdiff_t pref_idx = find_idx(paths, std::filesystem::path("/tmp/pref/data")); + CHECK(prefix_idx >= 0); + CHECK(pref_idx > prefix_idx); + +#ifndef _WIN32 + const ptrdiff_t xdh_idx = find_idx(paths, std::filesystem::path("/tmp/top-octane-xdg-home/top-octane/data")); + const ptrdiff_t xdd1_idx = find_idx(paths, std::filesystem::path("/tmp/top-octane-xdg1/top-octane/data")); + const ptrdiff_t xdd2_idx = find_idx(paths, std::filesystem::path("/tmp/top-octane-xdg2/top-octane/data")); + CHECK(xdh_idx > prefix_idx); + CHECK(xdd1_idx > xdh_idx); + CHECK(xdd2_idx > xdd1_idx); + CHECK(pref_idx > xdd2_idx); + + ScopedEnv env_ho_set("HIOCTANE_DATA", "/env/hioctane"); + auto prio_paths = Vfs::build_search_paths( + "/cli/data", + std::filesystem::path("/work/dir"), + std::filesystem::path("/opt/test-prefix/bin/"), + std::filesystem::path()); + CHECK(prio_paths.size() >= 2); + CHECK(prio_paths[0] == std::filesystem::path("/cli/data")); + CHECK(prio_paths[1] == std::filesystem::path("/env/hioctane")); +#endif + std::cout << "[PASS] Vfs relocatable & XDG data directory lookup validated\n"; + } + return 0; } diff --git a/xdg/icons/128x128/top-octane.png b/xdg/icons/128x128/top-octane.png new file mode 100644 index 0000000..cce12c9 Binary files /dev/null and b/xdg/icons/128x128/top-octane.png differ diff --git a/xdg/icons/256x256/top-octane.png b/xdg/icons/256x256/top-octane.png new file mode 100644 index 0000000..8b1d632 Binary files /dev/null and b/xdg/icons/256x256/top-octane.png differ diff --git a/xdg/icons/48x48/top-octane.png b/xdg/icons/48x48/top-octane.png new file mode 100644 index 0000000..e6d5bef Binary files /dev/null and b/xdg/icons/48x48/top-octane.png differ diff --git a/xdg/icons/64x64/top-octane.png b/xdg/icons/64x64/top-octane.png new file mode 100644 index 0000000..6950f56 Binary files /dev/null and b/xdg/icons/64x64/top-octane.png differ diff --git a/xdg/icons/scalable/top-octane.svg b/xdg/icons/scalable/top-octane.svg new file mode 100644 index 0000000..c788cc6 --- /dev/null +++ b/xdg/icons/scalable/top-octane.svg @@ -0,0 +1,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xdg/top-octane.desktop b/xdg/top-octane.desktop new file mode 100644 index 0000000..2dd9305 --- /dev/null +++ b/xdg/top-octane.desktop @@ -0,0 +1,14 @@ +[Desktop Entry] +Version=1.0 +Type=Application +Name=Top-Octane +GenericName=Vehicular Combat Racer +Comment=Modern SDL2 + OpenGL 3.3 source port of Bullfrog's Hi-Octane +Exec=top-octane +TryExec=top-octane +Icon=top-octane +Terminal=false +Categories=Game;ActionGame;ArcadeGame; +Keywords=racing;hovercraft;combat;hi-octane;bullfrog;retro;arcade; +StartupNotify=false +StartupWMClass=top-octane