aboutsummaryrefslogtreecommitdiff
path: root/src/script/graphics/luaopen_graphics.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-05-18 14:39:38 +0800
committerchai <chaifix@163.com>2018-05-18 14:39:38 +0800
commit1b773ad2c250e09c09c065eb3eec64bfebde09ca (patch)
treeb3f1f367694d5f86cc0caa7f4eea2e6a1d3424c5 /src/script/graphics/luaopen_graphics.cpp
parent91a592da979827b1735901388dba8712e6e3ecf3 (diff)
修改userdata创建方式
Diffstat (limited to 'src/script/graphics/luaopen_graphics.cpp')
-rw-r--r--src/script/graphics/luaopen_graphics.cpp43
1 files changed, 22 insertions, 21 deletions
diff --git a/src/script/graphics/luaopen_graphics.cpp b/src/script/graphics/luaopen_graphics.cpp
index 80acc94..0d240fe 100644
--- a/src/script/graphics/luaopen_graphics.cpp
+++ b/src/script/graphics/luaopen_graphics.cpp
@@ -76,9 +76,6 @@ namespace lua
*/
static int l_newImage(lua_State* L)
{
- Image* img = (Image*)luax_newinstance(L, TYPE_IMAGE, sizeof(Image));
- // pseudo constructor
- img->init();
Filesystem* fs = Filesystem::get();
const char* f = luax_checkstring(L, 1);
if (!fs->exists(f))
@@ -87,8 +84,11 @@ namespace lua
exit(1);
}
Buffer b;
- fs->read(f, &b);
- img->loadb((const char*)b.data, b.size);
+ fs->read(f, &b);
+
+ Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy));
+ Image* img = new Image((const char*)b.data, b.size);
+ proxy->bind(img);
return 1;
}
@@ -98,10 +98,10 @@ namespace lua
*/
static int l_newShader(lua_State* L)
{
- JSLProgram* j = (JSLProgram*)luax_newinstance(L, TYPE_JSL, sizeof(JSLProgram));
- const char* modestr = luax_checkstring(L, 1);
- j->init(modestr);
-
+ Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_JSL, sizeof(JSLProgram));
+ const char* program = luax_checkstring(L, 1);
+ JSLProgram* jsl = new JSLProgram(program);
+ proxy->bind(jsl);
return 1;
}
@@ -111,10 +111,11 @@ namespace lua
*/
static int l_newCanvas(lua_State* L)
{
- Canvas* cvs = (Canvas*)luax_newinstance(L, TYPE_CANVAS, sizeof(Canvas));
int w = luax_checknumber(L, 1);
int h = luax_checknumber(L, 2);
- cvs->init(w, h);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_CANVAS, sizeof(Proxy));
+ Canvas* cvs = new Canvas(w, h);
+ proxy->bind(cvs);
return 1;
}
@@ -155,14 +156,14 @@ namespace lua
float r = luax_optnumber(L, 6, 0);
if (luax_istype(L, 1, TYPE_IMAGE))
{
- /* is image */
- Image* p = (Image*)luax_toudata(L, 1);
- p->draw(x, y, sx, sy, r);
+ Proxy* proxy = (Proxy*)luax_toudata(L, 1);
+ Image* img = (Image*)proxy->object;
+ img->draw(x, y, sx, sy, r);
}
else if (luax_istype(L, 1, TYPE_CANVAS))
{
- /* is canvas */
- Canvas* p = (Canvas*)luax_toudata(L, 1);
+ Proxy* proxy = (Proxy*)luax_toudata(L, 1);
+ Canvas* p = (Canvas*)proxy->object;
p->draw(x, y, sx, sy, r);
}
else
@@ -212,7 +213,8 @@ namespace lua
Canvas::unbind();
return 0;
}
- Canvas* c = (Canvas*)luax_checktype(L, 1, TYPE_CANVAS);
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS);
+ Canvas* c = (Canvas*)proxy->object;
c->bind();
return 0;
}
@@ -232,13 +234,12 @@ namespace lua
}
if (luax_istype(L, 1, TYPE_JSL))
{
- /* is image */
- JSLProgram* jsl = (JSLProgram*)luax_toudata(L, 1);
- jsl->use();
+ Proxy* proxy = (Proxy*)luax_toudata(L, 1);
+ JSLProgram* jsl = (JSLProgram*)proxy->object;
+ jsl->use();
}
else
{
- /* wrong type */
luax_typerror(L, 1, "JSL shader");
}
return 0;