From ee1fd9fa9910fb46aa348d25cec800b4b41cf579 Mon Sep 17 00:00:00 2001 From: Rodrigo Delduca Date: Thu, 26 Dec 2024 12:04:37 -0300 Subject: [PATCH] Work in progress --- Makefile | 4 +- requirements.txt | 4 +- src/application.cpp | 2 +- src/common.hpp | 15 ++- src/entity.cpp | 4 +- src/entity.hpp | 4 +- src/entityprops.hpp | 2 +- src/scriptengine.cpp | 224 +++++++++++++++++++++---------------------- src/socket.cpp | 41 +++++--- src/socket.hpp | 9 +- src/vector2d.cpp | 2 +- src/vector2d.hpp | 2 +- 12 files changed, 170 insertions(+), 143 deletions(-) diff --git a/Makefile b/Makefile index 137b0ad..b9c5edc 100644 --- a/Makefile +++ b/Makefile @@ -20,5 +20,5 @@ build: ## Build configure: clean ## Configure conan remote update conancenter --url https://center2.conan.io - conan install . --output-folder=build --build=missing --profile=webassembly --settings compiler.cppstd=20 --settings build_type=Release - cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Release -DLOCAL=ON -DSANDBOX=OFF -DENABLE_PROFILING=ON + conan install . --output-folder=build --build=missing --profile=default --settings compiler.cppstd=20 --settings build_type=Release + cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Release -DLOCAL=ON -DSANDBOX=ON -DENABLE_PROFILING=ON diff --git a/requirements.txt b/requirements.txt index 81573bd..92c7212 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -conan==2.8.1 +conan==2.11.0 pre-commit==4.0.1 -pur==7.3.2 +pur==7.3.3 diff --git a/src/application.cpp b/src/application.cpp index fb2f528..03d4848 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -18,7 +18,7 @@ application::application(int argc, char **argv) { int application::run() { try { #if SANDBOX - storage::filesystem::mount("../../sandbox", "/"); + storage::filesystem::mount("../sandbox", "/"); #else storage::filesystem::mount("bundle.7z", "/"); #endif diff --git a/src/common.hpp b/src/common.hpp index 8a2377f..0235df2 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -10,10 +10,23 @@ #define NOMINMAX #endif +#define WEBSOCKET + #ifdef EMSCRIPTEN #include +#ifdef WEBSOCKET #include #endif +#endif + +#ifndef EMSCRIPTEN +#ifdef WEBSOCKET +#include +#include +#include +#include +#endif +#endif #ifdef STEAM #include @@ -109,7 +122,7 @@ class eventmanager; class eventreceiver; } -namespace math { +namespace algebra { class vector2d; } diff --git a/src/entity.cpp b/src/entity.cpp index 947a704..1af67fe 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -47,11 +47,11 @@ void entity::move(float_t x_velocity, float_t y_velocity) noexcept { // cpBodySetVelocity(_props.body.get(), {x_velocity, y_velocity}); } -void entity::set_velocity(const math::vector2d &velocity) noexcept { +void entity::set_velocity(const algebra::vector2d &velocity) noexcept { _props.velocity = velocity; } -math::vector2d entity::velocity() const noexcept { +algebra::vector2d entity::velocity() const noexcept { return _props.velocity; } diff --git a/src/entity.hpp b/src/entity.hpp index 8431adc..448737b 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -30,8 +30,8 @@ class entity : public std::enable_shared_from_this { int32_t y() const noexcept; void move(float_t x_velocity, float_t y_velocity) noexcept; - void set_velocity(const math::vector2d &velocity) noexcept; - math::vector2d velocity() const noexcept; + void set_velocity(const algebra::vector2d &velocity) noexcept; + algebra::vector2d velocity() const noexcept; void set_placement(int32_t x, int32_t y) noexcept; diff --git a/src/entityprops.hpp b/src/entityprops.hpp index 70afb09..8b45985 100644 --- a/src/entityprops.hpp +++ b/src/entityprops.hpp @@ -32,7 +32,7 @@ struct entityprops { geometry::point pivot{}; geometry::size size{}; float_t scale{1.0f}; - math::vector2d velocity{}; + algebra::vector2d velocity{}; std::string kind{}; std::string action{}; graphics::reflection reflection{graphics::reflection::none}; diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp index 9b674f2..0c8581f 100644 --- a/src/scriptengine.cpp +++ b/src/scriptengine.cpp @@ -11,6 +11,7 @@ #include "kv.hpp" #include "label.hpp" #include "loopable.hpp" +#include "overlay.hpp" #include "point.hpp" #include "postalservice.hpp" #include "socket.hpp" @@ -18,24 +19,15 @@ #include "vector2d.hpp" #include "widget.hpp" -using namespace audio; -using namespace framework; -using namespace input; -using namespace graphics; -using namespace math; -using namespace storage; -using namespace network; -using namespace memory; - sol::table require(sol::state &lua, const std::string &module) { - const auto data = io::read("scripts/" + module + ".lua"); + const auto data = storage::io::read("scripts/" + module + ".lua"); const auto script = std::string(data.begin(), data.end()); const auto result = lua.script(script); return result.get(); } -class lua_loopable : public loopable { +class lua_loopable : public framework::loopable { public: explicit lua_loopable(sol::function function) : _function(std::move(function)) {} @@ -113,7 +105,7 @@ auto _to_json(const sol::object &value) -> nlohmann::json { } } -void scriptengine::run() { +void framework::scriptengine::run() { sol::state lua; lua.open_libraries(); @@ -140,38 +132,38 @@ void scriptengine::run() { } ); - lua.new_usertype( + lua.new_usertype( "SoundManager", - "play", &soundmanager::play, - "stop", &soundmanager::stop + "play", &audio::soundmanager::play, + "stop", &audio::soundmanager::stop ); lua.new_enum( "Anchor", - "top", anchor::top, - "bottom", anchor::bottom, - "left", anchor::left, - "right", anchor::right, - "none", anchor::none + "top", framework::anchor::top, + "bottom", framework::anchor::bottom, + "left", framework::anchor::left, + "right", framework::anchor::right, + "none", framework::anchor::none ); lua.new_enum( "Reflection", - "none", reflection::none, - "horizontal", reflection::horizontal, - "vertical", reflection::vertical, - "both", reflection::both + "none", graphics::reflection::none, + "horizontal", graphics::reflection::horizontal, + "vertical", graphics::reflection::vertical, + "both", graphics::reflection::both ); struct reflectionproxy { entity &e; - void set(reflection value) { + void set(graphics::reflection value) { e.set_reflection(value); } void unset() { - e.set_reflection(reflection::none); + e.set_reflection(graphics::reflection::none); } }; @@ -219,7 +211,7 @@ void scriptengine::run() { e.set_velocity({x, y}); } - vector2d get() { + algebra::vector2d get() { return e.velocity(); } @@ -252,11 +244,11 @@ void scriptengine::run() { "y", sol::property(&velocityproxy::get_y, &velocityproxy::set_y) ); - lua.new_usertype( + lua.new_usertype( "KeyValue", - "get", [](kv &self, const std::string &key, sol::this_state state) { return self.get(key, state); }, - "set", [](kv &self, const std::string &key, const sol::object &new_value, sol::this_state state) { self.set(key, new_value, state); }, - "subscribe", [](kv &self, const std::string &key, const sol::function &callback, sol::this_state state) { self.subscribe(key, callback, state); } + "get", [](memory::kv &self, const std::string &key, sol::this_state state) { return self.get(key, state); }, + "set", [](memory::kv &self, const std::string &key, const sol::object &new_value, sol::this_state state) { self.set(key, new_value, state); }, + "subscribe", [](memory::kv &self, const std::string &key, const sol::function &callback, sol::this_state state) { self.subscribe(key, callback, state); } ); lua.new_usertype( @@ -275,7 +267,7 @@ void scriptengine::run() { "action", sol::property([](entity &e) -> actionproxy { return actionproxy{e}; }), "placement", sol::property([](entity &e) -> placementproxy { return placementproxy{e}; }), "velocity", sol::property([](entity &e) -> velocityproxy { return velocityproxy{e}; }), - "kv", sol::property([](entity &e) -> kv & { return e.kv(); }) + "kv", sol::property([](entity &e) -> memory::kv & { return e.kv(); }) ); lua.new_usertype( @@ -300,55 +292,55 @@ void scriptengine::run() { } ); - lua.new_usertype( + lua.new_usertype( "StateManager", - "is_keydown", &statemanager::is_keydown + "is_keydown", &framework::statemanager::is_keydown ); - lua.new_usertype( + lua.new_usertype( "SceneManager", - "set", &scenemanager::set + "set", &framework::scenemanager::set ); lua.new_enum( "WidgetType", - "label", widgettype::label + "label", graphics::widgettype::label ); - lua.new_usertype( + lua.new_usertype( "Overlay", - "create", &overlay::create, - "destroy", &overlay::destroy + "create", &graphics::overlay::create, + "destroy", &graphics::overlay::destroy ); - lua.new_usertype( + lua.new_usertype( "Engine", - "add_loopable", &engine::add_loopable, - "entitymanager", &engine::entitymanager, - "fontfactory", &engine::fontfactory, - "overlay", &engine::overlay, - "resourcemanager", &engine::resourcemanager, - "soundmanager", &engine::soundmanager, - "statemanager", &engine::statemanager, - "scenemanager", &engine::scenemanager, - "run", &engine::run + "add_loopable", &framework::engine::add_loopable, + "entitymanager", &framework::engine::entitymanager, + "fontfactory", &framework::engine::fontfactory, + "overlay", &framework::engine::overlay, + "resourcemanager", &framework::engine::resourcemanager, + "soundmanager", &framework::engine::soundmanager, + "statemanager", &framework::engine::statemanager, + "scenemanager", &framework::engine::scenemanager, + "run", &framework::engine::run ); - lua.new_usertype( + lua.new_usertype( "Font", - sol::constructors() + sol::constructors() ); - lua.new_usertype( + lua.new_usertype( "EngineFactory", - sol::constructors(), - "with_title", &enginefactory::with_title, - "with_width", &enginefactory::with_width, - "with_height", &enginefactory::with_height, - "with_scale", &enginefactory::with_scale, - "with_gravity", &enginefactory::with_gravity, - "with_fullscreen", &enginefactory::with_fullscreen, - "create", &enginefactory::create + sol::constructors(), + "with_title", &framework::enginefactory::with_title, + "with_width", &framework::enginefactory::with_width, + "with_height", &framework::enginefactory::with_height, + "with_scale", &framework::enginefactory::with_scale, + "with_gravity", &framework::enginefactory::with_gravity, + "with_fullscreen", &framework::enginefactory::with_fullscreen, + "create", &framework::enginefactory::create ); lua.new_usertype( @@ -368,20 +360,20 @@ void scriptengine::run() { "height", sol::property(&geometry::size::height, &geometry::size::set_height) ); - lua.new_usertype( + lua.new_usertype( "Socket", - sol::constructors(), - "emit", [](socket &sio, const std::string &event, sol::table data, sol::this_state state) { + sol::constructors(), + "emit", [](network::socket &sio, const std::string &event, sol::table data, sol::this_state state) { sol::state_view lua(state); const auto j = _to_json(data); sio.emit(event, j.dump()); }, - "on", [](socket &sio, const std::string &event, sol::function callback, sol::this_state state) { + "on", [](network::socket &sio, const std::string &event, sol::function callback, sol::this_state state) { sol::state_view lua(state); sio.on(event, [callback, lua](const std::string &data) { const auto j = nlohmann::json::parse(data); callback(_to_lua(j, lua)); }); }, - "rpc", [](socket &sio, const std::string &method, sol::table arguments, sol::function callback, sol::this_state state) { + "rpc", [](network::socket &sio, const std::string &method, sol::table arguments, sol::function callback, sol::this_state state) { sol::state_view lua(state); const auto args_json = _to_json(arguments); sio.rpc(method, args_json.dump(), [callback, lua](const std::string &response) { @@ -390,18 +382,18 @@ void scriptengine::run() { }); } ); - lua.new_usertype( + lua.new_usertype( "Color", - "color", sol::constructors(), + "color", sol::constructors(), - "r", sol::property(&color::r, &color::set_r), - "g", sol::property(&color::g, &color::set_g), - "b", sol::property(&color::b, &color::set_b), - "a", sol::property(&color::a, &color::set_a), + "r", sol::property(&graphics::color::r, &graphics::color::set_r), + "g", sol::property(&graphics::color::g, &graphics::color::set_g), + "b", sol::property(&graphics::color::b, &graphics::color::set_b), + "a", sol::property(&graphics::color::a, &graphics::color::set_a), - sol::meta_function::equal_to, &color::operator==, // sol::meta_function::not_equal_to, &color::operator!=, + sol::meta_function::equal_to, &graphics::color::operator==, // sol::meta_function::not_equal_to, &color::operator!=, - sol::meta_function::to_string, [](const color &c) { + sol::meta_function::to_string, [](const graphics::color &c) { return "color(" + std::to_string(c.r()) + ", " + std::to_string(c.g()) + ", " + std::to_string(c.b()) + ", " + @@ -411,73 +403,73 @@ void scriptengine::run() { lua.new_enum( "KeyEvent", - "up", keyevent::up, - "left", keyevent::left, - "down", keyevent::down, - "right", keyevent::right, - "space", keyevent::space + "up", input::keyevent::up, + "left", input::keyevent::left, + "down", input::keyevent::down, + "right", input::keyevent::right, + "space", input::keyevent::space ); - lua.new_usertype( + lua.new_usertype( "Mail", - sol::constructors, const std::string &, const std::string &)>() + sol::constructors, const std::string &, const std::string &)>() ); - lua.new_usertype( - "PostalService", sol::constructors(), - "post", &postalservice::post + lua.new_usertype( + "PostalService", sol::constructors(), + "post", &framework::postalservice::post ); - lua.new_usertype( - "TimeManager", sol::constructors(), - "set", &timermanager::set, - "singleshot", &timermanager::singleshot, - "clear", &timermanager::clear + lua.new_usertype( + "TimeManager", sol::constructors(), + "set", &framework::timermanager::set, + "singleshot", &framework::timermanager::singleshot, + "clear", &framework::timermanager::clear ); - lua.new_usertype( - "Vector2D", sol::constructors(), + lua.new_usertype( + "Vector2D", sol::constructors(), - "x", sol::property(&vector2d::x, &vector2d::set_x), - "y", sol::property(&vector2d::y, &vector2d::set_y), - "set", &vector2d::set, + "x", sol::property(&algebra::vector2d::x, &algebra::vector2d::set_x), + "y", sol::property(&algebra::vector2d::y, &algebra::vector2d::set_y), + "set", &algebra::vector2d::set, - "magnitude", &vector2d::magnitude, - "unit", &vector2d::unit, - "dot", &vector2d::dot, + "magnitude", &algebra::vector2d::magnitude, + "unit", &algebra::vector2d::unit, + "dot", &algebra::vector2d::dot, - sol::meta_function::addition, &vector2d::operator+, - sol::meta_function::subtraction, &vector2d::operator-, + sol::meta_function::addition, &algebra::vector2d::operator+, + sol::meta_function::subtraction, &algebra::vector2d::operator-, - "add_assign", &vector2d::operator+=, - "sub_assign", &vector2d::operator-=, - "mul_assign", &vector2d::operator*=, - "div_assign", &vector2d::operator/=, + "add_assign", &algebra::vector2d::operator+=, + "sub_assign", &algebra::vector2d::operator-=, + "mul_assign", &algebra::vector2d::operator*=, + "div_assign", &algebra::vector2d::operator/=, - sol::meta_function::equal_to, &vector2d::operator==, + sol::meta_function::equal_to, &algebra::vector2d::operator==, - "zero", &vector2d::zero, - "moving", &vector2d::moving, - "right", &vector2d::right, - "left", &vector2d::left + "zero", &algebra::vector2d::zero, + "moving", &algebra::vector2d::moving, + "right", &algebra::vector2d::right, + "left", &algebra::vector2d::left ); - lua.new_usertype