Added asynchronous model of running scripts

This commit is contained in:
Varun Patil 2018-05-03 11:21:41 +05:30
parent 01cf9243cf
commit a234f2d5b2
5 changed files with 52 additions and 7 deletions

View File

@ -31,6 +31,12 @@
#include "audio.h" #include "audio.h"
#include "boost-hash.h" #include "boost-hash.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include <SDL_timer.h>
#include <ruby.h> #include <ruby.h>
#ifndef RUBY_LEGACY_VERSION #ifndef RUBY_LEGACY_VERSION
#include <ruby/encoding.h> #include <ruby/encoding.h>
@ -277,8 +283,8 @@ RB_METHOD(mriRgssMain)
{ {
VALUE exc = Qnil; VALUE exc = Qnil;
rb_rescue2((VALUE(*)(VALUE *)) rgssMainCb, rb_block_proc(), rb_rescue2((VALUE(*)(ANYARGS)) rgssMainCb, rb_block_proc(),
(VALUE(*)(VALUE, VALUE)) rgssMainRescue, (VALUE) &exc, (VALUE(*)(ANYARGS)) rgssMainRescue, (VALUE) &exc,
rb_eException, (VALUE) 0); rb_eException, (VALUE) 0);
if (NIL_P(exc)) if (NIL_P(exc))
@ -352,9 +358,6 @@ static VALUE evalHelper(evalArg *arg)
return rb_funcall2(Qnil, rb_intern("eval"), ARRAY_SIZE(argv), argv); return rb_funcall2(Qnil, rb_intern("eval"), ARRAY_SIZE(argv), argv);
} }
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
static VALUE evalString(VALUE string, VALUE filename, int *state) static VALUE evalString(VALUE string, VALUE filename, int *state)
{ {
evalArg arg = { string, filename }; evalArg arg = { string, filename };
@ -385,6 +388,12 @@ struct BacktraceData
#define SCRIPT_SECTION_FMT (rgssVer >= 3 ? "{%04ld}" : "Section%03ld") #define SCRIPT_SECTION_FMT (rgssVer >= 3 ? "{%04ld}" : "Section%03ld")
void main_update_loop() {
int state;
VALUE result;
result = rb_eval_string_protect("main_update_loop", &state);
}
static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData) static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData)
{ {
const Config &conf = shState->rtData().config; const Config &conf = shState->rtData().config;
@ -485,7 +494,8 @@ static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData)
if (exc != Qnil) if (exc != Qnil)
return; return;
while (true)
/* Execute scripts */
{ {
for (long i = 0; i < scriptCount; ++i) for (long i = 0; i < scriptCount; ++i)
{ {
@ -513,6 +523,17 @@ static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData)
break; break;
} }
#ifdef __EMSCRIPTEN__
/* Use loop for emscripten */
emscripten_set_main_loop(main_update_loop,0, 0);
#else
while(true) {
int state;
VALUE result;
result = rb_eval_string_protect("main_update_loop", &state);
if (state) break;
}
printf("tick\n"); printf("tick\n");
shState->eThread().process(shState->rtData()); shState->eThread().process(shState->rtData());
@ -521,6 +542,7 @@ static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData)
break; break;
processReset(); processReset();
#endif
} }
} }
@ -628,6 +650,7 @@ static void mriBindingExecute()
else else
runRMXPScripts(btData); runRMXPScripts(btData);
#ifndef __EMSCRIPTEN__
VALUE exc = rb_errinfo(); VALUE exc = rb_errinfo();
if (!NIL_P(exc) && !rb_obj_is_kind_of(exc, rb_eSystemExit)) if (!NIL_P(exc) && !rb_obj_is_kind_of(exc, rb_eSystemExit))
showExc(exc, btData); showExc(exc, btData);
@ -635,6 +658,7 @@ static void mriBindingExecute()
ruby_cleanup(0); ruby_cleanup(0);
shState->rtData().rqTermAck.set(); shState->rtData().rqTermAck.set();
#endif
} }
static void mriBindingTerminate() static void mriBindingTerminate()

View File

@ -294,7 +294,6 @@ void EventThread::process(RGSSThreadData &rtData)
rtData.rqReset.set(); rtData.rqReset.set();
break; break;
} }
keyStates[event.key.keysym.scancode] = true; keyStates[event.key.keysym.scancode] = true;
break; break;

View File

@ -420,6 +420,10 @@ struct FPSLimiter
private: private:
void delayTicks(uint64_t ticks) void delayTicks(uint64_t ticks)
{ {
/* RETURN WITHOUT DOING ANYTHING */
return;
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
#if defined(HAVE_NANOSLEEP) #if defined(HAVE_NANOSLEEP)
struct timespec req; struct timespec req;

View File

@ -358,6 +358,17 @@ struct InputPrivate
void checkBindingChange(const RGSSThreadData &rtData) void checkBindingChange(const RGSSThreadData &rtData)
{ {
#ifdef __EMSCRIPTEN__
/* Run only once for emscripten. This is necessary due to *
* a bug (in mutable?) that keeps reinitializing bindings */
static bool once = false;
if (!once)
once = true;
else
return;
#endif
BDescVec d; BDescVec d;
if (!rtData.bindingUpdateMsg.poll(d)) if (!rtData.bindingUpdateMsg.poll(d))

View File

@ -150,13 +150,16 @@ int rgssThreadFun(void *userdata)
/* Start script execution */ /* Start script execution */
scriptBinding->execute(); scriptBinding->execute();
#ifndef __EMSCRIPTEN__
threadData->rqTermAck.set(); threadData->rqTermAck.set();
threadData->ethread->requestTerminate(); threadData->ethread->requestTerminate();
SharedState::finiInstance(); SharedState::finiInstance();
alcDestroyContext(alcCtx); alcDestroyContext(alcCtx);
SDL_GL_DeleteContext(glCtx); SDL_GL_DeleteContext(glCtx);
#endif
return 0; return 0;
} }
@ -345,6 +348,10 @@ int main(int argc, char *argv[])
/* Start event processing */ /* Start event processing */
eventThread.process(rtData); eventThread.process(rtData);
#ifdef __EMSCRIPTEN__
return 0;
#endif
/* Request RGSS thread to stop */ /* Request RGSS thread to stop */
rtData.rqTerm.set(); rtData.rqTerm.set();