diff options
author | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
commit | 40fc27154fe754181934dc7ee31375e6bdfb33fc (patch) | |
tree | 897ad98d759bc308ef66561181ba88b85f2ccd47 /src/lua/modules/graphics | |
parent | 1480c9445100075c9e1a894eb07c0ef727b509a1 (diff) |
*merge from minimal
Diffstat (limited to 'src/lua/modules/graphics')
-rw-r--r-- | src/lua/modules/graphics/bitmap.cpp | 113 | ||||
-rw-r--r-- | src/lua/modules/graphics/canvas.cpp | 108 | ||||
-rw-r--r-- | src/lua/modules/graphics/font.cpp | 49 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 1157 | ||||
-rw-r--r-- | src/lua/modules/graphics/image.cpp | 89 | ||||
-rw-r--r-- | src/lua/modules/graphics/jsl.cpp | 146 | ||||
-rw-r--r-- | src/lua/modules/graphics/page.cpp | 73 | ||||
-rw-r--r-- | src/lua/modules/graphics/shader.cpp | 135 | ||||
-rw-r--r-- | src/lua/modules/graphics/text.cpp | 32 | ||||
-rw-r--r-- | src/lua/modules/graphics/texture.cpp | 65 | ||||
-rw-r--r-- | src/lua/modules/graphics/texture_font.cpp | 67 | ||||
-rw-r--r-- | src/lua/modules/graphics/ttf.cpp | 73 | ||||
-rw-r--r-- | src/lua/modules/graphics/ttfData.cpp | 51 |
13 files changed, 1388 insertions, 770 deletions
diff --git a/src/lua/modules/graphics/bitmap.cpp b/src/lua/modules/graphics/bitmap.cpp new file mode 100644 index 0000000..13517f9 --- /dev/null +++ b/src/lua/modules/graphics/bitmap.cpp @@ -0,0 +1,113 @@ +#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<Bitmap>& BitmapRef; + + static inline BitmapRef checkBitmap(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); + return proxy->getRef<Bitmap>(); + } + + 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<Bitmap>(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 index f42dfba..e49e209 100644 --- a/src/lua/modules/graphics/canvas.cpp +++ b/src/lua/modules/graphics/canvas.cpp @@ -3,73 +3,63 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace jin::graphics; + using namespace JinEngine::Graphics; - typedef Ref<Canvas>& CanvasRef; + typedef Ref<Canvas>& CanvasRef; - static inline CanvasRef checkCanvas(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - return proxy->getRef<Canvas>(); - } - - static int l_getWidth(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } + static inline CanvasRef checkCanvas(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + return proxy->getRef<Canvas>(); + } - static int l_getHeight(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getHeight()); - return 1; - } + static int l_getWidth(lua_State* L) + { + CanvasRef ref = checkCanvas(L); + luax_pushnumber(L, ref->getWidth()); + 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_getHeight(lua_State* L) + { + CanvasRef ref = checkCanvas(L); + luax_pushnumber(L, ref->getHeight()); + return 1; + } - static int l_setAnchor(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - ref->setAnchor(x, y); - return 0; - } + 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 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 }, - { "setAnchor", l_setAnchor }, - { 0, 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; - } + int luaopen_Canvas(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_CANVAS, f); + return 0; + } -}// lua -}// jin
\ No newline at end of file + } // namespace Lua +} // namespace JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/font.cpp b/src/lua/modules/graphics/font.cpp deleted file mode 100644 index 926f2cc..0000000 --- a/src/lua/modules/graphics/font.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::graphics; - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); - proxy->release(); - return 0; - } - - static int l_box(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); - Ref<Font>& ref = proxy->getRef<Font>(); - const char* text = luax_checkstring(L, 2); - int fheight = luax_checknumber(L, 3); - int spacing = luax_checknumber(L, 4); - int lheight = luax_checknumber(L, 5); - int w, h; - ref->box(text, fheight, lheight, spacing, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "box", l_box }, - { 0, 0 } - }; - - int luaopen_Font(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_FONT, f); - - return 0; - } - -} // lua -} // jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 9c1d404..8b2d7cd 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -1,516 +1,819 @@ +#include <iostream> +#include <fstream> + +#include "libjin/jin.h" #include "lua/modules/luax.h" #include "lua/modules/types.h" -#include "libjin/jin.h" #include "lua/common/common.h" -#include "lua/modules/embed/graphics.lua.h" -namespace jin -{ -namespace lua -{ - using namespace jin::graphics; - using jin::filesystem::Filesystem; - using jin::filesystem::Buffer; - - typedef Texture Image; +using namespace std; +using namespace JinEngine; +using namespace JinEngine::Graphics; +using JinEngine::Filesystem::AssetDatabase; +using JinEngine::Filesystem::Buffer; - static struct +namespace JinEngine +{ + namespace Lua { - 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_getfield_integer(L, 1, "width"); - setting.height = luax_getfield_integer(L, 1, "height"); - setting.title = luax_getfield_string(L, 1, "title"); - setting.vsync = luax_getfield_bool(L, 1, "vsync"); - setting.fullscreen = luax_getfield_bool(L, 1, "fullscreen"); - setting.resizable= luax_getfield_bool(L, 1, "resizable"); - if (!wnd->init(&setting)) - { - luax_pushboolean(L, false); - return 1; - } - luax_pushboolean(L, true); - return 1; - } +#include "../../resources/font.ttf.h" - 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 struct + { + Color curRenderColor; + Color curClearColor; + Font* curFont = nullptr; + Font* defaultFont = nullptr; + } context; - static int l_getWidth(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getW()); - return 1; - } + 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; - static int l_getHeight(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getH()); - return 1; - } + luax_pushboolean(L, true); + return 1; + } - static int l_newImage(lua_State* L) - { - Filesystem* fs = Filesystem::get(); - const char* f = luax_checkstring(L, 1); - if (!fs->exists(f)) + static int l_setTitle(lua_State* L) { - printf("Error: no such image %s\n", f); - exit(1); + Window* wnd = Window::get(); + const char* title = luax_checkstring(L, 1); + wnd->setTitle(title); + return 0; } - Buffer b; - fs->read(f, &b); - - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_IMAGE, sizeof(Proxy)); - Image* img = Image::createTexture(b.data, b.size); - proxy->bind(new Ref<Image>(img, JIN_GRAPHICS_IMAGE)); - return 1; - } - static int l_newShader(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); - const char* program = luax_checkstring(L, 1); - JSLProgram* jsl = JSLProgram::createJSLProgram(program); - proxy->bind(new Ref<JSLProgram>(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<Canvas>(cvs, JIN_GRAPHICS_CANVAS)); - return 1; - } - - static int l_clear(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glClearColor(0, 0, 0, 1); - } - else - { - int r = luax_checknumber(L, 1); - int g = luax_checknumber(L, 2); - int b = luax_checknumber(L, 3); - int a = luax_checknumber(L, 4); - glClearColor(r / 255.f, g / 255.f, b / 255.f, a / 255.f); - } - glClear(GL_COLOR_BUFFER_BIT); - return 0; - } - - static int l_setClearColor(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glClearColor(1, 1, 1, 1); - return 0; - } - - context.curClearColor.rgba.r = luax_checknumber(L, 1); - context.curClearColor.rgba.g = luax_checknumber(L, 2); - context.curClearColor.rgba.b = luax_checknumber(L, 3); - context.curClearColor.rgba.a = luax_checknumber(L, 4); - - glClearColor(context.curClearColor.rgba.r / 255.f, - context.curClearColor.rgba.g / 255.f, - context.curClearColor.rgba.b / 255.f, - context.curClearColor.rgba.a / 255.f); - return 0; - } - - static int l_present(lua_State* L) - { - Window::get()->swapBuffers(); - return 0; - } + static int l_destroy(lua_State* L) + { + Window* wnd = Window::get(); + wnd->quit(); + return 0; + } - static int l_draw(lua_State* L) - { - 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); - if (luax_istype(L, 1, JIN_GRAPHICS_IMAGE)) + static int l_getSize(lua_State* L) { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Image>& tex = proxy->getRef<Image>(); - tex->draw(x, y, sx, sy, r); + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + luax_pushnumber(L, wnd->getH()); + return 2; } - else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) + + static int l_getWidth(lua_State* L) { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Canvas>& p = proxy->getRef<Canvas>(); - p->draw(x, y, sx, sy, r); + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + return 1; } - else + + static int l_getHeight(lua_State* L) { - /* wrong type */ - luax_typerror(L, 1, "image or canvas"); + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getH()); + return 1; } - return 0; - } - static int l_setColor(lua_State* L) - { - if (luax_gettop(L) == 0) + static int l_newBitmap(lua_State* L) { - glColor4f(1, 1, 1, 1); - return 0; + 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>(bitmap, JIN_GRAPHICS_BITMAP)); + return 1; + fail: + luax_pushnil(L); + return 1; } - context.curRenderColor.rgba.r = luax_checknumber(L, 1); - context.curRenderColor.rgba.g = luax_checknumber(L, 2); - context.curRenderColor.rgba.b = luax_checknumber(L, 3); - context.curRenderColor.rgba.a = luax_checknumber(L, 4); + /* 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<Bitmap>& refBitmap = p->getRef<Bitmap>(); + 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>(texture, JIN_GRAPHICS_TEXTURE)); + return 1; + } - glColor4f(context.curRenderColor.rgba.r / 255.f, - context.curRenderColor.rgba.g / 255.f, - context.curRenderColor.rgba.b / 255.f, - context.curRenderColor.rgba.a / 255.f); - return 0; - } + 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<Shader>(jsl, JIN_GRAPHICS_SHADER)); + return 1; + } - static int l_getColor(lua_State * L) - { - luax_pushnumber(L, context.curRenderColor.rgba.r); - luax_pushnumber(L, context.curRenderColor.rgba.g); - luax_pushnumber(L, context.curRenderColor.rgba.b); - luax_pushnumber(L, context.curRenderColor.rgba.a); - return 4; - } + 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<Shader>(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<Canvas>(cvs, JIN_GRAPHICS_CANVAS)); + return 1; + } - static int l_bindCanvas(lua_State* L) - { - if (luax_gettop(L) == 0) + static int l_clear(lua_State* L) { - // bind to default canvas - Canvas::unbind(); + glClear(GL_COLOR_BUFFER_BIT); return 0; } - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Ref<Canvas>& ref = proxy->getRef<Canvas>(); - ref->bind(); - 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) + 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) { - JSLProgram::unuse(); + Window::get()->swapBuffers(); return 0; } - if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) + + 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<Texture>& tex = proxy->getRef<Texture>(); + 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<Canvas>& p = proxy->getRef<Canvas>(); + 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<Text>(); + 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<TextureFont>(); + font = tf; + } + else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) + { + TTF* ttf = p2->getObject<TTF>(); + 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<Page>(); + Font* font = page->font; + font->print(page, x, y); + } + + static int l_draw(lua_State* L) { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<JSLProgram>& jsl = proxy->getRef<JSLProgram>(); - jsl->use(); + 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; } - else + + // draw(tex, quad, x, y, sx, sy, r, ax, ay) + static int l_drawq(lua_State* L) { - luax_typerror(L, 1, "JSL shader"); + 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<Texture>& tex = proxy->getRef<Texture>(); + 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<Canvas>& p = proxy->getRef<Canvas>(); + p->draw(q, x, y, sx, sy, r, ox, oy); + } + else + { + luax_typerror(L, 1, "texture or canvas"); + } } - return 0; - } - - static int l_unuseShader(lua_State* L) - { - JSLProgram::unuse(); - return 0; - } - static int l_setBlend(lua_State* L) - { - - return 0; - } + /* 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 RENDER_MODE strtomode(const char* str) - { - std::string s = std::string(str); - if (s == "fill") return RENDER_MODE::FILL; - else if (s == "line") return RENDER_MODE::LINE; - else return RENDER_MODE::NONE; - } + static int l_setColor(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glColor4f(1, 1, 1, 1); + return 0; + } - static int l_drawpoint(lua_State* L) - { - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - jin::graphics::point(x, y); - - return 0; - } - - static int l_drawLine(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); - jin::graphics::line(x1, y1, x2, y2); - - 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_drawRect(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) + static int l_getColor(lua_State * L) { - 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); + 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; } - else + + static int l_bindCanvas(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; + if (luax_gettop(L) == 0) + { + // bind to default canvas + Canvas::unbind(); + return 0; + } + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + Ref<Canvas>& ref = proxy->getRef<Canvas>(); + Canvas::bind(ref.getObject()); + return 0; } - return 0; - } + static int l_unbindCanvas(lua_State* L) + { + Canvas::unbind(); + return 0; + } - static int l_drawCircle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) + static int l_useShader(lua_State* L) { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - float r = luax_checknumber(L, 4); - circle(mode, x, y, r); + 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<Shader>& jsl = proxy->getRef<Shader>(); + jsl->use(); + } + else + { + luax_typerror(L, 1, "JSL shader"); + } + return 0; } - else + + static int l_setBlend(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; + + return 0; } - 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_drawTriangle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) + static int l_point(lua_State* L) { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); + 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; + } - int x3 = luax_checknumber(L, 5); - int y3 = luax_checknumber(L, 6); + 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; + } - triangle(mode, x, y, x2, y2, x3, y3); + return 0; } - else + + static int l_circle(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; + 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; } - return 0; - } - - static int l_drawPolygon(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - int n = luax_checknumber(L, 2); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) + static int l_triangle(lua_State* L) { - if (!luax_istable(L, 3)) + const char* modestr = luax_checkstring(L, 1); + RenderMode mode = strtomode(modestr); + if (mode != RenderMode::NONE) { - luax_typerror(L, 3, "table"); - return 1; + 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); } - int tn = luax_tableidxlen(L, 3); - if (tn != n * 2) + else { - 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); + luax_typerror(L, 1, "'fill' or 'line'"); return 1; } - float* p = new float[2 * n]; - for (int i = 1; i <= 2 * n; ++i) - p[i - 1] = luax_rawgetnumber(L, 3, i); - polygon(mode, p, n); - delete[] p; + + return 0; } - else + + static int l_polygon(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } + 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; - } + return 0; + } - static int l_newFont(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); - Font* font = new Font(); + static int l_newTTFData(lua_State* L) { - const char* path = luax_checkstring(L, 1); - Filesystem* fs = Filesystem::get(); - Buffer b = {}; - if (!fs->exists(path)) + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTFDATA, sizeof(Proxy)); + TTFData* fd = nullptr; { - printf("Error: no such font %s\n", path); - exit(1); + 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()); } - fs->read(path, &b); - font->loadb((const unsigned char*)b.data); + proxy->bind(new Ref<TTFData>(fd, JIN_GRAPHICS_TTFDATA)); + return 1; } - proxy->bind(new Ref<Font>(font, JIN_GRAPHICS_FONT)); - return 1; - } - static int l_study(lua_State* L) - { - int n = luax_gettop(L); - if (n == 0) - { - if (context.defaultFont == 0) + /* 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>(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<Bitmap>(); + Text* text; + if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) { - #include "lua/resources/font.ttf.h" - // load default font - context.defaultFont = new Font(); - context.defaultFont->loadb(font_ttf); + Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + text = pt->getObject<Text>(); } + 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>(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<TTF>(); + context.curFont = ttf; + } + else if (luax_istype(L, 1, JIN_GRAPHICS_TEXTUREFONT)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + TextureFont* tf = p->getObject<TextureFont>(); + context.curFont = tf; + } + return 0; + } + + static int l_unsetFont(lua_State* L) + { context.curFont = context.defaultFont; return 0; } - Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); - context.curFont = font; - return 0; - } - - static int l_write(lua_State* L) - { - if (context.curFont == 0) - return 0; - - const char* text = luax_checkstring(L, 1); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - - int fh = luax_optnumber(L, 4, 15); - int spacing = luax_optnumber(L, 5, 1); - int lh = luax_optnumber(L, 6, 18); - - context.curFont->render(text, x, y, fh, spacing, lh); - return 0; - } - - static int l_box(lua_State* L) - { - const char* text = luax_checkstring(L, 1); - int fontheight = luax_checknumber(L, 2); - int spacing = luax_checknumber(L, 3); - int lineheight = luax_checknumber(L, 4); - int w, h; - context.curFont->box(text, fontheight, spacing, lineheight, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; - } - - static const luaL_Reg f[] = { - { "init", l_init }, - { "getSize", l_getSize }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "newImage", l_newImage }, - { "newShader", l_newShader }, - { "newCanvas", l_newCanvas }, - { "newFont", l_newFont }, - { "box", l_box }, - { "write", l_write }, - { "setClearColor", l_setClearColor }, - { "clear", l_clear }, - { "draw", l_draw }, - { "setColor", l_setColor }, - { "palette", l_getColor }, - { "present", l_present }, - { "setFont", l_study }, - { "bindCanvas", l_bindCanvas }, - { "unbindCanvas", l_unbindCanvas }, - { "useShader", l_useShader }, - { "unuseShader", l_unuseShader }, - { "point", l_drawpoint }, - { "line", l_drawLine }, - { "rect", l_drawRect }, - { "circle", l_drawCircle }, - { "triangle", l_drawTriangle }, - { "polygon", l_drawPolygon }, - { "destroy", l_destroy }, - { 0, 0 } - }; - - extern int luaopen_Image(lua_State* L); - extern int luaopen_Font(lua_State* L); - extern int luaopen_Canvas(lua_State* L); - extern int luaopen_JSL(lua_State* L); + 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_Image(L); - luaopen_Canvas(L); - luaopen_Font(L); - luaopen_JSL(L); - - // load whole lib - luax_newlib(L, f); + 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; - } + return 1; + } -}// lua -}// jin
\ No newline at end of file + }// lua +}// jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/image.cpp b/src/lua/modules/graphics/image.cpp deleted file mode 100644 index 5824660..0000000 --- a/src/lua/modules/graphics/image.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::graphics; - - typedef Texture Image; - - static inline Ref<Image>& checkImage(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); - return proxy->getRef<Image>(); - } - - static int l_getWidth(lua_State* L) - { - Ref<Image>& ref = checkImage(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } - - static int l_getHeight(lua_State *L) - { - Ref<Image>& ref = checkImage(L); - luax_pushnumber(L, ref->getHeight()); - return 1; - } - - static int l_getPixel(lua_State* L) - { - Ref<Image>& ref = checkImage(L); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - color c = ref->getPixel(x, y); - luax_pushnumber(L, c.rgba.r); - luax_pushnumber(L, c.rgba.g); - luax_pushnumber(L, c.rgba.b); - luax_pushnumber(L, c.rgba.a); - return 4; - } - - static int l_setAnchor(lua_State* L) - { - Ref<Image>& ref = checkImage(L); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - ref->setAnchor(x, y); - return 0; - } - - static int l_getSize(lua_State* L) - { - Ref<Image>& ref = checkImage(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_IMAGE); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "getSize", l_getSize }, - { "getPixel", l_getPixel }, - { "setAnchor", l_setAnchor }, - { 0, 0 } - }; - - int luaopen_Image(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_IMAGE, f); - return 0; - } - -}// graphics -}// jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/jsl.cpp b/src/lua/modules/graphics/jsl.cpp deleted file mode 100644 index ccd9ebd..0000000 --- a/src/lua/modules/graphics/jsl.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::graphics; - - typedef Texture Image; - - static inline Ref<JSLProgram>& checkJSLProgram(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - return proxy->getRef<JSLProgram>(); - } - - static enum VARIABLE_TYPE - { - INVALID = 0, - - NUMBER, - IMAGE, - CANVAS, - VEC2, - VEC3, - VEC4, - COLOR, - }; - - static VARIABLE_TYPE strToType(const char* str) - { - std::string s = std::string(str); - if (s == "number") return NUMBER; - else if (s == "Image") return IMAGE; - else if (s == "Canvas") return CANVAS; - else if (s == "vec2") return VEC2; - else if (s == "vec3") return VEC3; - else if (s == "vec4") return VEC4; - else if (s == "Color") return COLOR; - else return INVALID; - } - - /** - * Use send function send variables to JSL program. - */ - static int l_send(lua_State* L) - { - Ref<JSLProgram>& ref = checkJSLProgram(L); - // number Image Texel - const char* typestr = luax_checkstring(L, 2); - // variable name - const char* variable = luax_checkstring(L, 3); - if (typestr != nullptr) - { - int type = strToType(typestr); - switch (type) - { - case NUMBER: - { - float number = luax_checknumber(L, 4); - ref->sendFloat(variable, number); - break; - } - case IMAGE: - { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE); - Ref<Image>& tex = proxy->getRef<Image>(); - ref->sendTexture(variable, tex.getObject()); - break; - } - case CANVAS: - { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); - Ref<Canvas>& canvas = proxy->getRef<Canvas>(); - ref->sendCanvas(variable, canvas.getObject()); - break; - } - case VEC2: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - ref->sendVec2(variable, x, y); - break; - } - case VEC3: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - float z = luax_checknumber(L, 6); - ref->sendVec3(variable, x, y, z); - break; - } - case VEC4: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - float z = luax_checknumber(L, 6); - float w = luax_checknumber(L, 7); - ref->sendVec4(variable, x, y, z, w); - break; - } - case COLOR: - { - color col; - col.rgba.r = luax_checkinteger(L, 4); - col.rgba.g = luax_checkinteger(L, 5); - col.rgba.b = luax_checkinteger(L, 6); - col.rgba.a = luax_checkinteger(L, 7); - ref->sendColor(variable, &col); - break; - } - default: - return 0; - } - } - return 1; - } - - 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 }, - { "send", l_send }, - { 0, 0 } - }; - - /** - * JSL program - */ - int luaopen_JSL(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_SHADER, f); - return 0; - } - -} -}
\ No newline at end of file diff --git a/src/lua/modules/graphics/page.cpp b/src/lua/modules/graphics/page.cpp new file mode 100644 index 0000000..8c9e918 --- /dev/null +++ b/src/lua/modules/graphics/page.cpp @@ -0,0 +1,73 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +#include <iostream> + +namespace JinEngine +{ + namespace Lua + { + + using namespace JinEngine::Graphics; + + typedef Ref<Font>& FontRef; + + Page* getPage(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); + return proxy->getObject<Page>(); + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); + { + /* release font */ + Ref<Page>* page = &proxy->getRef<Page>(); + 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 new file mode 100644 index 0000000..d7733d4 --- /dev/null +++ b/src/lua/modules/graphics/shader.cpp @@ -0,0 +1,135 @@ +#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<Shader>& ShaderRef; + + static inline ShaderRef checkShader(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + return proxy->getRef<Shader>(); + } + + /** + * 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<Texture>& tex = proxy->getRef<Texture>(); + 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>& canvas = proxy->getRef<Canvas>(); + 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 new file mode 100644 index 0000000..cbc82f1 --- /dev/null +++ b/src/lua/modules/graphics/text.cpp @@ -0,0 +1,32 @@ +#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 new file mode 100644 index 0000000..61bfaee --- /dev/null +++ b/src/lua/modules/graphics/texture.cpp @@ -0,0 +1,65 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +namespace JinEngine +{ + namespace Lua + { + + using namespace JinEngine::Graphics; + + typedef Ref<Texture>& TextureRef; + + static inline TextureRef checkTexture(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); + return proxy->getRef<Texture>(); + } + + 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 new file mode 100644 index 0000000..a2e88ba --- /dev/null +++ b/src/lua/modules/graphics/texture_font.cpp @@ -0,0 +1,67 @@ +#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<TextureFont>(); + 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<Text>(); + page = tf->typeset(*text, lineheight, spacing); + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); + Ref<Page>* refPage = new Ref<Page>(page, JIN_GRAPHICS_PAGE); + { + /* retain related ttf */ + Ref<TextureFont>& refTF = p->getRef<TextureFont>(); + 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 new file mode 100644 index 0000000..414c7eb --- /dev/null +++ b/src/lua/modules/graphics/ttf.cpp @@ -0,0 +1,73 @@ +#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>* ttf = &proxy->getRef<TTF>(); + 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<TTF>(); + 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<Text>(); + page = ttf->typeset(*text, lineheight, spacing); + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); + Ref<Page>* refPage = new Ref<Page>(page, JIN_GRAPHICS_PAGE); + { + /* retain related ttf */ + Ref<TTF>& refTTF = p->getRef<TTF>(); + 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 new file mode 100644 index 0000000..43c3613 --- /dev/null +++ b/src/lua/modules/graphics/ttfData.cpp @@ -0,0 +1,51 @@ +#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<TTFData>& refFontData = p->getRef<TTFData>(); + TTFData* fontData = refFontData.getObject(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTF, sizeof(Proxy)); + TTF* font = fontData->createTTF(fontsize); + Ref<TTF>* refTTF = new Ref<TTF>(font, JIN_GRAPHICS_TTF); + { + Ref<TTFData>& refTTFData = p->getRef<TTFData>(); + 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 |