static int ltest(lua_State *L) { struct sprite *s = self(L); struct srt srt; fill_srt(L,&srt,4); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); struct sprite * m = sprite_test(s, &srt, x*SCREEN_SCALE, y*SCREEN_SCALE); if (m == NULL) return 0; if (m==s) { lua_settop(L,1); return 1; } lua_settop(L,1); int depth = unwind(L, s , m); if (depth < 0) { return luaL_error(L, "Unwind an invalid sprite"); } int i; lua_pushvalue(L,1); for (i=depth+1;i>1;i--) { struct sprite * tmp = (struct sprite *)lua_touserdata(L, i); tmp = lookup(L, tmp); if (tmp == NULL) { return luaL_error(L, "find an invalid sprite"); } lua_replace(L, -2); } return 1; }
static int lgetwpos(lua_State *L) { struct sprite *s = self(L); if (s->type == TYPE_ANCHOR) { struct matrix* mat = s->s.mat; lua_pushnumber(L,mat->m[4] /(float)SCREEN_SCALE); lua_pushnumber(L,mat->m[5] /(float)SCREEN_SCALE); return 2; } else { struct srt srt; fill_srt(L,&srt,2); struct sprite *t = (struct sprite *)lua_touserdata(L, 3); if (t == NULL) { luaL_error(L, "Need target sprite"); } int pos[2]; if (sprite_pos(s, &srt, t, pos) == 0) { lua_pushinteger(L, pos[0]); lua_pushinteger(L, pos[1]); return 2; } else { return 0; } } return luaL_error(L, "Only anchor can get world matrix"); }
static int lmulti_draw(lua_State *L) { struct sprite *s = self(L); int cnt = (int)luaL_checkinteger(L,3); if (cnt == 0) return 0; luaL_checktype(L,4,LUA_TTABLE); luaL_checktype(L,5,LUA_TTABLE); if (lua_rawlen(L, 4) < cnt) { return luaL_error(L, "matrix length less then particle count"); } struct srt srt; fill_srt(L, &srt, 2); int i; for (i = 0; i < cnt; i++) { lua_rawgeti(L, 4, i+1); lua_rawgeti(L, 5, i+1); struct matrix * mat = (struct matrix *)lua_touserdata(L, -2); s->t.mat = mat; s->t.color = (uint32_t)lua_tounsigned(L, -1); lua_pop(L, 2); sprite_draw(s, &srt); } s->t.mat = NULL; return 0; }
static int lgetwpos(lua_State *L) { struct sprite *s = self(L); if (s->type == TYPE_ANCHOR) { struct matrix* mat = s->s.mat; lua_pushnumber(L,mat->m[4] /(float)SCREEN_SCALE); lua_pushnumber(L,mat->m[5] /(float)SCREEN_SCALE); return 2; } else { struct srt srt; fill_srt(L,&srt,2); struct matrix tmp; sprite_matrix(s, &tmp); int pos[2]; if (sprite_pos(s, &srt, &tmp, pos) == 0) { lua_pushinteger(L, pos[0]); lua_pushinteger(L, pos[1]); return 2; } else { return 0; } } return luaL_error(L, "Only anchor can get world matrix"); }
static int lmulti_draw(lua_State *L) { struct sprite *s = self(L); int cnt = (int)luaL_checkinteger(L,3); if (cnt == 0) return 0; int n = lua_gettop(L); luaL_checktype(L,4,LUA_TTABLE); luaL_checktype(L,5,LUA_TTABLE); if (lua_rawlen(L, 4) < cnt) { return luaL_error(L, "matrix length less then particle count"); } if (n == 6) { luaL_checktype(L,6,LUA_TTABLE); if (lua_rawlen(L, 6) < cnt) { return luaL_error(L, "additive length less then particle count"); } } struct srt srt; fill_srt(L, &srt, 2); if (s->t.mat == NULL) { s->t.mat = &s->mat; matrix_identity(&s->mat); } struct matrix *parent_mat = s->t.mat; uint32_t parent_color = s->t.color; int i; if (n == 5) { for (i = 0; i < cnt; i++) { lua_rawgeti(L, 4, i+1); lua_rawgeti(L, 5, i+1); s->t.mat = (struct matrix *)lua_touserdata(L, -2); s->t.color = (uint32_t)lua_tounsigned(L, -1); lua_pop(L, 2); sprite_draw_as_child(s, &srt, parent_mat, parent_color); } }else { for (i = 0; i < cnt; i++) { lua_rawgeti(L, 4, i+1); lua_rawgeti(L, 5, i+1); lua_rawgeti(L, 6, i+1); s->t.mat = (struct matrix *)lua_touserdata(L, -3); s->t.color = (uint32_t)lua_tounsigned(L, -2); s->t.additive = (uint32_t)lua_tounsigned(L, -1); lua_pop(L, 3); sprite_draw_as_child(s, &srt, parent_mat, parent_color); } } s->t.mat = parent_mat; s->t.color = parent_color; return 0; }
/* userdata sprite table { .x .y .sx .sy .rot } */ static int ldraw(lua_State *L) { struct sprite *s = self(L); struct srt srt; fill_srt(L,&srt,2); sprite_draw(s, &srt); return 0; }
/* userdata sprite table { .x .y .sx .sy .rot } */ static int ldraw(lua_State *L) { struct sprite *s = self(L); struct srt srt; fill_srt(L,&srt,2); struct program_param pp; fill_pp(L,&pp,3); sprite_draw(s, &srt, &pp); return 0; }
static int laabb(lua_State *L) { struct sprite *s = self(L); struct srt srt; fill_srt(L,&srt,2); int aabb[4]; sprite_aabb(s, &srt, aabb); int i; for (i=0;i<4;i++) { lua_pushinteger(L, aabb[i]); } return 4; }