From 831e814ce9bdb84e86c06c4a52008f6bdaaa00d6 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 16 Nov 2018 00:24:51 +0800 Subject: =?UTF-8?q?*=E5=90=88=E5=B9=B6master=E5=88=B0minimal=E5=88=86?= =?UTF-8?q?=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/graphics/bitmap.cpp | 113 --- src/lua/modules/graphics/canvas.cpp | 65 -- src/lua/modules/graphics/graphics.cpp | 819 ------------------ src/lua/modules/graphics/je_lua_bitmap.cpp | 114 +++ src/lua/modules/graphics/je_lua_bitmap.h | 16 + src/lua/modules/graphics/je_lua_canvas.cpp | 66 ++ src/lua/modules/graphics/je_lua_canvas.h | 16 + src/lua/modules/graphics/je_lua_graphics.cpp | 922 +++++++++++++++++++++ src/lua/modules/graphics/je_lua_graphics.h | 0 src/lua/modules/graphics/je_lua_page.cpp | 69 ++ src/lua/modules/graphics/je_lua_page.h | 22 + .../modules/graphics/je_lua_particle_system.cpp | 11 + src/lua/modules/graphics/je_lua_particle_system.h | 0 src/lua/modules/graphics/je_lua_shader.cpp | 137 +++ src/lua/modules/graphics/je_lua_shader.h | 16 + src/lua/modules/graphics/je_lua_sprite.cpp | 262 ++++++ src/lua/modules/graphics/je_lua_sprite.h | 24 + src/lua/modules/graphics/je_lua_spritesheet.cpp | 54 ++ src/lua/modules/graphics/je_lua_spritesheet.h | 21 + src/lua/modules/graphics/je_lua_text.cpp | 32 + src/lua/modules/graphics/je_lua_text.h | 16 + src/lua/modules/graphics/je_lua_texture.cpp | 66 ++ src/lua/modules/graphics/je_lua_texture.h | 16 + src/lua/modules/graphics/je_lua_texture_font.cpp | 66 ++ src/lua/modules/graphics/je_lua_texture_font.h | 16 + src/lua/modules/graphics/je_lua_ttf.cpp | 66 ++ src/lua/modules/graphics/je_lua_ttf.h | 16 + src/lua/modules/graphics/je_lua_ttf_data.cpp | 52 ++ src/lua/modules/graphics/je_lua_ttf_data.h | 21 + src/lua/modules/graphics/page.cpp | 73 -- src/lua/modules/graphics/shader.cpp | 135 --- src/lua/modules/graphics/text.cpp | 32 - src/lua/modules/graphics/texture.cpp | 65 -- src/lua/modules/graphics/texture_font.cpp | 67 -- src/lua/modules/graphics/ttf.cpp | 73 -- src/lua/modules/graphics/ttfData.cpp | 51 -- 36 files changed, 2117 insertions(+), 1493 deletions(-) delete mode 100644 src/lua/modules/graphics/bitmap.cpp delete mode 100644 src/lua/modules/graphics/canvas.cpp delete mode 100644 src/lua/modules/graphics/graphics.cpp create mode 100644 src/lua/modules/graphics/je_lua_bitmap.cpp create mode 100644 src/lua/modules/graphics/je_lua_bitmap.h create mode 100644 src/lua/modules/graphics/je_lua_canvas.cpp create mode 100644 src/lua/modules/graphics/je_lua_canvas.h create mode 100644 src/lua/modules/graphics/je_lua_graphics.cpp create mode 100644 src/lua/modules/graphics/je_lua_graphics.h create mode 100644 src/lua/modules/graphics/je_lua_page.cpp create mode 100644 src/lua/modules/graphics/je_lua_page.h create mode 100644 src/lua/modules/graphics/je_lua_particle_system.cpp create mode 100644 src/lua/modules/graphics/je_lua_particle_system.h create mode 100644 src/lua/modules/graphics/je_lua_shader.cpp create mode 100644 src/lua/modules/graphics/je_lua_shader.h create mode 100644 src/lua/modules/graphics/je_lua_sprite.cpp create mode 100644 src/lua/modules/graphics/je_lua_sprite.h create mode 100644 src/lua/modules/graphics/je_lua_spritesheet.cpp create mode 100644 src/lua/modules/graphics/je_lua_spritesheet.h create mode 100644 src/lua/modules/graphics/je_lua_text.cpp create mode 100644 src/lua/modules/graphics/je_lua_text.h create mode 100644 src/lua/modules/graphics/je_lua_texture.cpp create mode 100644 src/lua/modules/graphics/je_lua_texture.h create mode 100644 src/lua/modules/graphics/je_lua_texture_font.cpp create mode 100644 src/lua/modules/graphics/je_lua_texture_font.h create mode 100644 src/lua/modules/graphics/je_lua_ttf.cpp create mode 100644 src/lua/modules/graphics/je_lua_ttf.h create mode 100644 src/lua/modules/graphics/je_lua_ttf_data.cpp create mode 100644 src/lua/modules/graphics/je_lua_ttf_data.h delete mode 100644 src/lua/modules/graphics/page.cpp delete mode 100644 src/lua/modules/graphics/shader.cpp delete mode 100644 src/lua/modules/graphics/text.cpp delete mode 100644 src/lua/modules/graphics/texture.cpp delete mode 100644 src/lua/modules/graphics/texture_font.cpp delete mode 100644 src/lua/modules/graphics/ttf.cpp delete mode 100644 src/lua/modules/graphics/ttfData.cpp (limited to 'src/lua/modules/graphics') diff --git a/src/lua/modules/graphics/bitmap.cpp b/src/lua/modules/graphics/bitmap.cpp deleted file mode 100644 index 13517f9..0000000 --- a/src/lua/modules/graphics/bitmap.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - typedef Ref& BitmapRef; - - static inline BitmapRef checkBitmap(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - return proxy->getRef(); - } - - static int l_gc(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - ref.release(); - return 0; - } - - static int l_getWidth(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int w = ref->getWidth(); - luax_pushinteger(L, w); - return 1; - } - - static int l_getHeight(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int h = ref->getHeight(); - luax_pushinteger(L, h); - return 1; - } - - static int l_getSize(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int w = ref->getWidth(); - int h = ref->getHeight(); - luax_pushinteger(L, w); - luax_pushinteger(L, h); - return 2; - } - - static int l_getPixel(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int x = luax_checkinteger(L, 2); - int y = luax_checkinteger(L, 3); - Color col = ref->getPixel(x, y); - luax_pushinteger(L, col.r); - luax_pushinteger(L, col.g); - luax_pushinteger(L, col.b); - luax_pushinteger(L, col.a); - return 4; - } - - static int l_setPixel(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int x = luax_checkinteger(L, 2); - int y = luax_checkinteger(L, 3); - if (!luax_istable(L, 4)) - { - luax_typerror(L, 4, "table"); - return 1; - } - unsigned int r = luax_rawgetnumber(L, 4, 1); - unsigned int g = luax_rawgetnumber(L, 4, 2); - unsigned int b = luax_rawgetnumber(L, 4, 3); - unsigned int a = luax_rawgetnumber(L, 4, 4); - ref->setPixel(Color(r, g, b, a), x, y); - return 0; - } - - static int l_clone(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - Bitmap* bitmap = ref.getObject(); - Bitmap* b = Bitmap::clone(bitmap); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); - proxy->bind(new Ref(b, JIN_GRAPHICS_BITMAP)); - return 1; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "getSize", l_getSize }, - { "getPixel", l_getPixel }, - { "setPixel", l_setPixel }, - { "clone", l_clone }, - { 0, 0 } - }; - - int luaopen_Bitmap(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_BITMAP, f); - return 0; - } - - } // graphics -} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/canvas.cpp b/src/lua/modules/graphics/canvas.cpp deleted file mode 100644 index e49e209..0000000 --- a/src/lua/modules/graphics/canvas.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - typedef Ref& CanvasRef; - - static inline CanvasRef checkCanvas(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - return proxy->getRef(); - } - - static int l_getWidth(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } - - static int l_getHeight(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getHeight()); - return 1; - } - - static int l_getSize(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getWidth()); - luax_pushnumber(L, ref->getHeight()); - return 2; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "getSize", l_getSize }, - { 0, 0 } - }; - - int luaopen_Canvas(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_CANVAS, f); - return 0; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp deleted file mode 100644 index 8b2d7cd..0000000 --- a/src/lua/modules/graphics/graphics.cpp +++ /dev/null @@ -1,819 +0,0 @@ -#include -#include - -#include "libjin/jin.h" -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" - -using namespace std; -using namespace JinEngine; -using namespace JinEngine::Graphics; -using JinEngine::Filesystem::AssetDatabase; -using JinEngine::Filesystem::Buffer; - -namespace JinEngine -{ - namespace Lua - { - -#include "../../resources/font.ttf.h" - - static struct - { - Color curRenderColor; - Color curClearColor; - Font* curFont = nullptr; - Font* defaultFont = nullptr; - } context; - - static int l_init(lua_State* L) - { - Window* wnd = Window::get(); - Window::Setting setting; - setting.width = luax_getfieldinteger(L, 1, "width"); - setting.height = luax_getfieldinteger(L, 1, "height"); - setting.title = luax_getfieldstring(L, 1, "title"); - setting.vsync = luax_getfieldbool(L, 1, "vsync"); - setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen"); - setting.resizable = luax_getfieldbool(L, 1, "resizable"); - if (!wnd->init(&setting)) - { - luax_pushboolean(L, false); - return 1; - } - { - /* load default font */ - Bitmap* bitmap = Bitmap::createBitmap(default_font_bitmap, sizeof(default_font_bitmap)); - const Color* pixels = bitmap->getPixels(); - ofstream f = ofstream(); - f.open("font.pixels", ios_base::app); - for (int y = 0; y < bitmap->getHeight(); ++y) - { - for (int x = 0; x < bitmap->getWidth(); ++x) - { - Color c = pixels[x + y * bitmap->getWidth()]; - f << (int)c.r << ","; - f << (int)c.g << ","; - f << (int)c.b << ","; - f << (int)c.a << ","; - } - } - - TextureFont* tf = TextureFont::createTextureFont(bitmap, Text(Encode::UTF8, default_charset), default_font_split, bitmap->getHeight()); - context.defaultFont = tf; - delete bitmap; - } - context.curFont = context.defaultFont; - - luax_pushboolean(L, true); - return 1; - } - - static int l_setTitle(lua_State* L) - { - Window* wnd = Window::get(); - const char* title = luax_checkstring(L, 1); - wnd->setTitle(title); - return 0; - } - - static int l_destroy(lua_State* L) - { - Window* wnd = Window::get(); - wnd->quit(); - return 0; - } - - static int l_getSize(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getW()); - luax_pushnumber(L, wnd->getH()); - return 2; - } - - static int l_getWidth(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getW()); - return 1; - } - - static int l_getHeight(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getH()); - return 1; - } - - static int l_newBitmap(lua_State* L) - { - Bitmap* bitmap = nullptr; - if (luax_gettop(L) == 2) - { - int w = luax_checkinteger(L, 1); - int h = luax_checkinteger(L, 2); - bitmap = Bitmap::createBitmap(w, h); - } - else if (luax_gettop(L) == 3) - { - int w = luax_checkinteger(L, 1); - int h = luax_checkinteger(L, 2); - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - unsigned int r = luax_rawgetnumber(L, 3, 1); - unsigned int g = luax_rawgetnumber(L, 3, 2); - unsigned int b = luax_rawgetnumber(L, 3, 3); - unsigned int a = luax_rawgetnumber(L, 3, 4); - bitmap = Bitmap::createBitmap(w, h, Color(r, g, b, a)); - } - else - { - const char* f = luax_checkstring(L, 1); - AssetDatabase* fs = AssetDatabase::get(); - if (!fs->exists(f)) - { - error(L, "No such image file %s", f); - goto fail; - } - Buffer b; - if (!fs->read(f, b)) - { - error(L, "Failed to read image %s", f); - goto fail; - } - bitmap = Bitmap::createBitmap(&b, b.size()); - if (bitmap == nullptr) - { - error(L, "Failed to decode image file %s", f); - goto fail; - } - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); - proxy->bind(new Ref(bitmap, JIN_GRAPHICS_BITMAP)); - return 1; - fail: - luax_pushnil(L); - return 1; - } - - /* jin.graphics.newTexture(bitmap) */ - static int l_newTexture(lua_State* L) - { - Texture* texture = nullptr; - if (luax_istype(L, 1, JIN_GRAPHICS_BITMAP)) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - Ref& refBitmap = p->getRef(); - Bitmap* bitmap = refBitmap.getObject(); - texture = Texture::createTexture(bitmap); - } - else if (luax_isstring(L, 1)) - { - const char* path = luax_checkstring(L, 1); - texture = Texture::createTexture(path); - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); - proxy->bind(new Ref(texture, JIN_GRAPHICS_TEXTURE)); - return 1; - } - - static int l_newShader(lua_State* L) - { - const char* program = luax_checkstring(L, 1); - Shader* jsl = Shader::createShader(program); - 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(jsl, JIN_GRAPHICS_SHADER)); - return 1; - } - - static int l_newShaderf(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - AssetDatabase* fs = AssetDatabase::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); - 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(jsl, JIN_GRAPHICS_SHADER)); - return 1; - } - - static int l_newCanvas(lua_State* L) - { - int w = luax_checknumber(L, 1); - int h = luax_checknumber(L, 2); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy)); - Canvas* cvs = Canvas::createCanvas(w, h); - proxy->bind(new Ref(cvs, JIN_GRAPHICS_CANVAS)); - return 1; - } - - static int l_clear(lua_State* L) - { - glClear(GL_COLOR_BUFFER_BIT); - return 0; - } - - static int l_setClearColor(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glClearColor(0, 0, 0, 1); - return 0; - } - - context.curClearColor.r = luax_checknumber(L, 1); - context.curClearColor.g = luax_checknumber(L, 2); - context.curClearColor.b = luax_checknumber(L, 3); - context.curClearColor.a = luax_checknumber(L, 4); - - gl.setClearColor(context.curClearColor.r, - context.curClearColor.g, - context.curClearColor.b, - context.curClearColor.a); - return 0; - } - - static int l_present(lua_State* L) - { - Window::get()->swapBuffers(); - return 0; - } - - static void l_draw_texture(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) - return; - int x = luax_optnumber(L, 2, 0); - int y = luax_optnumber(L, 3, 0); - float sx = luax_optnumber(L, 4, 1); - float sy = luax_optnumber(L, 5, 1); - float r = luax_optnumber(L, 6, 0); - float ox = luax_optnumber(L, 7, 0); - float oy = luax_optnumber(L, 8, 0); - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& tex = proxy->getRef(); - tex->draw(x, y, sx, sy, r, ox, oy); - } - - static void l_draw_canvas(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) - return; - int x = luax_optnumber(L, 2, 0); - int y = luax_optnumber(L, 3, 0); - float sx = luax_optnumber(L, 4, 1); - float sy = luax_optnumber(L, 5, 1); - float r = luax_optnumber(L, 6, 0); - float ox = luax_optnumber(L, 7, 0); - float oy = luax_optnumber(L, 8, 0); - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& p = proxy->getRef(); - p->draw(x, y, sx, sy, r, ox, oy); - } - - /* jin.graphics.draw(text, font, x, y) */ - static void l_draw_text(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_TEXT)) - return; - Proxy* p = (Proxy*)luax_toudata(L, 1); - Text* text = p->getObject(); - int x = luax_optnumber(L, 3, 0); - int y = luax_optnumber(L, 4, 0); - 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(); - font = tf; - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) - { - TTF* ttf = p2->getObject(); - font = ttf; - } - else - { - font = context.defaultFont; - } - int lineheight = luax_optnumber(L, 5, font->getFontSize()); - font->print(*text, x, y, lineheight, spacing); - } - - /* jin.graphics.draw(page, x, y) */ - static void l_draw_page(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_PAGE)) - return; - int x = luax_optnumber(L, 2, 0); - int y = luax_optnumber(L, 3, 0); - Proxy* p = (Proxy*)luax_toudata(L, 1); - Page* page = p->getObject(); - Font* font = page->font; - font->print(page, x, y); - } - - static int l_draw(lua_State* L) - { - if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) - l_draw_texture(L); - else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) - l_draw_canvas(L); - else if (luax_istype(L, 1, JIN_GRAPHICS_TEXT)) - l_draw_text(L); - else if (luax_istype(L, 1, JIN_GRAPHICS_PAGE)) - l_draw_page(L); - else - { - luax_typerror(L, 1, "texture or canvas"); - return 1; - } - return 0; - } - - // draw(tex, quad, x, y, sx, sy, r, ax, ay) - static int l_drawq(lua_State* L) - { - if (!luax_istable(L, 2)) - { - luax_typerror(L, 2, "table"); - return 1; - } - Math::Quad q; - q.x = luax_rawgetnumber(L, 2, 1); - q.y = luax_rawgetnumber(L, 2, 2); - q.w = luax_rawgetnumber(L, 2, 3); - q.h = luax_rawgetnumber(L, 2, 4); - luax_pop(L, 4); - int x = luax_optnumber(L, 3, 0); - int y = luax_optnumber(L, 4, 0); - float sx = luax_optnumber(L, 5, 1); - float sy = luax_optnumber(L, 6, 1); - float r = luax_optnumber(L, 7, 0); - float ox = luax_optnumber(L, 8, 0); - float oy = luax_optnumber(L, 9, 0); - - if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& tex = proxy->getRef(); - tex->draw(q, x, y, sx, sy, r, ox, oy); - } - else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& p = proxy->getRef(); - p->draw(q, x, y, sx, sy, r, ox, oy); - } - else - { - luax_typerror(L, 1, "texture or canvas"); - } - } - - /* 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) - { - glColor4f(1, 1, 1, 1); - return 0; - } - - context.curRenderColor.r = luax_checknumber(L, 1); - context.curRenderColor.g = luax_checknumber(L, 2); - context.curRenderColor.b = luax_checknumber(L, 3); - if (luax_gettop(L) == 4) - context.curRenderColor.a = luax_checknumber(L, 4); - else - context.curRenderColor.a = 255; - glColor4f(context.curRenderColor.r / 255.f, - context.curRenderColor.g / 255.f, - context.curRenderColor.b / 255.f, - context.curRenderColor.a / 255.f); - return 0; - } - - static int l_getColor(lua_State * L) - { - luax_pushnumber(L, context.curRenderColor.r); - luax_pushnumber(L, context.curRenderColor.g); - luax_pushnumber(L, context.curRenderColor.b); - luax_pushnumber(L, context.curRenderColor.a); - return 4; - } - - static int l_bindCanvas(lua_State* L) - { - if (luax_gettop(L) == 0) - { - // bind to default canvas - Canvas::unbind(); - return 0; - } - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Ref& ref = proxy->getRef(); - Canvas::bind(ref.getObject()); - return 0; - } - - static int l_unbindCanvas(lua_State* L) - { - Canvas::unbind(); - return 0; - } - - static int l_useShader(lua_State* L) - { - if (luax_gettop(L) == 0) - { - Shader::unuse(); - return 0; - } - if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& jsl = proxy->getRef(); - jsl->use(); - } - else - { - luax_typerror(L, 1, "JSL shader"); - } - return 0; - } - - static int l_setBlend(lua_State* L) - { - - return 0; - } - - static RenderMode strtomode(const char* str) - { - std::string s = std::string(str); - if (s == "fill") return RenderMode::FILL; - else if (s == "line") return RenderMode::LINE; - else return RenderMode::NONE; - } - - static int l_point(lua_State* L) - { - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - JinEngine::Graphics::point(x, y); - - return 0; - } - - static int l_line(lua_State* L) - { - int x1 = luax_checknumber(L, 1); - int y1 = luax_checknumber(L, 2); - int x2 = luax_checknumber(L, 3); - int y2 = luax_checknumber(L, 4); - JinEngine::Graphics::line(x1, y1, x2, y2); - - return 0; - } - - static int l_rect(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - int w = luax_checknumber(L, 4); - int h = luax_checknumber(L, 5); - rect(mode, x, y, w, h); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_circle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - float r = luax_checknumber(L, 4); - circle(mode, x, y, r); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_triangle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - - int x2 = luax_checknumber(L, 3); - int y2 = luax_checknumber(L, 4); - - int x3 = luax_checknumber(L, 5); - int y3 = luax_checknumber(L, 6); - - triangle(mode, x, y, x2, y2, x3, y3); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_polygon(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - int n = luax_checknumber(L, 2); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) - { - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - int tn = luax_tableidxlen(L, 3); - if (tn != n * 2) - { - static char* emsg = \ - "number of polygon vertices doesn't match " \ - "provided n, expect %d numbers but get %d"; - luax_error(L, emsg, n * 2, tn); - return 1; - } - float* p = (float*)alloca(2 * n * sizeof(float)); - for (int i = 1; i <= 2 * n; ++i) - p[i - 1] = luax_rawgetnumber(L, 3, i); - polygon(mode, p, n); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_newTTFData(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTFDATA, sizeof(Proxy)); - TTFData* fd = nullptr; - { - const char* path = luax_checkstring(L, 1); - AssetDatabase* fs = AssetDatabase::get(); - if (!fs->exists(path)) - { - error(L, "No such font %s\n", path); - luax_pushnil(L); - return 1; - } - Buffer b; - fs->read(path, b); - fd = TTFData::createTTFData(&b, b.size()); - } - proxy->bind(new Ref(fd, JIN_GRAPHICS_TTFDATA)); - return 1; - } - - /* newText(str[, encode]) */ - static int l_newText(lua_State* L) - { - Encode encode = Encode::UTF8; - if (luax_gettop(L) == 2) - { - const char* e = luax_checkstring(L, 2); - if (strcmp(e, "UTF8") == 0) encode = Encode::UTF8; - //else if (strcmp(e, "UTF16") == 0) encode = Encode::UTF16; - else if (strcmp(e, "ASCII") == 0) encode = Encode::ASCII; - else - { - luax_error(L, "wrong text encode %s", e); - return 0; - } - } - unsigned length; - const char* data = luax_checklstring(L, 1, &length); - Text* text = new Text(encode, data, length); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXT, sizeof(Proxy)); - proxy->bind(new Ref(text, JIN_GRAPHICS_TEXT)); - return 1; - } - - /* newTextureFont(bitmap, text, color | cellw, cellh) */ - static int l_newTextureFont(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - Bitmap* bitmap = p->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)) - { - unsigned int r = luax_rawgetnumber(L, 3, 1); - unsigned int g = luax_rawgetnumber(L, 3, 2); - unsigned int b = luax_rawgetnumber(L, 3, 3); - unsigned int a = luax_rawgetnumber(L, 3, 4); - textureFont = TextureFont::createTextureFont(bitmap, *text, Color(r, g, b, a), cellh); - } - else if (luax_isnumber(L, 3)) - { - 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; - } - - /* 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 }, - { "setTitle", l_setTitle }, - { "getSize", l_getSize }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "destroy", l_destroy }, - /* creators */ - { "newBitmap", l_newBitmap }, - { "newTexture", l_newTexture }, - { "newShader", l_newShader }, - { "newShaderf", l_newShaderf }, - { "newCanvas", l_newCanvas }, - { "newTTFData", l_newTTFData }, - { "newText", l_newText }, - { "newTextureFont", l_newTextureFont }, - /* render */ - { "setClearColor", l_setClearColor }, - { "clear", l_clear }, - { "draw", l_draw }, - { "print", l_print }, - { "drawq", l_drawq }, - { "setColor", l_setColor }, - { "getColor", l_getColor }, - { "present", l_present }, - /* canvas */ - { "bindCanvas", l_bindCanvas }, - { "unbindCanvas", l_unbindCanvas }, - /* shader */ - { "useShader", l_useShader }, - /* shapes */ - { "point", l_point }, - { "line", l_line }, - { "rect", l_rect }, - { "circle", l_circle }, - { "triangle", l_triangle }, - { "polygon", l_polygon }, - /* font */ - { "setFont", l_setFont }, - { "unsetFont", l_unsetFont }, - { 0, 0 } - }; - - extern int luaopen_Texture(lua_State* L); - extern int luaopen_Text(lua_State* L); - extern int luaopen_TTF(lua_State* L); - extern int luaopen_TextureFont(lua_State* L); - extern int luaopen_TTFData(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); - - int luaopen_graphics(lua_State* L) - { - // register types - luaopen_Bitmap(L); - luaopen_Texture(L); - luaopen_Canvas(L); - luaopen_TTFData(L); - luaopen_TTF(L); - luaopen_Text(L); - luaopen_TextureFont(L); - luaopen_Page(L); - luaopen_JSL(L); - - // load whole lib - luax_newlib(L, f); - - return 1; - } - - }// lua -}// jin \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_bitmap.cpp b/src/lua/modules/graphics/je_lua_bitmap.cpp new file mode 100644 index 0000000..6b7655f --- /dev/null +++ b/src/lua/modules/graphics/je_lua_bitmap.cpp @@ -0,0 +1,114 @@ +#include "lua/common/je_lua_common.h" +#include "lua/modules/luax.h" + +#include "libjin/jin.h" +#include "je_lua_bitmap.h" + +using namespace JinEngine::Graphics; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Bitmap = "Bitmap"; + + typedef Shared& SharedBitmap; + + LUA_IMPLEMENT inline SharedBitmap checkBitmap(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Bitmap); + return proxy->getShared(); + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + SharedBitmap shared = checkBitmap(L); + shared.release(); + return 0; + } + + LUA_IMPLEMENT int l_getWidth(lua_State* L) + { + SharedBitmap shared = checkBitmap(L); + int w = shared->getWidth(); + luax_pushinteger(L, w); + return 1; + } + + LUA_IMPLEMENT int l_getHeight(lua_State* L) + { + SharedBitmap shared = checkBitmap(L); + int h = shared->getHeight(); + luax_pushinteger(L, h); + return 1; + } + + LUA_IMPLEMENT int l_getSize(lua_State* L) + { + SharedBitmap shared = checkBitmap(L); + int w = shared->getWidth(); + int h = shared->getHeight(); + luax_pushinteger(L, w); + luax_pushinteger(L, h); + return 2; + } + + LUA_IMPLEMENT int l_getPixel(lua_State* L) + { + SharedBitmap shared = checkBitmap(L); + int x = luax_checkinteger(L, 2); + int y = luax_checkinteger(L, 3); + Color col = shared->getPixel(x, y); + luax_pushinteger(L, col.r); + luax_pushinteger(L, col.g); + luax_pushinteger(L, col.b); + luax_pushinteger(L, col.a); + return 4; + } + + LUA_IMPLEMENT int l_setPixel(lua_State* L) + { + SharedBitmap shared = checkBitmap(L); + int x = luax_checkinteger(L, 2); + int y = luax_checkinteger(L, 3); + if (!luax_istable(L, 4)) + { + luax_typerror(L, 4, "table"); + return 1; + } + unsigned int r = luax_rawgetnumber(L, 4, 1); + unsigned int g = luax_rawgetnumber(L, 4, 2); + unsigned int b = luax_rawgetnumber(L, 4, 3); + unsigned int a = luax_rawgetnumber(L, 4, 4); + shared->setPixel(Color(r, g, b, a), x, y); + return 0; + } + + LUA_IMPLEMENT int l_clone(lua_State* L) + { + SharedBitmap shared = checkBitmap(L); + Bitmap* bitmap = shared.getObject(); + Bitmap* b = Bitmap::clone(bitmap); + Proxy* proxy = luax_newinstance(L, Jin_Lua_Bitmap); + proxy->bind(new Shared(b, Jin_Lua_Bitmap)); + return 1; + } + + LUA_EXPORT void luaopen_Bitmap(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { "getPixel", l_getPixel }, + { "setPixel", l_setPixel }, + { "clone", l_clone }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Bitmap, f); + } + + } // namespace Graphics +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_bitmap.h b/src/lua/modules/graphics/je_lua_bitmap.h new file mode 100644 index 0000000..b463d83 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_bitmap.h @@ -0,0 +1,16 @@ +#ifndef __JE_LUA_BITMAP_H__ +#define __JE_LUA_BITMAP_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_Bitmap; + + void luaopen_Bitmap(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_canvas.cpp b/src/lua/modules/graphics/je_lua_canvas.cpp new file mode 100644 index 0000000..f7bd650 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_canvas.cpp @@ -0,0 +1,66 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" +#include "je_lua_canvas.h" + +using namespace JinEngine::Graphics; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Canvas = "Canvas"; + + typedef Shared& SharedCanvas; + + LUA_IMPLEMENT inline SharedCanvas checkCanvas(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Canvas); + return proxy->getShared(); + } + + LUA_IMPLEMENT int l_getWidth(lua_State* L) + { + SharedCanvas shared = checkCanvas(L); + luax_pushnumber(L, shared->getWidth()); + return 1; + } + + LUA_IMPLEMENT int l_getHeight(lua_State* L) + { + SharedCanvas shared = checkCanvas(L); + luax_pushnumber(L, shared->getHeight()); + return 1; + } + + LUA_IMPLEMENT int l_getSize(lua_State* L) + { + SharedCanvas shared = checkCanvas(L); + luax_pushnumber(L, shared->getWidth()); + luax_pushnumber(L, shared->getHeight()); + return 2; + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Canvas); + proxy->release(); + return 0; + } + + LUA_EXPORT void luaopen_Canvas(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Canvas, f); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_canvas.h b/src/lua/modules/graphics/je_lua_canvas.h new file mode 100644 index 0000000..d1fa885 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_canvas.h @@ -0,0 +1,16 @@ +#ifndef __JE_LUA_CANVAS_H__ +#define __JE_LUA_CANVAS_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_Canvas; + + void luaopen_Canvas(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp new file mode 100644 index 0000000..ce569d0 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -0,0 +1,922 @@ +#include +#include + +#include "libjin/jin.h" +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" + +#include "je_lua_canvas.h" +#include "je_lua_sprite.h" +#include "je_lua_spritesheet.h" +#include "je_lua_bitmap.h" +#include "je_lua_ttf.h" +#include "je_lua_ttf_data.h" +#include "je_lua_texture.h" +#include "je_lua_shader.h" +#include "je_lua_text.h" +#include "je_lua_texture_font.h" +#include "je_lua_page.h" + +using namespace std; +using namespace JinEngine; +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Fonts; +using namespace JinEngine::Graphics::Shaders; +using namespace JinEngine::Filesystem; + +namespace JinEngine +{ + namespace Lua + { + +#include "../../resources/font.ttf.h" + + static struct + { + Color curRenderColor; + Color curClearColor; + Font* curFont = nullptr; + Font* defaultFont = nullptr; + bool initialized = false; + } context; + + LUA_IMPLEMENT int l_init(lua_State* L) + { + if (context.initialized) + { + luax_pushboolean(L, true); + return 1; + } + + Window* wnd = Window::get(); + Window::Setting setting; + setting.width = luax_getfieldinteger(L, 1, "width"); + setting.height = luax_getfieldinteger(L, 1, "height"); + setting.title = luax_getfieldstring(L, 1, "title"); + setting.icon = luax_getfieldstring(L, 1, "icon"); + setting.vsync = luax_getfieldbool(L, 1, "vsync"); + setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen"); + setting.resizable = luax_getfieldbool(L, 1, "resizable"); + context.initialized = wnd->init(&setting); + if (!context.initialized) + { + luax_pushboolean(L, context.initialized); + return 1; + } + + /* load default font */ + Bitmap* bitmap = Bitmap::createBitmap(default_font_bitmap, sizeof(default_font_bitmap)); + TextureFont* tf = TextureFont::createTextureFont(bitmap, Text(Encode::UTF8, default_charset), default_font_split, bitmap->getHeight()); + delete bitmap; + context.defaultFont = tf; + context.curFont = tf; + + luax_pushboolean(L, context.initialized); + return 1; + } + + LUA_IMPLEMENT int l_setTitle(lua_State* L) + { + Window* wnd = Window::get(); + const char* title = luax_checkstring(L, 1); + wnd->setTitle(title); + return 0; + } + + LUA_IMPLEMENT int l_destroy(lua_State* L) + { + Window* wnd = Window::get(); + wnd->quit(); + return 0; + } + + LUA_IMPLEMENT int l_showWindow(lua_State* L) + { + Window* wnd = Window::get(); + wnd->show(); + return 0; + } + + LUA_IMPLEMENT int l_hideWindow(lua_State* L) + { + Window* wnd = Window::get(); + wnd->hide(); + return 0; + } + + LUA_IMPLEMENT int l_getSize(lua_State* L) + { + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + luax_pushnumber(L, wnd->getH()); + return 2; + } + + LUA_IMPLEMENT int l_getWidth(lua_State* L) + { + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + return 1; + } + + LUA_IMPLEMENT int l_getHeight(lua_State* L) + { + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getH()); + return 1; + } + + LUA_IMPLEMENT int l_newBitmap(lua_State* L) + { + Bitmap* bitmap = nullptr; + if (luax_gettop(L) == 2) + { + int w = luax_checkinteger(L, 1); + int h = luax_checkinteger(L, 2); + bitmap = Bitmap::createBitmap(w, h); + } + else if (luax_gettop(L) == 3) + { + int w = luax_checkinteger(L, 1); + int h = luax_checkinteger(L, 2); + if (luax_istable(L, 3)) + { + unsigned int r = luax_rawgetnumber(L, 3, 1); + unsigned int g = luax_rawgetnumber(L, 3, 2); + unsigned int b = luax_rawgetnumber(L, 3, 3); + unsigned int a = luax_rawgetnumber(L, 3, 4); + bitmap = Bitmap::createBitmap(w, h, Color(r, g, b, a)); + } + else if (luax_isfunction(L, 3)) + { + std::function drawer = [=](int w, int h, int x, int y)->Color{ + luax_pushvalue(L, 3); + luax_pushnumber(L, w); + luax_pushnumber(L, h); + luax_pushnumber(L, x); + luax_pushnumber(L, y); + // Call drawer function. + luax_call(L, 4, 1); + // Get result color. + if (!luax_istable(L, -1)) + luax_error(L, "Return value of bitmap drawer is wrong, should be a color table."); + Color c; + c.r = luax_rawgetnumberthenpop(L, -1, 1); + c.g = luax_rawgetnumberthenpop(L, -1, 2); + c.b = luax_rawgetnumberthenpop(L, -1, 3); + c.a = luax_rawgetnumberthenpop(L, -1, 4); + // Pop return value. + luax_pop(L, 1); + return c; + }; + bitmap = Bitmap::createBitmap(w, h, drawer); + } + else + { + luax_typerror(L, 3, "color table or color setter"); + return 1; + } + } + else + { + const char* f = luax_checkstring(L, 1); + AssetDatabase* fs = AssetDatabase::get(); + Buffer b; + try + { + if (!fs->exists(f)) + throw Exception("No such image file %s.", f); + fs->read(f, b); + } + catch (Exception& e) + { + error(L, "Failed to read image %s", f); + luax_pushnil(L); + return 1; + } + bitmap = Bitmap::createBitmap(&b, b.size()); + if (bitmap == nullptr) + { + error(L, "Failed to decode image file %s", f); + luax_pushnil(L); + return 1; + } + } + Proxy* proxy = luax_newinstance(L, Jin_Lua_Bitmap); + proxy->bind(new Shared(bitmap, Jin_Lua_Bitmap)); + return 1; + } + + /* jin.graphics.newTexture(bitmap) */ + LUA_IMPLEMENT int l_newTexture(lua_State* L) + { + Texture* texture = nullptr; + if (luax_istype(L, 1, Jin_Lua_Bitmap)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_Bitmap); + Shared& refBitmap = p->getShared(); + Bitmap* bitmap = refBitmap.getObject(); + texture = Texture::createTexture(bitmap); + } + else if (luax_isstring(L, 1)) + { + const char* path = luax_checkstring(L, 1); + texture = Texture::createTexture(path); + } + Proxy* proxy = luax_newinstance(L, Jin_Lua_Texture); + proxy->bind(new Shared(texture, Jin_Lua_Texture)); + return 1; + } + + LUA_IMPLEMENT int l_newShader(lua_State* L) + { + const char* program = luax_checkstring(L, 1); + Shader* jsl = Shader::createShader(program); + if (jsl == nullptr) + { + error(L, "Failed to compile shader"); + luax_pushnil(L); + return 1; + } + Proxy* proxy = luax_newinstance(L, Jin_Lua_Shader); + proxy->bind(new Shared(jsl, Jin_Lua_Shader)); + return 1; + } + + LUA_IMPLEMENT int l_newShaderf(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + AssetDatabase* fs = AssetDatabase::get(); + if (!fs->exists(path)) + { + error(L, "No such shader file \"%s\"", path); + luax_pushnil(L); + return 1; + } + Buffer b; + fs->read(path, b); + Shader* jsl = Shader::createShader((char*)&b); + if (jsl == nullptr) + { + error(L, "Failed to compile shader"); + luax_pushnil(L); + return 1; + } + Proxy* proxy = luax_newinstance(L, Jin_Lua_Shader); + proxy->bind(new Shared(jsl, Jin_Lua_Shader)); + return 1; + } + + LUA_IMPLEMENT int l_newCanvas(lua_State* L) + { + int w = luax_checknumber(L, 1); + int h = luax_checknumber(L, 2); + Proxy* proxy = luax_newinstance(L, Jin_Lua_Canvas); + Canvas* cvs = Canvas::createCanvas(w, h); + proxy->bind(new Shared(cvs, Jin_Lua_Canvas)); + return 1; + } + + LUA_IMPLEMENT int l_clear(lua_State* L) + { + glClear(GL_COLOR_BUFFER_BIT); + return 0; + } + + LUA_IMPLEMENT int l_setClearColor(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glClearColor(0, 0, 0, 1); + return 0; + } + + context.curClearColor.r = luax_checknumber(L, 1); + context.curClearColor.g = luax_checknumber(L, 2); + context.curClearColor.b = luax_checknumber(L, 3); + context.curClearColor.a = luax_checknumber(L, 4); + + gl.setClearColor(context.curClearColor.r, + context.curClearColor.g, + context.curClearColor.b, + context.curClearColor.a); + return 0; + } + + LUA_IMPLEMENT int l_present(lua_State* L) + { + Window::get()->swapBuffers(); + return 0; + } + + LUA_IMPLEMENT void l_draw_texture(lua_State* L) + { + if (!luax_istype(L, 1, Jin_Lua_Texture)) + return; + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + float sx = luax_optnumber(L, 4, 1); + float sy = luax_optnumber(L, 5, 1); + float r = luax_optnumber(L, 6, 0); + float ox = luax_optnumber(L, 7, 0); + float oy = luax_optnumber(L, 8, 0); + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Shared& tex = proxy->getShared(); + tex->render(x, y, sx, sy, r, ox, oy); + } + + LUA_IMPLEMENT void l_draw_canvas(lua_State* L) + { + if (!luax_istype(L, 1, Jin_Lua_Canvas)) + return; + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + float sx = luax_optnumber(L, 4, 1); + float sy = luax_optnumber(L, 5, 1); + float r = luax_optnumber(L, 6, 0); + float ox = luax_optnumber(L, 7, 0); + float oy = luax_optnumber(L, 8, 0); + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Shared& p = proxy->getShared(); + p->render(x, y, sx, sy, r, ox, oy); + } + + /* jin.graphics.draw(text, font, x, y) */ + LUA_IMPLEMENT void l_draw_text(lua_State* L) + { + if (!luax_istype(L, 1, Jin_Lua_Text)) + return; + Proxy* p = (Proxy*)luax_toudata(L, 1); + Text* text = p->getObject(); + int x = luax_optnumber(L, 3, 0); + int y = luax_optnumber(L, 4, 0); + int spacing = luax_optnumber(L, 6, 0); + Font* font = nullptr; + Proxy* p2 = (Proxy*)luax_toudata(L, 2); + if (luax_istype(L, 2, Jin_Lua_TextureFont)) + { + TextureFont* tf = p2->getObject(); + font = tf; + } + else if (luax_istype(L, 2, Jin_Lua_TTF)) + { + TTF* ttf = p2->getObject(); + font = ttf; + } + else + { + font = context.defaultFont; + } + int lineheight = luax_optnumber(L, 5, font->getFontSize()); + font->render(*text, x, y, lineheight, spacing); + } + + /* jin.graphics.draw(page, x, y) */ + LUA_IMPLEMENT void l_draw_page(lua_State* L) + { + if (!luax_istype(L, 1, Jin_Lua_Page)) + return; + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + Proxy* p = (Proxy*)luax_toudata(L, 1); + Page* page = p->getObject(); + Font* font = page->font; + font->render(page, x, y); + } + + LUA_IMPLEMENT int l_draw(lua_State* L) + { + if (luax_istype(L, 1, Jin_Lua_Texture)) + l_draw_texture(L); + else if (luax_istype(L, 1, Jin_Lua_Canvas)) + l_draw_canvas(L); + else if (luax_istype(L, 1, Jin_Lua_Text)) + l_draw_text(L); + else if (luax_istype(L, 1, Jin_Lua_Page)) + l_draw_page(L); + else + { + luax_typerror(L, 1, "texture or canvas"); + return 1; + } + return 0; + } + + // draw(tex, quad, x, y, sx, sy, r, ax, ay) + LUA_IMPLEMENT int l_drawq(lua_State* L) + { + if (!luax_istable(L, 2)) + { + luax_typerror(L, 2, "table"); + return 1; + } + Math::Quad q; + q.x = luax_rawgetnumber(L, 2, 1); + q.y = luax_rawgetnumber(L, 2, 2); + q.w = luax_rawgetnumber(L, 2, 3); + q.h = luax_rawgetnumber(L, 2, 4); + luax_pop(L, 4); + int x = luax_optnumber(L, 3, 0); + int y = luax_optnumber(L, 4, 0); + float sx = luax_optnumber(L, 5, 1); + float sy = luax_optnumber(L, 6, 1); + float r = luax_optnumber(L, 7, 0); + float ox = luax_optnumber(L, 8, 0); + float oy = luax_optnumber(L, 9, 0); + + if (luax_istype(L, 1, Jin_Lua_Texture)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Shared& tex = proxy->getShared(); + tex->render(q, x, y, sx, sy, r, ox, oy); + } + else if (luax_istype(L, 1, Jin_Lua_Canvas)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Shared& p = proxy->getShared(); + p->render(q, x, y, sx, sy, r, ox, oy); + } + else + { + luax_typerror(L, 1, "texture or canvas"); + } + } + + /* print(string, x, y, lineheight, spacing) */ + /* need set font */ + LUA_IMPLEMENT 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->render(text, x, y, lineheight, spacing); + return 0; + } + + LUA_IMPLEMENT int l_setColor(lua_State* L) + { + if (luax_gettop(L) == 0) + { + gl.setColor(Color(255, 255, 255, 255)); + return 0; + } + + context.curRenderColor.r = luax_checknumber(L, 1); + context.curRenderColor.g = luax_checknumber(L, 2); + context.curRenderColor.b = luax_checknumber(L, 3); + if (luax_gettop(L) == 4) + context.curRenderColor.a = luax_checknumber(L, 4); + else + context.curRenderColor.a = 255; + gl.setColor(context.curRenderColor); + return 0; + } + + LUA_IMPLEMENT int l_getColor(lua_State * L) + { + luax_pushnumber(L, context.curRenderColor.r); + luax_pushnumber(L, context.curRenderColor.g); + luax_pushnumber(L, context.curRenderColor.b); + luax_pushnumber(L, context.curRenderColor.a); + return 4; + } + + LUA_IMPLEMENT int l_bindCanvas(lua_State* L) + { + if (luax_gettop(L) == 0) + { + // bind to default canvas + Canvas::unbind(); + return 0; + } + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Canvas); + Shared& shared = proxy->getShared(); + Canvas::bind(shared.getObject()); + return 0; + } + + LUA_IMPLEMENT int l_unbindCanvas(lua_State* L) + { + Canvas::unbind(); + return 0; + } + + LUA_IMPLEMENT int l_useShader(lua_State* L) + { + if (luax_gettop(L) == 0) + { + Shader::unuse(); + return 0; + } + if (luax_istype(L, 1, Jin_Lua_Shader)) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Shader); + Shared& jsl = proxy->getShared(); + jsl->use(); + } + else + { + luax_typerror(L, 1, "JSL shader"); + } + return 0; + } + + LUA_IMPLEMENT int l_setBlend(lua_State* L) + { + + return 0; + } + + LUA_IMPLEMENT int l_point(lua_State* L) + { + int x = luax_checknumber(L, 1); + int y = luax_checknumber(L, 2); + JinEngine::Graphics::point(x, y); + + return 0; + } + + LUA_IMPLEMENT int l_line(lua_State* L) + { + int x1 = luax_checknumber(L, 1); + int y1 = luax_checknumber(L, 2); + int x2 = luax_checknumber(L, 3); + int y2 = luax_checknumber(L, 4); + JinEngine::Graphics::line(x1, y1, x2, y2); + + return 0; + } + + LUA_IMPLEMENT int l_rect(lua_State* L) + { + RenderMode mode = static_cast(luax_checkinteger(L, 1)); + if (mode != RenderMode::NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + int w = luax_checknumber(L, 4); + int h = luax_checknumber(L, 5); + rect(mode, x, y, w, h); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + LUA_IMPLEMENT int l_circle(lua_State* L) + { + RenderMode mode = static_cast(luax_checkinteger(L, 1)); + if (mode != RenderMode::NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + float r = luax_checknumber(L, 4); + circle(mode, x, y, r); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + LUA_IMPLEMENT int l_triangle(lua_State* L) + { + RenderMode mode = static_cast(luax_checkinteger(L, 1)); + if (mode != RenderMode::NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + + int x2 = luax_checknumber(L, 3); + int y2 = luax_checknumber(L, 4); + + int x3 = luax_checknumber(L, 5); + int y3 = luax_checknumber(L, 6); + + triangle(mode, x, y, x2, y2, x3, y3); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + LUA_IMPLEMENT int l_polygon(lua_State* L) + { + RenderMode mode = static_cast(luax_checkinteger(L, 1)); + int n = luax_checknumber(L, 2); + if (mode != RenderMode::NONE) + { + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + int tn = luax_tableidxlen(L, 3); + if (tn != n * 2) + { + LUA_IMPLEMENT char* emsg = \ + "number of polygon vertices doesn't match " \ + "provided n, expect %d numbers but get %d"; + luax_error(L, emsg, n * 2, tn); + return 1; + } + float* p = (float*)alloca(2 * n * sizeof(float)); + for (int i = 1; i <= 2 * n; ++i) + p[i - 1] = luax_rawgetnumber(L, 3, i); + polygon(mode, p, n); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + LUA_IMPLEMENT int l_newTTFData(lua_State* L) + { + Proxy* proxy = luax_newinstance(L, Jin_Lua_TTFData); + TTFData* fd = nullptr; + { + const char* path = luax_checkstring(L, 1); + AssetDatabase* fs = AssetDatabase::get(); + if (!fs->exists(path)) + { + error(L, "No such font \"%s\"", path); + luax_pushnil(L); + return 1; + } + Buffer b; + fs->read(path, b); + fd = TTFData::createTTFData(&b, b.size()); + } + proxy->bind(new Shared(fd, Jin_Lua_TTFData)); + return 1; + } + + /* newText(str[, encode]) */ + LUA_IMPLEMENT int l_newText(lua_State* L) + { + Encode encode = Encode::UTF8; + if (luax_gettop(L) == 2) + { + const char* e = luax_checkstring(L, 2); + if (strcmp(e, "UTF8") == 0) encode = Encode::UTF8; + //else if (strcmp(e, "UTF16") == 0) encode = Encode::UTF16; + else if (strcmp(e, "ASCII") == 0) encode = Encode::ASCII; + else + { + luax_error(L, "wrong text encode %s", e); + return 0; + } + } + unsigned length; + const char* data = luax_checklstring(L, 1, &length); + Text* text = new Text(encode, data, length); + Proxy* proxy = luax_newinstance(L, Jin_Lua_Text); + proxy->bind(new Shared(text, Jin_Lua_Text)); + return 1; + } + + LUA_IMPLEMENT int l_newSprite(lua_State* L) + { + Proxy* p = luax_newinstance(L, Jin_Lua_Sprite); + p->bind(new Shared(new Sprite(), Jin_Lua_Sprite)); + return 1; + } + + LUA_IMPLEMENT int l_newSpriteSheet(lua_State* L) + { + Proxy* pxyGraphic = nullptr; + if (luax_istype(L, 1, Jin_Lua_Texture)) + pxyGraphic = (Proxy*)luax_checktype(L, 1, Jin_Lua_Texture); + else if(luax_istype(L, 1, Jin_Lua_Canvas)) + pxyGraphic = (Proxy*)luax_checktype(L, 1, Jin_Lua_Canvas); + if (pxyGraphic != nullptr) + { + Proxy* pxySSheet = luax_newinstance(L, Jin_Lua_SpriteSheet); + Graphic* graphic = pxyGraphic->getObject(); + Shared* shrSSheet = new Shared(new SpriteSheet(graphic), Jin_Lua_SpriteSheet); + Shared& shrGraphic = pxyGraphic->getShared(); + shrSSheet->setDependency((int)SpriteSheetDependency::DEP_GRAPHIC, &shrGraphic); + pxySSheet->bind(shrSSheet); + return 1; + } + else + return 0; + } + + /* newTextureFont(bitmap, text, color | cellw, cellh) */ + LUA_IMPLEMENT int l_newTextureFont(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_Bitmap); + Bitmap* bitmap = p->getObject(); + Text* text; + if (luax_istype(L, 2, Jin_Lua_Text)) + { + Proxy* pt = (Proxy*)luax_checktype(L, 2, Jin_Lua_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)) + { + unsigned int r = luax_rawgetnumber(L, 3, 1); + unsigned int g = luax_rawgetnumber(L, 3, 2); + unsigned int b = luax_rawgetnumber(L, 3, 3); + unsigned int a = luax_rawgetnumber(L, 3, 4); + textureFont = TextureFont::createTextureFont(bitmap, *text, Color(r, g, b, a), cellh); + } + else if (luax_isnumber(L, 3)) + { + 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 = luax_newinstance(L, Jin_Lua_TextureFont); + proxy->bind(new Shared(textureFont, Jin_Lua_TextureFont)); + return 1; + } + + /* setFont(font) */ + LUA_IMPLEMENT int l_setFont(lua_State* L) + { + if (luax_istype(L, 1, Jin_Lua_TTF)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_TTF); + TTF* ttf = p->getObject(); + context.curFont = ttf; + } + else if (luax_istype(L, 1, Jin_Lua_TextureFont)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_TextureFont); + TextureFont* tf = p->getObject(); + context.curFont = tf; + } + return 0; + } + + LUA_IMPLEMENT int l_unsetFont(lua_State* L) + { + context.curFont = context.defaultFont; + return 0; + } + + LUA_IMPLEMENT int l_clearMatrix(lua_State* L) + { + gl.clearMatrix(); + return 0; + } + + LUA_IMPLEMENT int l_pushMatrix(lua_State* L) + { + gl.push(); + return 0; + } + + LUA_IMPLEMENT int l_popMatrix(lua_State* L) + { + gl.pop(); + return 0; + } + + LUA_IMPLEMENT int l_scale(lua_State* L) + { + float sx = luax_checknumber(L, 1); + float sy = luax_checknumber(L, 2); + gl.scale(sx, sy); + return 0; + } + + LUA_IMPLEMENT int l_translate(lua_State* L) + { + float x = luax_checknumber(L, 1); + float y = luax_checknumber(L, 2); + gl.translate(x, y); + return 0; + } + + LUA_IMPLEMENT int l_rotate(lua_State* L) + { + float r = luax_checknumber(L, 1); + gl.rotate(r); + return 0; + } + + LUA_EXPORT int luaopen_graphics(lua_State* L) + { + luaopen_Bitmap(L); + luaopen_Texture(L); + luaopen_Canvas(L); + luaopen_TTFData(L); + luaopen_TTF(L); + luaopen_Text(L); + luaopen_TextureFont(L); + luaopen_Page(L); + luaopen_Shader(L); + luaopen_Sprite(L); + luaopen_SpriteSheet(L); + + luaL_Reg f[] = { + /* window */ + { "init", l_init }, + { "setTitle", l_setTitle }, + { "getSize", l_getSize }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "destroy", l_destroy }, + { "hideWindow", l_hideWindow }, + { "showWindow", l_showWindow }, + /* creators */ + { "newBitmap", l_newBitmap }, + { "newTexture", l_newTexture }, + { "newShader", l_newShader }, + { "newShaderf", l_newShaderf }, + { "newCanvas", l_newCanvas }, + { "newTTFData", l_newTTFData }, + { "newText", l_newText }, + { "newTextureFont", l_newTextureFont }, + { "newSprite", l_newSprite }, + { "newSpriteSheet", l_newSpriteSheet }, + /* render */ + { "setClearColor", l_setClearColor }, + { "clear", l_clear }, + { "draw", l_draw }, + { "print", l_print }, + { "drawq", l_drawq }, + { "setColor", l_setColor }, + { "getColor", l_getColor }, + { "present", l_present }, + /* canvas */ + { "bindCanvas", l_bindCanvas }, + { "unbindCanvas", l_unbindCanvas }, + /* shader */ + { "useShader", l_useShader }, + /* shapes */ + { "point", l_point }, + { "line", l_line }, + { "rect", l_rect }, + { "circle", l_circle }, + { "triangle", l_triangle }, + { "polygon", l_polygon }, + /* font */ + { "setFont", l_setFont }, + { "unsetFont", l_unsetFont }, + /* transform */ + { "pushMatrix", l_pushMatrix }, + { "clearMatrix", l_clearMatrix }, + { "popMatrix", l_popMatrix }, + { "translate", l_translate }, + { "rotate", l_rotate }, + { "scale", l_scale }, + { 0, 0 } + }; + + // Load whole lib. + luax_newlib(L, f); + + return 1; + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_graphics.h b/src/lua/modules/graphics/je_lua_graphics.h new file mode 100644 index 0000000..e69de29 diff --git a/src/lua/modules/graphics/je_lua_page.cpp b/src/lua/modules/graphics/je_lua_page.cpp new file mode 100644 index 0000000..20ec398 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_page.cpp @@ -0,0 +1,69 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +#include + +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Fonts; +using namespace JinEngine::Graphics::Shaders; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Page = "Page"; + + typedef Shared& SharedFont; + + Page* getPage(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Page); + return proxy->getObject(); + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Page); + proxy->release(); + return 0; + } + + LUA_IMPLEMENT int l_getSize(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->size.w); + luax_pushinteger(L, page->size.h); + return 2; + } + + LUA_IMPLEMENT int l_getWidth(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->size.w); + return 1; + } + + LUA_IMPLEMENT int l_getHeight(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->size.h); + return 1; + } + + LUA_EXPORT void luaopen_Page(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "getSize", l_getSize }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Page, f); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_page.h b/src/lua/modules/graphics/je_lua_page.h new file mode 100644 index 0000000..e4a21a3 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_page.h @@ -0,0 +1,22 @@ +#ifndef __JE_LUA_PAGE_H__ +#define __JE_LUA_PAGE_H__ + +namespace JinEngine +{ + namespace Lua + { + + enum class PageDependency + { + DEP_TTF = 1, + DEP_TEXTURE_FONT = 2, + }; + + extern const char* Jin_Lua_Page; + + void luaopen_Page(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_particle_system.cpp b/src/lua/modules/graphics/je_lua_particle_system.cpp new file mode 100644 index 0000000..7099a5c --- /dev/null +++ b/src/lua/modules/graphics/je_lua_particle_system.cpp @@ -0,0 +1,11 @@ +#include "libjin/jin.h" + +namespace JinEngine +{ + namespace Lua + { + + + + } // Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_particle_system.h b/src/lua/modules/graphics/je_lua_particle_system.h new file mode 100644 index 0000000..e69de29 diff --git a/src/lua/modules/graphics/je_lua_shader.cpp b/src/lua/modules/graphics/je_lua_shader.cpp new file mode 100644 index 0000000..1612a69 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_shader.cpp @@ -0,0 +1,137 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +#include "je_lua_shader.h" +#include "je_lua_canvas.h" +#include "je_lua_texture.h" + +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Shaders; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Shader = "Shader"; + + typedef Shared& ShaderRef; + + static inline ShaderRef checkShader(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Shader); + return proxy->getShared(); + } + + /** + * jsl:sendNumber("variable", 0.1) + */ + LUA_IMPLEMENT int l_sendNumber(lua_State* L) + { + ShaderRef shared = checkShader(L); + const char* variable = luax_checkstring(L, 2); + float number = luax_checknumber(L, 3); + shared->sendFloat(variable, number); + return 0; + } + + LUA_IMPLEMENT int l_sendTexture(lua_State* L) + { + ShaderRef shared = checkShader(L); + const char* variable = luax_checkstring(L, 2); + Proxy* proxy = (Proxy*)luax_checktype(L, 3, Jin_Lua_Texture); + Shared& tex = proxy->getShared(); + shared->sendTexture(variable, tex.getObject()); + return 0; + } + + LUA_IMPLEMENT int l_sendCanvas(lua_State* L) + { + ShaderRef shared = checkShader(L); + const char* variable = luax_checkstring(L, 2); + Proxy* proxy = (Proxy*)luax_checktype(L, 3, Jin_Lua_Canvas); + Shared& canvas = proxy->getShared(); + shared->sendCanvas(variable, canvas.getObject()); + return 0; + } + + LUA_IMPLEMENT int l_sendVec2(lua_State* L) + { + ShaderRef shared = checkShader(L); + const char* variable = luax_checkstring(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + float x = luax_rawgetnumber(L, 3, 1); + float y = luax_rawgetnumber(L, 3, 2); + shared->sendVec2(variable, x, y); + return 0; + } + + LUA_IMPLEMENT int l_sendVec3(lua_State* L) + { + ShaderRef shared = checkShader(L); + const char* variable = luax_checkstring(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + float x = luax_rawgetnumber(L, 3, 1); + float y = luax_rawgetnumber(L, 3, 2); + float z = luax_rawgetnumber(L, 3, 3); + shared->sendVec3(variable, x, y, z); + return 0; + } + + LUA_IMPLEMENT int l_sendVec4(lua_State* L) + { + ShaderRef shared = checkShader(L); + const char* variable = luax_checkstring(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + float x = luax_rawgetnumber(L, 3, 1); + float y = luax_rawgetnumber(L, 3, 2); + float z = luax_rawgetnumber(L, 3, 3); + float w = luax_rawgetnumber(L, 3, 4); + shared->sendVec4(variable, x, y, z, w); + return 0; + } + + LUA_IMPLEMENT int l_sendColor(lua_State* L) + { + return l_sendVec4(L); + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Shader); + proxy->release(); + return 0; + } + + LUA_EXPORT void luaopen_Shader(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "sendNumber", l_sendNumber }, + { "sendTexture", l_sendTexture }, + { "sendCanvas", l_sendCanvas }, + { "sendVec2", l_sendVec2 }, + { "sendVec3", l_sendVec3 }, + { "sendVec4", l_sendVec4 }, + { "sendColor", l_sendColor }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Shader, f); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_shader.h b/src/lua/modules/graphics/je_lua_shader.h new file mode 100644 index 0000000..5a84372 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_shader.h @@ -0,0 +1,16 @@ +#ifndef __JE_LUA_SHDER_H__ +#define __JE_LUA_SHDER_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_Shader; + + void luaopen_Shader(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_sprite.cpp b/src/lua/modules/graphics/je_lua_sprite.cpp new file mode 100644 index 0000000..97128a9 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_sprite.cpp @@ -0,0 +1,262 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +#include "je_lua_sprite.h" +#include "je_lua_canvas.h" +#include "je_lua_texture.h" +#include "je_lua_shader.h" + +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Shaders; + +namespace JinEngine +{ + namespace Lua + { + const char* Jin_Lua_Sprite = "Sprite"; + + typedef Shared& SharedSprite; + + LUA_IMPLEMENT inline SharedSprite checkSprite(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite); + return proxy->getShared(); + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite); + p->release(); + return 0; + } + + LUA_IMPLEMENT int l_setRotation(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + float r = luax_checknumber(L, 2); + sprite->setRotation(r); + return 0; + } + + LUA_IMPLEMENT int l_setOrigin(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + switch (luax_gettop(L)) + { + case 2: + { + int origin = luax_checkinteger(L, 2); + sprite->setOrigin(static_cast(origin)); + } + break; + case 3: + { + int x = luax_checkinteger(L, 2); + int y = luax_checkinteger(L, 3); + sprite->setOrigin(x, y); + } + break; + } + return 0; + } + + LUA_IMPLEMENT int l_setPosition(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + float x = luax_checknumber(L, 2); + float y = luax_checknumber(L, 3); + sprite->setPosition(x, y); + return 0; + } + + LUA_IMPLEMENT int l_setScale(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + float sx = luax_checknumber(L, 2); + float sy = luax_checknumber(L, 3); + sprite->setScale(sx, sy); + return 0; + } + + LUA_IMPLEMENT int l_setColor(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + Channel r = luax_checkinteger(L, 2); + Channel g = luax_checkinteger(L, 3); + Channel b = luax_checkinteger(L, 4); + Channel a = luax_checkinteger(L, 5); + sprite->setColor(Color(r, g, b, a)); + return 0; + } + + LUA_IMPLEMENT int l_setShader(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + Proxy* proxy = (Proxy*)luax_checktype(L, 2, Jin_Lua_Shader); + Shader* shader = proxy->getObject(); + sprite->setShader(shader); + sprite.setDependency((int)SpriteDependency::DEP_SHADER, &proxy->getShared()); + return 0; + } + + LUA_IMPLEMENT int l_setGraphic(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + Graphic* graphic = nullptr; + Proxy* p = nullptr; + if (luax_istype(L, 2, Jin_Lua_Texture)) + p = (Proxy*)luax_checktype(L, 2, Jin_Lua_Texture); + else if (luax_istype(L, 2, Jin_Lua_Canvas)) + p = (Proxy*)luax_checktype(L, 2, Jin_Lua_Canvas); + if (p != nullptr) + { + sprite->setGraphic(p->getObject()); + sprite.setDependency((int)SpriteDependency::DEP_GRAPHIC, &p->getShared()); + } + return 0; + } + + LUA_IMPLEMENT int l_move(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + float x = luax_checknumber(L, 2); + float y = luax_checknumber(L, 3); + sprite->move(x, y); + return 0; + } + + LUA_IMPLEMENT int l_scale(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + float sx = luax_checknumber(L, 2); + float sy = luax_checknumber(L, 3); + sprite->scale(sx, sy); + return 0; + } + + LUA_IMPLEMENT int l_rotate(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + float r = luax_checknumber(L, 2); + sprite->rotate(r); + return 0; + } + + LUA_IMPLEMENT int l_getRotation(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + float r = sprite->getRotation(); + luax_pushnumber(L, r); + return 1; + } + + LUA_IMPLEMENT int l_getPosition(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + const Math::Vector2& pos = sprite->getPosition(); + luax_pushnumber(L, pos.x); + luax_pushnumber(L, pos.y); + return 2; + } + + LUA_IMPLEMENT int l_getOrigin(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + const Math::Vector2& origin = sprite->getOrigin(); + luax_pushinteger(L, origin.x); + luax_pushinteger(L, origin.y); + return 2; + } + + LUA_IMPLEMENT int l_getScale(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + const Math::Vector2 scale = sprite->getScale(); + luax_pushnumber(L, scale.x); + luax_pushnumber(L, scale.y); + return 2; + } + + LUA_IMPLEMENT int l_getColor(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + const Color& c = sprite->getColor(); + luax_pushinteger(L, c.r); + luax_pushinteger(L, c.g); + luax_pushinteger(L, c.b); + luax_pushinteger(L, c.a); + return 4; + } + + LUA_IMPLEMENT int l_render(lua_State* L) + { + SharedSprite sprite = checkSprite(L); + sprite->render(); + return 0; + } + + LUA_IMPLEMENT int l_getGraphic(lua_State* L) + { + Proxy* pxySprite = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite); + Shared& shrSprite = pxySprite->getShared(); + SharedBase* shrGraphic = shrSprite.getDependency((int)SpriteDependency::DEP_GRAPHIC); + if (shrGraphic->isType(Jin_Lua_Canvas)) + { + Proxy* pxyCanvas = luax_newinstance(L, Jin_Lua_Canvas); + pxyCanvas->bind(shrGraphic); + return 1; + } + else if (shrGraphic->isType(Jin_Lua_Texture)) + { + Proxy* pxyTexture = luax_newinstance(L, Jin_Lua_Texture); + pxyTexture->bind(shrGraphic); + return 1; + } + return 0; + } + + LUA_IMPLEMENT int l_getShader(lua_State* L) + { + Proxy* pxySprite = (Proxy*)luax_checktype(L, 1, Jin_Lua_Sprite); + Shared& shrSprite = pxySprite->getShared(); + SharedBase* shrShader = shrSprite.getDependency((int)SpriteDependency::DEP_SHADER); + if (shrShader != nullptr && shrShader->isType(Jin_Lua_Shader)) + { + Proxy* pxyShader = luax_newinstance(L, Jin_Lua_Shader); + pxyShader->bind(shrShader); + return 1; + } + return 0; + } + + LUA_EXPORT void luaopen_Sprite(lua_State* L) + { + luaL_Reg methods[] = { + { "__gc", l_gc }, + { "render", l_render }, + { "setRotation", l_setRotation }, + { "setOrigin", l_setOrigin }, + { "setPosition", l_setPosition }, + { "setScale", l_setScale }, + { "setColor", l_setColor }, + { "setShader", l_setShader }, + { "setGraphic", l_setGraphic }, + { "move", l_move }, + { "scale", l_scale }, + { "rotate", l_rotate }, + { "getRotation", l_getRotation }, + { "getPosition", l_getPosition }, + { "getOrigin", l_getOrigin }, + { "getScale", l_getScale }, + { "getColor", l_getColor }, + { "getShader", l_getShader }, + { "getGraphic", l_getGraphic }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Sprite, methods); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_sprite.h b/src/lua/modules/graphics/je_lua_sprite.h new file mode 100644 index 0000000..02c44bf --- /dev/null +++ b/src/lua/modules/graphics/je_lua_sprite.h @@ -0,0 +1,24 @@ +#ifndef __JE_LUA_SPRITE_H__ +#define __JE_LUA_SPRITE_H__ + +namespace JinEngine +{ + namespace Lua + { + + // Sprite dependency slots. + enum class SpriteDependency + { + DEP_GRAPHIC = 1, + DEP_SHADER = 2, + DEP_SPRITESHEET = 3 + }; + + extern const char* Jin_Lua_Sprite; + + void luaopen_Sprite(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_spritesheet.cpp b/src/lua/modules/graphics/je_lua_spritesheet.cpp new file mode 100644 index 0000000..15469e9 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_spritesheet.cpp @@ -0,0 +1,54 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" +#include "je_lua_sprite.h" +#include "je_lua_spritesheet.h" + +using namespace JinEngine::Math; +using namespace JinEngine::Graphics; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_SpriteSheet = "SpriteSheet"; + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* pxySSheet = (Proxy*)luax_checktype(L, 1, Jin_Lua_SpriteSheet); + pxySSheet->release(); + return 0; + } + + LUA_IMPLEMENT int l_newSprite(lua_State* L) + { + Proxy* pxySSheet = (Proxy*)luax_checktype(L, 1, Jin_Lua_SpriteSheet); + Shared& shrSSheet = pxySSheet->getShared(); + SpriteSheet* sheet = pxySSheet->getObject(); + Quad quad; + quad.x = luax_checkinteger(L, 2); + quad.y = luax_checkinteger(L, 3); + quad.w = luax_checkinteger(L, 4); + quad.h = luax_checkinteger(L, 5); + Sprite* spr = sheet->createSprite(quad); + Proxy* pxySprite = luax_newinstance(L, Jin_Lua_Sprite); + Shared* shrSprite = new Shared(spr, Jin_Lua_Sprite); + shrSprite->setDependency((int)SpriteDependency::DEP_SPRITESHEET, &shrSSheet); + pxySprite->bind(shrSprite); + return 1; + } + + LUA_EXPORT void luaopen_SpriteSheet(lua_State* L) + { + luaL_Reg methods[] = { + { "__gc", l_gc }, + { "newSprite", l_newSprite }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_SpriteSheet, methods); + } + + } +} \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_spritesheet.h b/src/lua/modules/graphics/je_lua_spritesheet.h new file mode 100644 index 0000000..bcae60b --- /dev/null +++ b/src/lua/modules/graphics/je_lua_spritesheet.h @@ -0,0 +1,21 @@ +#ifndef __JE_LUA_SPRITE_SHEET_H__ +#define __JE_LUA_SPRITE_SHEET_H__ + +namespace JinEngine +{ + namespace Lua + { + + enum class SpriteSheetDependency + { + DEP_GRAPHIC = 1 + }; + + extern const char* Jin_Lua_SpriteSheet; + + void luaopen_SpriteSheet(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_text.cpp b/src/lua/modules/graphics/je_lua_text.cpp new file mode 100644 index 0000000..9377a0a --- /dev/null +++ b/src/lua/modules/graphics/je_lua_text.cpp @@ -0,0 +1,32 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +using namespace JinEngine::Graphics; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Text = "Text"; + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_Text); + p->release(); + return 0; + } + + LUA_EXPORT void luaopen_Text(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Text, f); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_text.h b/src/lua/modules/graphics/je_lua_text.h new file mode 100644 index 0000000..dfcc9cc --- /dev/null +++ b/src/lua/modules/graphics/je_lua_text.h @@ -0,0 +1,16 @@ +#ifndef __JE_LUA_TEXT_H__ +#define __JE_LUA_TEXT_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_Text; + + void luaopen_Text(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_texture.cpp b/src/lua/modules/graphics/je_lua_texture.cpp new file mode 100644 index 0000000..79ddc63 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_texture.cpp @@ -0,0 +1,66 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" +#include "je_lua_texture.h" + +using namespace JinEngine::Graphics; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Texture = "Texture"; + + typedef Shared& SharedTexture; + + LUA_IMPLEMENT inline SharedTexture checkTexture(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Texture); + return proxy->getShared(); + } + + LUA_IMPLEMENT int l_getWidth(lua_State* L) + { + SharedTexture shared = checkTexture(L); + luax_pushnumber(L, shared->getWidth()); + return 1; + } + + LUA_IMPLEMENT int l_getHeight(lua_State *L) + { + SharedTexture shared = checkTexture(L); + luax_pushnumber(L, shared->getHeight()); + return 1; + } + + LUA_IMPLEMENT int l_getSize(lua_State* L) + { + SharedTexture shared = checkTexture(L); + luax_pushnumber(L, shared->getWidth()); + luax_pushnumber(L, shared->getHeight()); + return 2; + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Texture); + proxy->release(); + return 0; + } + + LUA_EXPORT void luaopen_Texture(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Texture, f); + } + + }// namespace Lua +}// namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_texture.h b/src/lua/modules/graphics/je_lua_texture.h new file mode 100644 index 0000000..c8bb71c --- /dev/null +++ b/src/lua/modules/graphics/je_lua_texture.h @@ -0,0 +1,16 @@ +#ifndef __JE_LUA_TEXTURE_H__ +#define __JE_LUA_TEXTURE_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_Texture; + + void luaopen_Texture(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_texture_font.cpp b/src/lua/modules/graphics/je_lua_texture_font.cpp new file mode 100644 index 0000000..8ca3ce5 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_texture_font.cpp @@ -0,0 +1,66 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +#include "je_lua_page.h" +#include "je_lua_text.h" + +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Fonts; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_TextureFont = "TextureFont"; + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_TextureFont); + proxy->release(); + return 0; + } + + /* typeset(Text | string, lineheight, spacing) */ + LUA_IMPLEMENT int l_typeset(lua_State* L) + { + Proxy* pxyTexFont = (Proxy*)luax_checktype(L, 1, Jin_Lua_TextureFont); + Shared& shrTexFont = pxyTexFont->getShared(); + TextureFont* tf = pxyTexFont->getObject(); + int lineheight = luax_checkinteger(L, 3); + int spacing = luax_optnumber(L, 4, 0); + Page* page = nullptr; + if (luax_isstring(L, 2)) + { + unsigned length; + const char* str = luax_checklstring(L, 2, &length); + Text text(Encode::UTF8, str, length); + page = tf->typeset(text, lineheight, spacing); + } + else if (luax_istype(L, 2, Jin_Lua_Text)) + { + Proxy* p2 = (Proxy*)luax_checktype(L, 2, Jin_Lua_Text); + Text* text = p2->getObject(); + page = tf->typeset(*text, lineheight, spacing); + } + Proxy* pxyPage = luax_newinstance(L, Jin_Lua_Page); + Shared* shrPage = new Shared(page, Jin_Lua_Page); + shrPage->setDependency((int)PageDependency::DEP_TEXTURE_FONT, &shrTexFont); + pxyPage->bind(shrPage); + return 1; + } + + LUA_EXPORT void luaopen_TextureFont(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "typeset", l_typeset }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_TextureFont, f); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_texture_font.h b/src/lua/modules/graphics/je_lua_texture_font.h new file mode 100644 index 0000000..d1fffe5 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_texture_font.h @@ -0,0 +1,16 @@ +#ifndef __JE_LUA_TEXTURE_FONT_H__ +#define __JE_LUA_TEXTURE_FONT_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_TextureFont; + + void luaopen_TextureFont(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_ttf.cpp b/src/lua/modules/graphics/je_lua_ttf.cpp new file mode 100644 index 0000000..c5d922b --- /dev/null +++ b/src/lua/modules/graphics/je_lua_ttf.cpp @@ -0,0 +1,66 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +#include "je_lua_page.h" +#include "je_lua_text.h" + +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Fonts; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_TTF = "TTF"; + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_TTF); + proxy->release(); + return 0; + } + + /* typeset(Text | string, lineheight, spacing) */ + LUA_IMPLEMENT int l_typeset(lua_State* L) + { + Proxy* pxyTTF = (Proxy*)luax_checktype(L, 1, Jin_Lua_TTF); + Shared& shrTTF = pxyTTF->getShared(); + TTF* ttf = pxyTTF->getObject(); + int lineheight = luax_optnumber(L, 3, ttf->getFontSize()); + int spacing = luax_optnumber(L, 4, 0); + Page* page = nullptr; + if (luax_isstring(L, 2)) + { + unsigned length; + const char* str = luax_checklstring(L, 2, &length); + Text text(Encode::UTF8, str, length); + page = ttf->typeset(text, lineheight, spacing); + } + else if (luax_istype(L, 2, Jin_Lua_Text)) + { + Proxy* pxyText = (Proxy*)luax_checktype(L, 2, Jin_Lua_Text); + Text* text = pxyText->getObject(); + page = ttf->typeset(*text, lineheight, spacing); + } + Proxy* pxyPage = luax_newinstance(L, Jin_Lua_Page); + Shared* refPage = new Shared(page, Jin_Lua_Page); + refPage->setDependency((int)PageDependency::DEP_TTF, &shrTTF); + pxyPage->bind(refPage); + return 1; + } + + LUA_EXPORT void luaopen_TTF(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "typeset", l_typeset }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_TTF, f); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_ttf.h b/src/lua/modules/graphics/je_lua_ttf.h new file mode 100644 index 0000000..bfe503d --- /dev/null +++ b/src/lua/modules/graphics/je_lua_ttf.h @@ -0,0 +1,16 @@ +#ifndef __JE_LUA_TTF_H__ +#define __JE_LUA_TTF_H__ + +namespace JinEngine +{ + namespace Lua + { + + extern const char* Jin_Lua_TTF; + + void luaopen_TTF(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_ttf_data.cpp b/src/lua/modules/graphics/je_lua_ttf_data.cpp new file mode 100644 index 0000000..1277318 --- /dev/null +++ b/src/lua/modules/graphics/je_lua_ttf_data.cpp @@ -0,0 +1,52 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +#include "je_lua_ttf.h" +#include "je_lua_ttf_data.h" + +using namespace JinEngine::Graphics; +using namespace JinEngine::Graphics::Fonts; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_TTFData = "TTFData"; + + LUA_IMPLEMENT int l_newTTF(lua_State* L) + { + Proxy* pxyTTFData = (Proxy*)luax_checktype(L, 1, Jin_Lua_TTFData); + int fontsize = luax_checkinteger(L, 2); + Shared& shrFontData = pxyTTFData->getShared(); + TTFData* fontData = shrFontData.getObject(); + Proxy* pxyTTF = luax_newinstance(L, Jin_Lua_TTF); + TTF* font = fontData->createTTF(fontsize); + Shared* shrTTF = new Shared(font, Jin_Lua_TTF); + shrTTF->setDependency((int)TTFDependency::DEP_TTFDATA, &shrFontData); + pxyTTF->bind(shrTTF); + return 1; + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, Jin_Lua_TTFData); + p->release(); + return 0; + } + + LUA_EXPORT void luaopen_TTFData(lua_State* L) + { + luaL_Reg f[] = { + { "__gc", l_gc }, + { "newTTF", l_newTTF }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_TTFData, f); + + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/je_lua_ttf_data.h b/src/lua/modules/graphics/je_lua_ttf_data.h new file mode 100644 index 0000000..1fd832d --- /dev/null +++ b/src/lua/modules/graphics/je_lua_ttf_data.h @@ -0,0 +1,21 @@ +#ifndef __JE_LUA_TTFDATA_H__ +#define __JE_LUA_TTFDATA_H__ + +namespace JinEngine +{ + namespace Lua + { + + enum class TTFDependency + { + DEP_TTFDATA = 1, + }; + + extern const char* Jin_Lua_TTFData; + + void luaopen_TTFData(lua_State* L); + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/graphics/page.cpp b/src/lua/modules/graphics/page.cpp deleted file mode 100644 index 8c9e918..0000000 --- a/src/lua/modules/graphics/page.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -#include - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - typedef Ref& FontRef; - - Page* getPage(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); - return proxy->getObject(); - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); - { - /* release font */ - Ref* page = &proxy->getRef(); - RefBase* font = (RefBase*)page->getUserdata(); - font->release(); - } - proxy->release(); - return 0; - } - - static int l_getSize(lua_State* L) - { - Page* page = getPage(L); - luax_pushinteger(L, page->size.w); - luax_pushinteger(L, page->size.h); - return 2; - } - - static int l_getWidth(lua_State* L) - { - Page* page = getPage(L); - luax_pushinteger(L, page->size.w); - return 1; - } - - static int l_getHeight(lua_State* L) - { - Page* page = getPage(L); - luax_pushinteger(L, page->size.h); - 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; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/shader.cpp b/src/lua/modules/graphics/shader.cpp deleted file mode 100644 index d7733d4..0000000 --- a/src/lua/modules/graphics/shader.cpp +++ /dev/null @@ -1,135 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - typedef Ref& ShaderRef; - - static inline ShaderRef checkShader(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - return proxy->getRef(); - } - - /** - * jsl:sendNumber("variable", 0.1) - */ - static int l_sendNumber (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - float number = luax_checknumber(L, 3); - ref->sendFloat(variable, number); - return 0; - } - - static int l_sendTexture (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE); - Ref& tex = proxy->getRef(); - ref->sendTexture(variable, tex.getObject()); - return 0; - } - - static int l_sendCanvas (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS); - Ref& canvas = proxy->getRef(); - ref->sendCanvas(variable, canvas.getObject()); - return 0; - } - - static int l_sendVec2 (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - float x = luax_rawgetnumber(L, 3, 1); - float y = luax_rawgetnumber(L, 3, 2); - ref->sendVec2(variable, x, y); - return 0; - } - - static int l_sendVec3 (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - float x = luax_rawgetnumber(L, 3, 1); - float y = luax_rawgetnumber(L, 3, 2); - float z = luax_rawgetnumber(L, 3, 3); - ref->sendVec3(variable, x, y, z); - return 0; - } - - static int l_sendVec4 (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - float x = luax_rawgetnumber(L, 3, 1); - float y = luax_rawgetnumber(L, 3, 2); - float z = luax_rawgetnumber(L, 3, 3); - float w = luax_rawgetnumber(L, 3, 4); - ref->sendVec4(variable, x, y, z, w); - return 0; - } - - static int l_sendColor (lua_State* L) - { - return l_sendVec4(L); - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "sendNumber", l_sendNumber }, - { "sendTexture", l_sendTexture }, - { "sendCanvas", l_sendCanvas }, - { "sendVec2", l_sendVec2 }, - { "sendVec3", l_sendVec3 }, - { "sendVec4", l_sendVec4 }, - { "sendColor", l_sendColor }, - { 0, 0 } - }; - - /** - * JSL program - */ - int luaopen_JSL(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_SHADER, f); - return 0; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/text.cpp b/src/lua/modules/graphics/text.cpp deleted file mode 100644 index cbc82f1..0000000 --- a/src/lua/modules/graphics/text.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - static int l_gc(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXT); - p->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { 0, 0 } - }; - - int luaopen_Text(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TEXT, f); - return 0; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/texture.cpp b/src/lua/modules/graphics/texture.cpp deleted file mode 100644 index 61bfaee..0000000 --- a/src/lua/modules/graphics/texture.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - typedef Ref& TextureRef; - - static inline TextureRef checkTexture(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); - return proxy->getRef(); - } - - static int l_getWidth(lua_State* L) - { - TextureRef ref = checkTexture(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } - - static int l_getHeight(lua_State *L) - { - TextureRef ref = checkTexture(L); - luax_pushnumber(L, ref->getHeight()); - return 1; - } - - static int l_getSize(lua_State* L) - { - TextureRef ref = checkTexture(L); - luax_pushnumber(L, ref->getWidth()); - luax_pushnumber(L, ref->getHeight()); - return 2; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "getSize", l_getSize }, - { 0, 0 } - }; - - int luaopen_Texture(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TEXTURE, f); - return 0; - } - - }// lua -}// jin \ No newline at end of file diff --git a/src/lua/modules/graphics/texture_font.cpp b/src/lua/modules/graphics/texture_font.cpp deleted file mode 100644 index a2e88ba..0000000 --- a/src/lua/modules/graphics/texture_font.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - proxy->release(); - return 0; - } - - /* typeset(Text | string, lineheight, spacing) */ - static int l_typeset(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - TextureFont* tf = p->getObject(); - int lineheight = luax_checkinteger(L, 3); - int spacing = luax_optnumber(L, 4, 0); - Page* page = nullptr; - if (luax_isstring(L, 2)) - { - unsigned length; - const char* str = luax_checklstring(L, 2, &length); - Text text(Encode::UTF8, str, length); - page = tf->typeset(text, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) - { - Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = p2->getObject(); - page = tf->typeset(*text, lineheight, spacing); - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); - Ref* refPage = new Ref(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Ref& refTF = p->getRef(); - refTF.retain(); - refPage->setUserdata(&refTF); - } - proxy->bind(refPage); - return 1; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "typeset", l_typeset }, - { 0, 0 } - }; - - int luaopen_TextureFont(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TEXTUREFONT, f); - - return 0; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/ttf.cpp b/src/lua/modules/graphics/ttf.cpp deleted file mode 100644 index 414c7eb..0000000 --- a/src/lua/modules/graphics/ttf.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); - { - /* release ttf data */ - Ref* ttf = &proxy->getRef(); - RefBase* data = (RefBase*)ttf->getUserdata(); - data->release(); - } - proxy->release(); - return 0; - } - - /* typeset(Text | string, lineheight, spacing) */ - static int l_typeset(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); - TTF* ttf = p->getObject(); - int lineheight = luax_optnumber(L, 3, ttf->getFontSize()); - int spacing = luax_optnumber(L, 4, 0); - Page* page = nullptr; - if (luax_isstring(L, 2)) - { - unsigned length; - const char* str = luax_checklstring(L, 2, &length); - Text text(Encode::UTF8, str, length); - page = ttf->typeset(text, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) - { - Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = p2->getObject(); - page = ttf->typeset(*text, lineheight, spacing); - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); - Ref* refPage = new Ref(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Ref& refTTF = p->getRef(); - refTTF.retain(); - refPage->setUserdata(&refTTF); - } - proxy->bind(refPage); - return 1; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "typeset", l_typeset }, - { 0, 0 } - }; - - int luaopen_TTF(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TTF, f); - - return 0; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file diff --git a/src/lua/modules/graphics/ttfData.cpp b/src/lua/modules/graphics/ttfData.cpp deleted file mode 100644 index 43c3613..0000000 --- a/src/lua/modules/graphics/ttfData.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Graphics; - - static int l_newTTF(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); - int fontsize = luax_checkinteger(L, 2); - Ref& refFontData = p->getRef(); - TTFData* fontData = refFontData.getObject(); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTF, sizeof(Proxy)); - TTF* font = fontData->createTTF(fontsize); - Ref* refTTF = new Ref(font, JIN_GRAPHICS_TTF); - { - Ref& refTTFData = p->getRef(); - refTTFData.retain(); - refTTF->setUserdata(&refTTFData); - } - proxy->bind(refTTF); - return 1; - } - - static int l_gc(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); - p->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "newTTF", l_newTTF }, - { 0, 0 } - }; - - int luaopen_TTFData(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TTFDATA, f); - return 0; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file -- cgit v1.1-26-g67d0