aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/load_video.cpp
blob: 73be55068d06e97c9a333fe439a8915cd9f51ab3 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <irrlicht.h>
#include "video/smaterial.hpp"
#include "video/itexture.hpp"
#include "video/iimage.hpp"
#include "video/draw.hpp"

#include <shared/lua_api/common.hpp>

using namespace irr;
using namespace video;
using namespace core;

extern IrrlichtDevice* device;
extern IVideoDriver* driver;

//video.drawtexture
//{texture},{x,y}
//{texture},{x,y},{sourcerect},{color},use_alpha
int draw2dimage(lua_State* L){
	int nargs = lua_gettop(L);
	//printf("Drawing a 2d image\n");
	if(nargs == 2){
		//printf("2-argument version\n");
		long x,y;
		popvector2i(L,&x,&y);
		lua_getfield(L,-1,"texture");
		ITexture *tex = (ITexture*)lua_touserdata(L,-1);
		lua_pop(L,2);
		driver->draw2DImage(tex,position2d<s32>(x,y));
	}else if(nargs == 5){
		//printf("5-argument version\n");
		int usealpha = lua_toboolean(L,-1);
		lua_pop(L,1);
		//printf("Got usealpha: %d\n",usealpha);
		long r,g,b,a;
		popvector4i(L,&r,&g,&b,&a);
		long ssx,ssy,sex,sey;
		poprecti(L,&ssx,&ssy,&sex,&sey);
		long x,y;
		popvector2i(L,&x,&y);
		lua_getfield(L,-1,"texture");
		ITexture *tex = (ITexture*)lua_touserdata(L,-1);
		if(tex == NULL){
			lua_pushstring(L,"Tried to draw a NULL texture");
			lua_error(L);
		}
		lua_pop(L,2);
		rect<s32> clipedto;
		driver->draw2DImage(
				tex,
				position2d<s32>(x,y),
				rect<s32>(ssx,ssy,sex,sey),
				NULL,
				SColor(a,r,g,b),
				usealpha == 1
		);
	}else{
		lua_pushstring(L,"Incorrect number of arguments to video.drawtexture() (expected 2 or 6)");
		lua_error(L);
	}
	return 0;
}

//{sx,sy},{ex,ey},{color}
int draw2dline(lua_State* L){
	long sx,sy,ex,ey;
	long r,g,b,a;
	popvector4i(L,&r,&g,&b,&a);
	popvector2i(L,&ex,&ey);
	popvector2i(L,&sx,&sy);

	driver->draw2DLine(position2d<s32>(sx,sy),position2d<s32>(ex,ey),SColor(a,r,g,b));

	return 0;
}

//{sx,sy,sz},{ex,ey,ez},{color}
int draw3dline(lua_State* L){
	double sx,sy,sz;
	double ex,ey,ez;
	long r,g,b,a;
	popvector4i(L,&r,&g,&b,&a);
	popvector3d(L,&ex,&ey,&ez);
	popvector3d(L,&sx,&sy,&sz);

	driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
	driver->draw3DLine(vector3df(sx,sy,sz),vector3df(ex,ey,ez),SColor(a,r,g,b));

	return 0;
}

void load_videofuncs(lua_State* L){
	//printf("Loading video libraries...\n");
	lua_newtable(L);//{}
	lua_setglobal(L,"video");//

	lua_getglobal(L,"video");//{}
	lua_pushcfunction(L,draw2dimage);//{},draw2dimage()
	lua_setfield(L,-2,"drawtexture");//{}
	lua_pushcfunction(L,draw2dline);//{},draw2dline()
	lua_setfield(L,-2,"draw2dline");//{}
	lua_pushcfunction(L,draw3dline);
	lua_setfield(L,-2,"draw3dline");
	lua_pop(L,1);//

	smaterial_register(L);
	itexture_register(L);
	iimage_register(L);
	draw_register(L);
	
}