feat(脚本引擎): 添加SquirrelVM运行功能并重构主脚本
为SquirrelVM添加run()方法用于执行脚本中的main函数 重构main.nut脚本结构,将打印和错误调用封装到main函数中 在Application启动时自动执行脚本的main函数
This commit is contained in:
@@ -25,9 +25,11 @@ public:
|
||||
bool init();
|
||||
void shutdown();
|
||||
void loadScript();
|
||||
void run();
|
||||
|
||||
HSQUIRRELVM getVM() const { return vm_; }
|
||||
bool isValid() const { return vm_ != nullptr; }
|
||||
bool isInitialized() const { return initialized_; }
|
||||
|
||||
private:
|
||||
SquirrelVM() = default;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <frostbite2D/platform/windows.h>
|
||||
#include <frostbite2D/resource/asset.h>
|
||||
#include <frostbite2D/resource/npk_archive.h>
|
||||
#include <frostbite2D/script/squirrel_vm.h>
|
||||
#include <frostbite2D/resource/pvf_archive.h>
|
||||
#include <frostbite2D/resource/sound_pack_archive.h>
|
||||
#include <frostbite2D/scene/scene.h>
|
||||
@@ -191,7 +192,7 @@ void Application::run() {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Log("Starting application...");
|
||||
|
||||
running_ = true;
|
||||
shouldQuit_ = false;
|
||||
paused_ = false;
|
||||
@@ -203,6 +204,10 @@ void Application::run() {
|
||||
fpsTimer_ = 0.0f;
|
||||
currentFps_ = 0;
|
||||
|
||||
if (SquirrelVM::get().isInitialized()) {
|
||||
SquirrelVM::get().run();
|
||||
}
|
||||
|
||||
mainLoop();
|
||||
|
||||
running_ = false;
|
||||
|
||||
@@ -76,6 +76,28 @@ void SquirrelVM::loadScript() {
|
||||
}
|
||||
}
|
||||
|
||||
void SquirrelVM::run() {
|
||||
if (!initialized_ || !vm_) {
|
||||
return;
|
||||
}
|
||||
|
||||
sq_pushroottable(vm_);
|
||||
sq_pushstring(vm_, "main", -1);
|
||||
|
||||
if (SQ_SUCCEEDED(sq_get(vm_, -2))) {
|
||||
sq_pushroottable(vm_);
|
||||
|
||||
if (SQ_FAILED(sq_call(vm_, 1, SQFalse, SQTrue))) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
|
||||
"[Squirrel] Failed to call main function");
|
||||
}
|
||||
|
||||
sq_pop(vm_, 2);
|
||||
} else {
|
||||
sq_pop(vm_, 1);
|
||||
}
|
||||
}
|
||||
|
||||
bool SquirrelVM::loadScriptFile(const std::string &path) {
|
||||
Asset &asset = Asset::get();
|
||||
std::optional<std::string> content = asset.readFileToString(path);
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
|
||||
|
||||
|
||||
function main()
|
||||
{
|
||||
print("中文测试!")
|
||||
error("中文测试错误!")
|
||||
}
|
||||
Reference in New Issue
Block a user