109 lines
1.9 KiB
C++
109 lines
1.9 KiB
C++
#include <Windows.h>
|
||
#include "SquirrelClassEx.h"
|
||
#include <iostream>
|
||
|
||
#ifdef SQUNICODE
|
||
#define scfprintf fwprintf
|
||
#define scvprintf vfwprintf
|
||
#else
|
||
|
||
#define scvprintf vfprintf
|
||
#endif
|
||
|
||
#include <kiwano/kiwano.h>
|
||
using namespace kiwano;
|
||
#include <kiwano-audio/kiwano-audio.h>
|
||
using namespace kiwano::audio;
|
||
|
||
|
||
|
||
|
||
//原生输出函数
|
||
void printfunc(HSQUIRRELVM v, const SQChar* s, ...)
|
||
{
|
||
va_list vl;
|
||
va_start(vl, s);
|
||
scvprintf(stdout, s, vl);
|
||
va_end(vl);
|
||
}
|
||
|
||
void errorfunc(HSQUIRRELVM v, const SQChar* s, ...)
|
||
{
|
||
va_list vl;
|
||
va_start(vl, s);
|
||
scvprintf(stderr, s, vl);
|
||
va_end(vl);
|
||
}
|
||
|
||
|
||
|
||
|
||
SQInteger SquirrelClassEx::ReloadingScript()
|
||
{
|
||
return SQInteger();
|
||
}
|
||
|
||
SQInteger SquirrelClassEx::Exit(HSQUIRRELVM v)
|
||
{
|
||
Application::GetInstance().Quit();
|
||
return 0;
|
||
}
|
||
|
||
|
||
//初始化
|
||
void SquirrelClassEx::Init()
|
||
{
|
||
v = sq_open(4096); //创建虚拟机,其栈的初始大小为1024
|
||
sq_setprintfunc(v, printfunc, errorfunc);
|
||
|
||
sq_pushroottable(v);
|
||
|
||
sqstd_register_bloblib(v);
|
||
sqstd_register_iolib(v);
|
||
sqstd_register_systemlib(v);
|
||
sqstd_register_mathlib(v);
|
||
sqstd_register_stringlib(v);
|
||
sqstd_seterrorhandlers(v);
|
||
|
||
R_Register_Nut(v);
|
||
|
||
//输出版本信息
|
||
scfprintf(stdout, _SC("%s %s (%d bits)\n"), SQUIRREL_VERSION, SQUIRREL_COPYRIGHT, (int)sizeof(SQInteger) * 8);
|
||
|
||
//第一次加载脚本
|
||
ReloadingScript();
|
||
|
||
sqstd_dofile(v, _SC("Script/Main.nut"), false, true);
|
||
}
|
||
|
||
|
||
void SquirrelClassEx::RegisterNutApi(const SQChar* funcName, void* funcAddr, HSQUIRRELVM v)
|
||
{
|
||
sq_pushroottable(v);
|
||
sq_pushstring(v, funcName, -1);
|
||
sq_newclosure(v, (SQFUNCTION)funcAddr, 0);
|
||
sq_newslot(v, -3, false);
|
||
sq_poptop(v);
|
||
}
|
||
|
||
void SquirrelClassEx::R_Register_Nut(HSQUIRRELVM v)
|
||
{
|
||
RegisterNutApi(_SC("sq_ReloadingScript"), ReloadingScript, v);
|
||
RegisterNutApi(_SC("sq_Exit"), Exit, v);
|
||
|
||
|
||
}
|
||
|
||
void SquirrelClassEx::Run()
|
||
{
|
||
|
||
|
||
}
|
||
|
||
void SquirrelClassEx::Close()
|
||
{
|
||
sq_close(v);
|
||
}
|
||
|
||
|