diff options
Diffstat (limited to 'src/lua/modules')
-rw-r--r-- | src/lua/modules/filesystem/filesystem.cpp | 14 | ||||
-rw-r--r-- | src/lua/modules/graphics/font.cpp | 42 | ||||
-rw-r--r-- | src/lua/modules/graphics/fontData.cpp | 45 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 89 | ||||
-rw-r--r-- | src/lua/modules/graphics/page.cpp | 65 | ||||
-rw-r--r-- | src/lua/modules/graphics/shader.cpp | 18 | ||||
-rw-r--r-- | src/lua/modules/types.h | 28 |
7 files changed, 187 insertions, 114 deletions
diff --git a/src/lua/modules/filesystem/filesystem.cpp b/src/lua/modules/filesystem/filesystem.cpp index c3dabad..e388ae1 100644 --- a/src/lua/modules/filesystem/filesystem.cpp +++ b/src/lua/modules/filesystem/filesystem.cpp @@ -120,13 +120,13 @@ namespace lua } static const luaL_Reg f[] = { - { "init", l_init }, - { "mount", l_mount }, - { "isDir", l_isDir }, - { "isFile", l_isFile }, - { "exist", l_exist }, - { "read", l_read }, - { 0, 0 } + { "init", l_init }, + { "mount", l_mount }, + { "isDirectory", l_isDir }, + { "isFile", l_isFile }, + { "exist", l_exist }, + { "read", l_read }, + { 0, 0 } }; int luaopen_filesystem(lua_State* L) diff --git a/src/lua/modules/graphics/font.cpp b/src/lua/modules/graphics/font.cpp index 042eb19..15aa04e 100644 --- a/src/lua/modules/graphics/font.cpp +++ b/src/lua/modules/graphics/font.cpp @@ -19,25 +19,37 @@ namespace lua return 0; } - static int l_box(lua_State* L) + static int l_typeset(lua_State* L) { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); - FontRef ref = proxy->getRef<Font>(); - const char* text = luax_checkstring(L, 2); - int fheight = luax_checknumber(L, 3); - int spacing = luax_checknumber(L, 4); - int lheight = luax_checknumber(L, 5); - int w, h; - ref->box(text, fheight, lheight, spacing, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); + const char* text = luax_checkstring(L, 2); + int lineheight = luax_checkinteger(L, 3); + int spacing = luax_checkinteger(L, 4); + Ref<Font>& refFont = p->getRef<Font>(); + Font* font = refFont.getObject(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); + Page* page = font->typeset(text, lineheight, spacing); + proxy->bind(new Ref<Page>(page, JIN_GRAPHICS_PAGE)); + return 1; + } + + static int l_print(lua_State* L) + { + Proxy* pFont = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); + Font* font = pFont->getObject<Font>(); + Proxy* p = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_PAGE); + Page* page = p->getObject<Page>(); + int x = luax_checkinteger(L, 3); + int y = luax_checkinteger(L, 4); + font->print(page, x, y); + return 0; } static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "box", l_box }, - { 0, 0 } + { "__gc", l_gc }, + { "typeset", l_typeset }, + { "print", l_print }, + { 0, 0 } }; int luaopen_Font(lua_State* L) diff --git a/src/lua/modules/graphics/fontData.cpp b/src/lua/modules/graphics/fontData.cpp new file mode 100644 index 0000000..85704d7 --- /dev/null +++ b/src/lua/modules/graphics/fontData.cpp @@ -0,0 +1,45 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::graphics; + + static int l_newFont(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONTDATA); + int fontsize = luax_checkinteger(L, 2); + Ref<FontData>& refFontData = p->getRef<FontData>(); + FontData* fontData = refFontData.getObject(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Font* font = Font::createFont(fontData, fontsize); + proxy->bind(new Ref<Font>(font, JIN_GRAPHICS_FONT)); + return 1; + } + + static int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONTDATA); + p->release(); + return 0; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "newFont", l_newFont }, + { 0, 0 } + }; + + int luaopen_FontData(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_FONTDATA, f); + return 0; + } + +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 679b820..5c67421 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -145,7 +145,7 @@ namespace lua static int l_newShader(lua_State* L) { const char* program = luax_checkstring(L, 1); - JSLProgram* jsl = JSLProgram::createJSLProgram(program); + Shader* jsl = Shader::createShader(program); if (jsl == nullptr) { error(L, "Failed to compile shader"); @@ -153,7 +153,7 @@ namespace lua return 1; } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); - proxy->bind(new Ref<JSLProgram>(jsl, JIN_GRAPHICS_SHADER)); + proxy->bind(new Ref<Shader>(jsl, JIN_GRAPHICS_SHADER)); return 1; } @@ -252,7 +252,6 @@ namespace lua context.curRenderColor.a = luax_checknumber(L, 4); else context.curClearColor.a = 255; - glColor4f(context.curRenderColor.r / 255.f, context.curRenderColor.g / 255.f, context.curRenderColor.b / 255.f, @@ -293,13 +292,13 @@ namespace lua { if (luax_gettop(L) == 0) { - JSLProgram::unuse(); + Shader::unuse(); return 0; } if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<JSLProgram>& jsl = proxy->getRef<JSLProgram>(); + Ref<Shader>& jsl = proxy->getRef<Shader>(); jsl->use(); } else @@ -308,13 +307,13 @@ namespace lua } return 0; } - +/* static int l_unuseShader(lua_State* L) { - JSLProgram::unuse(); + Shader::unuse(); return 0; } - +*/ static int l_setBlend(lua_State* L) { @@ -451,10 +450,10 @@ namespace lua return 0; } - static int l_newFont(lua_State* L) + static int l_newFontData(lua_State* L) { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); - Font* font = new Font(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONTDATA, sizeof(Proxy)); + FontData* fd = nullptr; { const char* path = luax_checkstring(L, 1); Filesystem* fs = Filesystem::get(); @@ -464,64 +463,14 @@ namespace lua luax_pushnil(L); return 1; } - Buffer b = {}; + Buffer b; fs->read(path, &b); - font->loadMemory((const unsigned char*)b.data); + fd = FontData::createFontData((unsigned char*)b.data, b.size); } - proxy->bind(new Ref<Font>(font, JIN_GRAPHICS_FONT)); + proxy->bind(new Ref<FontData>(fd, JIN_GRAPHICS_FONTDATA)); return 1; } - static int l_setFont(lua_State* L) - { - int n = luax_gettop(L); - if (n == 0) - { - if (context.defaultFont == 0) - { - #include "lua/resources/font.ttf.h" - context.defaultFont = new Font(); - context.defaultFont->loadMemory(font_ttf); - } - context.curFont = context.defaultFont; - return 0; - } - Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); - context.curFont = font; - return 0; - } - - static int l_write(lua_State* L) - { - if (context.curFont == 0) - return 0; - - const char* text = luax_checkstring(L, 1); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - - int fh = luax_optnumber(L, 4, 15); - int spacing = luax_optnumber(L, 5, 1); - int lh = luax_optnumber(L, 6, 18); - - context.curFont->render(text, x, y, fh, spacing, lh); - - return 0; - } - - static int l_box(lua_State* L) - { - const char* text = luax_checkstring(L, 1); - int fontheight = luax_checknumber(L, 2); - int spacing = luax_checknumber(L, 3); - int lineheight = luax_checknumber(L, 4); - int w, h; - context.curFont->box(text, fontheight, spacing, lineheight, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; - } - static const luaL_Reg f[] = { /* window */ { "init", l_init }, @@ -535,7 +484,7 @@ namespace lua { "newTexture", l_newTexture }, { "newShader", l_newShader }, { "newCanvas", l_newCanvas }, - { "newFont", l_newFont }, + { "newFontData", l_newFontData }, /* render */ { "setClearColor", l_setClearColor }, { "clear", l_clear }, @@ -543,16 +492,12 @@ namespace lua { "setColor", l_setColor }, { "getColor", l_getColor }, { "present", l_present }, - /* font */ - { "box", l_box }, - { "write", l_write }, - { "setFont", l_setFont }, /* canvas */ { "bindCanvas", l_bindCanvas }, { "unbindCanvas", l_unbindCanvas }, /* shader */ { "useShader", l_useShader }, - { "unuseShader", l_unuseShader }, + //{ "unuseShader", l_unuseShader }, /* shapes */ { "point", l_point }, { "line", l_line }, @@ -565,6 +510,8 @@ namespace lua extern int luaopen_Texture(lua_State* L); extern int luaopen_Font(lua_State* L); + extern int luaopen_FontData(lua_State* L); + extern int luaopen_Page(lua_State* L); extern int luaopen_Canvas(lua_State* L); extern int luaopen_JSL(lua_State* L); extern int luaopen_Bitmap(lua_State* L); @@ -575,7 +522,9 @@ namespace lua luaopen_Bitmap(L); luaopen_Texture(L); luaopen_Canvas(L); + luaopen_FontData(L); luaopen_Font(L); + luaopen_Page(L); luaopen_JSL(L); // load whole lib diff --git a/src/lua/modules/graphics/page.cpp b/src/lua/modules/graphics/page.cpp new file mode 100644 index 0000000..fd96448 --- /dev/null +++ b/src/lua/modules/graphics/page.cpp @@ -0,0 +1,65 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::graphics; + + typedef Ref<Font>& FontRef; + + Page* getPage(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); + return proxy->getObject<Page>(); + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); + proxy->release(); + return 0; + } + + static int l_getSize(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->width); + luax_pushinteger(L, page->height); + return 2; + } + + static int l_getWidth(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->width); + return 1; + } + + static int l_getHeight(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->height); + return 1; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "getSize", l_getSize }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { 0, 0 } + }; + + int luaopen_Page(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_PAGE, f); + return 0; + } + +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/shader.cpp b/src/lua/modules/graphics/shader.cpp index db78c4d..a9603bc 100644 --- a/src/lua/modules/graphics/shader.cpp +++ b/src/lua/modules/graphics/shader.cpp @@ -10,12 +10,12 @@ namespace lua using namespace jin::graphics; - typedef Ref<JSLProgram>& JSLRef; + typedef Ref<Shader>& ShaderRef; - static inline JSLRef checkJSLProgram(lua_State* L) + static inline ShaderRef checkShader(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - return proxy->getRef<JSLProgram>(); + return proxy->getRef<Shader>(); } /** @@ -23,7 +23,7 @@ namespace lua */ static int l_sendNumber (lua_State* L) { - JSLRef ref = checkJSLProgram(L); + ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); float number = luax_checknumber(L, 3); ref->sendFloat(variable, number); @@ -32,7 +32,7 @@ namespace lua static int l_sendTexture (lua_State* L) { - JSLRef ref = checkJSLProgram(L); + ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE); Ref<Texture>& tex = proxy->getRef<Texture>(); @@ -42,7 +42,7 @@ namespace lua static int l_sendCanvas (lua_State* L) { - JSLRef ref = checkJSLProgram(L); + ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS); Ref<Canvas>& canvas = proxy->getRef<Canvas>(); @@ -52,7 +52,7 @@ namespace lua static int l_sendVec2 (lua_State* L) { - JSLRef ref = checkJSLProgram(L); + ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); if (!luax_istable(L, 3)) { @@ -67,7 +67,7 @@ namespace lua static int l_sendVec3 (lua_State* L) { - JSLRef ref = checkJSLProgram(L); + ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); if (!luax_istable(L, 3)) { @@ -83,7 +83,7 @@ namespace lua static int l_sendVec4 (lua_State* L) { - JSLRef ref = checkJSLProgram(L); + ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); if (!luax_istable(L, 3)) { diff --git a/src/lua/modules/types.h b/src/lua/modules/types.h index d61ce38..59b8ed6 100644 --- a/src/lua/modules/types.h +++ b/src/lua/modules/types.h @@ -2,20 +2,22 @@ #define __JIN_MODULES_TYPES_H // graphics module -#define JIN_GRAPHICS_TEXTURE "jin.graphics.Texture" -#define JIN_GRAPHICS_SHADER "jin.graphics.Shader" -#define JIN_GRAPHICS_CANVAS "jin.graphics.Canvas" -#define JIN_GRAPHICS_FONT "jin.graphics.Font" -#define JIN_GRAPHICS_BITMAP "jin.graphics.Bitmap" +#define JIN_GRAPHICS_TEXTURE "jin.graphics.Texture" +#define JIN_GRAPHICS_SHADER "jin.graphics.Shader" +#define JIN_GRAPHICS_CANVAS "jin.graphics.Canvas" +#define JIN_GRAPHICS_FONTDATA "jin.graphics.FontData" +#define JIN_GRAPHICS_FONT "jin.graphics.Font" +#define JIN_GRAPHICS_PAGE "jin.graphics.Page" +#define JIN_GRAPHICS_BITMAP "jin.graphics.Bitmap" // audio module -#define JIN_AUDIO_SOURCE "jin.Audio.Source" - -// thread module -#define JIN_THREAD_THREAD "jin.thread.Thread" - -// network module -#define JIN_NETWORK_SOCKET "jin.net.Socket" -#define JIN_NETWORK_BUFFER "jin.net.Buffer" +#define JIN_AUDIO_SOURCE "jin.Audio.Source" + +// thread module +#define JIN_THREAD_THREAD "jin.thread.Thread" + +// network module +#define JIN_NETWORK_SOCKET "jin.net.Socket" +#define JIN_NETWORK_BUFFER "jin.net.Buffer" #endif
\ No newline at end of file |