From 68b60be0d1da84aa670d29b87b26ab3e3db51b69 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 15 Oct 2018 12:49:54 +0800 Subject: =?UTF-8?q?*=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4=E5=AD=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/graphics/graphics.cpp | 68 ++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 21 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 a62802c..5d99915 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -12,6 +12,8 @@ namespace lua using jin::filesystem::Filesystem; using jin::filesystem::Buffer; +#include "../../resources/font.ttf.h" + static struct { Color curRenderColor; @@ -35,6 +37,11 @@ namespace lua luax_pushboolean(L, false); return 1; } + /* load default font */ + TTFData* ttfData = TTFData::createTTFData(font_ttf, sizeof(font_ttf)); + context.defaultFont = ttfData->createTTF(15); + context.curFont = context.defaultFont; + luax_pushboolean(L, true); return 1; } @@ -234,44 +241,43 @@ namespace lua return; Proxy* p = (Proxy*)luax_toudata(L, 1); Text* text = p->getObject(); - Proxy* p2 = (Proxy*)luax_toudata(L, 2); int x = luax_optnumber(L, 3, 0); int y = luax_optnumber(L, 4, 0); - int lineheight = luax_optnumber(L, 5, 12); int spacing = luax_optnumber(L, 6, 0); + Font* font = nullptr; + Proxy* p2 = (Proxy*)luax_toudata(L, 2); if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT)) { TextureFont* tf = p2->getObject(); - tf->print(*text, x, y, lineheight, spacing); + font = tf; } else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) { TTF* ttf = p2->getObject(); - ttf->print(*text, x, y, lineheight, spacing); + font = ttf; } + else + { + font = context.defaultFont; + } + int lineheight = luax_optnumber(L, 5, font->getFontSize()); + font->print(*text, x, y, lineheight, spacing); } - /* print(string[, font], x, y, lineheight, spacing) */ + /* print(string, x, y, lineheight, spacing) */ static int l_print(lua_State* L) { + Font* font = context.curFont; + if (font == nullptr) + return 0; unsigned length; const char* str = luax_checklstring(L, 1, &length); Text text(Encode::UTF8, str, length); - Proxy* p = (Proxy*)luax_toudata(L, 2); - int x = luax_optnumber(L, 3, 0); - int y = luax_optnumber(L, 4, 0); - int lineheight = luax_optnumber(L, 5, 12); - int spacing = luax_optnumber(L, 6, 0); - if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT)) - { - TextureFont* tf = p->getObject(); - tf->print(text, x, y, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) - { - TTF* ttf = p->getObject(); - ttf->print(text, x, y, lineheight, spacing); - } + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + int lineheight = luax_optnumber(L, 4, font->getFontSize()); + int spacing = luax_optnumber(L, 5, 0); + font->print(text, x, y, lineheight, spacing); return 0; } @@ -627,11 +633,30 @@ namespace lua return 1; } + /* setFont(font) */ static int l_setFont(lua_State* L) { + if (luax_istype(L, 1, JIN_GRAPHICS_TTF)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); + TTF* ttf = p->getObject(); + context.curFont = ttf; + } + else if (luax_istype(L, 1, JIN_GRAPHICS_TEXTUREFONT)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + TextureFont* tf = p->getObject(); + context.curFont = tf; + } return 0; } + static int l_unsetFont(lua_State* L) + { + context.curFont = context.defaultFont; + return 0; + } + static const luaL_Reg f[] = { /* window */ { "init", l_init }, @@ -670,7 +695,8 @@ namespace lua { "triangle", l_triangle }, { "polygon", l_polygon }, /* font */ - { "setFont", l_setFont }, + { "setFont", l_setFont }, + { "unsetFont", l_unsetFont }, { 0, 0 } }; -- cgit v1.1-26-g67d0