From 7e51ff3bfae0becc260452a427a1fc1232a4b348 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 15 Nov 2018 19:29:27 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/graphics/je_lua_graphics.cpp | 63 ++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 18 deletions(-) (limited to 'src/lua/modules/graphics/je_lua_graphics.cpp') 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 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 */ -- cgit v1.1-26-g67d0