// Runs on particles thread int thread_particles(void *data) { particle_thread *pt = (particle_thread*)data; lua_State *L = lua_open(); /* create state */ lua_pushlightuserdata(L, pt); lua_setglobal(L, "__threaddata"); lua_atpanic(L, particles_lua_panic_handler); luaL_openlibs(L); /* open libraries */ luaopen_core(L); luaopen_particles(L); luaopen_shaders(L); pt->L = L; lua_newtable(L); lua_setglobal(L, "__fcts"); luaL_dostring(L, "function core.shader.allow() return true end"); // Override "print" if requested if (no_debug) { lua_pushcfunction(L, noprint); lua_setglobal(L, "print"); } // And run the lua engine pre init scripts if (!luaL_loadfile(L, "/loader/pre-init.lua")) docall(L, 0, 0); else lua_pop(L, 1); plist *prev; plist *l; while (pt->running) { // Wait for a keyframe // printf("Runing particle thread %d (waiting for sem)\n", pt->id); SDL_SemWait(pt->keyframes); // printf("Runing particle thread %d (waiting for mutex; running(%d))\n", pt->id, pt->running); if (!pt->running) break; SDL_mutexP(pt->lock); int nb = 0; l = pt->list; prev = NULL; while (l) { if (l->ps && l->ps->alive && !l->ps->i_want_to_die) { if (l->ps->init) thread_particle_run(pt, l); else thread_particle_init(pt, l); prev = l; l = l->next; } else { thread_particle_die(pt, l); // Remove dead ones if (!prev) pt->list = l->next; else prev->next = l->next; l = l->next; } nb++; } // printf("Particles thread %d has %d systems\n", pt->id, nb); SDL_mutexV(pt->lock); } printf("Cleaning up particle thread %d\n", pt->id); // Cleanup SDL_mutexP(pt->lock); l = pt->list; while (l) { thread_particle_die(pt, l); l = l->next; } SDL_mutexV(pt->lock); lua_close(L); SDL_DestroySemaphore(pt->keyframes); SDL_DestroyMutex(pt->lock); printf("Cleaned up particle thread %d\n", pt->id); return(0); }
int luaopen_core_and(lua_State *L, JNIEnv *env_, jobject activity_) { env = env_; activity = activity_; return luaopen_core(L); }