aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/graphics.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-14 22:52:40 +0800
committerchai <chaifix@163.com>2018-10-14 22:52:40 +0800
commitb1bbc998960fff2169dc5a992c47d08723472f9b (patch)
tree220f3bd5de2266e248884e11161dd715d7632ef2 /src/lua/modules/graphics/graphics.cpp
parentfbf989f9950a38566e0fb0fc5b6a7aebc9f0fb45 (diff)
*直接渲染字符串
Diffstat (limited to 'src/lua/modules/graphics/graphics.cpp')
-rw-r--r--src/lua/modules/graphics/graphics.cpp314
1 files changed, 242 insertions, 72 deletions
diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp
index 5c67421..a62802c 100644
--- a/src/lua/modules/graphics/graphics.cpp
+++ b/src/lua/modules/graphics/graphics.cpp
@@ -169,18 +169,6 @@ namespace lua
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;
}
@@ -189,7 +177,7 @@ namespace lua
{
if (luax_gettop(L) == 0)
{
- glClearColor(1, 1, 1, 1);
+ glClearColor(0, 0, 0, 1);
return 0;
}
@@ -198,10 +186,10 @@ namespace lua
context.curClearColor.b = luax_checknumber(L, 3);
context.curClearColor.a = luax_checknumber(L, 4);
- glClearColor(context.curClearColor.r / 255.f,
- context.curClearColor.g / 255.f,
- context.curClearColor.b / 255.f,
- context.curClearColor.a / 255.f);
+ gl.setClearColor(context.curClearColor.r,
+ context.curClearColor.g,
+ context.curClearColor.b,
+ context.curClearColor.a);
return 0;
}
@@ -211,30 +199,153 @@ namespace lua
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);
+ Proxy* proxy = (Proxy*)luax_toudata(L, 1);
+ Ref<Texture>& tex = proxy->getRef<Texture>();
+ tex->draw(x, y, sx, sy, r);
+ }
+
+ 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);
+ Proxy* proxy = (Proxy*)luax_toudata(L, 1);
+ Ref<Canvas>& p = proxy->getRef<Canvas>();
+ p->draw(x, y, sx, sy, r);
+ }
+
+ /* 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>();
+ Proxy* p2 = (Proxy*)luax_toudata(L, 2);
+ int x = luax_optnumber(L, 3, 0);
+ int y = luax_optnumber(L, 4, 0);
+ int lineheight = luax_optnumber(L, 5, 12);
+ int spacing = luax_optnumber(L, 6, 0);
+ if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT))
+ {
+ TextureFont* tf = p2->getObject<TextureFont>();
+ tf->print(*text, x, y, lineheight, spacing);
+ }
+ else if (luax_istype(L, 2, JIN_GRAPHICS_TTF))
+ {
+ TTF* ttf = p2->getObject<TTF>();
+ ttf->print(*text, x, y, lineheight, spacing);
+ }
+ }
+
+ /* print(string[, font], x, y, lineheight, spacing) */
+ static int l_print(lua_State* L)
+ {
+ unsigned length;
+ const char* str = luax_checklstring(L, 1, &length);
+ Text text(Encode::UTF8, str, length);
+ Proxy* p = (Proxy*)luax_toudata(L, 2);
+ int x = luax_optnumber(L, 3, 0);
+ int y = luax_optnumber(L, 4, 0);
+ int lineheight = luax_optnumber(L, 5, 12);
+ int spacing = luax_optnumber(L, 6, 0);
+ if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT))
+ {
+ TextureFont* tf = p->getObject<TextureFont>();
+ tf->print(text, x, y, lineheight, spacing);
+ }
+ else if (luax_istype(L, 2, JIN_GRAPHICS_TTF))
+ {
+ TTF* ttf = p->getObject<TTF>();
+ ttf->print(text, x, y, lineheight, spacing);
+ }
+ return 0;
+ }
+
+ /* 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)
{
- 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_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 if (luax_isstring(L, 1))
+ l_print(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 ax = luax_optnumber(L, 8, 0);
+ float ay = 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(x, y, sx, sy, r);
+ Ref<Texture>& tex = proxy->getRef<Texture>();
+ tex->draw(q, x, y, sx, sy, r, ax, ay);
}
else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS))
{
Proxy* proxy = (Proxy*)luax_toudata(L, 1);
Ref<Canvas>& p = proxy->getRef<Canvas>();
- p->draw(x, y, sx, sy, r);
+ p->draw(q, x, y, sx, sy, r, ax, ay);
}
else
{
luax_typerror(L, 1, "texture or canvas");
}
- return 0;
}
static int l_setColor(lua_State* L)
@@ -307,13 +418,7 @@ namespace lua
}
return 0;
}
-/*
- static int l_unuseShader(lua_State* L)
- {
- Shader::unuse();
- return 0;
- }
-*/
+
static int l_setBlend(lua_State* L)
{
@@ -450,10 +555,10 @@ namespace lua
return 0;
}
- static int l_newFontData(lua_State* L)
+ static int l_newTTFData(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONTDATA, sizeof(Proxy));
- FontData* fd = nullptr;
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTFDATA, sizeof(Proxy));
+ TTFData* fd = nullptr;
{
const char* path = luax_checkstring(L, 1);
Filesystem* fs = Filesystem::get();
@@ -465,52 +570,115 @@ namespace lua
}
Buffer b;
fs->read(path, &b);
- fd = FontData::createFontData((unsigned char*)b.data, b.size);
+ fd = TTFData::createTTFData((unsigned char*)b.data, b.size);
}
- proxy->bind(new Ref<FontData>(fd, JIN_GRAPHICS_FONTDATA));
+ 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>();
+ Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT);
+ Text* text = pt->getObject<Text>();
+ 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);
+ }
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTUREFONT, sizeof(Proxy));
+ proxy->bind(new Ref<TextureFont>(textureFont, JIN_GRAPHICS_TEXTUREFONT));
+ return 1;
+ }
+
+ static int l_setFont(lua_State* L)
+ {
+ 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 },
+ { "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 },
- { "newCanvas", l_newCanvas },
- { "newFontData", l_newFontData },
+ { "newBitmap", l_newBitmap },
+ { "newTexture", l_newTexture },
+ { "newShader", l_newShader },
+ { "newCanvas", l_newCanvas },
+ { "newTTFData", l_newTTFData },
+ { "newText", l_newText },
+ { "newTextureFont", l_newTextureFont },
/* render */
- { "setClearColor", l_setClearColor },
- { "clear", l_clear },
- { "draw", l_draw },
- { "setColor", l_setColor },
- { "getColor", l_getColor },
- { "present", l_present },
+ { "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 },
+ { "bindCanvas", l_bindCanvas },
+ { "unbindCanvas", l_unbindCanvas },
/* shader */
- { "useShader", l_useShader },
- //{ "unuseShader", l_unuseShader },
+ { "useShader", l_useShader },
/* shapes */
- { "point", l_point },
- { "line", l_line },
- { "rect", l_rect },
- { "circle", l_circle },
- { "triangle", l_triangle },
- { "polygon", l_polygon },
- { 0, 0 }
+ { "point", l_point },
+ { "line", l_line },
+ { "rect", l_rect },
+ { "circle", l_circle },
+ { "triangle", l_triangle },
+ { "polygon", l_polygon },
+ /* font */
+ { "setFont", l_setFont },
+ { 0, 0 }
};
extern int luaopen_Texture(lua_State* L);
- extern int luaopen_Font(lua_State* L);
- extern int luaopen_FontData(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);
@@ -522,8 +690,10 @@ namespace lua
luaopen_Bitmap(L);
luaopen_Texture(L);
luaopen_Canvas(L);
- luaopen_FontData(L);
- luaopen_Font(L);
+ luaopen_TTFData(L);
+ luaopen_TTF(L);
+ luaopen_Text(L);
+ luaopen_TextureFont(L);
luaopen_Page(L);
luaopen_JSL(L);
@@ -533,5 +703,5 @@ namespace lua
return 1;
}
-}// lua
-}// jin \ No newline at end of file
+}// lua
+}// jin \ No newline at end of file