From 7f6e5a022b800af0a2f7ab502743805e6fbc448c Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Mon, 5 Oct 2015 23:49:07 +0800 Subject: Enable the BLR script VM (prototype). The code (definitely) still require improvements. TODOs: Reimplement the call stack. Unify float data types. --- src/blr3.dtp | Bin 459577 -> 459958 bytes src/core/bullet.cpp | 8 ++++---- src/core/bullet.hpp | 28 ++++++++++++++++++++++++++-- src/core/fncmodules.cpp | 20 +++++++++----------- src/core/fncwrapper.cpp | 2 -- src/core/gamescene.cpp | 8 ++++++++ src/core/vmrunner.cpp | 11 ++++++----- src/core/vmrunner.hpp | 7 +++++-- 8 files changed, 58 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/blr3.dtp b/src/blr3.dtp index 1f62320..08d6551 100644 Binary files a/src/blr3.dtp and b/src/blr3.dtp differ diff --git a/src/core/bullet.cpp b/src/core/bullet.cpp index f7bcc90..c8cb50f 100644 --- a/src/core/bullet.cpp +++ b/src/core/bullet.cpp @@ -42,7 +42,7 @@ void bulletManager::deinit() for(int i=0;i<(int)COLOR_COUNT;++i) {delete bulent2d[i];delete bulent3d[i];} } -template +/*template int bulletManager::allocBullet() { if(!alloced) @@ -69,10 +69,10 @@ int bulletManager::createBullet() int ptr=allocBullet(); bullets[ptr]->init(); return ptr; -} +}*/ void bulletManager::updateBullet() { - static int b=0; + /*static int b=0; ++b; if(b>15) { @@ -83,7 +83,7 @@ void bulletManager::updateBullet() x->vel.normalize(); x->rendercolor=0xC0FFFFFF; b=0; - } + }*/ for(int i=0;iexist) bullets[i]->update(); diff --git a/src/core/bullet.hpp b/src/core/bullet.hpp index b240e81..e3dc8fe 100644 --- a/src/core/bullet.hpp +++ b/src/core/bullet.hpp @@ -38,8 +38,32 @@ private: public: void init(); void deinit(); - templateint allocBullet(); - templateint createBullet(); + templateint allocBullet() + { + if(!alloced) + { + alloced=1; + bullets[0]=new T; + return 0; + } + else + { + int i; + for(i=0;iexist)break; + if(i==alloced) + bullets[alloced++]=new T; + return i; + } + return -1; + } + templateint createBullet() + { + //stub... + int ptr=allocBullet(); + bullets[ptr]->init(); + return ptr; + } void updateBullet(); void renderBullet(); bulletBase* getHandle(int id); diff --git a/src/core/fncmodules.cpp b/src/core/fncmodules.cpp index 14d2f48..08747c8 100644 --- a/src/core/fncmodules.cpp +++ b/src/core/fncmodules.cpp @@ -2,7 +2,6 @@ #include "bullet.hpp" #include "scriptshared.hpp" #include "vmrunner.hpp" -extern callStack callStk; Idata randr() { Idata ret;ret.type=1; @@ -19,17 +18,16 @@ Idata randi() } Idata createBullet() { - Idata x,y,bs,rad; - x=callStk.pop(); - y=callStk.pop(); - bs=callStk.pop(); - rad=callStk.pop(); + float x,y,bs,rad; + rad=callStk.pop().r(); + bs=callStk.pop().r(); + y=callStk.pop().r(); + x=callStk.pop().r(); 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())); + int i=bmInstance->createBullet(); + bmInstance->getHandle(i)->pos.x=x; + bmInstance->getHandle(i)->pos.y=y; + bmInstance->getHandle(i)->vel=bs*smvec2d(cos(rad),sin(rad)); bmInstance->getHandle(i)->acc=smvec2d(0,0); bmInstance->getHandle(i)->basecolor=blue; bmInstance->getHandle(i)->rendercolor=0xC0FFFFFF; diff --git a/src/core/fncwrapper.cpp b/src/core/fncwrapper.cpp index 0927541..8f08bae 100644 --- a/src/core/fncwrapper.cpp +++ b/src/core/fncwrapper.cpp @@ -1,7 +1,5 @@ #include "vmrunner.hpp" #include "fncmodules.hpp" -extern callStack callStk; -extern blrScriptVM *vm; extern unsigned getHash(const char *s); void callFnc(const char* fnc) { diff --git a/src/core/gamescene.cpp b/src/core/gamescene.cpp index 3bda53f..32fea88 100644 --- a/src/core/gamescene.cpp +++ b/src/core/gamescene.cpp @@ -3,6 +3,7 @@ #include "corepublic.hpp" #include "bullet.hpp" #include "player.hpp" +#include "vmrunner.hpp" SMELT* gameScene::sm=NULL; gameScene::gameScene() { @@ -23,14 +24,20 @@ gameScene::gameScene() tgquad.v[3].tx=0;tgquad.v[3].ty=600./rh; bmInstance=new bulletManager; player=new playerBase; + vm=new blrScriptVM; utime=0; ttfont.loadTTFFromMemory(blrdtp.getFilePtr("FreeMono.ttf"),blrdtp.getFileSize("FreeMono.ttf"),12); bmInstance->init(); + vm->vmInit(61616); + vm->loadLSBFromMemory(blrdtp.getFilePtr("test.lsb"),blrdtp.getFileSize("test.lsb")); + vm->vmRunFunction("init"); } gameScene::~gameScene() { bmInstance->deinit(); + vm->vmDeinit(); delete bmInstance; + delete vm; delete player; bmInstance=NULL; ttfont.releaseTTF(); @@ -63,6 +70,7 @@ bool gameScene::sceneRender() } bool gameScene::threadUpdate() { + vm->vmRunFunction("update"); bmInstance->updateBullet(); player->update(); return false; diff --git a/src/core/vmrunner.cpp b/src/core/vmrunner.cpp index 7cad11a..fb4c5e6 100644 --- a/src/core/vmrunner.cpp +++ b/src/core/vmrunner.cpp @@ -2,6 +2,7 @@ #include "coreshared.hpp" #include "fncwrapper.hpp" #include +#include blrScriptVM *vm; callStack callStk; unsigned getHash(const char *s) @@ -42,7 +43,7 @@ Idata& blrScriptVM::fetchData(SPara para,bool forcerw) int blrScriptVM::mgetc() { if(cbyte-sbytetype) { - case 1: l=8;break; - case 0: case 4: case 5: l=4;break; + case 0: case 1: case 4: case 5: l=4;break; case 2: case 3: case 6: case 7: l=1;break; default: break; } for(int i=0;idata.d<<=8LL; - para->data.d|=(unsigned long long)mgetc(); + int a=mgetc(); + para->data.d|=(unsigned long long)a; } } int blrScriptVM::loadLSBFromMemory(const char *ptr,DWORD size) { - cbyte=sbyte=ptr;fsize=size; + cbyte=sbyte=(const unsigned char*)ptr;fsize=size; int ibyt=0,infunc=0; while(~(ibyt=mgetc())) { diff --git a/src/core/vmrunner.hpp b/src/core/vmrunner.hpp index 2dec71f..ee03385 100644 --- a/src/core/vmrunner.hpp +++ b/src/core/vmrunner.hpp @@ -1,5 +1,6 @@ #ifndef VMRUNNER_H #define VMRUNNER_H +#include #include "smelt.hpp" #include "smrandom.hpp" #include "scriptshared.hpp" @@ -16,7 +17,7 @@ public: void clear(){l=0;r=-1;} int size(){return r-l+1;} bool empty(){return size()==0;} - void push(memb a){data[++r]=a;if(r>15)throw;} + void push(memb a){memcpy(data+(++r),&a,sizeof(a));if(r>15)throw;} memb pop(){if(l<=r+1)return data[l++];else throw;} memb front(){return data[l];} memb back(){return data[r];} @@ -30,7 +31,7 @@ private: SInst inst[65537]; int ic,fncnt,pinst; fncEntry fncent[8]; - const char *sbyte,*cbyte; + const unsigned char *sbyte,*cbyte; DWORD fsize; void readPara(SPara *para); int mgetc(); @@ -46,4 +47,6 @@ public: void vmInit(unsigned int seed); void vmDeinit(); }; +extern blrScriptVM *vm; +extern callStack callStk; #endif -- cgit v1.2.3