aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/modules/graphics/l_spritesheet.cpp
blob: b720723daaa819e08b556975d92f1f7e6193f2c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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);
		}

	}
}