From 06d3e8182d018ca613f177f6ff7a3bbb6494cc79 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Mon, 28 May 2018 17:07:04 -0400 Subject: Various additions Various additions, updates, and bugfixes while making mahjong solitare. --- src/client/lua_api/video/iimage.cpp | 53 +++++++++++++++++++++++++++++------ src/client/lua_api/video/itexture.cpp | 37 ++++++++++++++++++++---- 2 files changed, 76 insertions(+), 14 deletions(-) (limited to 'src/client/lua_api/video') diff --git a/src/client/lua_api/video/iimage.cpp b/src/client/lua_api/video/iimage.cpp index b832783..586085e 100644 --- a/src/client/lua_api/video/iimage.cpp +++ b/src/client/lua_api/video/iimage.cpp @@ -10,7 +10,7 @@ using namespace video; extern IrrlichtDevice* device; extern IVideoDriver* driver; -//newiimage(ECOLOR_FORMAT,{width,height}) +//newiimage(ECOLOR_FORMAT,{width,height}) -> {image} int newiimage(lua_State* L){ printf("Attempting to create new iimage\n"); long w,h; @@ -19,25 +19,62 @@ int newiimage(lua_State* L){ lua_pop(L,1); printf("All data collected, creating...\n"); IImage* img = driver->createImage((irr::video::ECOLOR_FORMAT)format,irr::core::dimension2d(w,h)); + lua_newtable(L); lua_pushlightuserdata(L,img); + lua_setfield(L,-2,"image"); luaL_getmetatable(L,"iimage"); lua_setmetatable(L,-2); printf("Everything sets up, returning\n"); return 1; } -//setPixel({x,y},{r,g,b,a},bool_shouldblend) +//newiimagefromfile("/path/to/file") -> {image} +int newiimagefromfile(lua_State* L){ + printf("Creating new iimage from file"); + const char* strpath = lua_tostring(L,-1); + lua_pop(L,1); + int numloaders = driver->getImageLoaderCount(); + bool hasloaded = false; + IImage* img; + for(int j = 0; j < numloaders; j++){ + IImageLoader* loader = driver->getImageLoader(j); + io::IReadFile* f = device->getFileSystem()->createAndOpenFile(strpath); + if(!loader->isALoadableFileExtension(strpath)) + continue; + if(!loader->isALoadableFileFormat(f)) + continue; + hasloaded = true; + img = loader->loadImage(f); + } + if(!hasloaded){ + lua_pushstring(L,"Failed to load file"); + lua_error(L); + } + if(!img){ + lua_pushstring(L,"Failed to load image"); + lua_error(L); + } + lua_newtable(L); + lua_pushlightuserdata(L,img); + lua_setfield(L,-2,"image"); + luaL_getmetatable(L,"iimage"); + lua_setmetatable(L,-2); + printf("IImage made, returning!"); + return 1; +} + +//setPixel({self},{x,y},{r,g,b,a},bool_shouldblend) int setiimagepixel(lua_State* L){ - printf("Setpixel called\n"); bool sb = lua_toboolean(L,-1);//{ud_iimage},{x,y},{r,g,b,a},bool_shouldblend lua_pop(L,1);//{ud_iimage},{x,y},{r,g,b,a} - long r,g,b,a; - popvector4i(L,&r,&g,&b,&a);//{ud_iimage},{x,y} + double r,g,b,a; + popvector4d(L,&r,&g,&b,&a);//{ud_iimage},{x,y} long x,y; popvector2i(L,&x,&y);//{ud_iimage} + lua_getfield(L,-1,"image"); IImage* img = (IImage*)lua_touserdata(L,-1);//{ud_iimage} img->setPixel(x,y,SColor(a,r,g,b),sb); - lua_pop(L,1); + lua_pop(L,2); return 0; } @@ -49,7 +86,6 @@ static const luaL_reg iimage_m[] = { #define set_const(l,x) lua_pushstring(l,#x);lua_pushinteger(l,x);lua_settable(l,-3); void iimage_register(lua_State* L){ - printf("Registering iimage...\n"); driver = device->getVideoDriver(); lua_getglobal(L,"video");//{} @@ -66,6 +102,8 @@ void iimage_register(lua_State* L){ lua_pushcfunction(L,newiimage);//{},newiimage() lua_setfield(L,-2,"newiimage");//{} + lua_pushcfunction(L,newiimagefromfile);//{},newiimagefromfile() + lua_setfield(L,-2,"newiimagefromfile");//{} lua_pop(L,1);// luaL_newmetatable(L,"iimage");//{m_iimage} @@ -77,5 +115,4 @@ void iimage_register(lua_State* L){ lua_pop(L,1);// - printf("registered iimage!\n"); } diff --git a/src/client/lua_api/video/itexture.cpp b/src/client/lua_api/video/itexture.cpp index a7f4652..599a448 100644 --- a/src/client/lua_api/video/itexture.cpp +++ b/src/client/lua_api/video/itexture.cpp @@ -14,20 +14,42 @@ using namespace video; extern IrrlichtDevice* device; extern IVideoDriver* driver; -//newtexture(string name,IImage* image) +//newtexture(string name,{image}) -> {texture} int newitexture(lua_State* L){ + printf("About to create new texture\n"); + lua_getfield(L,-1,"image");//"name",{image},image* IImage* im = (IImage*) lua_touserdata(L,-1); - lua_pop(L,1); + lua_pop(L,2);//"name" const char* name = lua_tostring(L,-1); - lua_pop(L,1); + lua_pop(L,1);// + printf("About to create texture\n"); ITexture* tex = driver->addTexture(name,im); if(!tex){ lua_pushstring(L,"Failed to create texture!"); lua_error(L); } + lua_newtable(L); + lua_pushlightuserdata(L,tex); + lua_setfield(L,-2,"texture"); + + return 1; +} + +//newtexturefromfile(string file_name) -> {texture} +int newitexturefromfile(lua_State* L){ + const char* strpath = lua_tostring(L,-1); + lua_pop(L,1); + ITexture* tex = driver->getTexture(strpath); + if(!tex){ + lua_pushstring(L,"Failed to create texture!"); + lua_error(L); + } + + lua_newtable(L); lua_pushlightuserdata(L,tex); + lua_setfield(L,-2,"texture"); return 1; } @@ -35,7 +57,10 @@ int newitexture(lua_State* L){ void itexture_register(lua_State* L){ - lua_getglobal(L,"video"); - lua_pushcfunction(L,newitexture); - lua_setfield(L,-2,"newtexture"); + lua_getglobal(L,"video");//{} + lua_pushcfunction(L,newitexture);//{},newitexture + lua_setfield(L,-2,"newtexture");//{} + lua_pushcfunction(L,newitexturefromfile); + lua_setfield(L,-2,"newtexturefromfile"); + lua_pop(L,1); } -- cgit v1.2.3-70-g09d2