static struct sprite * newsprite(lua_State *L, struct sprite_pack *pack, int id) { if (id == ANCHOR_ID) { return newanchor(L); } int sz = sprite_size(pack, id); if (sz == 0) { return NULL; } struct sprite * s = (struct sprite *)lua_newuserdata(L, sz); sprite_init(s, pack, id, sz); int i; for (i=0;;i++) { int childid = sprite_component(s, i); if (childid < 0) break; if (i==0) { lua_newtable(L); lua_pushvalue(L,-1); lua_setuservalue(L, -3); // set uservalue for sprite } struct sprite *c = newsprite(L, pack, childid); c->name = sprite_childname(s, i); sprite_mount(s, i, c); update_message(c, pack, id, i, s->frame); if (c) { lua_rawseti(L, -2, i+1); } } if (i>0) { lua_pop(L,1); } return s; }
static struct sprite * lookup(lua_State *L, struct sprite * spr) { int i; struct sprite * root = (struct sprite *)lua_touserdata(L, -1); lua_getuservalue(L,-1); for (i=0;sprite_component(root, i)>=0;i++) { struct sprite * child = root->data.children[i]; if (child) { lua_rawgeti(L, -1, i+1); if (child == spr) { lua_replace(L,-2); return child; } else { lua_pop(L,1); } } } lua_pop(L,1); return NULL; }
static struct sprite * lookup(lua_State *L, struct sprite * spr) { int i; struct sprite * root = (struct sprite *)lua_touserdata(L, -1); lua_getuservalue(L,-1); for (i=0;sprite_component(root, i)>=0;i++) { struct sprite * child = root->data.children[i]; if (child) { lua_rawgeti(L, -1, i+1); // parent, reftable, child if (child == spr) { int child_index = lua_gettop(L); int parent_index = child_index - 2; ref_parent(L, child_index, parent_index); lua_replace(L,-2); // parent return child; } else { lua_pop(L,1); } } } lua_pop(L,1); return NULL; }