Magic_Game/include/CursorEx.hpp

116 lines
3.5 KiB
C++
Raw Normal View History

2024-05-08 20:54:36 +08:00
#pragma once
#include <mutex>
#include <kiwano/core/Common.h>
#include <kiwano/base/Module.h>
#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<CursorModule>
, public Module
{
friend Singleton<CursorModule>;
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) {
//ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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();
//<2F><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ȥ<EFBFBD><C8A5>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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();
}
////<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
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);
}
}
}