Skip to content

Commit

Permalink
Don't use std types.
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Oct 30, 2024
1 parent 3c48bf6 commit ed2bd99
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 130 deletions.
6 changes: 2 additions & 4 deletions native/cocos/editor-support/spine-wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ set(CMAKE_VERBOSE_MAKEFILE ON)
set(BUILD_WASM 1)
set(ENABLE_JSON_PARSER 1)
set(ENABLE_BINARY_PARSER 1)
set(DEBUG_NEW 0)

set(CMAKE_BUILD_TYPE "MinSizeRel")
# set(CMAKE_BUILD_TYPE "RelWithDebInfo")
# set(CMAKE_BUILD_TYPE "Debug")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand All @@ -25,16 +25,14 @@ project(${APP_NAME}_wasm)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -DENABLE_JSON_PARSER=${ENABLE_JSON_PARSER} -DENABLE_BINARY_PARSER=${ENABLE_BINARY_PARSER}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti -Wno-inconsistent-missing-override \
-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -DENABLE_JSON_PARSER=${ENABLE_JSON_PARSER} -DENABLE_BINARY_PARSER=${ENABLE_BINARY_PARSER} \
-DDEBUG_NEW=${DEBUG_NEW}")
-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -DENABLE_JSON_PARSER=${ENABLE_JSON_PARSER} -DENABLE_BINARY_PARSER=${ENABLE_BINARY_PARSER}")

message(">>> --------------------------------------------------------------")
message(">>> Current directory: ${CMAKE_CURRENT_LIST_DIR}")
message(">>> CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(">>> ENABLE_CLOSURE_COMPILER: ${ENABLE_CLOSURE_COMPILER}")
message(">>> ENABLE_JSON_PARSER: ${ENABLE_JSON_PARSER}")
message(">>> ENABLE_BINARY_PARSER: ${ENABLE_BINARY_PARSER}")
message(">>> DEBUG_NEW: ${DEBUG_NEW}")
message(">>> --------------------------------------------------------------")

include_directories(${CMAKE_CURRENT_LIST_DIR}/..)
Expand Down
24 changes: 11 additions & 13 deletions native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "spine-skeleton-instance.h"
#include <spine/spine.h>
#include <vector>
#include "AtlasAttachmentLoaderExtension.h"
#include "spine-mesh-data.h"
#include "spine-wasm.h"
#include "util-function.h"

SlotMesh globalMesh(nullptr, nullptr, 0, 0);

Expand Down Expand Up @@ -66,9 +64,9 @@ Skeleton *SpineSkeletonInstance::initSkeleton(SkeletonData *data) {
return _skeleton;
}

TrackEntry *SpineSkeletonInstance::setAnimation(float trackIndex, const std::string &name, bool loop) {
TrackEntry *SpineSkeletonInstance::setAnimation(float trackIndex, const spine::String &name, bool loop) {
if (!_skeleton) return nullptr;
spine::Animation *animation = _skeleton->getData()->findAnimation(name.c_str());
spine::Animation *animation = _skeleton->getData()->findAnimation(name);
if (!animation) {
_animState->clearTracks();
_skeleton->setToSetupPose();
Expand All @@ -80,9 +78,9 @@ TrackEntry *SpineSkeletonInstance::setAnimation(float trackIndex, const std::str
return trackEntry;
}

void SpineSkeletonInstance::setSkin(const std::string &name) {
void SpineSkeletonInstance::setSkin(const spine::String &name) {
if (!_skeleton) return;
_skeleton->setSkin(name.c_str());
_skeleton->setSkin(name);
_skeleton->setSlotsToSetupPose();
}

Expand Down Expand Up @@ -135,7 +133,7 @@ void SpineSkeletonInstance::collectMeshData() {
for (uint32_t drawIdx = 0; drawIdx < slotCount; ++drawIdx) {
auto slot = slotArray[drawIdx];
auto& bone = slot->getBone();
if (bone.isActive() == false) {
if (!bone.isActive()) {
continue;
}

Expand Down Expand Up @@ -405,8 +403,8 @@ AnimationState *SpineSkeletonInstance::getAnimationState() {
return _animState;
}

void SpineSkeletonInstance::setMix(const std::string &from, const std::string &to, float duration) {
_animStateData->setMix(from.c_str(), to.c_str(), duration);
void SpineSkeletonInstance::setMix(const spine::String &from, const spine::String &to, float duration) {
_animStateData->setMix(from, to, duration);
}

void SpineSkeletonInstance::setTrackEntryListener(uint32_t trackId, TrackEntry *entry) {
Expand Down Expand Up @@ -448,9 +446,9 @@ Vector<SpineDebugShape> &SpineSkeletonInstance::getDebugShapes() {
return this->_debugShapes;
}

void SpineSkeletonInstance::resizeSlotRegion(const std::string &slotName, uint32_t width, uint32_t height, bool createNew) {
void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint32_t width, uint32_t height, bool createNew) {
if (!_skeleton) return;
auto slot = _skeleton->findSlot(slotName.c_str());
auto slot = _skeleton->findSlot(slotName);
if (!slot) return;
auto attachment = slot->getAttachment();
if (!attachment) return;
Expand Down Expand Up @@ -508,9 +506,9 @@ void SpineSkeletonInstance::resizeSlotRegion(const std::string &slotName, uint32
}
}

void SpineSkeletonInstance::setSlotTexture(const std::string &slotName, uint32_t textureID) {
void SpineSkeletonInstance::setSlotTexture(const spine::String &slotName, uint32_t textureID) {
if (!_skeleton) return;
auto slot = _skeleton->findSlot(slotName.c_str());
auto slot = _skeleton->findSlot(slotName);
if (!slot) return;
_userData.useSlotTexture = true;

Expand Down
10 changes: 5 additions & 5 deletions native/cocos/editor-support/spine-wasm/spine-skeleton-instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class SpineSkeletonInstance {
SpineSkeletonInstance();
~SpineSkeletonInstance();
spine::Skeleton *initSkeleton(spine::SkeletonData *data);
spine::TrackEntry *setAnimation(float trackIndex, const std::string &name, bool loop);
void setSkin(const std::string &name);
spine::TrackEntry *setAnimation(float trackIndex, const spine::String &name, bool loop);
void setSkin(const spine::String &name);
void updateAnimation(float dltTime);
SpineModel *updateRenderData();
void setPremultipliedAlpha(bool val);
Expand All @@ -47,14 +47,14 @@ class SpineSkeletonInstance {
void setSwirlEffect(spine::SwirlVertexEffect *effect);
void clearEffect();
spine::AnimationState *getAnimationState();
void setMix(const std::string &from, const std::string &to, float duration);
void setMix(const spine::String &from, const spine::String &to, float duration);
inline void setListener(uint32_t listenerID) { _eventListenerID = listenerID;}
void setTrackEntryListener(uint32_t trackId, spine::TrackEntry *entry);
void onAnimationStateEvent(spine::TrackEntry *entry, spine::EventType type, spine::Event *event);
void onTrackEntryEvent(spine::TrackEntry *entry, spine::EventType type, spine::Event *event);
spine::Vector<SpineDebugShape> &getDebugShapes();
void resizeSlotRegion(const std::string &slotName, uint32_t width, uint32_t height, bool createNew = false);
void setSlotTexture(const std::string &slotName, uint32_t index);
void resizeSlotRegion(const spine::String &slotName, uint32_t width, uint32_t height, bool createNew = false);
void setSlotTexture(const spine::String &slotName, uint32_t index);
void destroy();
bool isCache{false};
bool enable{true};
Expand Down
51 changes: 24 additions & 27 deletions native/cocos/editor-support/spine-wasm/spine-wasm.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "spine-wasm.h"
#include <map>
#include "AtlasAttachmentLoaderExtension.h"
#include "spine-mesh-data.h"
#include "util-function.h"
#include "wasmSpineExtension.h"

using namespace spine;

std::map<std::string, SkeletonData*> skeletonDataMap{};
namespace {
HashMap<String, SkeletonData*> skeletonDataMap{};
}

uint32_t SpineWasmUtil::s_listenerID = 0;
EventType SpineWasmUtil::s_currentType = EventType_Event;
Expand All @@ -17,57 +18,55 @@ uint8_t* SpineWasmUtil::s_mem = nullptr;
uint32_t SpineWasmUtil::s_memSize = 0;

void SpineWasmUtil::spineWasmInit() {
LogUtil::Initialize();
spine::SpineExtension* tension = new WasmSpineExtension();
spine::SpineExtension::setInstance(tension);
// LogUtil::Initialize();
SpineExtension* tension = new WasmSpineExtension();
SpineExtension::setInstance(tension);

SpineMeshData::initMeshMemory();

//LogUtil::PrintToJs("spineWasmInit");
}

void SpineWasmUtil::spineWasmDestroy() {
auto* extension = spine::SpineExtension::getInstance();
auto* extension = SpineExtension::getInstance();
delete extension;
freeStoreMemory();
SpineMeshData::releaseMeshMemory();
LogUtil::ReleaseBuffer();
// LogUtil::ReleaseBuffer();
}

SkeletonData* SpineWasmUtil::querySpineSkeletonDataByUUID(const std::string& uuid) {
auto iter = skeletonDataMap.find(uuid);
if (iter == skeletonDataMap.end()) {
SkeletonData* SpineWasmUtil::querySpineSkeletonDataByUUID(const String& uuid) {
if (!skeletonDataMap.containsKey(uuid)) {
return nullptr;
}
SkeletonData* ptrVal = iter->second;
return ptrVal;
return skeletonDataMap[uuid];
}

SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithJson(const std::string& jsonStr, const std::string& altasStr) {
SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithJson(const String& jsonStr, const String& altasStr) {
#if ENABLE_JSON_PARSER
auto* atlas = new Atlas(altasStr.c_str(), altasStr.size(), "", nullptr, false);
auto* atlas = new Atlas(altasStr.buffer(), altasStr.length(), "", nullptr, false);
if (!atlas) {
return nullptr;
}
AttachmentLoader* attachmentLoader = new AtlasAttachmentLoaderExtension(atlas);
spine::SkeletonJson json(attachmentLoader);
SkeletonJson json(attachmentLoader);
json.setScale(1.0F);
SkeletonData* skeletonData = json.readSkeletonData(jsonStr.c_str());
SkeletonData* skeletonData = json.readSkeletonData(jsonStr.buffer());

return skeletonData;
#else
return nullptr;
#endif
}

SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize, const std::string& altasStr) {
SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize, const String& altasStr) {
#if ENABLE_BINARY_PARSER
auto* atlas = new Atlas(altasStr.c_str(), altasStr.size(), "", nullptr, false);
auto* atlas = new Atlas(altasStr.buffer(), altasStr.length(), "", nullptr, false);
if (!atlas) {
return nullptr;
}
AttachmentLoader* attachmentLoader = new AtlasAttachmentLoaderExtension(atlas);
spine::SkeletonBinary binary(attachmentLoader);
SkeletonBinary binary(attachmentLoader);
binary.setScale(1.0F);
SkeletonData* skeletonData = binary.readSkeletonData(s_mem, byteSize);
return skeletonData;
Expand All @@ -76,19 +75,17 @@ SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize
#endif
}

void SpineWasmUtil::registerSpineSkeletonDataWithUUID(SkeletonData* data, const std::string& uuid) {
auto iter = skeletonDataMap.find(uuid);
if (iter == skeletonDataMap.end()) {
skeletonDataMap[uuid] = data;
void SpineWasmUtil::registerSpineSkeletonDataWithUUID(SkeletonData* data, const String& uuid) {
if (!skeletonDataMap.containsKey(uuid)) {
skeletonDataMap.put(uuid, data);
}
}

void SpineWasmUtil::destroySpineSkeletonDataWithUUID(const std::string& uuid) {
auto iter = skeletonDataMap.find(uuid);
if (iter != skeletonDataMap.end()) {
void SpineWasmUtil::destroySpineSkeletonDataWithUUID(const String& uuid) {
if (skeletonDataMap.containsKey(uuid)) {
auto* data = skeletonDataMap[uuid];
delete data;
skeletonDataMap.erase(iter);
skeletonDataMap.remove(uuid);
}
}

Expand Down
10 changes: 5 additions & 5 deletions native/cocos/editor-support/spine-wasm/spine-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class SpineWasmUtil {
static uint32_t queryStoreMemory(uint32_t size);
static void freeStoreMemory();

static spine::SkeletonData* querySpineSkeletonDataByUUID(const std::string& uuid);
static spine::SkeletonData* createSpineSkeletonDataWithJson(const std::string& jsonStr, const std::string& altasStr);
static spine::SkeletonData* createSpineSkeletonDataWithBinary(uint32_t byteSize, const std::string& altasStr);
static void registerSpineSkeletonDataWithUUID(spine::SkeletonData* data, const std::string& uuid);
static void destroySpineSkeletonDataWithUUID(const std::string& uuid);
static spine::SkeletonData* querySpineSkeletonDataByUUID(const spine::String& uuid);
static spine::SkeletonData* createSpineSkeletonDataWithJson(const spine::String& jsonStr, const spine::String& altasStr);
static spine::SkeletonData* createSpineSkeletonDataWithBinary(uint32_t byteSize, const spine::String& altasStr);
static void registerSpineSkeletonDataWithUUID(spine::SkeletonData* data, const spine::String& uuid);
static void destroySpineSkeletonDataWithUUID(const spine::String& uuid);
static void destroySpineSkeleton(spine::Skeleton* skeleton);

static uint32_t getCurrentListenerID();
Expand Down
110 changes: 55 additions & 55 deletions native/cocos/editor-support/spine-wasm/util-function.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
#include "util-function.h"
#include <stdint.h>
// extern "C" {
// extern void consoleInfo(char* ptr, uint32_t length);
// }
static char* logBuffer = nullptr;
const int LOG_LENGTH = 1024;
// #include <stdint.h>
// // extern "C" {
// // extern void consoleInfo(char* ptr, uint32_t length);
// // }
// static char* logBuffer = nullptr;
// const int LOG_LENGTH = 1024;

void LogUtil::Initialize() {
//logBuffer = new char[LOG_LENGTH];
}
// void LogUtil::Initialize() {
// //logBuffer = new char[LOG_LENGTH];
// }

void LogUtil::PrintToJs(std::string& message) {
// int length = message.length();
// if (length >= LOG_LENGTH) length = LOG_LENGTH -1;
// memcpy(logBuffer, message.c_str(), length);
// logBuffer[length] = 0;
// consoleInfo(logBuffer, length);
}
// void LogUtil::PrintToJs(std::string& message) {
// // int length = message.length();
// // if (length >= LOG_LENGTH) length = LOG_LENGTH -1;
// // memcpy(logBuffer, message.c_str(), length);
// // logBuffer[length] = 0;
// // consoleInfo(logBuffer, length);
// }

void LogUtil::PrintToJs(const char* message) {
// std::string strMessage(message);
// int length = strMessage.length();
// if (length >= LOG_LENGTH) length = LOG_LENGTH - 1;
// memcpy(logBuffer, strMessage.c_str(), length);
// logBuffer[length] = 0;
// consoleInfo(logBuffer, length);
}
// void LogUtil::PrintToJs(const char* message) {
// // std::string strMessage(message);
// // int length = strMessage.length();
// // if (length >= LOG_LENGTH) length = LOG_LENGTH - 1;
// // memcpy(logBuffer, strMessage.c_str(), length);
// // logBuffer[length] = 0;
// // consoleInfo(logBuffer, length);
// }

void LogUtil::PrintToJs(char* str, int length) {
// if (length >= LOG_LENGTH) length = LOG_LENGTH - 1;
// memcpy(logBuffer, str, length);
// logBuffer[length] = 0;
// consoleInfo(logBuffer, length);
}
// void LogUtil::PrintToJs(char* str, int length) {
// // if (length >= LOG_LENGTH) length = LOG_LENGTH - 1;
// // memcpy(logBuffer, str, length);
// // logBuffer[length] = 0;
// // consoleInfo(logBuffer, length);
// }

void LogUtil::PrintIntValue(int value, const char* message) {
// std::string strInt = std::to_string(value);
// std::string finalStr = std::string(message) + strInt;
// LogUtil::PrintToJs(finalStr);
}
// void LogUtil::PrintIntValue(int value, const char* message) {
// // std::string strInt = std::to_string(value);
// // std::string finalStr = std::string(message) + strInt;
// // LogUtil::PrintToJs(finalStr);
// }

void LogUtil::ReleaseBuffer() {
//delete[] logBuffer;
}
// void LogUtil::ReleaseBuffer() {
// //delete[] logBuffer;
// }

// const uint32_t MEMORY_SIZE = 8 * 1024 * 1024;
// static uint8_t* uint8Ptr = nullptr;
// // const uint32_t MEMORY_SIZE = 8 * 1024 * 1024;
// // static uint8_t* uint8Ptr = nullptr;

// uint8_t* StoreMemory::getStoreMemory() {
// if (uint8Ptr) return uint8Ptr;
// // uint8_t* StoreMemory::getStoreMemory() {
// // if (uint8Ptr) return uint8Ptr;

// uint32_t* uint32Ptr = new uint32_t[MEMORY_SIZE / 4];
// uint8Ptr = (uint8_t*)uint32Ptr;
// return uint8Ptr;
// }
// // uint32_t* uint32Ptr = new uint32_t[MEMORY_SIZE / 4];
// // uint8Ptr = (uint8_t*)uint32Ptr;
// // return uint8Ptr;
// // }

// void StoreMemory::freeStoreMemory() {
// if (uint8Ptr) {
// delete[] uint8Ptr;
// uint8Ptr = nullptr;
// }
// }
// // void StoreMemory::freeStoreMemory() {
// // if (uint8Ptr) {
// // delete[] uint8Ptr;
// // uint8Ptr = nullptr;
// // }
// // }

// uint32_t StoreMemory::storeMemorySize() {
// return MEMORY_SIZE;
// }
// // uint32_t StoreMemory::storeMemorySize() {
// // return MEMORY_SIZE;
// // }
Loading

0 comments on commit ed2bd99

Please sign in to comment.