A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor: Improve architecture with dependency injection and type safety
esengine5429b3901735
diff --git a/src/esengine/core/Application.cpp b/src/esengine/core/Application.cppindex d95762d73..7fe959895 100644--- a/src/esengine/core/Application.cpp+++ b/src/esengine/core/Application.cpp@@ -79,15 +79,20 @@ void Application::init() {// Set up platform callbacksplatform_->setTouchCallback([this](TouchType type, const TouchPoint& point) {- Input::onTouchEvent(type, point);+ input_.onTouchEvent(type, point);onTouch(type, point);});platform_->setKeyCallback([this](KeyCode key, bool pressed) {- Input::onKeyEvent(key, pressed);+ input_.onKeyEvent(key, pressed);onKey(key, pressed);});+ platform_->setScrollCallback([this](f32 deltaX, f32 deltaY, f32 x, f32 y) {+ input_.onScrollEvent(deltaX, deltaY);+ (void)x; (void)y;+ });+platform_->setResizeCallback([this](u32 width, u32 height) {config_.width = width;config_.height = height;@@ -98,7 +103,7 @@ void Application::init() {});// Initialize subsystems- Input::init();+ input_.init();// Initialize resource managerresourceManager_.init();@@ -127,7 +132,7 @@ void Application::mainLoop() {deltaTime_ = platform_->getDeltaTime();// Update input state- Input::update();+ input_.update();// Update systemssystems_.update(registry_, static_cast<f32>(deltaTime_));@@ -175,7 +180,7 @@ void Application::shutdown() {resourceManager_.shutdown();// Shutdown input- Input::shutdown();+ input_.shutdown();// Shutdown platformplatform_->shutdown();diff --git a/src/esengine/core/Application.hpp b/src/esengine/core/Application.hppindex 29dfd4a75..ab8459f9e 100644--- a/src/esengine/core/Application.hpp+++ b/src/esengine/core/Application.hpp@@ -19,6 +19,7 @@// Project includes#include "Types.hpp"#include "../platform/Platform.hpp"+#include "../platform/input/Input.hpp"#include "../ecs/Registry.hpp"#include "../ecs/System.hpp"#include "../resource/ResourceManager.hpp"@@ -166,6 +167,12 @@ public:*/Renderer& getRenderer() { return *renderer_; }+ /**+ * @brief Gets the input system+ * @return Reference to the Input for querying input state+ */+ Input& getInput() { return input_; }+/** @brief Gets the current viewport width in pixels */u32 getWidth() const { return config_.width; }@@ -254,6 +261,8 @@ protected:ApplicationConfig config_;/** @brief Platform abstraction layer */Unique<Platform> platform_;+ /** @brief Input system instance */+ Input input_;/** @brief ECS entity/component registry */ecs::Registry registry_;/** @brief System execution group */diff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hppindex 7a05f1d3f..ae323b0b1 100644--- a/src/esengine/ecs/Registry.hpp+++ b/src/esengine/ecs/Registry.hpp@@ -386,7 +386,7 @@ public:if (!pool) return;// Selection sort- auto& entities = const_cast<std::vector<Entity>&>(pool->entities());+ auto& entities = pool->entities();auto& components = pool->components();for (usize i = 0; i < entities.size(); ++i) {diff --git a/src/esengine/ecs/SparseSet.hpp b/src/esengine/ecs/SparseSet.hppindex 4aec575fc..2a6c4f010 100644--- a/src/esengine/ecs/SparseSet.hpp+++ b/src/esengine/ecs/SparseSet.hpp@@ -293,6 +293,15 @@ public:*/const std::vector<Entity>& entities() const override { return dense_; }+ /**+ * @brief Gets the dense entity array (non-const)+ * @return Reference to entity vector+ *+ * @details Allows direct modification of entity order (e.g., for sorting).+ * Use with caution - must maintain correspondence with components().+ */+ std::vector<Entity>& entities() { return dense_; }+/*** @brief Gets the component array for direct iteration* @return Reference to component vectordiff --git a/src/esengine/editor/core/Selection.hpp b/src/esengine/editor/core/Selection.hppindex a2bddd696..4e6aadf9a 100644--- a/src/esengine/editor/core/Selection.hpp+++ b/src/esengine/editor/core/Selection.hpp@@ -57,7 +57,8 @@ struct SelectableId {if constexpr (std::is_integral_v<T>) {id.rawId = static_cast<u64>(value);} else if constexpr (std::is_pointer_v<T>) {- id.rawId = reinterpret_cast<u64>(value);+ // Use uintptr_t as intermediate type for safer pointer-to-integer conversion+ id.rawId = static_cast<u64>(reinterpret_cast<uintptr_t>(value));} else {id.rawId = std::hash<T>{}(value);}diff --git a/src/esengine/platform/input/Input.cpp b/src/esengine/platform/input/Input.cppindex ca5d4f35f..44bf970ce 100644--- a/src/esengine/platform/input/Input.cpp+++ b/src/esengine/platform/input/Input.cpp@@ -15,14 +15,6 @@namespace esengine {-// Static member definitions-std::array<TouchState, MAX_TOUCH_POINTS> Input::touchStates_;-std::array<TouchState, MAX_TOUCH_POINTS> Input::prevTouchStates_;-std::unordered_map<u32, bool> Input::keyStates_;-std::unordered_map<u32, bool> Input::prevKeyStates_;-glm::vec2 Input::mousePosition_{0.0f};-glm::vec2 Input::scrollDelta_{0.0f};-void Input::init() {for (auto& state : touchStates_) {state = TouchState{};@@ -52,33 +44,33 @@ void Input::update() {}// Touch input-bool Input::isTouchDown(u32 index) {+bool Input::isTouchDown(u32 index) const {if (index >= MAX_TOUCH_POINTS) return false;return touchStates_[index].active;}-bool Input::isTouchPressed(u32 index) {+bool Input::isTouchPressed(u32 index) const {if (index >= MAX_TOUCH_POINTS) return false;return touchStates_[index].active && !prevTouchStates_[index].active;}-bool Input::isTouchReleased(u32 index) {+bool Input::isTouchReleased(u32 index) const {if (index >= MAX_TOUCH_POINTS) return false;return !touchStates_[index].active && prevTouchStates_[index].active;}-glm::vec2 Input::getTouchPosition(u32 index) {+glm::vec2 Input::getTouchPosition(u32 index) const {if (index >= MAX_TOUCH_POINTS) return glm::vec2(0.0f);return glm::vec2(touchStates_[index].x, touchStates_[index].y);}-glm::vec2 Input::getTouchDelta(u32 index) {+glm::vec2 Input::getTouchDelta(u32 index) const {if (index >= MAX_TOUCH_POINTS) return glm::vec2(0.0f);const auto& state = touchStates_[index];return glm::vec2(state.x - state.startX, state.y - state.startY);}-u32 Input::getTouchCount() {+u32 Input::getTouchCount() const {u32 count = 0;for (const auto& state : touchStates_) {if (state.active) ++count;@@ -87,31 +79,35 @@ u32 Input::getTouchCount() {}// Keyboard input-bool Input::isKeyDown(KeyCode key) {+bool Input::isKeyDown(KeyCode key) const {auto it = keyStates_.find(static_cast<u32>(key));return it != keyStates_.end() && it->second;}-bool Input::isKeyPressed(KeyCode key) {+bool Input::isKeyPressed(KeyCode key) const {u32 keyCode = static_cast<u32>(key);- bool current = keyStates_.count(keyCode) && keyStates_[keyCode];- bool previous = prevKeyStates_.count(keyCode) && prevKeyStates_[keyCode];+ auto currentIt = keyStates_.find(keyCode);+ auto previousIt = prevKeyStates_.find(keyCode);+ bool current = currentIt != keyStates_.end() && currentIt->second;+ bool previous = previousIt != prevKeyStates_.end() && previousIt->second;return current && !previous;}-bool Input::isKeyReleased(KeyCode key) {+bool Input::isKeyReleased(KeyCode key) const {u32 keyCode = static_cast<u32>(key);- bool current = keyStates_.count(keyCode) && keyStates_[keyCode];- bool previous = prevKeyStates_.count(keyCode) && prevKeyStates_[keyCode];+ auto currentIt = keyStates_.find(keyCode);+ auto previousIt = prevKeyStates_.find(keyCode);+ bool current = currentIt != keyStates_.end() && currentIt->second;+ bool previous = previousIt != prevKeyStates_.end() && previousIt->second;return !current && previous;}// Mouse input-glm::vec2 Input::getMousePosition() {+glm::vec2 Input::getMousePosition() const {return mousePosition_;}-bool Input::isMouseButtonDown(u32 button) {+bool Input::isMouseButtonDown(u32 button) const {// Mouse button 0 is treated as touch 0if (button == 0) {return isTouchDown(0);@@ -181,7 +177,7 @@ void Input::onScrollEvent(f32 deltaX, f32 deltaY) {scrollDelta_.y += deltaY;}-glm::vec2 Input::getScrollDelta() {+glm::vec2 Input::getScrollDelta() const {return scrollDelta_;}diff --git a/src/esengine/platform/input/Input.hpp b/src/esengine/platform/input/Input.hppindex 473a1a40e..7fd403bde 100644--- a/src/esengine/platform/input/Input.hpp+++ b/src/esengine/platform/input/Input.hpp@@ -63,30 +63,40 @@ struct TouchState {// =============================================================================/**- * @brief Static input state manager+ * @brief Instance-based input state manager** @details Provides query methods for touch, keyboard, and mouse input.* State is updated each frame by calling update(). The platform* layer feeds events via onTouchEvent() and onKeyEvent().** @code+ * // Access through Application+ * Input& input = app.getInput();+ ** // In game loop- * Input::update();+ * input.update();** // Check touch input- * if (Input::isTouchPressed()) {- * glm::vec2 pos = Input::getTouchPosition();+ * if (input.isTouchPressed()) {+ * glm::vec2 pos = input.getTouchPosition();* // Handle tap at pos* }** // Check keyboard- * if (Input::isKeyDown(KeyCode::Space)) {+ * if (input.isKeyDown(KeyCode::Space)) {* // Jump* }* @endcode*/class Input {public:+ Input() = default;+ ~Input() = default;++ // Non-copyable+ Input(const Input&) = delete;+ Input& operator=(const Input&) = delete;+// =========================================================================// Lifecycle// =========================================================================@@ -95,19 +105,19 @@ public:* @brief Initializes the input system* @details Clears all input state. Called automatically by Application.*/- static void init();+ void init();/*** @brief Shuts down the input system*/- static void shutdown();+ void shutdown();/*** @brief Updates input state for new frame* @details Copies current state to previous state for edge detection.* Must be called once per frame before checking input.*/- static void update();+ void update();// =========================================================================// Touch Input@@ -118,41 +128,41 @@ public:* @param index Touch point index (0 = primary touch)* @return True if touch is down*/- static bool isTouchDown(u32 index = 0);+ bool isTouchDown(u32 index = 0) const;/*** @brief Checks if a touch just started this frame* @param index Touch point index* @return True if touch began this frame*/- static bool isTouchPressed(u32 index = 0);+ bool isTouchPressed(u32 index = 0) const;/*** @brief Checks if a touch just ended this frame* @param index Touch point index* @return True if touch ended this frame*/- static bool isTouchReleased(u32 index = 0);+ bool isTouchReleased(u32 index = 0) const;/*** @brief Gets the current position of a touch point* @param index Touch point index* @return Position in screen coordinates*/- static glm::vec2 getTouchPosition(u32 index = 0);+ glm::vec2 getTouchPosition(u32 index = 0) const;/*** @brief Gets the movement delta since touch began* @param index Touch point index* @return (current position - start position)*/- static glm::vec2 getTouchDelta(u32 index = 0);+ glm::vec2 getTouchDelta(u32 index = 0) const;/*** @brief Gets the number of active touch points* @return Count of currently active touches*/- static u32 getTouchCount();+ u32 getTouchCount() const;// =========================================================================// Keyboard Input@@ -163,21 +173,21 @@ public:* @param key The key code* @return True if key is down*/- static bool isKeyDown(KeyCode key);+ bool isKeyDown(KeyCode key) const;/*** @brief Checks if a key was just pressed this frame* @param key The key code* @return True if key was just pressed*/- static bool isKeyPressed(KeyCode key);+ bool isKeyPressed(KeyCode key) const;/*** @brief Checks if a key was just released this frame* @param key The key code* @return True if key was just released*/- static bool isKeyReleased(KeyCode key);+ bool isKeyReleased(KeyCode key) const;// =========================================================================// Mouse Input for web debugging@@ -190,14 +200,14 @@ public:* @note Primarily for web/desktop debugging. On touch devices,* use getTouchPosition() instead.*/- static glm::vec2 getMousePosition();+ glm::vec2 getMousePosition() const;/*** @brief Checks if a mouse button is held down* @param button Mouse button index (0 = left, 1 = right, 2 = middle)* @return True if button is down*/- static bool isMouseButtonDown(u32 button = 0);+ bool isMouseButtonDown(u32 button = 0) const;// =========================================================================// Scroll Input@@ -207,7 +217,7 @@ public:* @brief Gets the scroll delta for the current frame* @return Scroll delta (x = horizontal, y = vertical)*/- static glm::vec2 getScrollDelta();+ glm::vec2 getScrollDelta() const;// =========================================================================// Platform Interface for internal use@@ -219,7 +229,7 @@ public:* @param point Touch point data* @note Internal use only - called by Platform implementation*/- static void onTouchEvent(TouchType type, const TouchPoint& point);+ void onTouchEvent(TouchType type, const TouchPoint& point);/*** @brief Called by platform on key events@@ -227,7 +237,7 @@ public:* @param pressed True if pressed, false if released* @note Internal use only - called by Platform implementation*/- static void onKeyEvent(KeyCode key, bool pressed);+ void onKeyEvent(KeyCode key, bool pressed);/*** @brief Called by platform on scroll events@@ -235,24 +245,24 @@ public:* @param deltaY Vertical scroll delta* @note Internal use only - called by Platform implementation*/- static void onScrollEvent(f32 deltaX, f32 deltaY);+ void onScrollEvent(f32 deltaX, f32 deltaY);private:/** @brief Current frame touch states */- static std::array<TouchState, MAX_TOUCH_POINTS> touchStates_;+ std::array<TouchState, MAX_TOUCH_POINTS> touchStates_;/** @brief Previous frame touch states */- static std::array<TouchState, MAX_TOUCH_POINTS> prevTouchStates_;+ std::array<TouchState, MAX_TOUCH_POINTS> prevTouchStates_;/** @brief Current frame key states */- static std::unordered_map<u32, bool> keyStates_;+ std::unordered_map<u32, bool> keyStates_;/** @brief Previous frame key states */- static std::unordered_map<u32, bool> prevKeyStates_;+ std::unordered_map<u32, bool> prevKeyStates_;/** @brief Current mouse position */- static glm::vec2 mousePosition_;+ glm::vec2 mousePosition_;/** @brief Current frame scroll delta */- static glm::vec2 scrollDelta_;+ glm::vec2 scrollDelta_;};} // namespace esenginediff --git a/src/esengine/platform/native/NativePlatform.cpp b/src/esengine/platform/native/NativePlatform.cppindex 24925bffb..b6cced88f 100644--- a/src/esengine/platform/native/NativePlatform.cpp+++ b/src/esengine/platform/native/NativePlatform.cpp@@ -325,8 +325,6 @@ void NativePlatform::glfwScrollCallback(GLFWwindow* window, double xoffset, doubauto* platform = static_cast<NativePlatform*>(glfwGetWindowUserPointer(window));if (!platform) return;- Input::onScrollEvent(static_cast<f32>(xoffset), static_cast<f32>(yoffset));-if (platform->scrollCallback_) {platform->scrollCallback_(static_cast<f32>(xoffset), static_cast<f32>(yoffset),static_cast<f32>(platform->mouseX_),diff --git a/src/esengine/renderer/Shader.cpp b/src/esengine/renderer/Shader.cppindex 43e90319d..7fd1e76ba 100644--- a/src/esengine/renderer/Shader.cpp+++ b/src/esengine/renderer/Shader.cpp@@ -13,6 +13,8 @@#include "Shader.hpp"#include "../core/Log.hpp"+#include <fstream>+#ifdef ES_PLATFORM_WEB#include <GLES3/gl3.h>#else@@ -71,11 +73,42 @@ Unique<Shader> Shader::create(const std::string& vertexSrc, const std::string& f}Unique<Shader> Shader::createFromFile(const std::string& vertexPath, const std::string& fragmentPath) {- // TODO: Implement file loading- ES_LOG_ERROR("Shader::createFromFile not implemented yet");- (void)vertexPath;- (void)fragmentPath;- return nullptr;+ auto readFile = [](const std::string& filepath) -> std::string {+ std::ifstream file(filepath, std::ios::in | std::ios::binary);+ if (!file.is_open()) {+ ES_LOG_ERROR("Failed to open shader file: {}", filepath);+ return "";+ }++ file.seekg(0, std::ios::end);+ const auto fileSize = file.tellg();+ if (fileSize <= 0) {+ ES_LOG_ERROR("Shader file is empty: {}", filepath);+ return "";+ }++ std::string content;+ content.resize(static_cast<usize>(fileSize));+ file.seekg(0, std::ios::beg);+ file.read(&content[0], fileSize);++ if (file.fail()) {+ ES_LOG_ERROR("Failed to read shader file: {}", filepath);+ return "";+ }++ return content;+ };++ std::string vertexSrc = readFile(vertexPath);+ std::string fragmentSrc = readFile(fragmentPath);++ if (vertexSrc.empty() || fragmentSrc.empty()) {+ ES_LOG_ERROR("Failed to load shader files: vertex={}, fragment={}", vertexPath, fragmentPath);+ return nullptr;+ }++ return create(vertexSrc, fragmentSrc);}void Shader::bind() const {