Added asynchronous model of running scripts
This commit is contained in:
		
							parent
							
								
									01cf9243cf
								
							
						
					
					
						commit
						a234f2d5b2
					
				
					 5 changed files with 52 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -31,6 +31,12 @@
 | 
			
		|||
#include "audio.h"
 | 
			
		||||
#include "boost-hash.h"
 | 
			
		||||
 | 
			
		||||
#ifdef __EMSCRIPTEN__
 | 
			
		||||
#include <emscripten.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <SDL_timer.h>
 | 
			
		||||
 | 
			
		||||
#include <ruby.h>
 | 
			
		||||
#ifndef RUBY_LEGACY_VERSION
 | 
			
		||||
#include <ruby/encoding.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -277,8 +283,8 @@ RB_METHOD(mriRgssMain)
 | 
			
		|||
	{
 | 
			
		||||
		VALUE exc = Qnil;
 | 
			
		||||
 | 
			
		||||
		rb_rescue2((VALUE(*)(VALUE *)) rgssMainCb, rb_block_proc(),
 | 
			
		||||
		           (VALUE(*)(VALUE, VALUE)) rgssMainRescue, (VALUE) &exc,
 | 
			
		||||
		rb_rescue2((VALUE(*)(ANYARGS)) rgssMainCb, rb_block_proc(),
 | 
			
		||||
		           (VALUE(*)(ANYARGS)) rgssMainRescue, (VALUE) &exc,
 | 
			
		||||
		           rb_eException, (VALUE) 0);
 | 
			
		||||
 | 
			
		||||
		if (NIL_P(exc))
 | 
			
		||||
| 
						 | 
				
			
			@ -352,9 +358,6 @@ static VALUE evalHelper(evalArg *arg)
 | 
			
		|||
	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)
 | 
			
		||||
{
 | 
			
		||||
	evalArg arg = { string, filename };
 | 
			
		||||
| 
						 | 
				
			
			@ -385,6 +388,12 @@ struct BacktraceData
 | 
			
		|||
 | 
			
		||||
#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)
 | 
			
		||||
{
 | 
			
		||||
	const Config &conf = shState->rtData().config;
 | 
			
		||||
| 
						 | 
				
			
			@ -485,7 +494,8 @@ static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData)
 | 
			
		|||
	if (exc != Qnil)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	while (true)
 | 
			
		||||
 | 
			
		||||
	/* Execute scripts */
 | 
			
		||||
	{
 | 
			
		||||
		for (long i = 0; i < scriptCount; ++i)
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			@ -513,6 +523,17 @@ static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData)
 | 
			
		|||
				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");
 | 
			
		||||
		shState->eThread().process(shState->rtData());
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -521,6 +542,7 @@ static void __attribute__ ((optnone)) runRMXPScripts(BacktraceData &btData)
 | 
			
		|||
			break;
 | 
			
		||||
 | 
			
		||||
		processReset();
 | 
			
		||||
#endif
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -628,6 +650,7 @@ static void mriBindingExecute()
 | 
			
		|||
	else
 | 
			
		||||
		runRMXPScripts(btData);
 | 
			
		||||
 | 
			
		||||
#ifndef __EMSCRIPTEN__
 | 
			
		||||
	VALUE exc = rb_errinfo();
 | 
			
		||||
	if (!NIL_P(exc) && !rb_obj_is_kind_of(exc, rb_eSystemExit))
 | 
			
		||||
		showExc(exc, btData);
 | 
			
		||||
| 
						 | 
				
			
			@ -635,6 +658,7 @@ static void mriBindingExecute()
 | 
			
		|||
	ruby_cleanup(0);
 | 
			
		||||
 | 
			
		||||
	shState->rtData().rqTermAck.set();
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void mriBindingTerminate()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -294,7 +294,6 @@ void EventThread::process(RGSSThreadData &rtData)
 | 
			
		|||
				rtData.rqReset.set();
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			keyStates[event.key.keysym.scancode] = true;
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -420,6 +420,10 @@ struct FPSLimiter
 | 
			
		|||
private:
 | 
			
		||||
	void delayTicks(uint64_t ticks)
 | 
			
		||||
	{
 | 
			
		||||
 | 
			
		||||
		/* RETURN WITHOUT DOING ANYTHING */
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
#ifndef __EMSCRIPTEN__
 | 
			
		||||
#if defined(HAVE_NANOSLEEP)
 | 
			
		||||
		struct timespec req;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -358,6 +358,17 @@ struct InputPrivate
 | 
			
		|||
 | 
			
		||||
	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;
 | 
			
		||||
 | 
			
		||||
		if (!rtData.bindingUpdateMsg.poll(d))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -150,13 +150,16 @@ int rgssThreadFun(void *userdata)
 | 
			
		|||
	/* Start script execution */
 | 
			
		||||
	scriptBinding->execute();
 | 
			
		||||
 | 
			
		||||
#ifndef __EMSCRIPTEN__
 | 
			
		||||
	threadData->rqTermAck.set();
 | 
			
		||||
 | 
			
		||||
	threadData->ethread->requestTerminate();
 | 
			
		||||
 | 
			
		||||
	SharedState::finiInstance();
 | 
			
		||||
 | 
			
		||||
	alcDestroyContext(alcCtx);
 | 
			
		||||
	SDL_GL_DeleteContext(glCtx);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -345,6 +348,10 @@ int main(int argc, char *argv[])
 | 
			
		|||
	/* Start event processing */
 | 
			
		||||
	eventThread.process(rtData);
 | 
			
		||||
 | 
			
		||||
#ifdef __EMSCRIPTEN__
 | 
			
		||||
	return 0;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	/* Request RGSS thread to stop */
 | 
			
		||||
	rtData.rqTerm.set();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue