From 80d088316bfa5144317c2ca73f28c81bc555d834 Mon Sep 17 00:00:00 2001 From: Lenheart <947330670@qq.com> Date: Sun, 26 Oct 2025 14:39:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B5=84=E6=BA=90=20?= =?UTF-8?q?=E9=80=9A=E7=94=A8=20=E7=B3=BB=E7=BB=9F=20sqr=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source_game/Asset/Squirrel/Sqr_Asset.hpp | 63 +++++++++++++ source_game/Asset/Squirrel/Sqr_CommonFunc.hpp | 93 +++++++++++++++++++ source_game/Asset/Squirrel/Sqr_System.hpp | 48 ++++++++++ 3 files changed, 204 insertions(+) create mode 100644 source_game/Asset/Squirrel/Sqr_Asset.hpp create mode 100644 source_game/Asset/Squirrel/Sqr_CommonFunc.hpp create mode 100644 source_game/Asset/Squirrel/Sqr_System.hpp diff --git a/source_game/Asset/Squirrel/Sqr_Asset.hpp b/source_game/Asset/Squirrel/Sqr_Asset.hpp new file mode 100644 index 0000000..37d08b3 --- /dev/null +++ b/source_game/Asset/Squirrel/Sqr_Asset.hpp @@ -0,0 +1,63 @@ +#pragma once +#include "Sqr_CommonFunc.hpp" +#include "Asset/Asset_ImagePack.h" + +static SQInteger SQR_GetImg(HSQUIRRELVM v) +{ + const SQChar *ImgPath; + sq_getstring(v, 2, &ImgPath); + Asset_ImagePack::IMG *Info = Asset_ImagePack::GetInstance().GetIMG(ImgPath); + + sq_newtable(v); + sq_pushstring(v, _SC("PngCount"), -1); + sq_pushfloat(v, Info->png_sum); + sq_newslot(v, 3, SQFalse); + sq_pushstring(v, _SC("NpkName"), -1); + sq_pushstring(v, Info->lpBelongsFile.c_str(), -1); + sq_newslot(v, 3, SQFalse); + return 1; +} + +static SQInteger SQR_GetPng(HSQUIRRELVM v) +{ + const SQChar *ImgPath; + sq_getstring(v, 2, &ImgPath); + SQInteger Index; + sq_getinteger(v, 3, &Index); + Asset_ImagePack::IMG *Info = Asset_ImagePack::GetInstance().GetIMG(ImgPath); + Asset_ImagePack::ImgInfo PngInfo = Info->lp_lplist[Index]; + + sq_newtable(v); + struct TableEntry + { + const SQChar *key; + SQInteger value; + } entries[] = { + {_SC("Format"), PngInfo.Type}, + {_SC("CompressType"), PngInfo.CmpType}, + {_SC("Width"), PngInfo.Width}, + {_SC("Height"), PngInfo.Height}, + {_SC("XPos"), PngInfo.Xpos}, + {_SC("YPos"), PngInfo.Ypos}, + {_SC("FrameXPos"), PngInfo.FrameXpos}, + {_SC("FrameYPos"), PngInfo.FrameYpos}}; + + for (const auto &entry : entries) + { + sq_pushstring(v, entry.key, -1); + sq_pushinteger(v, entry.value); + sq_newslot(v, -3, SQFalse); + } + + return 1; +} + +static void RegisterAsset() +{ + HSQUIRRELVM v = SquirrelEx::GetInstance().GetSquirrelVM(); + + // 获取Img数据 + RegisterNutApi(_SC("sq_GetImg"), SQR_GetImg, v); + // 获取Png数据 + RegisterNutApi(_SC("sq_GetPng"), SQR_GetPng, v); +} diff --git a/source_game/Asset/Squirrel/Sqr_CommonFunc.hpp b/source_game/Asset/Squirrel/Sqr_CommonFunc.hpp new file mode 100644 index 0000000..598beb1 --- /dev/null +++ b/source_game/Asset/Squirrel/Sqr_CommonFunc.hpp @@ -0,0 +1,93 @@ +#pragma once +#include "squirrel/SquirrelEx.h" +#include "Global/Global_Game.h" +#include "EngineFrame/Actor/Actor.h" + +static SQRESULT sq_GetVecSize(HSQUIRRELVM v, int Index, VecSize *Size) +{ + sq_pushnull(v); // null iterator + while (SQ_SUCCEEDED(sq_next(v, Index))) + { + SQFloat value; + sq_getfloat(v, -1, &value); + const SQChar *key; + sq_getstring(v, -2, &key); + + if (strcmp(key, _SC("w")) == 0) + { + Size->width = value; + } + else if (strcmp(key, _SC("h")) == 0) + { + Size->height = value; + } + sq_pop(v, 2); + } + sq_pop(v, 1); + return SQ_OK; +} + +static SQRESULT sq_GetVec2(HSQUIRRELVM v, int Index, Vec2 *Pos) +{ + sq_pushnull(v); // null iterator + while (SQ_SUCCEEDED(sq_next(v, Index))) + { + SQFloat value; + sq_getfloat(v, -1, &value); + const SQChar *key; + sq_getstring(v, -2, &key); + + if (strcmp(key, _SC("x")) == 0) + { + Pos->x = value; + } + else if (strcmp(key, _SC("y")) == 0) + { + Pos->y = value; + } + sq_pop(v, 2); + } + sq_pop(v, 1); + return SQ_OK; +} + +static SQRESULT sq_GetFRect(HSQUIRRELVM v, int Index, SDL_FRect *Pos) +{ + sq_pushnull(v); // null iterator + while (SQ_SUCCEEDED(sq_next(v, Index))) + { + SQFloat value; + sq_getfloat(v, -1, &value); + const SQChar *key; + sq_getstring(v, -2, &key); + + if (strcmp(key, _SC("x")) == 0) + { + Pos->x = value; + } + else if (strcmp(key, _SC("y")) == 0) + { + Pos->y = value; + } + else if (strcmp(key, _SC("w")) == 0) + { + Pos->w = value; + } + else if (strcmp(key, _SC("h")) == 0) + { + Pos->h = value; + } + sq_pop(v, 2); + } + sq_pop(v, 1); + return SQ_OK; +} + +static void RegisterNutApi(const SQChar *funcName, SQFUNCTION funcAddr, HSQUIRRELVM v) +{ + sq_pushroottable(v); + sq_pushstring(v, funcName, -1); + sq_newclosure(v, funcAddr, 0); + sq_newslot(v, -3, false); + sq_poptop(v); +} \ No newline at end of file diff --git a/source_game/Asset/Squirrel/Sqr_System.hpp b/source_game/Asset/Squirrel/Sqr_System.hpp new file mode 100644 index 0000000..6328f66 --- /dev/null +++ b/source_game/Asset/Squirrel/Sqr_System.hpp @@ -0,0 +1,48 @@ +#pragma once +#include "Sqr_CommonFunc.hpp" +#include "EngineFrame/Render/RenderManager.h" +#include "json.hpp" + +static SQInteger SQR_OutPutTable(HSQUIRRELVM v) +{ + const SQChar *Str; + sq_getstring(v, 2, &Str); + + nlohmann::json ex1 = nlohmann::json::parse(Str); + std::string formattedJson = ex1.dump(4); + SDL_Log(formattedJson.c_str()); + return 0; +} + +static SQInteger SQR_GetDebugInfo(HSQUIRRELVM v) +{ + + sq_newtable(v); + struct TableEntry + { + const SQChar *key; + SQInteger value; + } entries[] = { + {_SC("fps"), Game::GetInstance().m_fps}, + {_SC("frametime"), Game::GetInstance().m_frameTime_ms}, + {_SC("rendercount"), Game::GetInstance().GetRenderer()->_frameRenderCount}, + {_SC("nodecount"), Game::GetInstance().m_nodeCount}}; + + for (const auto &entry : entries) + { + sq_pushstring(v, entry.key, -1); + sq_pushinteger(v, entry.value); + sq_newslot(v, -3, SQFalse); + } + return 1; +} + +static void RegisterSystem() +{ + HSQUIRRELVM v = SquirrelEx::GetInstance().GetSquirrelVM(); + + // 打印表 + RegisterNutApi(_SC("sq_OutPutTable"), SQR_OutPutTable, v); + // 获取调试信息表 + RegisterNutApi(_SC("sq_GetDebugInfo"), SQR_GetDebugInfo, v); +} \ No newline at end of file