From 1b773ad2c250e09c09c065eb3eec64bfebde09ca Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 18 May 2018 14:39:38 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=94=B9userdata=E5=88=9B=E5=BB=BA=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/script/graphics/luaopen_graphics.cpp | 43 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'src/script/graphics/luaopen_graphics.cpp') 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; -- cgit v1.1-26-g67d0