Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 lines
13 KiB

  1. #include "stdafx.h"
  2. #include "sqplus.h"
  3. #include "DXSquirrel_Binds.h"
  4. _IMPL_NATIVE_CONSTRUCTION(Vector3,D3DXVECTOR3);
  5. _MEMBER_FUNCTION_IMPL(Vector3,constructor)
  6. {
  7. D3DXVECTOR3 temp;
  8. D3DXVECTOR3 *newv = NULL;
  9. StackHandler sa(v);
  10. int nparams = sa.GetParamCount();
  11. switch(nparams) {
  12. case 1:
  13. temp.x = 0;
  14. temp.y = 0;
  15. temp.z = 0;
  16. break;
  17. case 2:
  18. if(sa.GetType(2) == OT_INSTANCE) {
  19. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  20. if(vec) temp = *vec;
  21. else return sa.ThrowError(_T("Vector3() invalid instance type"));
  22. }
  23. break;
  24. case 4:
  25. temp.x = sa.GetFloat(2);
  26. temp.y = sa.GetFloat(3);
  27. temp.z = sa.GetFloat(4);
  28. break;
  29. default:
  30. return sa.ThrowError(_T("Vector3() wrong parameters"));
  31. }
  32. newv = new D3DXVECTOR3(temp);
  33. return construct_Vector3(newv);
  34. }
  35. _MEMBER_FUNCTION_IMPL(Vector3,_set)
  36. {
  37. StackHandler sa(v);
  38. _CHECK_SELF(D3DXVECTOR3,Vector3);
  39. const SQChar *s = sa.GetString(2);
  40. int index = s?s[0]:sa.GetInt(2);
  41. switch(index) {
  42. case 0: case 'x': case 'r':
  43. return sa.Return(self->x = sa.GetFloat(3));
  44. break;
  45. case 1: case 'y': case 'g':
  46. return sa.Return(self->y = sa.GetFloat(3));
  47. break;
  48. case 2: case 'z': case 'b':
  49. return sa.Return(self->z = sa.GetFloat(3));
  50. break;
  51. }
  52. return SQ_ERROR;
  53. }
  54. _MEMBER_FUNCTION_IMPL(Vector3,_get)
  55. {
  56. StackHandler sa(v);
  57. _CHECK_SELF(D3DXVECTOR3,Vector3);
  58. const SQChar *s = sa.GetString(2);
  59. if(s && (s[1] != 0))
  60. return SQ_ERROR;
  61. int index = s && (s[1] == 0)?s[0]:sa.GetInt(2);
  62. switch(index) {
  63. case 0: case 'x': case 'r': return sa.Return(self->x); break;
  64. case 1: case 'y': case 'g': return sa.Return(self->y); break;
  65. case 2: case 'z': case 'b': return sa.Return(self->z); break;
  66. }
  67. return SQ_ERROR;
  68. }
  69. _MEMBER_FUNCTION_IMPL(Vector3,_nexti)
  70. {
  71. StackHandler sa(v);
  72. _CHECK_SELF(D3DXVECTOR3,Vector3);
  73. int index = -1;
  74. if(sa.GetType(2) == OT_NULL) {
  75. index = -1;
  76. }else {
  77. const SQChar *s = sa.GetString(2);
  78. if(s && (s[1] != 0))
  79. return SQ_ERROR;
  80. index = s && (s[1] == 0)?s[0]:sa.GetInt(2);
  81. }
  82. switch(index) {
  83. case 0xFFFFFFFF: return sa.Return(_T("x"));
  84. case 0: case 'x': case 'r': return sa.Return(_T("y"));
  85. case 1: case 'y': case 'g': return sa.Return(_T("z"));
  86. case 2: case 'z': case 'b': return sa.Return();
  87. }
  88. return sa.Return(_T("invalid index"));
  89. }
  90. _MEMBER_FUNCTION_IMPL(Vector3,_cmp)
  91. {
  92. StackHandler sa(v);
  93. _CHECK_SELF(D3DXVECTOR3,Vector3);
  94. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  95. if((*self) == (*vec))
  96. return sa.Return(0);
  97. if((*self) < (*vec))
  98. return sa.Return(-1);
  99. return sa.Return(1);
  100. }
  101. _MEMBER_FUNCTION_IMPL(Vector3,_add)
  102. {
  103. StackHandler sa(v);
  104. _CHECK_SELF(D3DXVECTOR3,Vector3);
  105. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  106. D3DXVECTOR3 tv = (*self)+(*vec);
  107. SquirrelObject so = new_Vector3(tv);
  108. return sa.Return(so);
  109. }
  110. _MEMBER_FUNCTION_IMPL(Vector3,_sub)
  111. {
  112. StackHandler sa(v);
  113. _CHECK_SELF(D3DXVECTOR3,Vector3);
  114. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  115. D3DXVECTOR3 tv = (*self)-(*vec);
  116. SquirrelObject so = new_Vector3(tv);
  117. return sa.Return(so);
  118. }
  119. _MEMBER_FUNCTION_IMPL(Vector3,_mul)
  120. {
  121. StackHandler sa(v);
  122. _CHECK_SELF(D3DXVECTOR3,Vector3);
  123. D3DXVECTOR3 tv = (*self)*sa.GetFloat(2);
  124. SquirrelObject so = new_Vector3(tv);
  125. return sa.Return(so);
  126. }
  127. _MEMBER_FUNCTION_IMPL(Vector3,_div)
  128. {
  129. StackHandler sa(v);
  130. _CHECK_SELF(D3DXVECTOR3,Vector3);
  131. D3DXVECTOR3 tv = (*self)/sa.GetFloat(2);
  132. SquirrelObject so = new_Vector3(tv);
  133. return sa.Return(so);
  134. }
  135. _MEMBER_FUNCTION_IMPL(Vector3,DotProduct)
  136. {
  137. StackHandler sa(v);
  138. _CHECK_SELF(D3DXVECTOR3,Vector3);
  139. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  140. return sa.Return(D3DXVec3Dot(self,vec));
  141. }
  142. _MEMBER_FUNCTION_IMPL(Vector3,CrossProduct)
  143. {
  144. StackHandler sa(v);
  145. _CHECK_SELF(D3DXVECTOR3,Vector3);
  146. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  147. D3DXVECTOR3 ret;
  148. D3DXVec3Cross(&ret,self,vec);
  149. SquirrelObject so = new_Vector3(ret);
  150. return sa.Return(so);
  151. }
  152. _MEMBER_FUNCTION_IMPL(Vector3,SquareDistance)
  153. {
  154. StackHandler sa(v);
  155. _CHECK_SELF(D3DXVECTOR3,Vector3);
  156. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  157. D3DXVECTOR3 tmp = *self - *vec;
  158. return sa.Return(D3DXVec3LengthSq(&tmp));
  159. }
  160. _MEMBER_FUNCTION_IMPL(Vector3,Distance)
  161. {
  162. StackHandler sa(v);
  163. _CHECK_SELF(D3DXVECTOR3,Vector3);
  164. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  165. D3DXVECTOR3 tmp = *self - *vec;
  166. return sa.Return(D3DXVec3Length(&tmp));
  167. }
  168. _MEMBER_FUNCTION_IMPL(Vector3,Length)
  169. {
  170. StackHandler sa(v);
  171. _CHECK_SELF(D3DXVECTOR3,Vector3);
  172. return sa.Return(D3DXVec3Length(self));
  173. }
  174. _MEMBER_FUNCTION_IMPL(Vector3,SquareLength)
  175. {
  176. StackHandler sa(v);
  177. _CHECK_SELF(D3DXVECTOR3,Vector3);
  178. return sa.Return(D3DXVec3LengthSq(self));
  179. }
  180. _MEMBER_FUNCTION_IMPL(Vector3,Normalize)
  181. {
  182. _CHECK_SELF(D3DXVECTOR3,Vector3);
  183. D3DXVec3Normalize(self,self);
  184. return 0;
  185. }
  186. _MEMBER_FUNCTION_IMPL(Vector3,GetNormalized)
  187. {
  188. StackHandler sa(v);
  189. _CHECK_SELF(D3DXVECTOR3,Vector3);
  190. D3DXVECTOR3 tmp;
  191. D3DXVec3Normalize(&tmp,self);
  192. SquirrelObject so = new_Vector3(tmp);
  193. return sa.Return(so);
  194. }
  195. _BEGIN_CLASS(Vector3)
  196. _MEMBER_FUNCTION(Vector3,constructor,-1,_T(".n|xnn"))
  197. _MEMBER_FUNCTION(Vector3,_set,3,_T("xs|n"))
  198. _MEMBER_FUNCTION(Vector3,_get,2,_T("xs|n"))
  199. _MEMBER_FUNCTION(Vector3,_add,2,_T("xx"))
  200. _MEMBER_FUNCTION(Vector3,_sub,2,_T("xx"))
  201. _MEMBER_FUNCTION(Vector3,_mul,2,_T("xn"))
  202. _MEMBER_FUNCTION(Vector3,_div,2,_T("xn"))
  203. _MEMBER_FUNCTION(Vector3,_nexti,2,_T("x"))
  204. _MEMBER_FUNCTION(Vector3,_cmp,2,_T("xx"))
  205. _MEMBER_FUNCTION(Vector3,DotProduct,2,_T("xx"))
  206. _MEMBER_FUNCTION(Vector3,CrossProduct,2,_T("xx"))
  207. _MEMBER_FUNCTION(Vector3,SquareDistance,2,_T("xx"))
  208. _MEMBER_FUNCTION(Vector3,Distance,2,_T("xx"))
  209. _MEMBER_FUNCTION(Vector3,Length,1,_T("x"))
  210. _MEMBER_FUNCTION(Vector3,SquareLength,1,_T("x"))
  211. _MEMBER_FUNCTION(Vector3,Normalize,1,_T("x"))
  212. _MEMBER_FUNCTION(Vector3,GetNormalized,1,_T("x"))
  213. _END_CLASS(Vector3)
  214. //////////////////////////////////////////////////////////////////////////
  215. //////////////////////////////////////////////////////////////////////////
  216. //////////////////////////////////////////////////////////////////////////
  217. _IMPL_NATIVE_CONSTRUCTION(Matrix,D3DXMATRIX);
  218. _MEMBER_FUNCTION_IMPL(Matrix,constructor)
  219. {
  220. D3DXMATRIX temp;
  221. D3DXMATRIX *newm = NULL;
  222. StackHandler sa(v);
  223. switch(sa.GetParamCount()) {
  224. case 1:
  225. D3DXMatrixIdentity(&temp);
  226. break;
  227. case 2:
  228. if(sa.GetType(2) == OT_INSTANCE) {
  229. _CHECK_INST_PARAM(mat,2,D3DXMATRIX,Matrix);
  230. if(mat) temp = *mat;
  231. else return sa.ThrowError(_T("Matrix() invalid instance type"));
  232. }
  233. else {
  234. SquirrelObject arr = sa.GetObjectHandle(2);
  235. if(arr.Len() != 16) {
  236. return sa.ThrowError(_T("Matrix(array) need a 16 elements array"));
  237. }
  238. FLOAT *fp = (FLOAT*)&temp.m;
  239. SquirrelObject idx,val;
  240. if(arr.BeginIteration()) {
  241. while(arr.Next(idx,val)) {
  242. fp[idx.ToInteger()] = val.ToFloat();
  243. }
  244. arr.EndIteration();
  245. }
  246. }
  247. break;
  248. default:
  249. return sa.ThrowError(_T("Matrix() wrong number of parameters"));
  250. break;
  251. }
  252. newm = new D3DXMATRIX(temp);
  253. return construct_Matrix(newm);
  254. }
  255. _MEMBER_FUNCTION_IMPL(Matrix,_set)
  256. {
  257. StackHandler sa(v);
  258. _CHECK_SELF(D3DXMATRIX,Matrix);
  259. int index = sa.GetInt(2);
  260. if(index < 0 && index > 4*4)
  261. return SQ_ERROR;
  262. ((FLOAT *)self->m)[index] = sa.GetFloat(2);
  263. return SQ_OK;
  264. }
  265. _MEMBER_FUNCTION_IMPL(Matrix,_get)
  266. {
  267. StackHandler sa(v);
  268. _CHECK_SELF(D3DXMATRIX,Matrix);
  269. int index = sa.GetInt(2);
  270. if(index < 0 && index > 4*4)
  271. return SQ_ERROR;
  272. return sa.Return(((FLOAT *)self->m)[index]);
  273. }
  274. _MEMBER_FUNCTION_IMPL(Matrix,_add)
  275. {
  276. StackHandler sa(v);
  277. _CHECK_SELF(D3DXMATRIX,Matrix);
  278. _CHECK_INST_PARAM(mat,2,D3DXMATRIX,Matrix);
  279. D3DXMATRIX tm = (*self)+(*mat);
  280. SquirrelObject so = new_Matrix(tm);
  281. return sa.Return(so);
  282. }
  283. _MEMBER_FUNCTION_IMPL(Matrix,_sub)
  284. {
  285. StackHandler sa(v);
  286. _CHECK_SELF(D3DXMATRIX,Matrix);
  287. _CHECK_INST_PARAM(mat,2,D3DXMATRIX,Matrix);
  288. D3DXMATRIX tm = (*self)-(*mat);
  289. SquirrelObject so = new_Matrix(tm);
  290. return sa.Return(so);
  291. }
  292. _MEMBER_FUNCTION_IMPL(Matrix,_mul)
  293. {
  294. StackHandler sa(v);
  295. _CHECK_SELF(D3DXMATRIX,Matrix);
  296. int t = sa.GetType(2);
  297. if(t == OT_INSTANCE) {
  298. _CHECK_INST_PARAM(mat,2,D3DXMATRIX,Matrix);
  299. D3DXMATRIX tm = (*self)*(*mat);
  300. SquirrelObject so = new_Matrix(tm);
  301. return sa.Return(so);
  302. }
  303. D3DXMATRIX tm = (*self)*sa.GetFloat(2);
  304. SquirrelObject so = new_Matrix(tm);
  305. return sa.Return(so);
  306. }
  307. _MEMBER_FUNCTION_IMPL(Matrix,_div)
  308. {
  309. StackHandler sa(v);
  310. _CHECK_SELF(D3DXMATRIX,Matrix);
  311. D3DXMATRIX tm = (*self)/sa.GetFloat(2);
  312. SquirrelObject so = new_Matrix(tm);
  313. return sa.Return(so);
  314. }
  315. _MEMBER_FUNCTION_IMPL(Matrix,CreateIdentity)
  316. {
  317. _CHECK_SELF(D3DXMATRIX,Matrix);
  318. D3DXMatrixIdentity(self);
  319. return 0;
  320. }
  321. _MEMBER_FUNCTION_IMPL(Matrix,CreateRotationAxis)
  322. {
  323. StackHandler sa(v);
  324. _CHECK_SELF(D3DXMATRIX,Matrix);
  325. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  326. D3DXMatrixRotationAxis(self,vec,sa.GetFloat(3));
  327. return 0;
  328. }
  329. _MEMBER_FUNCTION_IMPL(Matrix,CreateRotationAngles)
  330. {
  331. StackHandler sa(v);
  332. _CHECK_SELF(D3DXMATRIX,Matrix);
  333. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  334. D3DXMatrixRotationAxis(self,vec,sa.GetFloat(3));
  335. return 0;
  336. }
  337. _MEMBER_FUNCTION_IMPL(Matrix,CreateScalingMatrix)
  338. {
  339. StackHandler sa(v);
  340. _CHECK_SELF(D3DXMATRIX,Matrix);
  341. D3DXMatrixScaling(self,sa.GetFloat(2),sa.GetFloat(3),sa.GetFloat(4));
  342. return 0;
  343. }
  344. _MEMBER_FUNCTION_IMPL(Matrix,CreateTranslationMatrix)
  345. {
  346. StackHandler sa(v);
  347. _CHECK_SELF(D3DXMATRIX,Matrix);
  348. D3DXMatrixTranslation(self,sa.GetFloat(2),sa.GetFloat(3),sa.GetFloat(4));
  349. return 0;
  350. }
  351. _MEMBER_FUNCTION_IMPL(Matrix,CreateLookAtMatrix)
  352. {
  353. StackHandler sa(v);
  354. _CHECK_SELF(D3DXMATRIX,Matrix);
  355. _CHECK_INST_PARAM(eye,2,D3DXVECTOR3,Vector3);
  356. _CHECK_INST_PARAM(at,3,D3DXVECTOR3,Vector3);
  357. _CHECK_INST_PARAM(up,4,D3DXVECTOR3,Vector3);
  358. D3DXMatrixLookAtLH(self,eye,at,up);
  359. return 0;
  360. }
  361. _MEMBER_FUNCTION_IMPL(Matrix,CreatePerspectiveFovMatrix)
  362. {
  363. StackHandler sa(v);
  364. _CHECK_SELF(D3DXMATRIX,Matrix);
  365. D3DXMatrixPerspectiveFovLH(self,sa.GetFloat(2),sa.GetFloat(3),sa.GetFloat(4),sa.GetFloat(5));
  366. return 0;
  367. }
  368. _MEMBER_FUNCTION_IMPL(Matrix,RotateAngles)
  369. {
  370. StackHandler sa(v);
  371. _CHECK_SELF(D3DXMATRIX,Matrix);
  372. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  373. D3DXMatrixRotationYawPitchRoll(self,vec->y,vec->x,vec->z);
  374. return 0;
  375. }
  376. _MEMBER_FUNCTION_IMPL(Matrix,RotateAxis)
  377. {
  378. StackHandler sa(v);
  379. _CHECK_SELF(D3DXMATRIX,Matrix);
  380. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  381. D3DXMatrixRotationAxis(self,vec,sa.GetFloat(3));
  382. return 0;
  383. }
  384. _MEMBER_FUNCTION_IMPL(Matrix,Translate)
  385. {
  386. _CHECK_SELF(D3DXMATRIX,Matrix);
  387. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  388. D3DXMATRIX temp;
  389. D3DXMatrixTranslation(&temp,vec->x,vec->y,vec->z);
  390. D3DXMatrixMultiply(self,&temp,self);
  391. return 0;
  392. }
  393. _MEMBER_FUNCTION_IMPL(Matrix,Scale)
  394. {
  395. _CHECK_SELF(D3DXMATRIX,Matrix);
  396. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  397. D3DXMATRIX temp;
  398. D3DXMatrixScaling(&temp,vec->x,vec->y,vec->z);
  399. D3DXMatrixMultiply(self,&temp,self);
  400. return 0;
  401. }
  402. _MEMBER_FUNCTION_IMPL(Matrix,GetInverse)
  403. {
  404. StackHandler sa(v);
  405. _CHECK_SELF(D3DXMATRIX,Matrix);
  406. D3DXMATRIX temp;
  407. D3DXMatrixInverse(&temp,NULL,self);
  408. SquirrelObject so = new_Matrix(temp);
  409. return sa.Return(so);
  410. }
  411. _MEMBER_FUNCTION_IMPL(Matrix,GetTransposed)
  412. {
  413. StackHandler sa(v);
  414. _CHECK_SELF(D3DXMATRIX,Matrix);
  415. D3DXMATRIX temp;
  416. D3DXMatrixTranspose(&temp,self);
  417. SquirrelObject so = new_Matrix(temp);
  418. return sa.Return(so);
  419. }
  420. _MEMBER_FUNCTION_IMPL(Matrix,GetInverseTransposed)
  421. {
  422. StackHandler sa(v);
  423. _CHECK_SELF(D3DXMATRIX,Matrix);
  424. D3DXMATRIX temp;
  425. D3DXMatrixInverse(&temp,NULL,self);
  426. D3DXMatrixTranspose(&temp,&temp);
  427. SquirrelObject so = new_Matrix(temp);
  428. return sa.Return(so);
  429. }
  430. _MEMBER_FUNCTION_IMPL(Matrix,Invert)
  431. {
  432. _CHECK_SELF(D3DXMATRIX,Matrix);
  433. D3DXMatrixInverse(self,NULL,self);
  434. return 0;
  435. }
  436. _MEMBER_FUNCTION_IMPL(Matrix,TransformCoord)
  437. {
  438. StackHandler sa(v);
  439. _CHECK_SELF(D3DXMATRIX,Matrix);
  440. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  441. D3DXVECTOR3 ret;
  442. D3DXVec3TransformCoord(&ret,vec,self);
  443. SquirrelObject so = new_Vector3(ret);
  444. return sa.Return(so);
  445. }
  446. _MEMBER_FUNCTION_IMPL(Matrix,TransformNormal)
  447. {
  448. StackHandler sa(v);
  449. _CHECK_SELF(D3DXMATRIX,Matrix);
  450. _CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
  451. D3DXVECTOR3 ret;
  452. D3DXVec3TransformNormal(&ret,vec,self);
  453. SquirrelObject so = new_Vector3(ret);
  454. return sa.Return(so);
  455. }
  456. _BEGIN_CLASS(Matrix)
  457. _MEMBER_FUNCTION(Matrix,constructor,-1,_T(".a|x"))
  458. _MEMBER_FUNCTION(Matrix,_set,2,_T("xn"))
  459. _MEMBER_FUNCTION(Matrix,_get,2,_T("xn"))
  460. _MEMBER_FUNCTION(Matrix,_add,2,_T("xx"))
  461. _MEMBER_FUNCTION(Matrix,_sub,2,_T("xx"))
  462. _MEMBER_FUNCTION(Matrix,_mul,2,_T("xx|n"))
  463. _MEMBER_FUNCTION(Matrix,_div,2,_T("xn"))
  464. _MEMBER_FUNCTION(Matrix,CreateIdentity,1,_T("x"))
  465. _MEMBER_FUNCTION(Matrix,CreateRotationAxis,3,_T("xxn"))
  466. _MEMBER_FUNCTION(Matrix,CreateRotationAngles,2,_T("xx"))
  467. _MEMBER_FUNCTION(Matrix,CreateScalingMatrix,4,_T("xnnn"))
  468. _MEMBER_FUNCTION(Matrix,CreateTranslationMatrix,4,_T("xnnn"))
  469. _MEMBER_FUNCTION(Matrix,CreateLookAtMatrix,4,_T("xxxx"))
  470. _MEMBER_FUNCTION(Matrix,CreatePerspectiveFovMatrix,5,_T("xnnnn"))
  471. _MEMBER_FUNCTION(Matrix,RotateAngles,2,_T("xx"))
  472. _MEMBER_FUNCTION(Matrix,RotateAxis,3,_T("xxn"))
  473. _MEMBER_FUNCTION(Matrix,Translate,2,_T("xx"))
  474. _MEMBER_FUNCTION(Matrix,Scale,2,_T("xx"))
  475. _MEMBER_FUNCTION(Matrix,GetInverse,1,_T("x"))
  476. _MEMBER_FUNCTION(Matrix,GetTransposed,1,_T("x"))
  477. _MEMBER_FUNCTION(Matrix,GetInverseTransposed,1,_T("x"))
  478. _MEMBER_FUNCTION(Matrix,Invert,1,_T("x"))
  479. _MEMBER_FUNCTION(Matrix,TransformCoord,2,_T("xx"))
  480. _MEMBER_FUNCTION(Matrix,TransformNormal,2,_T("xx"))
  481. _END_CLASS(Matrix)