aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/je_lua_graphics.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-23 15:56:01 +0800
committerchai <chaifix@163.com>2018-10-23 15:56:01 +0800
commit6551adeca70d4299a99d45245d4e13dbfdfa87e5 (patch)
treea766ff9a0cdc52588a6aa62e41d0b134c04c2ba0 /src/lua/modules/graphics/je_lua_graphics.cpp
parenta164919e8eb3409d13bb225cc0e6f0b95446e8e0 (diff)
*修改lua绑定文件的文件名
Diffstat (limited to 'src/lua/modules/graphics/je_lua_graphics.cpp')
-rw-r--r--src/lua/modules/graphics/je_lua_graphics.cpp819
1 files changed, 819 insertions, 0 deletions
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..f98d640
--- /dev/null
+++ b/src/lua/modules/graphics/je_lua_graphics.cpp
@@ -0,0 +1,819 @@
+#include <iostream>
+#include <fstream>
+
+#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>(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<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;
+ }
+
+ 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_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_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<Texture>& tex = proxy->getRef<Texture>();
+ tex->render(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->render(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->render(*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->render(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<Texture>& tex = proxy->getRef<Texture>();
+ tex->render(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->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 */
+ 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->render(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<Canvas>& ref = proxy->getRef<Canvas>();
+ 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<Shader>& jsl = proxy->getRef<Shader>();
+ 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<TTFData>(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>(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))
+ {
+ 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;
+ }
+
+ 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