diff options
author | chai <chaifix@163.com> | 2018-10-15 13:51:56 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-15 13:51:56 +0800 |
commit | bd3c1f268d959d351631b51d32d9912370144ddd (patch) | |
tree | a724836aa121fd4406a45915e6ece7cec7e3ba05 | |
parent | 252e074c41a73312be3e235b07be266a9aee59fb (diff) |
*修改打印文字接口
-rw-r--r-- | bin/Jin.exe | bin | 628736 -> 629760 bytes | |||
-rw-r--r-- | bin/jin.exe | bin | 628736 -> 629760 bytes | |||
-rw-r--r-- | bin/main.lua | 20 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 63 |
4 files changed, 59 insertions, 24 deletions
diff --git a/bin/Jin.exe b/bin/Jin.exe Binary files differindex c1fbb6f..9cccd2b 100644 --- a/bin/Jin.exe +++ b/bin/Jin.exe diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex c1fbb6f..9cccd2b 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/bin/main.lua b/bin/main.lua index f5f2a34..dd5745e 100644 --- a/bin/main.lua +++ b/bin/main.lua @@ -5,11 +5,10 @@ local text local page local tf function jin.core.onLoad() - local font_shader = jin.filesystem.read("font.shader") - shader = jin.graphics.newShader(font_shader) + shader = jin.graphics.newShaderf("font.shader") local bitmap = jin.graphics.newBitmap("font2.png") local tfdata = jin.graphics.newTTFData("font.ttf") - tf = tfdata:newTTF(15) + tf = tfdata:newTTF(13) page = tf:typeset("this is a test") end @@ -19,10 +18,23 @@ function jin.core.onEvent(e) end end +local t = 0 +local fps = 0 +local f = 0 +function jin.core.onUpdate(dt) + t = t + dt + f = f + 1 + if t > 1 then + fps = f + f = 0 + t = t - 1 + end +end + function jin.core.onDraw() jin.graphics.useShader(shader) jin.graphics.setFont(tf) - jin.graphics.print("你好error: this is a test", 0, 0, 16, 0) + jin.graphics.print(string.format("FPS: %d\nok this is another test", fps), 0, 0) jin.graphics.unsetFont() jin.graphics.unuseShader() end
\ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 645ae4a..700c8a5 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -164,6 +164,30 @@ namespace lua return 1; } + static int l_newShaderf(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + Filesystem* fs = Filesystem::get(); + if (!fs->exists(path)) + { + error(L, "No such shader file %s\n", path); + luax_pushnil(L); + return 1; + } + Buffer b; + fs->read(path, &b); + Shader* jsl = Shader::createShader((char*)b.data); + if (jsl == nullptr) + { + error(L, "Failed to compile shader"); + luax_pushnil(L); + return 1; + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); + proxy->bind(new Ref<Shader>(jsl, JIN_GRAPHICS_SHADER)); + return 1; + } + static int l_newCanvas(lua_State* L) { int w = luax_checknumber(L, 1); @@ -277,24 +301,6 @@ namespace lua font->print(page, x, y); } - /* print(string, x, y, lineheight, spacing) */ - /* need set font */ - 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); - 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; - } - static int l_draw(lua_State* L) { if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) @@ -305,8 +311,6 @@ namespace lua l_draw_text(L); else if (luax_istype(L, 1, JIN_GRAPHICS_PAGE)) l_draw_page(L); - else if (luax_isstring(L, 1)) - l_print(L); else { luax_typerror(L, 1, "texture or canvas"); @@ -355,6 +359,24 @@ namespace lua } } + /* print(string, x, y, lineheight, spacing) */ + /* need set font */ + 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); + 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; + } + static int l_setColor(lua_State* L) { if (luax_gettop(L) == 0) @@ -670,6 +692,7 @@ namespace lua { "newBitmap", l_newBitmap }, { "newTexture", l_newTexture }, { "newShader", l_newShader }, + { "newShaderf", l_newShaderf }, { "newCanvas", l_newCanvas }, { "newTTFData", l_newTTFData }, { "newText", l_newText }, |