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: deduplicate BatchVertex and packColor into shared header
esengineb54a38d520e7
diff --git a/src/esengine/renderer/BatchVertex.hpp b/src/esengine/renderer/BatchVertex.hppnew file mode 100644index 000000000..aa77fab29--- /dev/null+++ b/src/esengine/renderer/BatchVertex.hpp@@ -0,0 +1,31 @@+#pragma once++#include "../core/Types.hpp"+#include <glm/glm.hpp>++namespace esengine {++struct BatchVertex {+ glm::vec2 position;+ u32 color;+ glm::vec2 texCoord;+};++inline u32 packColor(const glm::vec4& c) {+ u8 r = static_cast<u8>(c.r * 255.0f + 0.5f);+ u8 g = static_cast<u8>(c.g * 255.0f + 0.5f);+ u8 b = static_cast<u8>(c.b * 255.0f + 0.5f);+ u8 a = static_cast<u8>(c.a * 255.0f + 0.5f);+ return static_cast<u32>(r) | (static_cast<u32>(g) << 8)+ | (static_cast<u32>(b) << 16) | (static_cast<u32>(a) << 24);+}++inline u32 packColor(f32 r, f32 g, f32 b, f32 a) {+ auto clamp = [](f32 v) -> u8 {+ return static_cast<u8>(std::min(std::max(v, 0.0f), 1.0f) * 255.0f + 0.5f);+ };+ return static_cast<u32>(clamp(r)) | (static_cast<u32>(clamp(g)) << 8)+ | (static_cast<u32>(clamp(b)) << 16) | (static_cast<u32>(clamp(a)) << 24);+}++} // namespace esenginediff --git a/src/esengine/renderer/RenderFrameSubmit.cpp b/src/esengine/renderer/RenderFrameSubmit.cppindex f79e30f4e..3d084d527 100644--- a/src/esengine/renderer/RenderFrameSubmit.cpp+++ b/src/esengine/renderer/RenderFrameSubmit.cpp@@ -1,4 +1,5 @@#include "RenderFrame.hpp"+#include "BatchVertex.hpp"#include <glm/gtc/type_ptr.hpp>@@ -8,23 +9,7 @@namespace esengine {namespace {--struct TileVertex {- glm::vec2 position;- u32 color;- glm::vec2 texCoord;-};static constexpr u16 TILE_QUAD_IDX[6] = { 0, 1, 2, 2, 3, 0 };--static u32 packColor(const glm::vec4& c) {- u8 r = static_cast<u8>(c.r * 255.0f + 0.5f);- u8 g = static_cast<u8>(c.g * 255.0f + 0.5f);- u8 b = static_cast<u8>(c.b * 255.0f + 0.5f);- u8 a = static_cast<u8>(c.a * 255.0f + 0.5f);- return static_cast<u32>(r) | (static_cast<u32>(g) << 8)- | (static_cast<u32>(b) << 16) | (static_cast<u32>(a) << 24);-}-} // namespacevoid RenderFrame::submitTileQuad(@@ -37,14 +22,14 @@ void RenderFrame::submitTileQuad(f32 hh = size.y * 0.5f;u32 pc = packColor(color);- TileVertex verts[4];+ BatchVertex verts[4];verts[0] = { {position.x - hw, position.y - hh}, pc, uvOffset };verts[1] = { {position.x + hw, position.y - hh}, pc, {uvOffset.x + uvScale.x, uvOffset.y} };verts[2] = { {position.x + hw, position.y + hh}, pc, {uvOffset.x + uvScale.x, uvOffset.y + uvScale.y} };verts[3] = { {position.x - hw, position.y + hh}, pc, {uvOffset.x, uvOffset.y + uvScale.y} };u32 vOff = pool_.appendVertices(verts, sizeof(verts));- u32 baseVertex = vOff / sizeof(TileVertex);+ u32 baseVertex = vOff / sizeof(BatchVertex);u16 indices[6];for (u32 i = 0; i < 6; ++i) {@@ -86,9 +71,9 @@ void RenderFrame::submitSpineBatch(glm::mat4 model = glm::make_mat4(transform16);BlendMode blend = static_cast<BlendMode>(std::clamp(blendMode, 0, 5));- u32 vBytes = static_cast<u32>(vertexCount) * sizeof(TileVertex);+ u32 vBytes = static_cast<u32>(vertexCount) * sizeof(BatchVertex);u32 vOff = pool_.allocVertices(vBytes);- auto* dst = reinterpret_cast<TileVertex*>(pool_.vertexData() + vOff);+ auto* dst = reinterpret_cast<BatchVertex*>(pool_.vertexData() + vOff);for (i32 i = 0; i < vertexCount; ++i) {const f32* v = vertices + i * FLOATS_PER_VERTEX;@@ -97,7 +82,7 @@ void RenderFrame::submitSpineBatch(dst[i] = { {worldPos.x, worldPos.y}, pc, {v[2], v[3]} };}- u32 baseVertex = vOff / sizeof(TileVertex);+ u32 baseVertex = vOff / sizeof(BatchVertex);std::vector<u16> offsetIndices(indexCount);for (i32 i = 0; i < indexCount; ++i) {offsetIndices[i] = static_cast<u16>(baseVertex + indices[i]);diff --git a/src/esengine/renderer/Renderer.cpp b/src/esengine/renderer/Renderer.cppindex 3be06eb94..2cac92185 100644--- a/src/esengine/renderer/Renderer.cpp+++ b/src/esengine/renderer/Renderer.cpp@@ -11,6 +11,7 @@*/#include "Renderer.hpp"+#include "BatchVertex.hpp"#include "RenderCommand.hpp"#include "TextureSlotAllocator.hpp"#include "ShaderEmbeds.generated.hpp"@@ -269,21 +270,6 @@ void Renderer::resetStats() {// BatchRenderer2D Implementation// ========================================-// Batch vertex structure-struct BatchVertex {- glm::vec2 position;- u32 color;- glm::vec2 texCoord;-};--static u32 packColor(const glm::vec4& c) {- u8 r = static_cast<u8>(c.r * 255.0f + 0.5f);- u8 g = static_cast<u8>(c.g * 255.0f + 0.5f);- u8 b = static_cast<u8>(c.b * 255.0f + 0.5f);- u8 a = static_cast<u8>(c.a * 255.0f + 0.5f);- return static_cast<u32>(r) | (static_cast<u32>(g) << 8)- | (static_cast<u32>(b) << 16) | (static_cast<u32>(a) << 24);-}// Batch rendering constantsconstexpr u32 MAX_QUADS = 10000;diff --git a/src/esengine/renderer/plugins/SpinePlugin.cpp b/src/esengine/renderer/plugins/SpinePlugin.cppindex 1791fb600..29b069e40 100644--- a/src/esengine/renderer/plugins/SpinePlugin.cpp+++ b/src/esengine/renderer/plugins/SpinePlugin.cpp@@ -24,14 +24,6 @@ void SpinePlugin::init(RenderFrameContext& ctx) {world_vertices_.reserve(512);}-u32 SpinePlugin::packColor(f32 r, f32 g, f32 b, f32 a) {- auto clamp = [](f32 v) -> u8 { return static_cast<u8>(std::min(std::max(v, 0.0f), 1.0f) * 255.0f + 0.5f); };- return static_cast<u32>(clamp(r))- | (static_cast<u32>(clamp(g)) << 8)- | (static_cast<u32>(clamp(b)) << 16)- | (static_cast<u32>(clamp(a)) << 24);-}-u32 SpinePlugin::getTextureId(void* spineTexture, RenderFrameContext& ctx) {if (!spineTexture) {return ctx.white_texture_id;diff --git a/src/esengine/renderer/plugins/SpinePlugin.hpp b/src/esengine/renderer/plugins/SpinePlugin.hppindex 6e7a990ef..0ed6caa5c 100644--- a/src/esengine/renderer/plugins/SpinePlugin.hpp+++ b/src/esengine/renderer/plugins/SpinePlugin.hpp@@ -1,6 +1,7 @@#pragma once#include "../RenderTypePlugin.hpp"+#include "../BatchVertex.hpp"#ifdef ES_ENABLE_SPINE@@ -30,13 +31,6 @@ public:) override;private:- struct BatchVertex {- glm::vec2 position;- u32 color;- glm::vec2 texCoord;- };-- static u32 packColor(f32 r, f32 g, f32 b, f32 a);u32 getTextureId(void* spineTexture, RenderFrameContext& ctx);static BlendMode mapBlendMode(::spine::BlendMode mode);diff --git a/src/esengine/renderer/plugins/SpritePlugin.hpp b/src/esengine/renderer/plugins/SpritePlugin.hppindex 22d306467..5478c9822 100644--- a/src/esengine/renderer/plugins/SpritePlugin.hpp+++ b/src/esengine/renderer/plugins/SpritePlugin.hpp@@ -1,6 +1,7 @@#pragma once#include "../RenderTypePlugin.hpp"+#include "../BatchVertex.hpp"#include "../../resource/TextureMetadata.hpp"namespace esengine {@@ -20,21 +21,6 @@ public:) override;private:- struct BatchVertex {- glm::vec2 position;- u32 color;- glm::vec2 texCoord;- };-- static u32 packColor(const glm::vec4& c) {- u8 r = static_cast<u8>(c.r * 255.0f + 0.5f);- u8 g = static_cast<u8>(c.g * 255.0f + 0.5f);- u8 b = static_cast<u8>(c.b * 255.0f + 0.5f);- u8 a = static_cast<u8>(c.a * 255.0f + 0.5f);- return static_cast<u32>(r) | (static_cast<u32>(g) << 8)- | (static_cast<u32>(b) << 16) | (static_cast<u32>(a) << 24);- }-static constexpr glm::vec4 QUAD_POSITIONS[4] = {{ -0.5f, -0.5f, 0.0f, 1.0f },{ 0.5f, -0.5f, 0.0f, 1.0f },diff --git a/src/esengine/renderer/plugins/TextPlugin.hpp b/src/esengine/renderer/plugins/TextPlugin.hppindex d10ce593a..dc49aaa7b 100644--- a/src/esengine/renderer/plugins/TextPlugin.hpp+++ b/src/esengine/renderer/plugins/TextPlugin.hpp@@ -1,6 +1,7 @@#pragma once#include "../RenderTypePlugin.hpp"+#include "../BatchVertex.hpp"namespace esengine {namespace text { class BitmapFont; }@@ -20,21 +21,6 @@ public:) override;private:- struct BatchVertex {- glm::vec2 position;- u32 color;- glm::vec2 texCoord;- };-- static u32 packColor(const glm::vec4& c) {- u8 r = static_cast<u8>(c.r * 255.0f + 0.5f);- u8 g = static_cast<u8>(c.g * 255.0f + 0.5f);- u8 b = static_cast<u8>(c.b * 255.0f + 0.5f);- u8 a = static_cast<u8>(c.a * 255.0f + 0.5f);- return static_cast<u32>(r) | (static_cast<u32>(g) << 8)- | (static_cast<u32>(b) << 16) | (static_cast<u32>(a) << 24);- }-static constexpr u16 QUAD_INDICES[6] = { 0, 1, 2, 2, 3, 0 };static u32 decodeUtf8(const char* data, u16 length, u16& pos);diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cppindex 5b9543d26..0d328bf92 100644--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp@@ -15,17 +15,6 @@ void TilemapRenderPlugin::init(RenderFrameContext& ctx) {batch_shader_id_ = ctx.batch_shader_id;}-u32 TilemapRenderPlugin::packColor(const glm::vec4& c) {- u8 r = static_cast<u8>(c.r * 255.0f + 0.5f);- u8 g = static_cast<u8>(c.g * 255.0f + 0.5f);- u8 b = static_cast<u8>(c.b * 255.0f + 0.5f);- u8 a = static_cast<u8>(c.a * 255.0f + 0.5f);- return (static_cast<u32>(a) << 24) |- (static_cast<u32>(b) << 16) |- (static_cast<u32>(g) << 8) |- static_cast<u32>(r);-}-void TilemapRenderPlugin::collect(ecs::Registry& registry,const Frustum& /* frustum */,@@ -146,9 +135,9 @@ void TilemapRenderPlugin::collect(if (indices_.empty()) continue;- u32 vBytes = static_cast<u32>(vertices_.size()) * sizeof(TileVertex);+ u32 vBytes = static_cast<u32>(vertices_.size()) * sizeof(BatchVertex);u32 vOff = buffers.appendVertices(vertices_.data(), vBytes);- u32 baseVertex = vOff / sizeof(TileVertex);+ u32 baseVertex = vOff / sizeof(BatchVertex);for (auto& idx : indices_) {idx = static_cast<u16>(idx + baseVertex);diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.hppindex 4dd9cb619..fd85e5a32 100644--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.hpp@@ -1,6 +1,7 @@#pragma once#include "../RenderTypePlugin.hpp"+#include "../BatchVertex.hpp"#include <vector>@@ -25,18 +26,10 @@ public:) override;private:- struct TileVertex {- glm::vec2 position;- u32 color;- glm::vec2 texCoord;- };-- static u32 packColor(const glm::vec4& c);-tilemap::TilemapSystem* tilemap_system_ = nullptr;u32 batch_shader_id_ = 0;- std::vector<TileVertex> vertices_;+ std::vector<BatchVertex> vertices_;std::vector<u16> indices_;};diff --git a/src/esengine/renderer/plugins/UIElementPlugin.hpp b/src/esengine/renderer/plugins/UIElementPlugin.hppindex e0f10fa2b..71f61b35c 100644--- a/src/esengine/renderer/plugins/UIElementPlugin.hpp+++ b/src/esengine/renderer/plugins/UIElementPlugin.hpp@@ -1,6 +1,7 @@#pragma once#include "../RenderTypePlugin.hpp"+#include "../BatchVertex.hpp"#include "../../resource/TextureMetadata.hpp"namespace esengine {@@ -20,21 +21,6 @@ public:) override;private:- struct BatchVertex {- glm::vec2 position;- u32 color;- glm::vec2 texCoord;- };-- static u32 packColor(const glm::vec4& c) {- u8 r = static_cast<u8>(c.r * 255.0f + 0.5f);- u8 g = static_cast<u8>(c.g * 255.0f + 0.5f);- u8 b = static_cast<u8>(c.b * 255.0f + 0.5f);- u8 a = static_cast<u8>(c.a * 255.0f + 0.5f);- return static_cast<u32>(r) | (static_cast<u32>(g) << 8)- | (static_cast<u32>(b) << 16) | (static_cast<u32>(a) << 24);- }-static constexpr glm::vec4 QUAD_POSITIONS[4] = {{ -0.5f, -0.5f, 0.0f, 1.0f },{ 0.5f, -0.5f, 0.0f, 1.0f },