diff options
Diffstat (limited to 'src/libjin-lua')
-rw-r--r-- | src/libjin-lua/embed/graphics.lua.h | 102 | ||||
-rw-r--r-- | src/libjin-lua/embed/scripts/graphics/graphics.lua | 21 | ||||
-rw-r--r-- | src/libjin-lua/modules/graphics/je_lua_graphics.cpp | 29 | ||||
-rw-r--r-- | src/libjin-lua/modules/graphics/je_lua_mesh.cpp | 87 | ||||
-rw-r--r-- | src/libjin-lua/modules/graphics/je_lua_mesh.h | 21 |
5 files changed, 214 insertions, 46 deletions
diff --git a/src/libjin-lua/embed/graphics.lua.h b/src/libjin-lua/embed/graphics.lua.h index ba80565..071d862 100644 --- a/src/libjin-lua/embed/graphics.lua.h +++ b/src/libjin-lua/embed/graphics.lua.h @@ -97,49 +97,65 @@ static unsigned char graphics_lua[] = { 10,9,32,32,32,32,123,13,10,9,9,32,32,32,32,114,101,116,117,114, 110,32,99,111,108,59,13,10,9,32,32,32,32,125,13,10,9,32,32,32, 32,35,69,78,68,95,70,82,65,71,77,69,78,84,95,83,72,65,68,69, -82,13,10,32,32,32,32,93,93,13,10,9,106,103,46,83,104,97,100,101, -114,115,46,68,101,102,97,117,108,116,32,61,32,106,103,46,110,101,119,83, -104,97,100,101,114,91,91,13,10,9,9,35,86,69,82,84,69,88,95,83, -72,65,68,69,82,13,10,9,9,86,101,114,116,101,120,32,118,101,114,116, -40,86,101,114,116,101,120,32,118,41,13,10,9,9,123,13,10,9,9,9, -114,101,116,117,114,110,32,118,59,13,10,9,9,125,13,10,9,9,35,69, -78,68,95,86,69,82,84,69,88,95,83,72,65,68,69,82,13,10,9,9, -35,70,82,65,71,77,69,78,84,95,83,72,65,68,69,82,13,10,9,9, -67,111,108,111,114,32,102,114,97,103,40,67,111,108,111,114,32,99,111,108, -44,32,84,101,120,116,117,114,101,32,116,101,120,44,32,86,101,114,116,101, +82,13,10,32,32,32,32,93,93,13,10,32,32,32,32,106,103,46,83,104, +97,100,101,114,115,46,77,101,115,104,32,61,32,106,103,46,110,101,119,83, +104,97,100,101,114,91,91,32,13,10,32,32,32,32,32,32,32,9,35,86, +69,82,84,69,88,95,83,72,65,68,69,82,13,10,32,32,32,32,32,32, +32,32,105,110,32,86,101,99,52,32,99,111,108,111,114,59,13,10,9,9, +86,101,114,116,101,120,32,118,101,114,116,40,86,101,114,116,101,120,32,118, +41,13,10,9,9,123,13,10,9,9,9,114,101,116,117,114,110,32,118,59, +13,10,9,9,125,13,10,9,9,35,69,78,68,95,86,69,82,84,69,88, +95,83,72,65,68,69,82,13,10,9,9,35,70,82,65,71,77,69,78,84, +95,83,72,65,68,69,82,13,10,9,9,67,111,108,111,114,32,102,114,97, +103,40,67,111,108,111,114,32,99,111,108,44,32,84,101,120,116,117,114,101, +32,116,101,120,44,32,86,101,114,116,101,120,32,118,41,13,10,9,9,123, +13,10,9,9,9,114,101,116,117,114,110,32,99,111,108,32,42,32,116,101, +120,101,108,40,116,101,120,44,32,118,46,117,118,41,32,42,32,118,46,99, +111,108,111,114,59,13,10,9,9,125,13,10,9,9,35,69,78,68,95,70, +82,65,71,77,69,78,84,95,83,72,65,68,69,82,32,13,10,32,32,32, +32,93,93,13,10,9,106,103,46,83,104,97,100,101,114,115,46,68,101,102, +97,117,108,116,32,61,32,106,103,46,110,101,119,83,104,97,100,101,114,91, +91,13,10,9,9,35,86,69,82,84,69,88,95,83,72,65,68,69,82,13, +10,9,9,86,101,114,116,101,120,32,118,101,114,116,40,86,101,114,116,101, 120,32,118,41,13,10,9,9,123,13,10,9,9,9,114,101,116,117,114,110, -32,99,111,108,32,42,32,116,101,120,101,108,40,116,101,120,44,32,118,46, -117,118,41,59,13,10,9,9,125,13,10,9,9,35,69,78,68,95,70,82, -65,71,77,69,78,84,95,83,72,65,68,69,82,13,10,9,93,93,13,10, -101,110,100,13,10,13,10,108,111,99,97,108,32,95,105,110,105,116,32,61, -32,106,103,46,105,110,105,116,13,10,108,111,99,97,108,32,105,110,105,116, -105,97,108,105,122,101,100,32,61,32,102,97,108,115,101,13,10,106,103,46, -105,110,105,116,32,61,32,102,117,110,99,116,105,111,110,40,115,101,116,116, -105,110,103,41,32,13,10,32,32,32,32,105,102,32,105,110,105,116,105,97, -108,105,122,101,100,32,116,104,101,110,32,13,10,32,32,32,32,32,32,32, -32,114,101,116,117,114,110,32,105,110,105,116,105,97,108,105,122,101,100,13, -10,32,32,32,32,101,110,100,13,10,32,32,32,32,105,110,105,116,105,97, -108,105,122,101,100,32,61,32,95,105,110,105,116,40,115,101,116,116,105,110, -103,41,13,10,32,32,32,32,105,102,32,105,110,105,116,105,97,108,105,122, -101,100,32,116,104,101,110,32,13,10,32,32,32,32,32,32,32,32,99,111, -109,112,105,108,101,66,117,105,108,116,73,110,83,104,97,100,101,114,115,40, -41,13,10,32,32,32,32,32,32,32,32,106,103,46,117,115,101,83,104,97, -100,101,114,40,106,103,46,83,104,97,100,101,114,115,46,70,111,110,116,41, -13,10,32,32,32,32,101,110,100,13,10,32,32,32,32,114,101,116,117,114, -110,32,105,110,105,116,105,97,108,105,122,101,100,32,13,10,101,110,100,13, -10,13,10,106,103,46,117,110,117,115,101,83,104,97,100,101,114,32,61,32, -102,117,110,99,116,105,111,110,40,41,13,10,32,32,32,32,106,103,46,117, -115,101,83,104,97,100,101,114,40,106,103,46,83,104,97,100,101,114,115,46, -70,111,110,116,41,13,10,101,110,100,13,10,13,10,45,45,32,82,101,115, -101,116,32,97,108,108,32,97,116,116,114,105,98,117,116,101,115,32,116,111, -32,100,101,102,97,117,108,116,32,118,97,108,117,101,46,13,10,106,103,46, -114,101,115,101,116,32,61,32,102,117,110,99,116,105,111,110,40,41,13,10, -32,32,32,32,106,103,46,115,101,116,67,111,108,111,114,40,50,53,53,44, -32,50,53,53,44,32,50,53,53,44,32,50,53,53,41,13,10,32,32,32, -32,106,103,46,115,101,116,67,108,101,97,114,67,111,108,111,114,40,48,44, -32,48,44,32,48,44,32,50,53,53,41,13,10,32,32,32,32,106,103,46, -99,108,101,97,114,40,41,13,10,32,32,32,32,106,103,46,117,110,115,101, -116,70,111,110,116,40,41,13,10,32,32,32,32,106,103,46,117,110,117,115, -101,83,104,97,100,101,114,40,41,13,10,101,110,100,13,10 +32,118,59,13,10,9,9,125,13,10,9,9,35,69,78,68,95,86,69,82, +84,69,88,95,83,72,65,68,69,82,13,10,9,9,35,70,82,65,71,77, +69,78,84,95,83,72,65,68,69,82,13,10,9,9,67,111,108,111,114,32, +102,114,97,103,40,67,111,108,111,114,32,99,111,108,44,32,84,101,120,116, +117,114,101,32,116,101,120,44,32,86,101,114,116,101,120,32,118,41,13,10, +9,9,123,13,10,9,9,9,114,101,116,117,114,110,32,99,111,108,32,42, +32,116,101,120,101,108,40,116,101,120,44,32,118,46,117,118,41,59,13,10, +9,9,125,13,10,9,9,35,69,78,68,95,70,82,65,71,77,69,78,84, +95,83,72,65,68,69,82,13,10,9,93,93,32,13,10,101,110,100,13,10, +13,10,108,111,99,97,108,32,95,105,110,105,116,32,61,32,106,103,46,105, +110,105,116,13,10,108,111,99,97,108,32,105,110,105,116,105,97,108,105,122, +101,100,32,61,32,102,97,108,115,101,13,10,106,103,46,105,110,105,116,32, +61,32,102,117,110,99,116,105,111,110,40,115,101,116,116,105,110,103,41,32, +13,10,32,32,32,32,105,102,32,105,110,105,116,105,97,108,105,122,101,100, +32,116,104,101,110,32,13,10,32,32,32,32,32,32,32,32,114,101,116,117, +114,110,32,105,110,105,116,105,97,108,105,122,101,100,13,10,32,32,32,32, +101,110,100,13,10,32,32,32,32,105,110,105,116,105,97,108,105,122,101,100, +32,61,32,95,105,110,105,116,40,115,101,116,116,105,110,103,41,13,10,32, +32,32,32,105,102,32,105,110,105,116,105,97,108,105,122,101,100,32,116,104, +101,110,32,13,10,32,32,32,32,32,32,32,32,99,111,109,112,105,108,101, +66,117,105,108,116,73,110,83,104,97,100,101,114,115,40,41,13,10,32,32, +32,32,32,32,32,32,106,103,46,117,115,101,83,104,97,100,101,114,40,106, +103,46,83,104,97,100,101,114,115,46,68,101,102,97,117,108,116,41,13,10, +32,32,32,32,101,110,100,13,10,32,32,32,32,114,101,116,117,114,110,32, +105,110,105,116,105,97,108,105,122,101,100,32,13,10,101,110,100,13,10,13, +10,106,103,46,117,110,117,115,101,83,104,97,100,101,114,32,61,32,102,117, +110,99,116,105,111,110,40,41,13,10,32,32,32,32,106,103,46,117,115,101, +83,104,97,100,101,114,40,106,103,46,83,104,97,100,101,114,115,46,68,101, +102,97,117,108,116,41,13,10,101,110,100,13,10,13,10,45,45,32,82,101, +115,101,116,32,97,108,108,32,97,116,116,114,105,98,117,116,101,115,32,116, +111,32,100,101,102,97,117,108,116,32,118,97,108,117,101,46,13,10,106,103, +46,114,101,115,101,116,32,61,32,102,117,110,99,116,105,111,110,40,41,13, +10,32,32,32,32,106,103,46,115,101,116,67,111,108,111,114,40,50,53,53, +44,32,50,53,53,44,32,50,53,53,44,32,50,53,53,41,13,10,32,32, +32,32,106,103,46,115,101,116,67,108,101,97,114,67,111,108,111,114,40,48, +44,32,48,44,32,48,44,32,50,53,53,41,13,10,32,32,32,32,106,103, +46,99,108,101,97,114,40,41,13,10,32,32,32,32,106,103,46,117,110,115, +101,116,70,111,110,116,40,41,13,10,32,32,32,32,106,103,46,117,110,117, +115,101,83,104,97,100,101,114,40,41,13,10,101,110,100,13,10 }; diff --git a/src/libjin-lua/embed/scripts/graphics/graphics.lua b/src/libjin-lua/embed/scripts/graphics/graphics.lua index a0efe03..8b16a35 100644 --- a/src/libjin-lua/embed/scripts/graphics/graphics.lua +++ b/src/libjin-lua/embed/scripts/graphics/graphics.lua @@ -103,6 +103,21 @@ local function compileBuiltInShaders() } #END_FRAGMENT_SHADER ]] + jg.Shaders.Mesh = jg.newShader[[ + #VERTEX_SHADER + in Vec4 color; + Vertex vert(Vertex v) + { + return v; + } + #END_VERTEX_SHADER + #FRAGMENT_SHADER + Color frag(Color col, Texture tex, Vertex v) + { + return col * texel(tex, v.uv) * v.color; + } + #END_FRAGMENT_SHADER + ]] jg.Shaders.Default = jg.newShader[[ #VERTEX_SHADER Vertex vert(Vertex v) @@ -116,7 +131,7 @@ local function compileBuiltInShaders() return col * texel(tex, v.uv); } #END_FRAGMENT_SHADER - ]] + ]] end local _init = jg.init @@ -128,13 +143,13 @@ jg.init = function(setting) initialized = _init(setting) if initialized then compileBuiltInShaders() - jg.useShader(jg.Shaders.Font) + jg.useShader(jg.Shaders.Default) end return initialized end jg.unuseShader = function() - jg.useShader(jg.Shaders.Font) + jg.useShader(jg.Shaders.Default) end -- Reset all attributes to default value. diff --git a/src/libjin-lua/modules/graphics/je_lua_graphics.cpp b/src/libjin-lua/modules/graphics/je_lua_graphics.cpp index fba77d7..aeb1921 100644 --- a/src/libjin-lua/modules/graphics/je_lua_graphics.cpp +++ b/src/libjin-lua/modules/graphics/je_lua_graphics.cpp @@ -9,6 +9,7 @@ #include "je_lua_sprite.h" #include "je_lua_spritesheet.h" #include "je_lua_bitmap.h" +#include "je_lua_mesh.h" #include "je_lua_ttf.h" #include "je_lua_ttf_data.h" #include "je_lua_texture.h" @@ -398,6 +399,22 @@ namespace JinEngine sprite->render(x, y, sx, sy, r); } + LUA_IMPLEMENT void l_draw_mesh(lua_State* L) + { + if (!luax_istype(L, 1, Jin_Lua_Mesh)) + return; + LuaObject* obj= (LuaObject*)luax_toudata(L, 1); + Mesh* mesh = obj->getObject<Mesh>(); + 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); + mesh->render(x, y, sx, sy, r, ox, oy); + } + LUA_IMPLEMENT int l_draw(lua_State* L) { if (luax_istype(L, 1, Jin_Lua_Texture)) @@ -410,6 +427,8 @@ namespace JinEngine l_draw_page(L); else if (luax_istype(L, 1, Jin_Lua_Sprite)) l_draw_sprite(L); + else if (luax_istype(L, 1, Jin_Lua_Mesh)) + l_draw_mesh(L); else { luax_typerror(L, 1, "texture or canvas"); @@ -455,7 +474,9 @@ namespace JinEngine else { luax_typerror(L, 1, "texture or canvas"); + return 1; } + return 0; } /* print(string, x, y, lineheight, spacing) */ @@ -900,6 +921,12 @@ namespace JinEngine return 1; } + LUA_IMPLEMENT int l_newMesh(lua_State* L) + { + LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Mesh, new Shared(new Mesh())); + return 1; + } + /* setFont(font) */ LUA_IMPLEMENT int l_setFont(lua_State* L) { @@ -981,6 +1008,7 @@ namespace JinEngine luaopen_Animation(L); luaopen_Animator(L); luaopen_ParticleSystem(L); + luaopen_Mesh(L); luaL_Reg methods[] = { /* window */ @@ -1006,6 +1034,7 @@ namespace JinEngine { "newAnimation", l_newAnimation }, { "newAnimator", l_newAnimator }, { "newParticleSystem", l_newParticleSystem }, + { "newMesh", l_newMesh }, /* render */ { "setClearColor", l_setClearColor }, { "clear", l_clear }, diff --git a/src/libjin-lua/modules/graphics/je_lua_mesh.cpp b/src/libjin-lua/modules/graphics/je_lua_mesh.cpp new file mode 100644 index 0000000..fc42096 --- /dev/null +++ b/src/libjin-lua/modules/graphics/je_lua_mesh.cpp @@ -0,0 +1,87 @@ +#include "common/je_lua_object.h" +#include "common/je_lua_common.h" +#include "libjin/jin.h" +#include "je_lua_mesh.h" +#include "je_lua_texture.h" + +using namespace JinEngine::Math; +using namespace JinEngine::Graphics; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Mesh = "Mesh"; + + LUA_IMPLEMENT inline Mesh* checkMesh(lua_State* L) + { + LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Mesh); + return luaObj->getObject<Mesh>(); + } + + LUA_IMPLEMENT int l_getBound(lua_State* L) + { + Mesh* mesh = checkMesh(L); + BBox bbox = mesh->getBound(); + luax_pushnumber(L, bbox.l); + luax_pushnumber(L, bbox.r); + luax_pushnumber(L, bbox.t); + luax_pushnumber(L, bbox.b); + return 4; + } + + LUA_IMPLEMENT int l_setGraphic(lua_State* L) + { + LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Mesh); + Mesh* mesh = luaObj->getObject<Mesh>(); + luaObj->removeDependency((int)MeshDependency::DEP_GRAPHIC); + LuaObject* obj = (LuaObject*)luax_checktype(L, 2, Jin_Lua_Texture); + mesh->setGraphic(obj->getObject<Texture>()); + luaObj->setDependency((int)MeshDependency::DEP_GRAPHIC, obj); + return 0; + } + + // x, y, u, v, color_table + LUA_IMPLEMENT int l_pushVertex(lua_State* L) + { + Mesh* mesh = checkMesh(L); + float x = luax_checknumber(L, 2); + float y = luax_checknumber(L, 3); + float u = luax_checknumber(L, 4); + float v = luax_checknumber(L, 5); + if (!luax_istable(L, 6)) + { + luax_typerror(L, 6, "color table"); + return 1; + } + 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); + mesh->pushVertex(x, y, u, v, c); + return 0; + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Mesh); + luaObj->release(); + return 0; + } + + LUA_EXPORT void luaopen_Mesh(lua_State* L) + { + luaL_Reg methods[] = { + { "__gc", l_gc }, + { "getBound", l_getBound }, + { "setGraphic", l_setGraphic }, + { "pushVertex", l_pushVertex }, + { 0, 0 } + }; + luax_newtype(L, Jin_Lua_Mesh, methods); + } + + } // namespace Lua +} // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin-lua/modules/graphics/je_lua_mesh.h b/src/libjin-lua/modules/graphics/je_lua_mesh.h new file mode 100644 index 0000000..b3fa00e --- /dev/null +++ b/src/libjin-lua/modules/graphics/je_lua_mesh.h @@ -0,0 +1,21 @@ +#ifndef __JE_LUA_MESH_H__ +#define __JE_LUA_MESH_H__ + +namespace JinEngine +{ + namespace Lua + { + + enum class MeshDependency + { + DEP_GRAPHIC = 1, + }; + + extern const char* Jin_Lua_Mesh; + + void luaopen_Mesh(lua_State* L); + + } +} + +#endif
\ No newline at end of file |