From 58ad98213763348b49d8313675203b287a5442bb Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 20 Oct 2018 10:13:31 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E5=A4=B4=E6=96=87=E4=BB=B6=E5=AE=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/graphics/graphics.cpp | 41 +++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'src/lua/modules/graphics/graphics.cpp') diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 4ef2b56..31f6fba 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -1,18 +1,16 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "libjin/jin.h" -#include "lua/common/common.h" - #include #include -using namespace std; +#include "libjin/jin.h" +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" namespace jin { namespace lua { - + using namespace std; using namespace jin::graphics; using jin::filesystem::Filesystem; using jin::filesystem::Buffer; @@ -655,8 +653,23 @@ namespace lua { Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); Bitmap* bitmap = p->getObject(); - Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = pt->getObject(); + Text* text; + if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) + { + Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + text = pt->getObject(); + } + else if (luax_isstring(L, 2)) + { + unsigned len; + const char* str = luax_checklstring(L, 2, &len); + text = new Text(Encode::UTF8, str, len); + } + else + { + luax_typerror(L, 2, "Text or string"); + return 1; + } float cellh = luax_checknumber(L, 4); TextureFont* textureFont = nullptr; if (luax_istable(L, 3)) @@ -672,6 +685,16 @@ namespace lua float cellw = luax_checknumber(L, 3); textureFont = TextureFont::createTextureFont(bitmap, *text, cellw, cellh); } + else + { + luax_error(L, "bad arguments #3 to 'newTextureFont', need to be table or number"); + return 0; + } + if (luax_isstring(L, 2)) + { + // Delete temporary text. + delete text; + } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTUREFONT, sizeof(Proxy)); proxy->bind(new Ref(textureFont, JIN_GRAPHICS_TEXTUREFONT)); return 1; -- cgit v1.1-26-g67d0