aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/je_lua_graphics.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-11-15 19:29:27 +0800
committerchai <chaifix@163.com>2018-11-15 19:29:27 +0800
commit7e51ff3bfae0becc260452a427a1fc1232a4b348 (patch)
treee2c4cddcd5ed719a611be4c92edf1991a63203c5 /src/lua/modules/graphics/je_lua_graphics.cpp
parenta6f2d5fff89b7322009c46a9272668ca4c32ce64 (diff)
*修改代码结构
Diffstat (limited to 'src/lua/modules/graphics/je_lua_graphics.cpp')
-rw-r--r--src/lua/modules/graphics/je_lua_graphics.cpp63
1 files changed, 45 insertions, 18 deletions
diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp
index c535216..0a5394d 100644
--- a/src/lua/modules/graphics/je_lua_graphics.cpp
+++ b/src/lua/modules/graphics/je_lua_graphics.cpp
@@ -140,16 +140,43 @@ namespace JinEngine
{
int w = luax_checkinteger(L, 1);
int h = luax_checkinteger(L, 2);
- if (!luax_istable(L, 3))
+ if (luax_istable(L, 3))
{
- luax_typerror(L, 3, "table");
+ unsigned int r = luax_rawgetnumber(L, 3, 1);
+ unsigned int g = luax_rawgetnumber(L, 3, 2);
+ unsigned int b = luax_rawgetnumber(L, 3, 3);
+ unsigned int a = luax_rawgetnumber(L, 3, 4);
+ bitmap = Bitmap::createBitmap(w, h, Color(r, g, b, a));
+ }
+ else if (luax_isfunction(L, 3))
+ {
+ std::function<Color(int, int, int, int)> drawer = [=](int w, int h, int x, int y)->Color{
+ luax_pushvalue(L, 3);
+ luax_pushnumber(L, w);
+ luax_pushnumber(L, h);
+ luax_pushnumber(L, x);
+ luax_pushnumber(L, y);
+ // Call drawer function.
+ luax_call(L, 4, 1);
+ // Get result color.
+ if (!luax_istable(L, -1))
+ luax_error(L, "Return value of bitmap drawer is wrong, should be a color table.");
+ Color c;
+ c.r = luax_rawgetnumberthenpop(L, -1, 1);
+ c.g = luax_rawgetnumberthenpop(L, -1, 2);
+ c.b = luax_rawgetnumberthenpop(L, -1, 3);
+ c.a = luax_rawgetnumberthenpop(L, -1, 4);
+ // Pop return value.
+ luax_pop(L, 1);
+ return c;
+ };
+ bitmap = Bitmap::createBitmap(w, h, drawer);
+ }
+ else
+ {
+ luax_typerror(L, 3, "color table or color setter");
return 1;
}
- unsigned int r = luax_rawgetnumber(L, 3, 1);
- unsigned int g = luax_rawgetnumber(L, 3, 2);
- unsigned int b = luax_rawgetnumber(L, 3, 3);
- unsigned int a = luax_rawgetnumber(L, 3, 4);
- bitmap = Bitmap::createBitmap(w, h, Color(r, g, b, a));
}
else
{
@@ -777,17 +804,17 @@ namespace JinEngine
LUA_EXPORT int luaopen_graphics(lua_State* L)
{
- luax_newclass(L, luaopen_Bitmap);
- luax_newclass(L, luaopen_Texture);
- luax_newclass(L, luaopen_Canvas);
- luax_newclass(L, luaopen_TTFData);
- luax_newclass(L, luaopen_TTF);
- luax_newclass(L, luaopen_Text);
- luax_newclass(L, luaopen_TextureFont);
- luax_newclass(L, luaopen_Page);
- luax_newclass(L, luaopen_JSL);
- luax_newclass(L, luaopen_Sprite);
- luax_newclass(L, luaopen_SpriteSheet);
+ luaopen_Bitmap(L);
+ luaopen_Texture(L);
+ luaopen_Canvas(L);
+ luaopen_TTFData(L);
+ luaopen_TTF(L);
+ luaopen_Text(L);
+ luaopen_TextureFont(L);
+ luaopen_Page(L);
+ luaopen_Shader(L);
+ luaopen_Sprite(L);
+ luaopen_SpriteSheet(L);
luaL_Reg f[] = {
/* window */