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
chore: remove dead platform abstraction — Input, Platform, WebPlatform
esengine136459bd235f
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 8a27030b0..552145cfb 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -59,7 +59,6 @@ set(ESENGINE_SOURCESsrc/esengine/renderer/plugins/UIElementPlugin.cppsrc/esengine/renderer/plugins/TextPlugin.cppsrc/esengine/renderer/plugins/ShapePlugin.cpp- src/esengine/platform/input/Input.cppsrc/esengine/platform/PathResolver.cppsrc/esengine/animation/TweenSystem.cppsrc/esengine/ecs/UILayoutSystem.cpp@@ -159,9 +158,7 @@ set(ESENGINE_HEADERSsrc/esengine/renderer/plugins/TextPlugin.hppsrc/esengine/renderer/plugins/ShapePlugin.hppsrc/esengine/renderer/plugins/SpinePlugin.hpp- src/esengine/platform/Platform.hppsrc/esengine/platform/FileSystem.hpp- src/esengine/platform/input/Input.hppsrc/esengine/events/Events.hppsrc/esengine/events/Connection.hppsrc/esengine/events/Signal.hpp@@ -222,7 +219,6 @@ set(GENERATED_EDITOR_API "${CMAKE_CURRENT_SOURCE_DIR}/src/esengine/bindings/Editset(GENERATED_TS_DEFINITIONS "${CMAKE_CURRENT_SOURCE_DIR}/sdk/src/wasm.generated.ts")list(APPEND ESENGINE_SOURCES- src/esengine/platform/web/WebPlatform.cppsrc/esengine/platform/web/WebFileSystem.cpp)diff --git a/src/esengine/ESEngine.hpp b/src/esengine/ESEngine.hppindex 267f4bbae..7f3808f19 100644--- a/src/esengine/ESEngine.hpp+++ b/src/esengine/ESEngine.hpp@@ -42,10 +42,6 @@#include "renderer/Shader.hpp"#include "renderer/Texture.hpp"-// Platform-#include "platform/Platform.hpp"-#include "platform/input/Input.hpp"-// =============================================================================// Version Information// =============================================================================diff --git a/src/esengine/platform/Platform.hpp b/src/esengine/platform/Platform.hppdeleted file mode 100644index 576c5d3be..000000000--- a/src/esengine/platform/Platform.hpp+++ /dev/null@@ -1,371 +0,0 @@-/**- * @file Platform.hpp- * @brief Platform abstraction layer interface- * @details Defines the abstract interface for platform-specific functionality- * including window management, event handling, and timing.- *- * @author ESEngine Team- * @date 2025- *- * @copyright Copyright (c) 2025 ESEngine Team- * Licensed under the MIT License.- */-#pragma once--// =============================================================================-// Includes-// =============================================================================--// Project includes-#include "../core/Types.hpp"--// Standard library-#include <functional>-#include <string>--namespace esengine {--// =============================================================================-// Touch Events-// =============================================================================--/**- * @brief Types of touch events- */-enum class TouchType : u8 {- Begin = 0, ///< Touch started (finger down)- Move = 1, ///< Touch moved (finger dragged)- End = 2, ///< Touch ended (finger lifted)- Cancel = 3 ///< Touch cancelled (system interrupted)-};--/**- * @brief Touch point data- *- * @details Contains the position and identifier for a single touch point.- * The ID is used to track individual fingers in multi-touch.- */-struct TouchPoint {- /** @brief Unique identifier for this touch (for multi-touch tracking) */- i32 id;- /** @brief X position in screen coordinates */- f32 x;- /** @brief Y position in screen coordinates */- f32 y;-};--// =============================================================================-// Key Codes-// =============================================================================--/**- * @brief Keyboard key codes- *- * @details Subset of key codes suitable for mobile/web platforms.- * Maps to standard JavaScript key codes for web compatibility.- */-enum class KeyCode : u32 {- Unknown = 0,- Backspace = 8, ///< Backspace key- Tab = 9, ///< Tab key- Enter = 13, ///< Enter/Return- Escape = 27, ///< Escape key- Space = 32, ///< Spacebar- Delete = 46, ///< Delete key- Home = 36, ///< Home key- End = 35, ///< End key- Left = 37, ///< Left arrow- Up = 38, ///< Up arrow- Right = 39, ///< Right arrow- Down = 40, ///< Down arrow-- // Letter keys- A = 65, ///< A key- C = 67, ///< C key- D = 68, ///< D key- N = 78, ///< N key- O = 79, ///< O key- S = 83, ///< S key- V = 86, ///< V key- W = 87, ///< W key- X = 88, ///< X key- Y = 89, ///< Y key- Z = 90, ///< Z key-- // Function keys- F1 = 112, ///< F1 key- F2 = 113, ///< F2 key- F3 = 114, ///< F3 key- F4 = 115, ///< F4 key- F5 = 116, ///< F5 key- F6 = 117, ///< F6 key- F7 = 118, ///< F7 key- F8 = 119, ///< F8 key- F9 = 120, ///< F9 key- F10 = 121, ///< F10 key- F11 = 122, ///< F11 key- F12 = 123, ///< F12 key-- // Modifier keys- LeftShift = 16, ///< Left Shift key- RightShift = 161, ///< Right Shift key- LeftControl = 17, ///< Left Control key- RightControl = 162, ///< Right Control key- LeftAlt = 18, ///< Left Alt key- RightAlt = 165 ///< Right Alt key-};--// =============================================================================-// Event Callbacks-// =============================================================================--/**- * @brief Callback type for touch events- * @param type The type of touch event- * @param point Touch position and identifier- */-using TouchCallback = std::function<void(TouchType type, const TouchPoint& point)>;--/**- * @brief Callback type for keyboard events- * @param key The key code- * @param pressed True if pressed, false if released- */-using KeyCallback = std::function<void(KeyCode key, bool pressed)>;--/**- * @brief Callback type for window resize events- * @param width New width in pixels- * @param height New height in pixels- */-using ResizeCallback = std::function<void(u32 width, u32 height)>;--/**- * @brief Callback type for scroll events- * @param deltaX Horizontal scroll delta- * @param deltaY Vertical scroll delta- * @param x Mouse X position- * @param y Mouse Y position- */-using ScrollCallback = std::function<void(f32 deltaX, f32 deltaY, f32 x, f32 y)>;--/**- * @brief Callback type for text input events- * @param text UTF-8 encoded text input- */-using TextInputCallback = std::function<void(const std::string& text)>;--/**- * @brief Callback type for mouse move events- * @param x Mouse X position- * @param y Mouse Y position- */-using MouseMoveCallback = std::function<void(f32 x, f32 y)>;--/**- * @brief Mouse button identifiers- */-enum class MouseButton : u8 {- Left = 0,- Right = 1,- Middle = 2-};--/**- * @brief Callback type for mouse button events- * @param button The mouse button- * @param pressed True if pressed, false if released- * @param x Mouse X position- * @param y Mouse Y position- */-using MouseButtonCallback = std::function<void(MouseButton button, bool pressed, f32 x, f32 y)>;--// =============================================================================-// Platform Class-// =============================================================================--/**- * @brief Abstract interface for platform-specific functionality- *- * @details Provides a common API for window management, event handling,- * and timing across different platforms (Web, Native, etc.).- * Implementations exist for specific platforms.- *- * @code- * auto platform = Platform::create();- * platform->initialize(800, 600);- *- * platform->setTouchCallback([](TouchType type, const TouchPoint& pt) {- * // Handle touch- * });- *- * while (platform->isRunning()) {- * platform->pollEvents();- * // Update and render- * platform->swapBuffers();- * }- *- * platform->shutdown();- * @endcode- */-class Platform {-public:- /** @brief Virtual destructor for proper cleanup */- virtual ~Platform() = default;-- // =========================================================================- // Lifecycle- // =========================================================================-- /**- * @brief Initializes the platform- * @param width Initial window width in pixels- * @param height Initial window height in pixels- * @return True on success- *- * @details Creates window/canvas and initializes graphics context.- */- virtual bool initialize(u32 width, u32 height) = 0;-- /**- * @brief Shuts down the platform- * @details Releases all platform resources and destroys window.- */- virtual void shutdown() = 0;-- // =========================================================================- // Frame Handling- // =========================================================================-- /**- * @brief Polls and dispatches pending events- * @details Should be called once per frame at the start of the loop.- */- virtual void pollEvents() = 0;-- /**- * @brief Swaps front and back buffers- * @details Presents the rendered frame. Should be called at end of loop.- */- virtual void swapBuffers() = 0;-- // =========================================================================- // Time- // =========================================================================-- /**- * @brief Gets time since platform initialization- * @return Time in seconds (high precision)- */- virtual f64 getTime() const = 0;-- /**- * @brief Gets time since last frame- * @return Delta time in seconds- */- virtual f64 getDeltaTime() const = 0;-- // =========================================================================- // Window Properties- // =========================================================================-- /**- * @brief Gets the window/canvas width- * @return Width in pixels- */- virtual u32 getWindowWidth() const = 0;-- /**- * @brief Gets the window/canvas height- * @return Height in pixels- */- virtual u32 getWindowHeight() const = 0;-- /**- * @brief Gets the window aspect ratio- * @return Width divided by height- */- virtual f32 getAspectRatio() const = 0;-- /**- * @brief Gets the device pixel ratio- * @return Ratio of physical pixels to CSS pixels (for high-DPI displays)- */- virtual f32 getDevicePixelRatio() const = 0;-- // =========================================================================- // State- // =========================================================================-- /**- * @brief Checks if the application should continue running- * @return True if running, false if quit was requested- */- virtual bool isRunning() const = 0;-- /**- * @brief Requests the application to quit- * @details Sets a flag that causes isRunning() to return false.- */- virtual void requestQuit() = 0;-- // =========================================================================- // Event Callbacks- // =========================================================================-- /**- * @brief Sets the touch event callback- * @param callback Function to call on touch events- */- virtual void setTouchCallback(TouchCallback callback) = 0;-- /**- * @brief Sets the keyboard event callback- * @param callback Function to call on key events- */- virtual void setKeyCallback(KeyCallback callback) = 0;-- /**- * @brief Sets the window resize callback- * @param callback Function to call on resize- */- virtual void setResizeCallback(ResizeCallback callback) = 0;-- /**- * @brief Sets the scroll event callback- * @param callback Function to call on scroll events- */- virtual void setScrollCallback(ScrollCallback callback) = 0;-- /**- * @brief Sets the text input callback- * @param callback Function to call on text input events- */- virtual void setTextInputCallback(TextInputCallback callback) = 0;-- /**- * @brief Sets the mouse move callback- * @param callback Function to call on mouse move events- */- virtual void setMouseMoveCallback(MouseMoveCallback callback) = 0;-- /**- * @brief Sets the mouse button callback- * @param callback Function to call on mouse button events- */- virtual void setMouseButtonCallback(MouseButtonCallback callback) = 0;-- // =========================================================================- // Factory- // =========================================================================-- /**- * @brief Creates a platform-appropriate implementation- * @return Unique pointer to the platform instance- *- * @details Returns WebPlatform for ES_PLATFORM_WEB.- */- static Unique<Platform> create();-};--} // namespace esenginediff --git a/src/esengine/platform/input/Input.cpp b/src/esengine/platform/input/Input.cppdeleted file mode 100644index a79293446..000000000--- a/src/esengine/platform/input/Input.cpp+++ /dev/null@@ -1,189 +0,0 @@-/**- * @file Input.cpp- * @brief Input system implementation- * @details Manages touch, mouse, and keyboard input state tracking.- *- * @author ESEngine Team- * @date 2025- *- * @copyright Copyright (c) 2025 ESEngine Team- * Licensed under the MIT License.- */--#include "Input.hpp"-#include "../../core/Log.hpp"--namespace esengine {--void Input::init() {- for (auto& state : touchStates_) {- state = TouchState{};- }- for (auto& state : prevTouchStates_) {- state = TouchState{};- }- keyStates_.clear();- prevKeyStates_.clear();- mousePosition_ = glm::vec2(0.0f);- scrollDelta_ = glm::vec2(0.0f);-- ES_LOG_INFO("Input system initialized");-}--void Input::shutdown() {- ES_LOG_INFO("Input system shutdown");-}--void Input::update() {- prevTouchStates_ = touchStates_;- std::swap(prevKeyStates_, keyStates_);- keyStates_.clear();- for (const auto& [key, pressed] : prevKeyStates_) {- if (pressed) {- keyStates_[key] = true;- }- }- scrollDelta_ = glm::vec2(0.0f);-}--// Touch input-bool Input::isTouchDown(u32 index) const {- if (index >= MAX_TOUCH_POINTS) return false;- return touchStates_[index].active;-}--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) const {- if (index >= MAX_TOUCH_POINTS) return false;- return !touchStates_[index].active && prevTouchStates_[index].active;-}--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) 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() const {- u32 count = 0;- for (const auto& state : touchStates_) {- if (state.active) ++count;- }- return count;-}--// Keyboard input-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) const {- u32 keyCode = static_cast<u32>(key);- 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) const {- u32 keyCode = static_cast<u32>(key);- 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() const {- return mousePosition_;-}--bool Input::isMouseButtonDown(u32 button) const {- // Mouse button 0 is treated as touch 0- if (button == 0) {- return isTouchDown(0);- }- return false;-}--// Event handlers-void Input::onTouchEvent(TouchType type, const TouchPoint& point) {- // Find or allocate touch slot- u32 index = MAX_TOUCH_POINTS;-- // Find existing touch with this ID- for (u32 i = 0; i < MAX_TOUCH_POINTS; ++i) {- if (touchStates_[i].active && touchStates_[i].touchId == point.id) {- index = i;- break;- }- }-- // If new touch, find empty slot- if (index == MAX_TOUCH_POINTS && type == TouchType::Begin) {- for (u32 i = 0; i < MAX_TOUCH_POINTS; ++i) {- if (!touchStates_[i].active) {- index = i;- break;- }- }- }-- if (index >= MAX_TOUCH_POINTS) return;-- auto& state = touchStates_[index];-- switch (type) {- case TouchType::Begin:- state.active = true;- state.touchId = point.id;- state.x = point.x;- state.y = point.y;- state.startX = point.x;- state.startY = point.y;- break;-- case TouchType::Move:- state.x = point.x;- state.y = point.y;- break;-- case TouchType::End:- case TouchType::Cancel:- state.active = false;- state.touchId = -1;- break;- }-- // Also update mouse position for debugging- if (index == 0) {- mousePosition_ = glm::vec2(point.x, point.y);- }-}--void Input::onKeyEvent(KeyCode key, bool pressed) {- keyStates_[static_cast<u32>(key)] = pressed;-}--void Input::onScrollEvent(f32 deltaX, f32 deltaY) {- scrollDelta_.x += deltaX;- scrollDelta_.y += deltaY;-}--glm::vec2 Input::getScrollDelta() const {- return scrollDelta_;-}--} // namespace esenginediff --git a/src/esengine/platform/input/Input.hpp b/src/esengine/platform/input/Input.hppdeleted file mode 100644index 7760ac33a..000000000--- a/src/esengine/platform/input/Input.hpp+++ /dev/null@@ -1,269 +0,0 @@-/**- * @file Input.hpp- * @brief Input state management for touch and keyboard- * @details Provides a static interface for querying touch, keyboard, and- * mouse input state. Tracks current and previous frame states- * for detecting pressed/released transitions.- *- * @author ESEngine Team- * @date 2025- *- * @copyright Copyright (c) 2025 ESEngine Team- * Licensed under the MIT License.- */-#pragma once--// =============================================================================-// Includes-// =============================================================================--// Project includes-#include "../../core/Types.hpp"-#include "../../math/Math.hpp"-#include "../Platform.hpp"--// Standard library-#include <array>-#include <unordered_map>--namespace esengine {--// =============================================================================-// Constants-// =============================================================================--/** @brief Maximum number of simultaneous touch points supported */-constexpr u32 MAX_TOUCH_POINTS = 10;--// =============================================================================-// Touch State-// =============================================================================--/**- * @brief State of a single touch point- *- * @details Tracks the current position and starting position of a touch- * for calculating movement deltas.- */-struct TouchState {- /** @brief Whether this touch point is currently active */- bool active = false;- /** @brief Platform-assigned touch identifier */- i32 touchId = -1;- /** @brief Current X position */- f32 x = 0.0f;- /** @brief Current Y position */- f32 y = 0.0f;- /** @brief X position when touch began */- f32 startX = 0.0f;- /** @brief Y position when touch began */- f32 startY = 0.0f;-};--// =============================================================================-// Input Class-// =============================================================================--/**- * @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- * Input& input = app.require<Input>();- *- * // In game loop- * input.update();- *- * // Check touch input- * if (input.isTouchPressed()) {- * glm::vec2 pos = input.getTouchPosition();- * // Handle tap at pos- * }- *- * // Check keyboard- * 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- // =========================================================================-- /**- * @brief Initializes the input system- * @details Clears all input state. Called automatically by App.- */- void init();-- /**- * @brief Shuts down the input system- */- 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.- */- void update();-- // =========================================================================- // Touch Input- // =========================================================================-- /**- * @brief Checks if a touch point is currently active- * @param index Touch point index (0 = primary touch)- * @return True if touch is down- */- 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- */- 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- */- 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- */- 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)- */- glm::vec2 getTouchDelta(u32 index = 0) const;-- /**- * @brief Gets the number of active touch points- * @return Count of currently active touches- */- u32 getTouchCount() const;-- // =========================================================================- // Keyboard Input- // =========================================================================-- /**- * @brief Checks if a key is currently held down- * @param key The key code- * @return True if key is down- */- 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- */- 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- */- bool isKeyReleased(KeyCode key) const;-- // =========================================================================- // Mouse Input for web debugging- // =========================================================================-- /**- * @brief Gets the current mouse position- * @return Mouse position in screen coordinates- *- * @note Primarily for web/desktop debugging. On touch devices,- * use getTouchPosition() instead.- */- 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- */- bool isMouseButtonDown(u32 button = 0) const;-- // =========================================================================- // Scroll Input- // =========================================================================-- /**- * @brief Gets the scroll delta for the current frame- * @return Scroll delta (x = horizontal, y = vertical)- */- glm::vec2 getScrollDelta() const;-- // =========================================================================- // Platform Interface for internal use- // =========================================================================-- /**- * @brief Called by platform on touch events- * @param type Touch event type- * @param point Touch point data- * @note Internal use only - called by Platform implementation- */- void onTouchEvent(TouchType type, const TouchPoint& point);-- /**- * @brief Called by platform on key events- * @param key Key code- * @param pressed True if pressed, false if released- * @note Internal use only - called by Platform implementation- */- void onKeyEvent(KeyCode key, bool pressed);-- /**- * @brief Called by platform on scroll events- * @param deltaX Horizontal scroll delta- * @param deltaY Vertical scroll delta- * @note Internal use only - called by Platform implementation- */- void onScrollEvent(f32 deltaX, f32 deltaY);--private:- /** @brief Current frame touch states */- std::array<TouchState, MAX_TOUCH_POINTS> touchStates_;- /** @brief Previous frame touch states */- std::array<TouchState, MAX_TOUCH_POINTS> prevTouchStates_;-- /** @brief Current frame key states */- std::unordered_map<u32, bool> keyStates_;- /** @brief Previous frame key states */- std::unordered_map<u32, bool> prevKeyStates_;-- /** @brief Current mouse position */- glm::vec2 mousePosition_;-- /** @brief Current frame scroll delta */- glm::vec2 scrollDelta_;-};--} // namespace esenginediff --git a/src/esengine/platform/web/WebPlatform.cpp b/src/esengine/platform/web/WebPlatform.cppdeleted file mode 100644index 8d7e1bcbd..000000000--- a/src/esengine/platform/web/WebPlatform.cpp+++ /dev/null@@ -1,321 +0,0 @@-/**- * @file WebPlatform.cpp- * @brief Web/Emscripten platform implementation- * @details Implements Platform interface for WebGL context management,- * event handling, and JavaScript interop via C API.- *- * @author ESEngine Team- * @date 2025- *- * @copyright Copyright (c) 2025 ESEngine Team- * Licensed under the MIT License.- */--#include "../Platform.hpp"-#include "../../core/Log.hpp"-#include "../../math/Math.hpp"--#include <cstring>--#ifdef ES_PLATFORM_WEB- #include <emscripten.h>- #include <emscripten/html5.h>- #include <GLES3/gl3.h>-#endif--namespace esengine {--class WebPlatform : public Platform {-public:- WebPlatform() = default;- ~WebPlatform() override = default;-- bool initialize(u32 width, u32 height) override {- width_ = width;- height_ = height;- running_ = true;--#ifdef ES_PLATFORM_WEB- context_ = EM_ASM_INT({- var canvas = Module.canvas;- if (!canvas) {- console.error('[ESEngine] Module.canvas not set');- return 0;- }- var ctx = canvas.getContext('webgl2', {- alpha: false,- depth: true,- stencil: false,- antialias: true,- premultipliedAlpha: true,- preserveDrawingBuffer: false- });- if (!ctx) {- ctx = canvas.getContext('webgl', {- alpha: false,- depth: true,- stencil: false,- antialias: true,- premultipliedAlpha: true,- preserveDrawingBuffer: false- });- }- if (!ctx) {- console.error('[ESEngine] Failed to create WebGL context');- return 0;- }- var handle = GL.registerContext(ctx, {- majorVersion: ctx.getParameter(0x1F02).indexOf('WebGL 2') === 0 ? 2 : 1,- minorVersion: 0,- enableExtensionsByDefault: true- });- return handle;- });-- ES_LOG_INFO("WebGL context creation result: {}", context_);- if (context_ <= 0) {- ES_LOG_ERROR("Failed to create WebGL context");- return false;- }-- EMSCRIPTEN_RESULT res = emscripten_webgl_make_context_current(context_);- if (res != EMSCRIPTEN_RESULT_SUCCESS) {- ES_LOG_ERROR("Failed to make WebGL context current, error: {}", res);- return false;- }-- devicePixelRatio_ = EM_ASM_DOUBLE({- return Module.canvas ? (window.devicePixelRatio || 1) : 1;- });-- width_ = width;- height_ = height;-- ES_LOG_INFO("WebPlatform initialized ({}x{}, DPR: {})", width_, height_, devicePixelRatio_);-#endif- lastTime_ = getTime();- return true;- }-- void shutdown() override {-#ifdef ES_PLATFORM_WEB- if (context_ > 0) {- emscripten_webgl_destroy_context(context_);- context_ = 0;- }-#endif- running_ = false;- ES_LOG_INFO("WebPlatform shutdown");- }-- void pollEvents() override {- // Events are handled via callbacks in Emscripten- f64 currentTime = getTime();- deltaTime_ = currentTime - lastTime_;- lastTime_ = currentTime;- }-- void swapBuffers() override {- // Emscripten handles buffer swapping automatically- }-- f64 getTime() const override {-#ifdef ES_PLATFORM_WEB- return emscripten_get_now() / 1000.0;-#else- return 0.0;-#endif- }-- f64 getDeltaTime() const override {- return deltaTime_;- }-- u32 getWindowWidth() const override { return width_; }- u32 getWindowHeight() const override { return height_; }-- f32 getAspectRatio() const override {- return height_ > 0 ? static_cast<f32>(width_) / static_cast<f32>(height_) : 1.0f;- }-- f32 getDevicePixelRatio() const override { return devicePixelRatio_; }-- bool isRunning() const override { return running_; }- void requestQuit() override { running_ = false; }-- void setTouchCallback(TouchCallback callback) override {- touchCallback_ = std::move(callback);- }-- void setKeyCallback(KeyCallback callback) override {- keyCallback_ = std::move(callback);- }-- void setResizeCallback(ResizeCallback callback) override {- resizeCallback_ = std::move(callback);- }-- void setScrollCallback(ScrollCallback callback) override {- scrollCallback_ = std::move(callback);- }-- void setTextInputCallback(TextInputCallback callback) override {- textInputCallback_ = std::move(callback);- }-- void setMouseMoveCallback(MouseMoveCallback callback) override {- mouseMoveCallback_ = std::move(callback);- }-- void setMouseButtonCallback(MouseButtonCallback callback) override {- mouseButtonCallback_ = std::move(callback);- }-- // Static instance for callbacks- static WebPlatform* instance_;--private:- void setupEventCallbacks() {-#ifdef ES_PLATFORM_WEB- instance_ = this;-- // Touch events- emscripten_set_touchstart_callback("", this, true, touchCallback);- emscripten_set_touchmove_callback("", this, true, touchCallback);- emscripten_set_touchend_callback("", this, true, touchCallback);- emscripten_set_touchcancel_callback("", this, true, touchCallback);-- // Mouse events (simulate touch)- emscripten_set_mousedown_callback("", this, true, mouseCallback);- emscripten_set_mousemove_callback("", this, true, mouseCallback);- emscripten_set_mouseup_callback("", this, true, mouseCallback);-- // Keyboard events- emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCallback);- emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCallback);-- // Resize event- emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true, resizeCallback);-#endif- }--#ifdef ES_PLATFORM_WEB- static EM_BOOL touchCallback(int eventType, const EmscriptenTouchEvent* event, void* userData) {- auto* platform = static_cast<WebPlatform*>(userData);- if (!platform->touchCallback_) return false;-- TouchType type;- switch (eventType) {- case EMSCRIPTEN_EVENT_TOUCHSTART: type = TouchType::Begin; break;- case EMSCRIPTEN_EVENT_TOUCHMOVE: type = TouchType::Move; break;- case EMSCRIPTEN_EVENT_TOUCHEND: type = TouchType::End; break;- case EMSCRIPTEN_EVENT_TOUCHCANCEL: type = TouchType::Cancel; break;- default: return false;- }-- for (int i = 0; i < event->numTouches; ++i) {- if (event->touches[i].isChanged) {- TouchPoint point;- point.id = event->touches[i].identifier;- point.x = static_cast<f32>(event->touches[i].targetX);- point.y = static_cast<f32>(event->touches[i].targetY);- platform->touchCallback_(type, point);- }- }-- return true;- }-- static EM_BOOL mouseCallback(int eventType, const EmscriptenMouseEvent* event, void* userData) {- auto* platform = static_cast<WebPlatform*>(userData);-- f32 x = static_cast<f32>(event->targetX);- f32 y = static_cast<f32>(event->targetY);-- if (eventType == EMSCRIPTEN_EVENT_MOUSEMOVE && platform->mouseMoveCallback_) {- platform->mouseMoveCallback_(x, y);- }-- if (!platform->touchCallback_) return false;-- TouchType type;- switch (eventType) {- case EMSCRIPTEN_EVENT_MOUSEDOWN: type = TouchType::Begin; break;- case EMSCRIPTEN_EVENT_MOUSEMOVE:- if (!(event->buttons & 1)) return true;- type = TouchType::Move;- break;- case EMSCRIPTEN_EVENT_MOUSEUP: type = TouchType::End; break;- default: return false;- }-- TouchPoint point;- point.id = 0;- point.x = x;- point.y = y;- platform->touchCallback_(type, point);-- return true;- }-- static EM_BOOL keyCallback(int eventType, const EmscriptenKeyboardEvent* event, void* userData) {- auto* platform = static_cast<WebPlatform*>(userData);- if (!platform->keyCallback_) return false;-- bool pressed = (eventType == EMSCRIPTEN_EVENT_KEYDOWN);- KeyCode keyCode = static_cast<KeyCode>(event->keyCode);-- platform->keyCallback_(keyCode, pressed);- return true;- }-- static EM_BOOL resizeCallback(int eventType, const EmscriptenUiEvent* event, void* userData) {- (void)eventType;- auto* platform = static_cast<WebPlatform*>(userData);-- f64 cssWidth, cssHeight;- emscripten_get_element_css_size("", &cssWidth, &cssHeight);-- platform->width_ = static_cast<u32>(cssWidth * platform->devicePixelRatio_);- platform->height_ = static_cast<u32>(cssHeight * platform->devicePixelRatio_);-- emscripten_set_canvas_element_size("", platform->width_, platform->height_);-- if (platform->resizeCallback_) {- platform->resizeCallback_(platform->width_, platform->height_);- }-- ES_LOG_DEBUG("Window resized to {}x{}", platform->width_, platform->height_);- return true;- }-#endif-- u32 width_ = 0;- u32 height_ = 0;- f32 devicePixelRatio_ = 1.0f;- f64 lastTime_ = 0.0;- f64 deltaTime_ = 0.0;- bool running_ = false;--#ifdef ES_PLATFORM_WEB- EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context_ = 0;-#endif-- TouchCallback touchCallback_;- KeyCallback keyCallback_;- ResizeCallback resizeCallback_;- ScrollCallback scrollCallback_;- TextInputCallback textInputCallback_;- MouseMoveCallback mouseMoveCallback_;- MouseButtonCallback mouseButtonCallback_;-};--WebPlatform* WebPlatform::instance_ = nullptr;--// Factory implementation-Unique<Platform> Platform::create() {- return makeUnique<WebPlatform>();-}--} // namespace esengine-