#pragma once #include #include #include #include "SquirrelClassEx.h" #include "KiwanoEx/SpriteEx.hpp" extern HSQUIRRELVM v; extern std::mutex VmMtx; extern WThreadPool threadPool; namespace kiwano { namespace cursor { class KGE_API CursorModule : public Singleton , public Module { friend Singleton; public: ~CursorModule() {}; void OnUpdate(UpdateModuleContext& ctx) override; void OnRender(RenderModuleContext& ctx) override; private: CursorModule(); private: SpriteEx* cursor_actor_; }; } } void InitGameRecFunc() { PVF_M::getInstance().Init(); } namespace kiwano { namespace cursor { CursorModule::CursorModule() { } bool IsMouseInWindow(HWND hWnd) { POINT mousePos; GetCursorPos(&mousePos); ScreenToClient(hWnd, &mousePos); RECT windowRect; GetClientRect(hWnd, &windowRect); return PtInRect(&windowRect, mousePos); } void CursorModule::OnUpdate(UpdateModuleContext& ctx) { //确保虚拟机已经初始化了 从虚拟机回调中加载鼠标 if (v && !cursor_actor_) { if (VmMtx.try_lock()) { SQUserPointer Cursor; SQInteger top = sq_gettop(v); //saves the stack size before the call sq_pushroottable(v); //pushes the global table sq_pushstring(v, _SC("InitCursor"), -1); if (SQ_SUCCEEDED(sq_get(v, -2))) { //gets the field 'foo' from the global table sq_pushroottable(v); //push the 'this' (in this case is the global table) sq_call(v, 1, SQTrue, SQTrue); //calls the function sq_getuserpointer(v, -1, &Cursor); cursor_actor_ = (SpriteEx*)Cursor; } sq_settop(v, top); //restores the original stack size VmMtx.unlock(); //分配线程去初始化数据 threadPool.concurrentRun(InitGameRecFunc); } } else { if (VmMtx.try_lock()) { SQInteger top = sq_gettop(v); //saves the stack size before the call sq_pushroottable(v); //pushes the global table sq_pushstring(v, _SC("UpdateCursor"), -1); if (SQ_SUCCEEDED(sq_get(v, -2))) { //gets the field 'foo' from the global table sq_pushroottable(v); //push the 'this' (in this case is the global table) sq_pushinteger(v, ctx.dt.GetMilliseconds()); sq_call(v, 2, SQFalse, SQTrue); //calls the function } sq_settop(v, top); //restores the original stack size VmMtx.unlock(); } ////获取输入 Input& input = Input::GetInstance(); cursor_actor_->SetPosition(input.GetMousePos()); if (IsMouseInWindow(GetForegroundWindow())) ShowCursor(FALSE); else ShowCursor(TRUE); } } void CursorModule::OnRender(RenderModuleContext& ctx) { if (cursor_actor_)cursor_actor_->Render(ctx.render_ctx); } } }