aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/video/iimage.cpp
blob: b83278398d1257680021d6e292ad0c09da0e0392 (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
#include "iimage.hpp"
#include <irrlicht.h>

#include "../../../shared/lua_api/common.h"

using namespace irr;
using namespace video;

extern IrrlichtDevice* device;
extern IVideoDriver* driver;

//newiimage(ECOLOR_FORMAT,{width,height})
int newiimage(lua_State* L){
	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");
	IImage* img = driver->createImage((irr::video::ECOLOR_FORMAT)format,irr::core::dimension2d<u32>(w,h));
	lua_pushlightuserdata(L,img);
	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)
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}
	long x,y;
	popvector2i(L,&x,&y);//{ud_iimage}
	IImage* img = (IImage*)lua_touserdata(L,-1);//{ud_iimage}
	img->setPixel(x,y,SColor(a,r,g,b),sb);
	lua_pop(L,1);
	return 0;
}

static const luaL_reg iimage_m[] = {
  {"setPixel",        setiimagepixel},
  {0,0},
};

#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");//{}

	set_const(L,ECF_A1R5G5B5);
	set_const(L,ECF_R5G6B5);
	set_const(L,ECF_A8R8G8B8);
	set_const(L,ECF_R16F);
	set_const(L,ECF_G16R16F);
	set_const(L,ECF_A16B16G16R16F);
	set_const(L,ECF_R32F);
	set_const(L,ECF_G32R32F);
	set_const(L,ECF_A32B32G32R32F);
	set_const(L,ECF_UNKNOWN);

	lua_pushcfunction(L,newiimage);//{},newiimage()
	lua_setfield(L,-2,"newiimage");//{}
	lua_pop(L,1);//

	luaL_newmetatable(L,"iimage");//{m_iimage}
	
	lua_newtable(L);//{m_iimage},{}
	luaL_register(L,NULL,iimage_m);//{m_iimage},{iimage}

	lua_setfield(L,-2,"__index");//{m_iimage}

	lua_pop(L,1);//
	
	printf("registered iimage!\n");
}