From 3bf42c6ff3805a0d42bbc661794a95ff31bedc26 Mon Sep 17 00:00:00 2001 From: untodesu Date: Sat, 15 Mar 2025 16:22:09 +0500 Subject: Add whatever I was working on for the last month --- game/shared/splash.cc | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 game/shared/splash.cc (limited to 'game/shared/splash.cc') diff --git a/game/shared/splash.cc b/game/shared/splash.cc new file mode 100644 index 0000000..ec7b3c8 --- /dev/null +++ b/game/shared/splash.cc @@ -0,0 +1,34 @@ +#include "shared/pch.hh" +#include "shared/splash.hh" + +constexpr static const char *SPLASHES_FILENAME = "misc/splashes.txt"; + +static std::mt19937_64 splash_random; +static std::vector splash_lines; + +void splash::init(void) +{ + if(auto file = PHYSFS_openRead(SPLASHES_FILENAME)) { + auto source = std::string(PHYSFS_fileLength(file), char(0x00)); + PHYSFS_readBytes(file, source.data(), source.size()); + PHYSFS_close(file); + + std::string line; + std::istringstream stream(source); + + while(std::getline(stream, line)) { + splash_lines.push_back(line); + } + + splash_random.seed(std::random_device()()); + } + else { + splash_lines.push_back(fmt::format("{}: {}", SPLASHES_FILENAME, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))); + } +} + +const char* splash::get(void) +{ + std::uniform_int_distribution dist(0, splash_lines.size() - 1); + return splash_lines.at(dist(splash_random)).c_str(); +} -- cgit