diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/asset_manager.cc | 20 | ||||
| -rw-r--r-- | src/util/asset_manager.h | 6 | ||||
| -rw-r--r-- | src/util/file_watcher.cc | 44 | ||||
| -rw-r--r-- | src/util/file_watcher.h | 33 |
4 files changed, 103 insertions, 0 deletions
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc index a0e2a97..5067ebe 100644 --- a/src/util/asset_manager.cc +++ b/src/util/asset_manager.cc @@ -189,3 +189,23 @@ void DropAsset(AssetId asset_id, const uint8_t* asset) { } // For static assets, no dynamic memory to free. } + +#if !defined(STRIP_ALL) +// Hot-reload: Clear asset cache to force reload from disk +// Note: This only works for assets that read from disk at runtime. +// Compiled-in assets cannot be hot-reloaded. +bool ReloadAssetsFromFile(const char* config_path) { + (void)config_path; // Unused - just for API consistency + + // Clear cache to force reload + for (size_t i = 0; i < (size_t)AssetId::ASSET_LAST_ID; ++i) { + if (g_asset_cache[i].is_procedural && g_asset_cache[i].data) { + delete[] g_asset_cache[i].data; + } + g_asset_cache[i] = {}; + } + + fprintf(stderr, "[ReloadAssets] Cache cleared\n"); + return true; +} +#endif // !defined(STRIP_ALL) diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h index 1e0638c..1714c21 100644 --- a/src/util/asset_manager.h +++ b/src/util/asset_manager.h @@ -10,6 +10,7 @@ struct AssetRecord { size_t size; // Size of the asset data bool is_procedural; // True if data was dynamically allocated by a procedural // generator + bool is_gpu_procedural; // True if GPU compute shader generates texture const char* proc_func_name_str; // Name of procedural generation function // (string literal) const float* proc_params; // Parameters for procedural generation (static, @@ -28,3 +29,8 @@ void DropAsset(AssetId asset_id, const uint8_t* asset); // Returns the AssetId for a given asset name, or AssetId::ASSET_LAST_ID if not // found. AssetId GetAssetIdByName(const char* name); + +#if !defined(STRIP_ALL) +// Hot-reload: Parse and reload assets from config file (debug only) +bool ReloadAssetsFromFile(const char* config_path); +#endif diff --git a/src/util/file_watcher.cc b/src/util/file_watcher.cc new file mode 100644 index 0000000..22eb824 --- /dev/null +++ b/src/util/file_watcher.cc @@ -0,0 +1,44 @@ +// file_watcher.cc - Hot-reload file change detection (debug only) + +#include "file_watcher.h" + +#if !defined(STRIP_ALL) + +#include <sys/stat.h> + +void FileWatcher::add_file(const char* path) { + WatchEntry entry; + entry.path = path; + entry.last_mtime = get_mtime(path); + entry.changed = false; + files_.push_back(entry); +} + +bool FileWatcher::check_changes() { + bool any_changed = false; + for (auto& entry : files_) { + time_t current_mtime = get_mtime(entry.path.c_str()); + if (current_mtime != entry.last_mtime && current_mtime != 0) { + entry.changed = true; + entry.last_mtime = current_mtime; + any_changed = true; + } + } + return any_changed; +} + +void FileWatcher::reset() { + for (auto& entry : files_) { + entry.changed = false; + } +} + +time_t FileWatcher::get_mtime(const char* path) { + struct stat st; + if (stat(path, &st) == 0) { + return st.st_mtime; + } + return 0; +} + +#endif // !defined(STRIP_ALL) diff --git a/src/util/file_watcher.h b/src/util/file_watcher.h new file mode 100644 index 0000000..2766a43 --- /dev/null +++ b/src/util/file_watcher.h @@ -0,0 +1,33 @@ +// file_watcher.h - Hot-reload file change detection (debug only) + +#ifndef FILE_WATCHER_H_ +#define FILE_WATCHER_H_ + +#if !defined(STRIP_ALL) + +#include <string> +#include <vector> +#include <ctime> + +class FileWatcher { + public: + FileWatcher() = default; + + void add_file(const char* path); + bool check_changes(); + void reset(); + + private: + struct WatchEntry { + std::string path; + time_t last_mtime; + bool changed; + }; + + std::vector<WatchEntry> files_; + time_t get_mtime(const char* path); +}; + +#endif // !defined(STRIP_ALL) + +#endif // FILE_WATCHER_H_ |
