aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/video/iimage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/lua_api/video/iimage.cpp')
-rw-r--r--src/client/lua_api/video/iimage.cpp59
1 files changed, 48 insertions, 11 deletions
diff --git a/src/client/lua_api/video/iimage.cpp b/src/client/lua_api/video/iimage.cpp
index b832783..3090057 100644
--- a/src/client/lua_api/video/iimage.cpp
+++ b/src/client/lua_api/video/iimage.cpp
@@ -10,34 +10,71 @@ 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");
+ //printf("Attempting to create new iimage\n");
long w,h;
popvector2i(L,&w,&h);
int format = lua_tonumber(L,-1);
lua_pop(L,1);
- printf("All data collected, creating...\n");
+ //printf("All data collected, creating...\n");
IImage* img = driver->createImage((irr::video::ECOLOR_FORMAT)format,irr::core::dimension2d<u32>(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");
+ //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");
}