aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/graphics/l_spritesheet.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin-lua/modules/graphics/l_spritesheet.cpp')
-rw-r--r--src/libjin-lua/modules/graphics/l_spritesheet.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/libjin-lua/modules/graphics/l_spritesheet.cpp b/src/libjin-lua/modules/graphics/l_spritesheet.cpp
new file mode 100644
index 0000000..e8400c3
--- /dev/null
+++ b/src/libjin-lua/modules/graphics/l_spritesheet.cpp
@@ -0,0 +1,118 @@
+#include <vector>
+
+#include "common/l_object.h"
+#include "common/l_common.h"
+#include "libjin/jin.h"
+#include "l_sprite.h"
+#include "l_spritesheet.h"
+
+using namespace std;
+using namespace JinEngine::Math;
+using namespace JinEngine::Graphics;
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ const char* Jin_Lua_SpriteSheet = "SpriteSheet";
+
+ LUA_IMPLEMENT int l_gc(lua_State* L)
+ {
+ LuaObject* luaSSheet = (LuaObject*)luax_checktype(L, 1, Jin_Lua_SpriteSheet);
+ luaSSheet->release();
+ return 0;
+ }
+
+ LUA_IMPLEMENT int l_newSprite(lua_State* L)
+ {
+ LuaObject* luaSSheet = (LuaObject*)luax_checktype(L, 1, Jin_Lua_SpriteSheet);
+ SpriteSheet* sheet = luaSSheet->getObject<SpriteSheet>();
+ Quad quad;
+ quad.x = luax_rawgetnumberthenpop(L, 2, 1);
+ quad.y = luax_rawgetnumberthenpop(L, 2, 2);
+ quad.w = luax_rawgetnumberthenpop(L, 2, 3);
+ quad.h = luax_rawgetnumberthenpop(L, 2, 4);
+ Sprite* spr = nullptr;
+ if (luax_gettop(L) >= 4)
+ {
+ float ox = luax_checknumber(L, 3);
+ float oy = luax_checknumber(L, 4);
+ spr = sheet->createSprite(quad, ox, oy);
+ }
+ else if (luax_gettop(L) == 3)
+ {
+ int o = luax_checkinteger(L, 3);
+ Origin origin;
+ origin = static_cast<Origin>(o);
+ spr = sheet->createSprite(quad, origin);
+ }
+ Shared* shrSprite = new Shared(spr);
+ LuaObject* luaSprite = luax_newinstance(L, Jin_Lua_Sprite, shrSprite);
+ luaSprite->setDependency((int)SpriteDependency::DEP_SPRITESHEET, luaSSheet);
+ return 1;
+ }
+
+ // {} = newSprites
+ LUA_IMPLEMENT int l_newSprites(lua_State* L)
+ {
+ LuaObject* luaSS = (LuaObject*)luax_checktype(L, 1, Jin_Lua_SpriteSheet);
+ SpriteSheet* ss = luaSS->getObject<SpriteSheet>();
+ int count = luax_checkinteger(L, 2);
+ int r = luax_checkinteger(L, 3);
+ int c = luax_checkinteger(L, 4);
+ int w = luax_checkinteger(L, 5);
+ int h = luax_checkinteger(L, 6);
+ vector<Sprite*> sprs;
+ int argc = luax_gettop(L);
+ if (argc == 6)
+ {
+ sprs = ss->createSprites(count, r, c, w, h, Origin::TOPLEFT);
+ }
+ else if (argc >= 8)
+ {
+ int ox = luax_checkinteger(L, 7);
+ int oy = luax_checkinteger(L, 8);
+ int offx = luax_optinteger(L, 9, 0);
+ int offy = luax_optinteger(L, 10, 0);
+ sprs = ss->createSprites(count, r, c, w, h, ox, oy, offx, offy);
+ }
+ else if (argc >= 7)
+ {
+ int o = luax_checkinteger(L, 7);
+ Origin origin = static_cast<Origin>(o);
+ int offx = luax_optinteger(L, 8, 0);
+ int offy = luax_optinteger(L, 9, 0);
+ sprs = ss->createSprites(count, r, c, w, h, origin, offx, offy);
+ }
+ else
+ {
+ luax_error(L, "No matched override function.");
+ return 1;
+ }
+ luax_newtable(L);
+ LuaObject* graphic = luaSS->getDependency((int)SpriteSheetDependency::DEP_GRAPHIC);
+ for (int i = 0; i < sprs.size(); ++i)
+ {
+ Sprite* spr = sprs[i];
+ Shared* shrSpr = new Shared(spr);
+ LuaObject* luaSpr = (LuaObject*)luax_newinstance(L, Jin_Lua_Sprite, shrSpr);
+ luaSpr->setDependency((int)SpriteDependency::DEP_GRAPHIC, graphic);
+ luax_rawseti(L, -2, i + 1);
+ }
+ return 1;
+ }
+
+ LUA_EXPORT void luaopen_SpriteSheet(lua_State* L)
+ {
+ luaL_Reg methods[] = {
+ { "__gc", l_gc },
+ { "newSprite", l_newSprite },
+ { "newSprites", l_newSprites },
+ { 0, 0 }
+ };
+ luax_newtype(L, Jin_Lua_SpriteSheet, methods);
+ }
+
+ }
+} \ No newline at end of file