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.

perf: optimize texture flip using canvas transform for WeChat

esengineb8937c73b7be
diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
index 9413df613..1699c9490 100644
--- a/.vscode/c_cpp_properties.json
+++ b/.vscode/c_cpp_properties.json
@@ -10,7 +10,7 @@
"${workspaceFolder}/third_party/glad/include",
"${workspaceFolder}/third_party/stb",
"${workspaceFolder}/third_party/spine-runtimes/spine-cpp/spine-cpp/include",
- "${workspaceFolder}/third_party/json/single_include",
+ "${workspaceFolder}/third_party/nlohmann",
"${workspaceFolder}/build/quickjs_headers"
],
"defines": [
@@ -21,8 +21,7 @@
],
"cStandard": "c17",
"cppStandard": "c++20",
- "intelliSenseMode": "macos-clang-arm64",
- "compileCommands": "${workspaceFolder}/build/compile_commands.json"
+ "intelliSenseMode": "macos-clang-arm64"
},
{
"name": "Win32",
@@ -34,7 +33,7 @@
"${workspaceFolder}/third_party/glad/include",
"${workspaceFolder}/third_party/stb",
"${workspaceFolder}/third_party/spine-runtimes/spine-cpp/spine-cpp/include",
- "${workspaceFolder}/third_party/json/single_include",
+ "${workspaceFolder}/third_party/nlohmann",
"${workspaceFolder}/build/quickjs_headers"
],
"defines": [
@@ -46,8 +45,26 @@
],
"cStandard": "c17",
"cppStandard": "c++20",
- "intelliSenseMode": "windows-gcc-x64",
- "compileCommands": "${workspaceFolder}/build/compile_commands.json"
+ "intelliSenseMode": "windows-gcc-x64"
+ },
+ {
+ "name": "Web",
+ "includePath": [
+ "${workspaceFolder}/src",
+ "${workspaceFolder}/include",
+ "${workspaceFolder}/third_party/glm",
+ "${workspaceFolder}/third_party/stb",
+ "${workspaceFolder}/third_party/spine-runtimes/spine-cpp/spine-cpp/include",
+ "${workspaceFolder}/third_party/nlohmann"
+ ],
+ "defines": [
+ "ES_PLATFORM_WEB",
+ "__EMSCRIPTEN__"
+ ],
+ "cStandard": "c17",
+ "cppStandard": "c++20",
+ "intelliSenseMode": "clang-x64",
+ "compileCommands": "${workspaceFolder}/build-web/compile_commands.json"
}
],
"version": 4
diff --git a/sdk/src/platform/wechat/image.ts b/sdk/src/platform/wechat/image.ts
index 5700ab2d3..9904ae1e1 100644
--- a/sdk/src/platform/wechat/image.ts
+++ b/sdk/src/platform/wechat/image.ts
@@ -68,14 +68,16 @@ export function wxGetImagePixels(img: WechatMinigame.Image, flipY = true): Image
const { ctx } = getOffscreenCanvas(width, height);
ctx.clearRect(0, 0, width, height);
+ ctx.save();
+ if (flipY) {
+ ctx.translate(0, height);
+ ctx.scale(1, -1);
+ }
ctx.drawImage(img as unknown as CanvasImageSource, 0, 0);
+ ctx.restore();
const imageData = ctx.getImageData(0, 0, width, height);
const pixels = new Uint8Array(imageData.data.buffer);
- if (flipY) {
- return { width, height, pixels: flipVertically(pixels, width, height) };
- }
-
return { width, height, pixels };
}
@@ -89,19 +91,3 @@ export async function wxLoadImagePixels(path: string, flipY = true): Promise<Ima
return wxGetImagePixels(img, flipY);
}
-// =============================================================================
-// Utility Functions
-// =============================================================================
-
-function flipVertically(pixels: Uint8Array, width: number, height: number): Uint8Array {
- const rowSize = width * 4;
- const flipped = new Uint8Array(pixels.length);
-
- for (let y = 0; y < height; y++) {
- const srcOffset = y * rowSize;
- const dstOffset = (height - 1 - y) * rowSize;
- flipped.set(pixels.subarray(srcOffset, srcOffset + rowSize), dstOffset);
- }
-
- return flipped;
-}
diff --git a/src/esengine/renderer/Texture.cpp b/src/esengine/renderer/Texture.cpp
index cbce81dad..dbfbe9f80 100644
--- a/src/esengine/renderer/Texture.cpp
+++ b/src/esengine/renderer/Texture.cpp
@@ -135,8 +135,6 @@ Unique<Texture> Texture::createRaw(u32 width, u32 height, const void* data, Text
#ifndef ES_PLATFORM_WEB
Unique<Texture> Texture::createFromFile(const std::string& path) {
- stbi_set_flip_vertically_on_load(true);
-
int width, height, channels;
unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index d6876343e..28e4e5724 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -14,6 +14,7 @@
#include "loaders/ShaderLoader.hpp"
#include <json.hpp>
#include <fstream>
+#include <stb_image.h>
#endif
#include "../core/Log.hpp"
#include "../platform/PathResolver.hpp"
@@ -29,6 +30,10 @@ void ResourceManager::init() {
return;
}
+#ifndef ES_PLATFORM_WEB
+ stbi_set_flip_vertically_on_load(true);
+#endif
+
stats_ = {};
initialized_ = true;
ES_LOG_INFO("ResourceManager initialized");