Skip to content

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.

2.9 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    EstellaContext.hpp
 * @brief   Instance-based engine context for estella renderer
 * @details Owns all engine subsystems (GfxDevice, RenderContext, RenderFrame,
 *          ResourceManager, etc.) with explicit lifecycle management.
 *          Replaces the EngineContext singleton for multi-instance support.
 *
 * @author  ESEngine Team
 * @date    2026
 */
#pragma once

#include "Types.hpp"
#include "ServiceRegistry.hpp"
#include "../bindings/EngineState.hpp"

namespace esengine {

class GfxDevice;

class EstellaContext {
public:
    EstellaContext();
    ~EstellaContext();

    EstellaContext(const EstellaContext&) = delete;
    EstellaContext& operator=(const EstellaContext&) = delete;

    /**
     * @brief Initialize with an existing WebGL context handle
     * @param webglContextHandle Valid WebGL context (from emscripten_webgl_create_context)
     * @return True on success
     * @details Convenience for the GL backend: makes the context current and
     *          delegates to init(UniquePtr<GfxDevice>) with a GLDevice.
     *          Borrows the handle; its owner must keep it registered until shutdown.
     */
    bool init(int webglContextHandle);

    /** Adopt a WebGL handle only when its creator explicitly transfers ownership. */
    bool init(int webglContextHandle, bool ownsContext);

    /**
     * @brief Initialize with an injected render backend.
     * @param device A ready-to-use GfxDevice (e.g. a WebGPUDevice whose surface
     *        is already configured). The platform edge owns backend selection —
     *        exactly as the WebGL overload receives an externally created
     *        context handle — so this class needs no backend-specific code.
     * @return True on success
     */
    bool init(Unique<GfxDevice> device);

    /**
     * @brief Shut down all subsystems and release resources
     */
    void shutdown();

    /** @brief Check if the context is initialized */
    bool isInitialized() const { return state_.initialized; }

    ServiceRegistry& services() { return services_; }

    template<typename T>
    T& require() { return services_.require<T>(); }

    template<typename T>
    T* tryGet() { return services_.getService<T>(); }

    EngineState& state() { return state_; }
    const EngineState& state() const { return state_; }

private:
    void initSubsystems(Unique<GfxDevice> gfxDevice);
    /**
     * Register the GPU-independent logic systems (Transform/UI/Tween/Tilemap).
     * Called from the constructor *and* initSubsystems so they exist even for
     * headless apps that never call init() — otherwise require<UISystem>() etc.
     * return a null reference (silent wasm address-0 corruption). Idempotent.
     */
    void registerLogicSystems();

    ServiceRegistry services_;
    EngineState state_;
};

}  // namespace esengine