diff --git a/Frostbite2D/include/frostbite2D/script/squirrel_vm.h b/Frostbite2D/include/frostbite2D/script/squirrel_vm.h index 652ebe3..80f747f 100644 --- a/Frostbite2D/include/frostbite2D/script/squirrel_vm.h +++ b/Frostbite2D/include/frostbite2D/script/squirrel_vm.h @@ -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; diff --git a/Frostbite2D/src/frostbite2D/core/application.cpp b/Frostbite2D/src/frostbite2D/core/application.cpp index 4d542cb..ddfb2aa 100644 --- a/Frostbite2D/src/frostbite2D/core/application.cpp +++ b/Frostbite2D/src/frostbite2D/core/application.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -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; diff --git a/Frostbite2D/src/frostbite2D/script/squirrel_vm.cpp b/Frostbite2D/src/frostbite2D/script/squirrel_vm.cpp index 2bb8aa1..ad5c046 100644 --- a/Frostbite2D/src/frostbite2D/script/squirrel_vm.cpp +++ b/Frostbite2D/src/frostbite2D/script/squirrel_vm.cpp @@ -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 content = asset.readFileToString(path); diff --git a/Game/assets/scripts/main.nut b/Game/assets/scripts/main.nut index 7463211..5571809 100644 --- a/Game/assets/scripts/main.nut +++ b/Game/assets/scripts/main.nut @@ -1,2 +1,8 @@ -print("中文测试!") -error("中文测试错误!") + + + +function main() +{ + print("中文测试!") + error("中文测试错误!") +} \ No newline at end of file