enginefs replaced with EnginePaths class + command line arguments

This commit is contained in:
MihailRis 2023-11-26 14:07:16 +03:00
parent 7a39ba6b2e
commit fe220ee172
9 changed files with 54 additions and 85 deletions

View File

@ -2,14 +2,15 @@
#include "Assets.h"
#include <iostream>
#include <filesystem>
#include <memory>
#include "../constants.h"
using std::filesystem::path;
using std::unique_ptr;
AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) {
AssetsLoader::AssetsLoader(Assets* assets, path resdir)
: assets(assets), resdir(resdir) {
}
void AssetsLoader::addLoader(int tag, aloader_func func) {
@ -105,12 +106,17 @@ void AssetsLoader::createDefaults(AssetsLoader& loader) {
}
void AssetsLoader::addDefaults(AssetsLoader& loader) {
loader.add(ASSET_SHADER, SHADERS_FOLDER"/main", "main");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/lines", "lines");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/ui", "ui");
path resdir = loader.getDirectory();
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/main"), "main");
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/lines"), "lines");
loader.add(ASSET_SHADER, resdir/path(SHADERS_FOLDER"/ui"), "ui");
loader.add(ASSET_ATLAS, TEXTURES_FOLDER"/blocks", "blocks");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/menubg.png", "menubg");
loader.add(ASSET_ATLAS, resdir/path(TEXTURES_FOLDER"/blocks"), "blocks");
loader.add(ASSET_TEXTURE, resdir/path(TEXTURES_FOLDER"/menubg.png"), "menubg");
loader.add(ASSET_FONT, FONTS_FOLDER"/font", "normal");
loader.add(ASSET_FONT, resdir/path(FONTS_FOLDER"/font"), "normal");
}
path AssetsLoader::getDirectory() const {
return resdir;
}

View File

@ -3,6 +3,7 @@
#include <string>
#include <functional>
#include <filesystem>
#include <map>
#include <queue>
@ -25,8 +26,9 @@ class AssetsLoader {
Assets* assets;
std::map<int, aloader_func> loaders;
std::queue<aloader_entry> entries;
std::filesystem::path resdir;
public:
AssetsLoader(Assets* assets);
AssetsLoader(Assets* assets, std::filesystem::path resdir);
void addLoader(int tag, aloader_func func);
void add(int tag, const std::string filename, const std::string alias);
@ -35,6 +37,8 @@ public:
static void createDefaults(AssetsLoader& loader);
static void addDefaults(AssetsLoader& loader);
std::filesystem::path getDirectory() const;
};
#endif // ASSETS_ASSETS_LOADER_H

View File

@ -18,10 +18,8 @@ inline uint vox_index(int x, int y, int z, int w=CHUNK_W, int d=CHUNK_D) {
return (y * d + z) * w + x;
}
#define RES_FLODER "res/"
#define SHADERS_FOLDER "res/shaders"
#define TEXTURES_FOLDER "res/textures"
#define FONTS_FOLDER "res/fonts"
#define SHADERS_FOLDER "shaders"
#define TEXTURES_FOLDER "textures"
#define FONTS_FOLDER "fonts"
#endif // SRC_CONSTANTS_H_

View File

@ -23,7 +23,7 @@
#include "coders/json.h"
#include "coders/png.h"
#include "files/files.h"
#include "files/engine_files.h"
#include "files/engine_paths.h"
using std::unique_ptr;
using std::shared_ptr;
@ -32,13 +32,13 @@ using std::filesystem::path;
using glm::vec3;
using gui::GUI;
Engine::Engine(EngineSettings& settings, Content* content)
: settings(settings), content(content) {
Engine::Engine(EngineSettings& settings, EnginePaths* paths, Content* content)
: settings(settings), content(content), paths(paths) {
Window::initialize(settings.display);
assets = new Assets();
std::cout << "-- loading assets" << std::endl;
AssetsLoader loader(assets);
AssetsLoader loader(assets, paths->getResources());
AssetsLoader::createDefaults(loader);
AssetsLoader::addDefaults(loader);
while (loader.hasNext()) {
@ -64,7 +64,7 @@ void Engine::updateHotkeys() {
if (Events::jpressed(keycode::F2)) {
unique_ptr<ImageData> image(Window::takeScreenshot());
image->flipY();
path filename = enginefs::get_screenshot_file("png");
path filename = paths->getScreenshotFile("png");
png::write_image(filename.string(), image.get());
std::cout << "saved screenshot as " << filename << std::endl;
}
@ -128,3 +128,7 @@ void Engine::setScreen(shared_ptr<Screen> screen) {
const Content* Engine::getContent() const {
return content;
}
EnginePaths* Engine::getPaths() {
return paths;
}

View File

@ -11,6 +11,7 @@ class Assets;
class Level;
class Screen;
class Content;
class EnginePaths;
namespace gui {
class GUI;
@ -26,6 +27,7 @@ class Engine {
std::shared_ptr<Screen> screen = nullptr;
EngineSettings& settings;
Content* content;
EnginePaths* paths;
uint64_t frame = 0;
double lastTime = 0.0;
@ -33,7 +35,7 @@ class Engine {
gui::GUI* gui;
public:
Engine(EngineSettings& settings, Content* content);
Engine(EngineSettings& settings, EnginePaths* paths, Content* content);
~Engine();
void updateTimers();
@ -44,7 +46,7 @@ public:
gui::GUI* getGUI();
EngineSettings& getSettings();
void setScreen(std::shared_ptr<Screen> screen);
EnginePaths* getPaths();
const Content* getContent() const;
};

View File

@ -1,40 +0,0 @@
#include "engine_files.h"
#include <filesystem>
#include <sstream>
#include "../typedefs.h"
namespace fs = std::filesystem;
using std::string;
using fs::path;
path enginefs::get_screenshot_file(string ext) {
path folder = SCREENSHOTS_FOLDER;
if (!fs::is_directory(folder)) {
fs::create_directory(folder);
}
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
const char* format = "%Y-%m-%d_%H-%M-%S";
std::stringstream ss;
ss << std::put_time(&tm, format);
string datetimestr = ss.str();
path filename = folder/path("screenshot-"+datetimestr+"."+ext);
uint index = 0;
while (fs::exists(filename)) {
filename = folder/path("screenshot-"+datetimestr+"-"+std::to_string(index)+"."+ext);
index++;
}
return filename;
}
path enginefs::get_worlds_folder() {
return path("worlds");
}
bool enginefs::is_world_name_used(std::string name) {
return fs::exists(enginefs::get_worlds_folder()/fs::u8path(name));
}

View File

@ -1,15 +0,0 @@
#ifndef FILES_ENGINE_FILES_H_
#define FILES_ENGINE_FILES_H_
#include <string>
#include <filesystem>
#define SCREENSHOTS_FOLDER "screenshots"
namespace enginefs {
extern std::filesystem::path get_screenshot_file(std::string ext);
extern std::filesystem::path get_worlds_folder();
extern bool is_world_name_used(std::string name);
}
#endif // FILES_ENGINE_FILES_H_

View File

@ -12,7 +12,7 @@
#include "gui/controls.h"
#include "screens.h"
#include "../util/stringutil.h"
#include "../files/engine_files.h"
#include "../files/engine_paths.h"
#include "../world/World.h"
#include "../window/Events.h"
#include "../window/Window.h"
@ -42,6 +42,8 @@ inline Button* backButton(PagesControl* menu) {
}
Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
EnginePaths* paths = engine->getPaths();
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
@ -50,7 +52,7 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f));
worldsPanel->color(vec4(0.1f));
worldsPanel->maxLength(400);
path worldsFolder = enginefs::get_worlds_folder();
path worldsFolder = paths->getWorldsFolder();
if (std::filesystem::is_directory(worldsFolder)) {
for (auto const& entry : directory_iterator(worldsFolder)) {
if (!entry.is_directory()) {
@ -60,10 +62,10 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
Button* button = new Button(util::str2wstr_utf8(name),
vec4(10.0f, 8.0f, 10.0f, 8.0f));
button->color(vec4(0.5f));
button->listenAction([engine, panel, name](GUI*) {
button->listenAction([=](GUI*) {
EngineSettings& settings = engine->getSettings();
auto folder = enginefs::get_worlds_folder()/u8path(name);
auto folder = paths->getWorldsFolder()/u8path(name);
World* world = new World(name, folder, 42, settings);
auto screen = new LevelScreen(engine,
world->load(settings, engine->getContent()));
@ -117,10 +119,11 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) {
button->listenAction([=](GUI*) {
wstring name = worldNameInput->text();
string nameutf8 = util::wstr2str_utf8(name);
EnginePaths* paths = engine->getPaths();
// Basic validation
if (!util::is_valid_filename(name) ||
enginefs::is_world_name_used(nameutf8)) {
paths->isWorldNameUsed(nameutf8)) {
// blink red two times
panel->listenInterval(0.1f, [worldNameInput, basecolor]() {
static bool flag = true;
@ -151,7 +154,7 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) {
EngineSettings& settings = engine->getSettings();
auto folder = enginefs::get_worlds_folder()/u8path(nameutf8);
auto folder = paths->getWorldsFolder()/u8path(nameutf8);
std::filesystem::create_directories(folder);
World* world = new World(nameutf8, folder, seed, settings);
auto screen = new LevelScreen(engine, world->load(settings, engine->getContent()));

View File

@ -11,13 +11,20 @@
#include "coders/toml.h"
#include "files/files.h"
#include "files/settings_io.h"
#include "files/engine_paths.h"
#include "content/Content.h"
#include "coders/png.h"
#include "graphics/Atlas.h"
#include "graphics/ImageData.h"
int main() {
#include "util/command_line.h"
int main(int argc, char** argv) {
EnginePaths paths;
if (!parse_cmdline(argc, argv, paths))
return EXIT_SUCCESS;
platform::configure_encoding();
ContentBuilder contentBuilder;
setup_definitions(&contentBuilder);
@ -35,7 +42,7 @@ int main() {
toml::Reader reader(&wrapper, settings_file.string(), content);
reader.read();
}
Engine engine(settings, content.get());
Engine engine(settings, &paths, content.get());
setup_bindings();
if (std::filesystem::is_regular_file(controls_file)) {
std::cout << "-- loading controls" << std::endl;
@ -52,5 +59,5 @@ int main() {
std::cerr << "could not to initialize engine" << std::endl;
std::cerr << err.what() << std::endl;
}
return 0;
return EXIT_SUCCESS;
}