aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin-lua/modules')
-rw-r--r--src/libjin-lua/modules/graphics/je_lua_graphics.cpp29
-rw-r--r--src/libjin-lua/modules/graphics/je_lua_mesh.cpp87
-rw-r--r--src/libjin-lua/modules/graphics/je_lua_mesh.h21
3 files changed, 137 insertions, 0 deletions
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