From 8e208d0f5bca187588da8ece1b955bdd47e75acd Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sat, 3 Oct 2015 23:42:21 +0800 Subject: Port the VM function caller from the testbed version. This part still requires refinement. --- src/core/bullet.cpp | 3 ++- src/core/bullet.hpp | 1 + src/core/fncmodules.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/core/fncmodules.hpp | 5 ++++- src/core/fncwrapper.cpp | 15 +++++++++++++++ src/core/vmrunner.cpp | 10 +++++++++- src/core/vmrunner.hpp | 5 ++--- 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/core/fncwrapper.cpp (limited to 'src') diff --git a/src/core/bullet.cpp b/src/core/bullet.cpp index 175bae4..f7bcc90 100644 --- a/src/core/bullet.cpp +++ b/src/core/bullet.cpp @@ -17,7 +17,7 @@ void bulletBase::update() void bulletBase::render() { //test view mode - bmInstance->getBulEntity2D(basecolor)->setColor(0xC0FFFFFF); + bmInstance->getBulEntity2D(basecolor)->setColor(rendercolor); bmInstance->getBulEntity2D(basecolor)->render(pos.x,pos.y,0,renderscale*0.6); } bulletBase::~bulletBase(){} @@ -81,6 +81,7 @@ void bulletManager::updateBullet() x->pos=smvec2d(400,300); x->vel=smvec2d(rand()%100-50,rand()%100-50); x->vel.normalize(); + x->rendercolor=0xC0FFFFFF; b=0; } for(int i=0;i +#include "smmath.hpp" +#include "bullet.hpp" +#include "scriptshared.hpp" +#include "vmrunner.hpp" +Idata randr(Idata a,...) +{ + Idata ret;ret.type=1; + va_list val; + va_start(val,a); + extern blrScriptVM *vm; + ret.r()=vm->re->nextDouble(a.r(),va_arg(val,Idata).r()); + va_end(val); + return ret; +} +Idata randi(Idata a,...) +{ + Idata ret;ret.type=0; + va_list val; + va_start(val,a); + extern blrScriptVM *vm; + ret.i()=vm->re->nextInt(a.i(),va_arg(val,Idata).i()); + va_end(val); + return ret; +} +Idata createBullet(Idata x,...) +{ + Idata y,bs,rad; + va_list val; + va_start(val,x); + y=va_arg(val,Idata); + bs=va_arg(val,Idata); + rad=va_arg(val,Idata); + extern bulletManager *bmInstance; + int i=bmInstance->allocBullet(); + bmInstance->getHandle(i)->init(); + bmInstance->getHandle(i)->pos.x=x.r(); + bmInstance->getHandle(i)->pos.y=y.r(); + bmInstance->getHandle(i)->vel=bs.r()*smvec2d(cos(rad.r()),sin(rad.r())); + bmInstance->getHandle(i)->acc=smvec2d(0,0); + bmInstance->getHandle(i)->basecolor=blue; + bmInstance->getHandle(i)->rendercolor=0xC0FFFFFF; + bmInstance->getHandle(i)->collrange=4; + bmInstance->getHandle(i)->scollrange=16; + Idata ret;ret.type=0;ret.i()=i; + return ret; +} diff --git a/src/core/fncmodules.hpp b/src/core/fncmodules.hpp index 8d1c8b6..fe8eccf 100644 --- a/src/core/fncmodules.hpp +++ b/src/core/fncmodules.hpp @@ -1 +1,4 @@ - +#include "scriptshared.hpp" +extern Idata randr(Idata a,...); +extern Idata randi(Idata a,...); +extern Idata createBullet(Idata x,...); diff --git a/src/core/fncwrapper.cpp b/src/core/fncwrapper.cpp new file mode 100644 index 0000000..b1dd4d7 --- /dev/null +++ b/src/core/fncwrapper.cpp @@ -0,0 +1,15 @@ +#include "vmrunner.hpp" +#include "fncmodules.hpp" +extern blrScriptVM *vm; +extern callStack callStk; +extern unsigned getHash(const char *s); +void callFnc(const char* fnc) +{ + if(getHash(fnc)==getHash("randi")) + randi(callStk.pop(),callStk.pop()); + if(getHash(fnc)==getHash("randr")) + randr(callStk.pop(),callStk.pop()); + if(getHash(fnc)==getHash("createBullet")) + createBullet(callStk.pop(),callStk.pop(),callStk.pop(),callStk.pop()); + if(callStk.empty())callStk.clear(); +} diff --git a/src/core/vmrunner.cpp b/src/core/vmrunner.cpp index 1942d74..4da775b 100644 --- a/src/core/vmrunner.cpp +++ b/src/core/vmrunner.cpp @@ -1,6 +1,8 @@ #include "vmrunner.hpp" #include "coreshared.hpp" #include +blrScriptVM *vm; +callStack callStk; unsigned getHash(const char *s) { unsigned r=5381;char c; @@ -272,9 +274,15 @@ void blrScriptVM::runFunction(const char *fncnym) if(!jmp)++cur; } } -void blrScriptVM::vmInit() +void blrScriptVM::vmInit(unsigned int seed) { + for(int i=0;i<103;++i)rr[i].type=1; + for(int i=0;i<101;++i)ir[i].type=0; + for(int i=0;i<10000;++i)ra[i].type=1,ia[i].type=0; + re=new smRandomEngine; + re->setSeed(seed); } void blrScriptVM::vmDeinit() { + delete re; } diff --git a/src/core/vmrunner.hpp b/src/core/vmrunner.hpp index 60bc2ce..d9d9a59 100644 --- a/src/core/vmrunner.hpp +++ b/src/core/vmrunner.hpp @@ -21,12 +21,10 @@ public: memb front(){return data[l];} memb back(){return data[r];} }; -callStack callStk; typedef struct _fncEntry{int hash,pos;}fncEntry; class blrScriptVM { private: - smRandomEngine* re; Idata ir[101],ia[10000]; Idata rr[103],ra[10000]; SInst inst[65537]; @@ -38,11 +36,12 @@ private: int mgetc(); Idata& fetchData(SPara para,bool forcerw=false); public: + smRandomEngine* re; blrScriptVM(){ic=fncnt=pinst=0;} int loadLSBFromMemory(const char* ptr,DWORD size); int getInstCount(); void runFunction(const char *fncnym); - void vmInit(); + void vmInit(unsigned int seed); void vmDeinit(); }; #endif -- cgit v1.2.3