diff options
author | chai <chaifix@163.com> | 2018-10-15 12:49:54 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-15 12:49:54 +0800 |
commit | 68b60be0d1da84aa670d29b87b26ab3e3db51b69 (patch) | |
tree | 661b2b1aff66ce3068c2bd6e4abe66734e3d1706 /src/lua/modules/graphics/graphics.cpp | |
parent | 437ea76fc9b61240b2f8d1b0b800f3f7a73af766 (diff) |
*增加默认字体
Diffstat (limited to 'src/lua/modules/graphics/graphics.cpp')
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 68 |
1 files changed, 47 insertions, 21 deletions
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<Text>(); - 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<TextureFont>(); - tf->print(*text, x, y, lineheight, spacing); + font = tf; } else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) { TTF* ttf = p2->getObject<TTF>(); - 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<TextureFont>(); - tf->print(text, x, y, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) - { - TTF* ttf = p->getObject<TTF>(); - 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<TTF>(); + 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<TextureFont>(); + 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 } }; |