From ccebc91f0da26200f2e4dbbda1af827d6fee00f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Nov=C3=A1k?= Date: Thu, 30 Jan 2025 20:43:42 +0100 Subject: [PATCH] import ADV settings, platform files --- game/CMakeLists.txt | 4 +-- game/advconfig.c | 56 ++++++++++++++++++++++++++++++++ game/advconfig.h | 14 ++++++++ platform/CMakeLists.txt | 40 +++++++++++++++++++++-- platform/config.cpp | 28 +++++++++++++++- platform/config.h | 13 ++++++-- platform/error.cpp | 3 +- platform/linux/CMakeLists.txt | 2 -- platform/linux/save_folder.cpp | 25 ++++++++++++++ platform/mac_os/save_folder.cpp | 25 ++++++++++++++ platform/mac_os/todo.txt | 1 + platform/save_folder.h | 11 +++++++ platform/{linux => }/timer.cpp | 2 +- platform/{linux => }/timer.h | 0 platform/windows/save_folder.cpp | 31 ++++++++++++++++++ platform/windows/todo.txt | 1 + skeldal.ini | 2 +- 17 files changed, 244 insertions(+), 14 deletions(-) create mode 100644 game/advconfig.c create mode 100644 game/advconfig.h delete mode 100644 platform/linux/CMakeLists.txt create mode 100644 platform/linux/save_folder.cpp create mode 100644 platform/mac_os/save_folder.cpp create mode 100644 platform/mac_os/todo.txt create mode 100644 platform/save_folder.h rename platform/{linux => }/timer.cpp (95%) rename platform/{linux => }/timer.h (100%) create mode 100644 platform/windows/save_folder.cpp create mode 100644 platform/windows/todo.txt diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 5c218ec..2d613ea 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -32,6 +32,7 @@ setup.c chargen.c sndandmus.c specproc.c +advconfig.c temp_storage.cpp ${CMAKE_BINARY_DIR}/default_font.cpp ) @@ -39,7 +40,6 @@ ${CMAKE_BINARY_DIR}/default_font.cpp add_executable(skeldal ${files}) target_link_libraries(skeldal skeldal_libs - skeldal_platform_libs - skeldal_linux + skeldal_platform skeldal_sdl ${SDL2_LIBRARIES} pthread) diff --git a/game/advconfig.c b/game/advconfig.c new file mode 100644 index 0000000..b7c5e35 --- /dev/null +++ b/game/advconfig.c @@ -0,0 +1,56 @@ +#include +#include "advconfig.h" + + +#include +#include + + +static void process_row(INI_CONFIG_SECTION *sec, const char *row) { + + char *buff = strcpy((char *)alloca(strlen(row)+1), row); + char *sep = strchr(buff,' '); + if (sep == NULL) return; + *sep = 0; + + const char *key = buff; + const char *value = sep+1; + + + if (stricmp(key,"CESTA_GRAFIKA") == 0) { + ini_replace_key(sec, "graphics", value); + } else if (stricmp(key, "CESTA_ZVUKY") == 0) { + ini_replace_key(sec, "sounds", value); + } else if (stricmp(key, "CESTA_FONTY") == 0) { + ini_replace_key(sec, "fonts", value); + } else if (stricmp(key, "CESTA_MAPY") == 0) { + ini_replace_key(sec, "maps", value); + } else if (stricmp(key, "CESTA_MUSIC") == 0) { + ini_replace_key(sec, "music_adv", value); + } else if (stricmp(key, "CESTA_BGRAFIKA") == 0) { + ini_replace_key(sec, "gui", value); + } else if (stricmp(key, "CESTA_ITEMY") == 0) { + ini_replace_key(sec, "items", value); + } else if (stricmp(key, "CESTA_ENEMY") == 0) { + ini_replace_key(sec, "enemy", value); + } else if (stricmp(key, "CESTA_VIDEO") == 0) { + ini_replace_key(sec, "video", value); + } else if (stricmp(key, "CESTA_DIALOGY") == 0) { + ini_replace_key(sec, "dialogs", value); + } +} + + + +void adv_patch_config(INI_CONFIG *srcconfig, TSTR_LIST lst) { + INI_CONFIG_SECTION *sect = ini_create_section(srcconfig, "paths"); + int cnt = str_count(lst); + for (int i = 0; i < cnt; ++i) { + const char *row = lst[i]; + if (row) { + process_row(sect, row); + } + } +} + + diff --git a/game/advconfig.h b/game/advconfig.h new file mode 100644 index 0000000..c2589b5 --- /dev/null +++ b/game/advconfig.h @@ -0,0 +1,14 @@ + +#ifndef SKELDAL_GAME_ADVCONFIG_H_ +#define SKELDAL_GAME_ADVCONFIG_H_ + + +#include +#include + + +void adv_patch_config(INI_CONFIG *srcconfig, TSTR_LIST lst); + + + +#endif /* SKELDAL_GAME_ADVCONFIG_H_ */ diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt index 576dd1d..0692d44 100644 --- a/platform/CMakeLists.txt +++ b/platform/CMakeLists.txt @@ -1,14 +1,48 @@ SET(files error.cpp +) + +# Základní knihovna mylib +add_library(skeldal_platform STATIC) + +# Přidejte soubory společné pro všechny platformy +target_sources(skeldal_platform PRIVATE legacy_coroutines.cpp platform.cpp int2ascii.c istr.c file_access.cpp config.cpp + error.cpp + timer.cpp ) -add_library(skeldal_platform_libs ${files}) -set_property(TARGET skeldal_platform_libs PROPERTY CXX_STANDARD 20) +# Podmínky pro platformu Windows +if(WIN32) + target_sources(skeldal_platform PRIVATE + windows/save_folder.cpp + ) + target_compile_definitions(skeldal_platform PRIVATE PLATFORM_WINDOWS) + message(STATUS "Building for Windows") + +# Podmínky pro platformu Linux +elseif(UNIX AND NOT APPLE) + target_sources(skeldal_platform PRIVATE + linux/save_folder.cpp + linux/map_file.cpp + ) + target_compile_definitions(skeldal_platform PRIVATE PLATFORM_LINUX) + message(STATUS "Building for Linux") + +# Podmínky pro platformu macOS +elseif(APPLE) + target_sources(skeldal_platform PRIVATE + mac_os/save_folder.cpp + ) + target_compile_definitions(mylib PRIVATE PLATFORM_MACOS) + message(STATUS "Building for macOS") +else() + error("Platform not detected, please add new platform here") +endif() +set_property(TARGET skeldal_platform PROPERTY CXX_STANDARD 20) add_subdirectory(sdl) -add_subdirectory(linux) diff --git a/platform/config.cpp b/platform/config.cpp index 1eaa900..b6fda2a 100644 --- a/platform/config.cpp +++ b/platform/config.cpp @@ -1,6 +1,7 @@ #include "config.h" #include "platform.h" #include +#include #include #include #include @@ -50,7 +51,7 @@ void parseIniStream(std::istream& input, Callback &&callback) { } -const INI_CONFIG* ini_open(const char *filename) { +INI_CONFIG* ini_open(const char *filename) { INI_CONFIG *c = new INI_CONFIG; std::fstream input(filename); @@ -168,3 +169,28 @@ int ini_get_boolean(const INI_CONFIG_SECTION *section, const char *key, } return defval; } + +void ini_replace_key( INI_CONFIG_SECTION *section, const char *key, const char *value) { + auto s = reinterpret_cast(section); + (*s)[std::string(key)] = std::string(value); +} + +INI_CONFIG_SECTION* ini_create_section(INI_CONFIG *cfg, const char *section_name) { + auto § = cfg->data[std::string(section_name)]; + return reinterpret_cast(§); +} + + INI_CONFIG* ini_create_empty(void) { + INI_CONFIG *c = new INI_CONFIG; + return c; +} + +void ini_store_to_file(const INI_CONFIG *config, const char *filename) { + std::ofstream out(filename,std::ios::out|std::ios::trunc); + for (const auto &[sname, s]: config->data) { + out << '[' << sname << ']' << std::endl; + for (const auto &[k,v]: s) { + out << k << '=' << v << std::endl; + } + } +} diff --git a/platform/config.h b/platform/config.h index fb9456c..69ec183 100644 --- a/platform/config.h +++ b/platform/config.h @@ -7,9 +7,9 @@ typedef struct ini_config_tag INI_CONFIG; typedef struct ini_config_section_tag INI_CONFIG_SECTION; ///Opens config, returns handle -const INI_CONFIG *ini_open(const char *filename); +INI_CONFIG *ini_open(const char *filename); ///closes the config and frees memory -void ini_close(const INI_CONFIG *config); +void ini_close(INI_CONFIG *config); ///Opens section /** @@ -54,6 +54,15 @@ double ini_get_value_double(const char *value, int *conv_ok); */ int ini_get_value_boolean(const char *value); +INI_CONFIG_SECTION* ini_create_section(INI_CONFIG *cfg, const char *section_name); + +INI_CONFIG *ini_create_empty(void); + +void ini_store_to_file(const INI_CONFIG *cfg, const char *filename); + + +///Replace existing key with different value +void ini_replace_key(INI_CONFIG_SECTION *section, const char *key, const char *value); ///retrieve string from ini /** diff --git a/platform/error.cpp b/platform/error.cpp index 5b8e622..eee4023 100644 --- a/platform/error.cpp +++ b/platform/error.cpp @@ -9,9 +9,8 @@ extern "C" { void display_error(const char *text) { - char failed_because_error = 0; std::cerr << "ERROR:" << text << std::endl; - assert(failed_because_error); + abort(); } diff --git a/platform/linux/CMakeLists.txt b/platform/linux/CMakeLists.txt deleted file mode 100644 index c0caaa2..0000000 --- a/platform/linux/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ - -add_library(skeldal_linux map_file.cpp timer.cpp) \ No newline at end of file diff --git a/platform/linux/save_folder.cpp b/platform/linux/save_folder.cpp new file mode 100644 index 0000000..dad3a32 --- /dev/null +++ b/platform/linux/save_folder.cpp @@ -0,0 +1,25 @@ +#include "../save_folder.h" +#include "../error.h" +#include + +static std::string get_default_savegame_dir() { + + // Linux + char* home = std::getenv("HOME"); + if (home) { + return std::filesystem::path(home) / ".local/share/" SAVEGAME_FOLDERNAME; + } else { + display_error("$HOME has no value (user with no home)"); + abort(); + } +} + + + + +const char *get_default_savegame_directory() { + + static std::string dir = get_default_savegame_dir(); + return dir.c_str(); + +} diff --git a/platform/mac_os/save_folder.cpp b/platform/mac_os/save_folder.cpp new file mode 100644 index 0000000..dad3a32 --- /dev/null +++ b/platform/mac_os/save_folder.cpp @@ -0,0 +1,25 @@ +#include "../save_folder.h" +#include "../error.h" +#include + +static std::string get_default_savegame_dir() { + + // Linux + char* home = std::getenv("HOME"); + if (home) { + return std::filesystem::path(home) / ".local/share/" SAVEGAME_FOLDERNAME; + } else { + display_error("$HOME has no value (user with no home)"); + abort(); + } +} + + + + +const char *get_default_savegame_directory() { + + static std::string dir = get_default_savegame_dir(); + return dir.c_str(); + +} diff --git a/platform/mac_os/todo.txt b/platform/mac_os/todo.txt new file mode 100644 index 0000000..edbd96c --- /dev/null +++ b/platform/mac_os/todo.txt @@ -0,0 +1 @@ +Platform files are not complete diff --git a/platform/save_folder.h b/platform/save_folder.h new file mode 100644 index 0000000..e730256 --- /dev/null +++ b/platform/save_folder.h @@ -0,0 +1,11 @@ +#ifdef __cplusplus__ +extern "C" { +#endif + +#define SAVEGAME_FOLDERNAME "Skeldal"; + +const char *get_default_savegame_directory(); + +#ifdef __cplusplus__ +} +#endif diff --git a/platform/linux/timer.cpp b/platform/timer.cpp similarity index 95% rename from platform/linux/timer.cpp rename to platform/timer.cpp index 6295e85..3a68a03 100644 --- a/platform/linux/timer.cpp +++ b/platform/timer.cpp @@ -1,6 +1,6 @@ #include "timer.h" #include -#include "../platform.h" +#include "platform.h" #include int get_timer_value() { diff --git a/platform/linux/timer.h b/platform/timer.h similarity index 100% rename from platform/linux/timer.h rename to platform/timer.h diff --git a/platform/windows/save_folder.cpp b/platform/windows/save_folder.cpp new file mode 100644 index 0000000..851b9f1 --- /dev/null +++ b/platform/windows/save_folder.cpp @@ -0,0 +1,31 @@ +#include "../save_folder.h" +#include "../error.h" +#include +#include +#include + +#pragma comment(lib, "shell32.lib") +#pragma comment(lib, "ole32.lib") + +// Pro získání uživatelské složky +namespace fs = std::filesystem; + +// Funkce pro získání složky "Saved Games" +std::string getSavedGamesDirectory() { + PWSTR path = nullptr; + // Použití identifikátoru složky FOLDERID_SavedGames + if (SUCCEEDED(SHGetKnownFolderPathA(FOLDERID_SavedGames, 0, NULL, &path))) { + fs::path savedGamesPath(path); + CoTaskMemFree(path); // Uvolnění paměti + return savedGamesPath / SAVEGAME_FOLDERNAME + } else { + display_error("Failed to retrieve FOLDEROD_SavedGames"); + abort(); + } +} +const char *get_default_savegame_directory() { + + static std::string dir = get_default_savgetSavedGamesDirectoryegame_dir(); + return dir.c_str(); + +} diff --git a/platform/windows/todo.txt b/platform/windows/todo.txt new file mode 100644 index 0000000..edbd96c --- /dev/null +++ b/platform/windows/todo.txt @@ -0,0 +1 @@ +Platform files are not complete diff --git a/skeldal.ini b/skeldal.ini index a1c71a6..da18ded 100644 --- a/skeldal.ini +++ b/skeldal.ini @@ -7,7 +7,7 @@ # savegame = path to savegame, if not defined, retrieved from platform settings -[path] +[paths] game_path=/home/ondra/skeldal_game/ # maps=./maps/ # video=./video/