Merge branch 'headless-mode' into Fix-usage-vcpkg-for-windows-and-add-cmake-preset-for-vscode
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "engine.hpp"
|
||||
#include "Engine.hpp"
|
||||
|
||||
#ifndef GLEW_STATIC
|
||||
#define GLEW_STATIC
|
||||
@@ -186,7 +186,7 @@ void Engine::run() {
|
||||
|
||||
void Engine::postUpdate() {
|
||||
network->update();
|
||||
processPostRunnables();
|
||||
postRunnables.run();
|
||||
}
|
||||
|
||||
void Engine::updateFrontend() {
|
||||
@@ -215,15 +215,6 @@ void Engine::renderFrame() {
|
||||
gui->draw(ctx, *assets);
|
||||
}
|
||||
|
||||
void Engine::processPostRunnables() {
|
||||
std::lock_guard<std::recursive_mutex> lock(postRunnablesMutex);
|
||||
while (!postRunnables.empty()) {
|
||||
postRunnables.front()();
|
||||
postRunnables.pop();
|
||||
}
|
||||
scripting::process_post_runnables();
|
||||
}
|
||||
|
||||
void Engine::saveSettings() {
|
||||
logger.info() << "saving settings";
|
||||
files::write_string(paths.getSettingsFile(), toml::stringify(settingsHandler));
|
||||
@@ -296,13 +287,8 @@ void Engine::loadAssets() {
|
||||
auto task = loader.startTask([=](){});
|
||||
task->waitForEnd();
|
||||
} else {
|
||||
try {
|
||||
while (loader.hasNext()) {
|
||||
loader.loadNext();
|
||||
}
|
||||
} catch (const assetload::error& err) {
|
||||
new_assets.reset();
|
||||
throw;
|
||||
while (loader.hasNext()) {
|
||||
loader.loadNext();
|
||||
}
|
||||
}
|
||||
assets = std::move(new_assets);
|
||||
@@ -519,11 +505,6 @@ std::shared_ptr<Screen> Engine::getScreen() {
|
||||
return screen;
|
||||
}
|
||||
|
||||
void Engine::postRunnable(const runnable& callback) {
|
||||
std::lock_guard<std::recursive_mutex> lock(postRunnablesMutex);
|
||||
postRunnables.push(callback);
|
||||
}
|
||||
|
||||
SettingsHandler& Engine::getSettingsHandler() {
|
||||
return settingsHandler;
|
||||
}
|
||||
@@ -11,15 +11,14 @@
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "files/settings_io.hpp"
|
||||
#include "util/ObjectsKeeper.hpp"
|
||||
#include "PostRunnables.hpp"
|
||||
#include "Time.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
class Level;
|
||||
class Screen;
|
||||
@@ -65,13 +64,12 @@ class Engine : public util::ObjectsKeeper {
|
||||
std::vector<ContentPack> contentPacks;
|
||||
std::unique_ptr<Content> content;
|
||||
std::unique_ptr<ResPaths> resPaths;
|
||||
std::queue<runnable> postRunnables;
|
||||
std::recursive_mutex postRunnablesMutex;
|
||||
std::unique_ptr<EngineController> controller;
|
||||
std::unique_ptr<cmd::CommandsInterpreter> interpreter;
|
||||
std::unique_ptr<network::Network> network;
|
||||
std::vector<std::string> basePacks;
|
||||
std::unique_ptr<gui::GUI> gui;
|
||||
PostRunnables postRunnables;
|
||||
Time time;
|
||||
consumer<std::unique_ptr<Level>> levelConsumer;
|
||||
bool quitSignal = false;
|
||||
@@ -80,7 +78,6 @@ class Engine : public util::ObjectsKeeper {
|
||||
void loadSettings();
|
||||
void saveSettings();
|
||||
void updateHotkeys();
|
||||
void processPostRunnables();
|
||||
void loadAssets();
|
||||
public:
|
||||
Engine(CoreParameters coreParameters);
|
||||
@@ -157,7 +154,9 @@ public:
|
||||
std::shared_ptr<Screen> getScreen();
|
||||
|
||||
/// @brief Enqueue function call to the end of current frame in draw thread
|
||||
void postRunnable(const runnable& callback);
|
||||
void postRunnable(const runnable& callback) {
|
||||
postRunnables.postRunnable(callback);
|
||||
}
|
||||
|
||||
void saveScreenshot();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "Mainloop.hpp"
|
||||
|
||||
#include "Engine.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "frontend/screens/MenuScreen.hpp"
|
||||
#include "frontend/screens/LevelScreen.hpp"
|
||||
#include "window/Window.hpp"
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include "delegates.hpp"
|
||||
|
||||
class PostRunnables {
|
||||
std::queue<runnable> runnables;
|
||||
std::recursive_mutex mutex;
|
||||
public:
|
||||
void postRunnable(runnable task) {
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex);
|
||||
runnables.push(std::move(task));
|
||||
}
|
||||
|
||||
void run() {
|
||||
std::queue<runnable> tasksToRun;
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex);
|
||||
std::swap(tasksToRun, runnables);
|
||||
}
|
||||
|
||||
while (!tasksToRun.empty()) {
|
||||
auto& task = tasksToRun.front();
|
||||
task();
|
||||
tasksToRun.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ServerMainloop.hpp"
|
||||
|
||||
#include "Engine.hpp"
|
||||
#include "logic/scripting/scripting.hpp"
|
||||
#include "logic/LevelController.hpp"
|
||||
#include "interfaces/Process.hpp"
|
||||
@@ -7,7 +8,6 @@
|
||||
#include "world/Level.hpp"
|
||||
#include "world/World.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include "engine.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "audio/audio.hpp"
|
||||
#include "delegates.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "hud.hpp"
|
||||
#include "content/Content.hpp"
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "content/Content.hpp"
|
||||
#include "core_defs.hpp"
|
||||
#include "delegates.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "graphics/core/Atlas.hpp"
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
#include "graphics/core/Batch3D.hpp"
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "screens/MenuScreen.hpp"
|
||||
|
||||
#include "delegates.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "data/dv.hpp"
|
||||
#include "interfaces/Task.hpp"
|
||||
#include "files/engine_paths.hpp"
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "content/Content.hpp"
|
||||
#include "core_defs.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "frontend/LevelFrontend.hpp"
|
||||
#include "frontend/hud.hpp"
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "maths/UVRegion.hpp"
|
||||
#include "window/Window.hpp"
|
||||
#include "window/Camera.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
|
||||
MenuScreen::MenuScreen(Engine& engine) : Screen(engine) {
|
||||
engine.resetContent();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "Screen.hpp"
|
||||
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
|
||||
Screen::Screen(Engine& engine)
|
||||
: engine(engine),
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#include "objects/Players.hpp"
|
||||
#include "logic/LevelController.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/files.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "assets/Assets.hpp"
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "frontend/LevelFrontend.hpp"
|
||||
#include "frontend/ContentGfxCache.hpp"
|
||||
#include "items/Inventory.hpp"
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "coders/commons.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "content/ContentReport.hpp"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/WorldFiles.hpp"
|
||||
#include "maths/voxmaths.hpp"
|
||||
#include "objects/Entities.hpp"
|
||||
|
||||
@@ -37,7 +37,7 @@ extern const luaL_Reg packlib[];
|
||||
extern const luaL_Reg particleslib[]; // gfx.particles
|
||||
extern const luaL_Reg playerlib[];
|
||||
extern const luaL_Reg quatlib[];
|
||||
extern const luaL_Reg testlib[];
|
||||
extern const luaL_Reg applib[];
|
||||
extern const luaL_Reg text3dlib[]; // gfx.text3d
|
||||
extern const luaL_Reg timelib[];
|
||||
extern const luaL_Reg tomllib[];
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "api_lua.hpp"
|
||||
|
||||
const luaL_Reg testlib[] = {
|
||||
const luaL_Reg applib[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "audio/audio.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "api_lua.hpp"
|
||||
|
||||
inline const char* DEFAULT_CHANNEL = "regular";
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "coders/commons.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "logic/CommandsInterpreter.hpp"
|
||||
#include "api_lua.hpp"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "constants.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "files/settings_io.hpp"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "libentity.hpp"
|
||||
|
||||
#include "content/Content.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "objects/Entities.hpp"
|
||||
#include "objects/EntityDef.hpp"
|
||||
#include "objects/Player.hpp"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <set>
|
||||
|
||||
#include "coders/gzip.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "world/Level.hpp"
|
||||
#include "world/generator/VoxelFragment.hpp"
|
||||
#include "content/ContentLoader.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "../lua_custom_types.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "assets/Assets.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "frontend/UiDocument.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "graphics/ui/elements/Button.hpp"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "frontend/UiDocument.hpp"
|
||||
#include "frontend/hud.hpp"
|
||||
#include "graphics/ui/elements/InventoryView.hpp"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <filesystem>
|
||||
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "frontend/hud.hpp"
|
||||
#include "frontend/screens/Screen.hpp"
|
||||
@@ -30,7 +30,21 @@ static int l_mousecode(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_add_callback(lua::State* L) {
|
||||
auto bindname = lua::require_string(L, 1);
|
||||
std::string bindname = lua::require_string(L, 1);
|
||||
size_t pos = bindname.find(':');
|
||||
if (pos != std::string::npos) {
|
||||
std::string prefix = bindname.substr(0, pos);
|
||||
if (prefix == "key") {
|
||||
if (hud == nullptr) {
|
||||
throw std::runtime_error("on_hud_open is not called yet");
|
||||
}
|
||||
auto key = input_util::keycode_from(bindname.substr(pos + 1));
|
||||
auto callback = lua::create_runnable(L);
|
||||
hud->keepAlive(Events::keyCallbacks[key].add(callback));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const auto& bind = Events::bindings.find(bindname);
|
||||
if (bind == Events::bindings.end()) {
|
||||
throw std::runtime_error("unknown binding " + util::quote(bindname));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "api_lua.hpp"
|
||||
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "network/Network.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "assets/AssetsLoader.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/WorldFiles.hpp"
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "world/Level.hpp"
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "graphics/render/ParticlesRenderer.hpp"
|
||||
#include "graphics/render/Emitter.hpp"
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "graphics/render/WorldRenderer.hpp"
|
||||
#include "graphics/render/TextsRenderer.hpp"
|
||||
#include "graphics/render/TextNote.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "api_lua.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "coders/compression.hpp"
|
||||
#include "coders/gzip.hpp"
|
||||
#include "coders/json.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "lighting/Lighting.hpp"
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "util/stringutil.hpp"
|
||||
#include "libs/api_lua.hpp"
|
||||
#include "lua_custom_types.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
|
||||
static debug::Logger logger("lua-state");
|
||||
static lua::State* main_thread = nullptr;
|
||||
@@ -58,10 +58,10 @@ static void create_libs(State* L, StateType stateType) {
|
||||
openlib(L, "vec3", vec3lib);
|
||||
openlib(L, "vec4", vec4lib);
|
||||
|
||||
if (stateType == StateType::TEST) {
|
||||
openlib(L, "test", testlib);
|
||||
if (stateType == StateType::SCRIPT) {
|
||||
openlib(L, "app", applib);
|
||||
}
|
||||
if (stateType == StateType::BASE || stateType == StateType::TEST) {
|
||||
if (stateType == StateType::BASE || stateType == StateType::SCRIPT) {
|
||||
openlib(L, "gui", guilib);
|
||||
openlib(L, "input", inputlib);
|
||||
openlib(L, "inventory", inventorylib);
|
||||
@@ -119,10 +119,10 @@ void lua::initialize(const EnginePaths& paths, const CoreParameters& params) {
|
||||
logger.info() << LUAJIT_VERSION;
|
||||
|
||||
main_thread = create_state(
|
||||
paths, params.headless ? StateType::TEST : StateType::BASE
|
||||
paths, params.headless ? StateType::SCRIPT : StateType::BASE
|
||||
);
|
||||
lua::pushstring(main_thread, params.scriptFile.stem().u8string());
|
||||
lua::setglobal(main_thread, "__VC_TEST_NAME");
|
||||
lua::setglobal(main_thread, "__VC_SCRIPT_NAME");
|
||||
}
|
||||
|
||||
void lua::finalize() {
|
||||
|
||||
@@ -13,7 +13,7 @@ struct CoreParameters;
|
||||
namespace lua {
|
||||
enum class StateType {
|
||||
BASE,
|
||||
TEST,
|
||||
SCRIPT,
|
||||
GENERATOR,
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include "files/util.hpp"
|
||||
#include "graphics/core/ImageData.hpp"
|
||||
#include "maths/Heightmap.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "../lua_util.hpp"
|
||||
|
||||
using namespace lua;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "content/Content.hpp"
|
||||
#include "content/ContentPack.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "frontend/UiDocument.hpp"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "scripting_hud.hpp"
|
||||
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "frontend/hud.hpp"
|
||||
#include "graphics/render/WorldRenderer.hpp"
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#include "world/generator/GeneratorDef.hpp"
|
||||
#include "util/timeutil.hpp"
|
||||
#include "files/files.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
using namespace lua;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include "util/command_line.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
@@ -16,7 +16,7 @@ struct UVFace {
|
||||
}
|
||||
|
||||
template<int n>
|
||||
inline void rotate(int times) {
|
||||
inline void rotate() {
|
||||
int times = n % 4;
|
||||
if (times < 0) {
|
||||
times += 4;
|
||||
|
||||
+14
-5
@@ -9,6 +9,12 @@
|
||||
#include <glm/gtx/norm.hpp>
|
||||
|
||||
namespace util {
|
||||
inline uint64_t shuffle_bits_step(uint64_t x, uint64_t m, unsigned shift) {
|
||||
uint64_t t = ((x >> shift) ^ x) & m;
|
||||
x = (x ^ t) ^ (t << shift);
|
||||
return x;
|
||||
}
|
||||
|
||||
constexpr inline float EPSILON = 1e-6f;
|
||||
|
||||
class PseudoRandom {
|
||||
@@ -57,17 +63,20 @@ namespace util {
|
||||
return randU64() / static_cast<double>(UINT64_MAX);
|
||||
}
|
||||
|
||||
void setSeed(int number) {
|
||||
seed = (static_cast<unsigned short>(number * 23729) ^
|
||||
static_cast<unsigned short>(number + 16786));
|
||||
rand();
|
||||
}
|
||||
void setSeed(int number1, int number2) {
|
||||
seed = ((static_cast<unsigned short>(number1 * 23729) |
|
||||
static_cast<unsigned short>(number2 % 16786)) ^
|
||||
static_cast<unsigned short>(number2 * number1));
|
||||
rand();
|
||||
}
|
||||
|
||||
void setSeed(long number) {
|
||||
number = shuffle_bits_step(number, 0x2222222222222222ull, 1);
|
||||
number = shuffle_bits_step(number, 0x0c0c0c0c0c0c0c0cull, 2);
|
||||
number = shuffle_bits_step(number, 0x00f000f000f000f0ull, 4);
|
||||
seed = number;
|
||||
rand();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "content/Content.hpp"
|
||||
#include "data/dv_util.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "graphics/core/LineBatch.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
@@ -11,17 +12,33 @@ namespace util {
|
||||
class RunnablesList {
|
||||
int nextid = 1;
|
||||
std::unordered_map<int, runnable> runnables;
|
||||
std::mutex mutex;
|
||||
public:
|
||||
RunnablesList() = default;
|
||||
|
||||
RunnablesList(RunnablesList&& o) {
|
||||
runnables = std::move(o.runnables);
|
||||
nextid = o.nextid;
|
||||
}
|
||||
|
||||
void operator=(RunnablesList&& o) {
|
||||
runnables = std::move(o.runnables);
|
||||
nextid = o.nextid;
|
||||
}
|
||||
|
||||
observer_handler add(runnable callback) {
|
||||
std::lock_guard lock(mutex);
|
||||
int id = nextid++;
|
||||
runnables[id] = std::move(callback);
|
||||
return observer_handler(new int(id), [this](int* id) { //-V508
|
||||
std::lock_guard lock(mutex);
|
||||
runnables.erase(*id);
|
||||
delete id;
|
||||
});
|
||||
}
|
||||
|
||||
void notify() {
|
||||
std::lock_guard lock(mutex);
|
||||
for (auto& entry : runnables) {
|
||||
entry.second();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "files/engine_paths.hpp"
|
||||
#include "util/ArgsReader.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
||||
+11
-1
@@ -23,6 +23,7 @@ bool Events::_cursor_locked = false;
|
||||
std::vector<uint> Events::codepoints;
|
||||
std::vector<keycode> Events::pressedKeys;
|
||||
std::unordered_map<std::string, Binding> Events::bindings;
|
||||
std::unordered_map<keycode, util::RunnablesList> Events::keyCallbacks;
|
||||
|
||||
bool Events::pressed(keycode keycode) {
|
||||
return pressed(static_cast<int>(keycode));
|
||||
@@ -156,6 +157,12 @@ bool Events::jactive(const std::string& name) {
|
||||
void Events::setKey(int key, bool b) {
|
||||
Events::keys[key] = b;
|
||||
Events::frames[key] = currentFrame;
|
||||
if (b) {
|
||||
const auto& callbacks = keyCallbacks.find(static_cast<keycode>(key));
|
||||
if (callbacks != keyCallbacks.end()) {
|
||||
callbacks->second.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Events::setButton(int button, bool b) {
|
||||
@@ -173,6 +180,10 @@ void Events::setPosition(float xpos, float ypos) {
|
||||
Events::cursor.y = ypos;
|
||||
}
|
||||
|
||||
observer_handler Events::addKeyCallback(keycode key, runnable callback) {
|
||||
return keyCallbacks[key].add(std::move(callback));
|
||||
}
|
||||
|
||||
#include "coders/json.hpp"
|
||||
#include "coders/toml.hpp"
|
||||
|
||||
@@ -228,7 +239,6 @@ void Events::loadBindings(
|
||||
} else if (bindType == BindType::REBIND) {
|
||||
Events::rebind(key, type, code);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ public:
|
||||
static std::vector<uint> codepoints;
|
||||
static std::vector<keycode> pressedKeys;
|
||||
static std::unordered_map<std::string, Binding> bindings;
|
||||
static std::unordered_map<keycode, util::RunnablesList> keyCallbacks;
|
||||
|
||||
static void pollEvents();
|
||||
|
||||
@@ -50,6 +51,8 @@ public:
|
||||
static bool active(const std::string& name);
|
||||
static bool jactive(const std::string& name);
|
||||
|
||||
static observer_handler addKeyCallback(keycode key, runnable callback);
|
||||
|
||||
static void setKey(int key, bool b);
|
||||
static void setButton(int button, bool b);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user