Leaked source code of windows server 2003
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.

1366 lines
36 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: dxglob7obj.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #define DIRECTINPUT_VERSION 0x0500
  11. #define DIRECTSOUND_VERSION 0x600
  12. // dSoundResourceObj.cpp : Implementation of CDirectApp and DLL registration.
  13. // DHF_DS entire file
  14. #include "windows.h"
  15. #include "mmsystem.h"
  16. #include "stdafx.h"
  17. #include "Direct.h"
  18. #include "dms.h"
  19. #include "math.h"
  20. #include "dxGlob7Obj.h"
  21. #include "d3drm.h"
  22. #include "ddraw.h"
  23. #include "dmusici.h"
  24. #include "dmusicf.h"
  25. #include "ddraw4Obj.h"
  26. #include "ddraw7Obj.h"
  27. #include "DDEnumObj.h"
  28. #include "d3drm3Obj.h"
  29. #include "dsoundObj.h"
  30. #include "dsoundCaptureObj.h"
  31. #include "DSEnumObj.h"
  32. #include "dplay4obj.h"
  33. #include "dplaylobby3obj.h"
  34. #include "DPEnumObj.h"
  35. #include "dinput1Obj.h"
  36. #include "dmSegmentObj.h"
  37. #include "dmSegmentStateObj.h"
  38. #include "dmChordMapObj.h"
  39. #include "dmBandObj.h"
  40. #include "dmCollectionObj.h"
  41. #include "dmStyleObj.h"
  42. #include "dmPerformanceObj.h"
  43. #include "dmLoaderObj.h"
  44. #include "dmComposerObj.h"
  45. //#include "webObj.h"
  46. extern HINSTANCE g_hInstD3DRMDLL;
  47. extern HINSTANCE g_hDSoundHandle;
  48. extern HINSTANCE g_hDPlay;
  49. extern HINSTANCE g_hDDrawHandle;
  50. extern HINSTANCE g_hInstDINPUTDLL;
  51. extern HINSTANCE g_hInst;
  52. extern HRESULT BSTRtoPPGUID(LPGUID*,BSTR);
  53. extern HRESULT BSTRtoGUID(LPGUID,BSTR);
  54. extern void *g_dxj_DirectMusicLoader;
  55. extern void *g_dxj_DirectMusicComposer;
  56. extern void *g_dxj_DirectMusicPerformance;
  57. extern BSTR GUIDtoBSTR(LPGUID);
  58. extern HRESULT DPLBSTRtoGUID(LPGUID pGuid,BSTR str);
  59. extern HINSTANCE LoadDDrawDLL();
  60. extern HINSTANCE LoadD3DXOFDLL();
  61. extern HINSTANCE LoadDPlayDLL();
  62. extern HINSTANCE LoadDSoundDLL();
  63. extern HINSTANCE LoadD3DRMDLL();
  64. extern HINSTANCE LoadDINPUTDLL();
  65. DWORD WINAPI ThreadFunc(LPVOID param);
  66. C_dxj_DirectX7Object::C_dxj_DirectX7Object(){
  67. m_pDirectDrawCreate=NULL;
  68. m_pDirectDrawCreateEx=NULL;
  69. m_pDirectDrawCreateClipper=NULL;
  70. m_pDirectSoundCreate=NULL;
  71. m_pDirectSoundEnumerate=NULL;
  72. m_pDirectSoundCaptureEnumerate=NULL;
  73. m_pDirectSoundCaptureCreate=NULL;
  74. m_pDirectPlayCreate=NULL;
  75. m_pDirectPlayEnumerate=NULL;
  76. m_pDirectPlayLobbyCreate=NULL;
  77. m_pDirect3DRMCreate=NULL;
  78. m_pDirectDrawEnumerate=NULL;
  79. m_pDirectDrawEnumerateEx=NULL;
  80. m_pEventList=NULL;
  81. }
  82. void C_dxj_DirectX7Object::LoadDSOUND()
  83. {
  84. if (!g_hDSoundHandle ) LoadDSoundDLL();
  85. if (!m_pDirectSoundCreate) m_pDirectSoundCreate = (DSOUNDCREATE)GetProcAddress( g_hDSoundHandle, "DirectSoundCreate" );
  86. if (!m_pDirectSoundCaptureCreate) m_pDirectSoundCaptureCreate = (DSOUNDCAPTURECREATE)GetProcAddress( g_hDSoundHandle, "DirectSoundCaptureCreate" );
  87. if (!m_pDirectSoundEnumerate) m_pDirectSoundEnumerate = (DSOUNDENUMERATE)GetProcAddress( g_hDSoundHandle, "DirectSoundEnumerateA" );
  88. if (!m_pDirectSoundCaptureEnumerate) m_pDirectSoundCaptureEnumerate = (DSOUNDCAPTUREENUMERATE)GetProcAddress( g_hDSoundHandle, "DirectSoundCaptureEnumerateA" );
  89. }
  90. void C_dxj_DirectX7Object::LoadDDRAW()
  91. {
  92. if (!g_hDDrawHandle ) LoadDDrawDLL();
  93. if (!m_pDirectDrawCreate) m_pDirectDrawCreate = (DDRAWCREATE)GetProcAddress( g_hDDrawHandle, "DirectDrawCreate" );
  94. if (!m_pDirectDrawCreateEx) m_pDirectDrawCreateEx = (DDRAWCREATEEX)GetProcAddress( g_hDDrawHandle, "DirectDrawCreateEx" );
  95. if (!m_pDirectDrawCreateClipper) m_pDirectDrawCreateClipper = (DDCREATECLIPPER)GetProcAddress( g_hDDrawHandle, "DirectDrawCreateClipper" );
  96. if (!m_pDirectDrawEnumerate) m_pDirectDrawEnumerate = (DDENUMERATE)GetProcAddress( g_hDDrawHandle, "DirectDrawEnumerateA" );
  97. if (!m_pDirectDrawEnumerateEx) m_pDirectDrawEnumerateEx = (DDENUMERATEEX)GetProcAddress( g_hDDrawHandle, "DirectDrawEnumerateExA" );
  98. }
  99. void C_dxj_DirectX7Object::LoadDPLAY()
  100. {
  101. if (!g_hDPlay ) LoadDPlayDLL();
  102. //Note use of Wide char for DirectPlayLobby Create and
  103. //Ansi for DirectPlay
  104. if (!m_pDirectPlayCreate) m_pDirectPlayCreate = (DIRECTPLAYCREATE)GetProcAddress( g_hDPlay, "DirectPlayCreate" );
  105. if (!m_pDirectPlayEnumerate) m_pDirectPlayEnumerate = (DIRECTPLAYENUMERATE)GetProcAddress( g_hDPlay, "DirectPlayEnumerateW" );
  106. if (!m_pDirectPlayLobbyCreate) m_pDirectPlayLobbyCreate = (DIRECTPLAYLOBBYCREATE)GetProcAddress( g_hDPlay, "DirectPlayLobbyCreateW" );
  107. }
  108. void C_dxj_DirectX7Object::LoadD3DRM()
  109. {
  110. if (!g_hInstD3DRMDLL) {
  111. LoadD3DXOFDLL();
  112. LoadD3DRMDLL();
  113. }
  114. //Note: always ansi no unicode version
  115. if (!m_pDirect3DRMCreate) m_pDirect3DRMCreate =(DIRECT3DRMCREATE)GetProcAddress( g_hInstD3DRMDLL, "Direct3DRMCreate" );
  116. }
  117. C_dxj_DirectX7Object::~C_dxj_DirectX7Object()
  118. {
  119. DWORD i=1;
  120. while (m_pEventList) {
  121. //bugbug SUNDOWN gotcha
  122. //vb does not have 64 bit values
  123. //and we use the same api to release the event
  124. //but handles are 64 bit
  125. //note handles never get near 32 bits in win32
  126. //but whose knows on win64
  127. destroyEvent((long)PtrToLong(m_pEventList->hEvent));
  128. //m_pEventList=m_pEventList->pNext; done by destroyEvent
  129. }
  130. }
  131. STDMETHODIMP C_dxj_DirectX7Object::direct3dRMCreate( I_dxj_Direct3dRM3 **ret){
  132. HRESULT hr;
  133. LPDIRECT3DRM realrm1=NULL;
  134. LPDIRECT3DRM3 realrm3=NULL;
  135. LoadD3DRM();
  136. if (!m_pDirect3DRMCreate) return E_FAIL;
  137. hr=(m_pDirect3DRMCreate)(&realrm1);
  138. if FAILED(hr) return hr;
  139. hr=realrm1->QueryInterface(IID_IDirect3DRM3,(void**) &realrm3);
  140. if FAILED(hr){
  141. realrm1->Release();
  142. return hr;
  143. }
  144. INTERNAL_CREATE(_dxj_Direct3dRM3,realrm3,ret);
  145. realrm1->Release();
  146. if (*ret==NULL) {
  147. realrm3->Release();
  148. return E_FAIL;
  149. }
  150. return hr;
  151. }
  152. STDMETHODIMP C_dxj_DirectX7Object::directDraw4Create(BSTR strGuid, I_dxj_DirectDraw4 **ret){
  153. HRESULT hr;
  154. LPDIRECTDRAW realdraw1=NULL;
  155. LPDIRECTDRAW4 realdraw4=NULL;
  156. GUID guid;
  157. LPGUID pguid=&guid;
  158. DWORD i=0;
  159. LoadDDRAW();
  160. hr=BSTRtoPPGUID(&pguid,strGuid);
  161. if FAILED(hr) return E_FAIL;
  162. if (!m_pDirectDrawCreate) return E_FAIL;
  163. hr=(m_pDirectDrawCreate)((GUID*)pguid,&realdraw1,NULL);
  164. if FAILED(hr) return hr;
  165. hr=realdraw1->QueryInterface(IID_IDirectDraw4,(void**) &realdraw4);
  166. if FAILED(hr){
  167. i=realdraw1->Release();
  168. return hr;
  169. }
  170. i=realdraw1->Release();
  171. INTERNAL_CREATE(_dxj_DirectDraw4,realdraw4,ret);
  172. if (*ret==NULL) {
  173. i=realdraw4->Release();
  174. return E_FAIL;
  175. }
  176. return hr;
  177. }
  178. STDMETHODIMP C_dxj_DirectX7Object::directDrawCreate(BSTR strGuid, I_dxj_DirectDraw7 **ret){
  179. HRESULT hr;
  180. LPDIRECTDRAW7 realdraw7=NULL;
  181. GUID guid;
  182. LPGUID pguid=&guid;
  183. DWORD i=0;
  184. LoadDDRAW();
  185. ZeroMemory(&guid,sizeof(GUID)); //andrewke bugfix post dx7
  186. hr=BSTRtoPPGUID(&pguid,strGuid);
  187. if FAILED(hr) return E_FAIL;
  188. if (!m_pDirectDrawCreateEx) return E_FAIL;
  189. hr=(m_pDirectDrawCreateEx)(pguid,(void**)&realdraw7,IID_IDirectDraw7,NULL);
  190. if FAILED(hr) return hr;
  191. INTERNAL_CREATE(_dxj_DirectDraw7,realdraw7,ret);
  192. if (*ret==NULL) {
  193. i=realdraw7->Release();
  194. return E_FAIL;
  195. }
  196. return hr;
  197. }
  198. STDMETHODIMP C_dxj_DirectX7Object::directSoundCreate(BSTR strGuid, I_dxj_DirectSound **ret){
  199. HRESULT hr;
  200. LPDIRECTSOUND realsound1=NULL;
  201. GUID guid;
  202. LPGUID pguid=&guid;
  203. LoadDSOUND();
  204. hr=BSTRtoPPGUID(&pguid,strGuid);
  205. if FAILED(hr) return hr;
  206. if (!m_pDirectSoundCreate) return E_FAIL;
  207. hr=(m_pDirectSoundCreate)((GUID*)pguid,&realsound1,NULL);
  208. if FAILED(hr) return hr;
  209. INTERNAL_CREATE(_dxj_DirectSound,realsound1,ret);
  210. if (*ret==NULL) {
  211. realsound1->Release();
  212. return E_FAIL;
  213. }
  214. return hr;
  215. }
  216. STDMETHODIMP C_dxj_DirectX7Object::directSoundCaptureCreate(BSTR strGuid, I_dxj_DirectSoundCapture **ret){
  217. HRESULT hr;
  218. LPDIRECTSOUNDCAPTURE realsound1=NULL;
  219. GUID guid;
  220. LPGUID pguid=&guid;
  221. hr=BSTRtoPPGUID(&pguid,strGuid);
  222. if FAILED(hr) return hr;
  223. LoadDSOUND();
  224. if (!m_pDirectSoundCaptureCreate) return E_FAIL;
  225. hr=(m_pDirectSoundCaptureCreate)(pguid,&realsound1,NULL);
  226. if FAILED(hr) return hr;
  227. INTERNAL_CREATE(_dxj_DirectSoundCapture,realsound1,ret);
  228. if (*ret==NULL) {
  229. realsound1->Release();
  230. return E_FAIL;
  231. }
  232. return hr;
  233. }
  234. STDMETHODIMP C_dxj_DirectX7Object::getDSCaptureEnum( I_dxj_DSEnum **retVal)
  235. {
  236. HRESULT hr;
  237. LoadDSOUND();
  238. if (!m_pDirectSoundCaptureEnumerate) return E_FAIL;
  239. hr=C_dxj_DSEnumObject::create(NULL,m_pDirectSoundCaptureEnumerate,retVal);
  240. return hr;
  241. }
  242. STDMETHODIMP C_dxj_DirectX7Object::getDDEnum( I_dxj_DirectDrawEnum **retVal)
  243. {
  244. LoadDDRAW();
  245. HRESULT hr;
  246. if (!m_pDirectDrawEnumerateEx) return E_FAIL;
  247. hr=C_dxj_DirectDrawEnumObject::create(m_pDirectDrawEnumerateEx,retVal);
  248. return hr;
  249. }
  250. /////////////////////////////////////////////////////////////////////////////
  251. STDMETHODIMP C_dxj_DirectX7Object::getDSEnum( I_dxj_DSEnum **retVal)
  252. {
  253. HRESULT hr;
  254. LoadDSOUND();
  255. if (!m_pDirectSoundEnumerate) return E_FAIL;
  256. hr=C_dxj_DSEnumObject::create(m_pDirectSoundEnumerate,NULL,retVal);
  257. return hr;
  258. }
  259. /////////////////////////////////////////////////////////////////////////////
  260. STDMETHODIMP C_dxj_DirectX7Object::colorGetAlpha(long color, float *retval)
  261. {
  262. static float (WINAPI *ProcAdd)(long)=NULL;
  263. LoadD3DRMDLL();
  264. if (ProcAdd==NULL){
  265. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  266. ProcAdd = (float (WINAPI*)(long)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMColorGetAlpha");
  267. if (ProcAdd==NULL) return E_FAIL;
  268. }
  269. *retval=(ProcAdd) ((D3DCOLOR) color);
  270. return S_OK;
  271. }
  272. STDMETHODIMP C_dxj_DirectX7Object::colorGetRed(long color, float *retval)
  273. {
  274. static float (WINAPI *ProcAdd)(long)=NULL;
  275. LoadD3DRMDLL();
  276. if (ProcAdd==NULL){
  277. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  278. ProcAdd = (float (WINAPI*)(long)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMColorGetRed");
  279. if (ProcAdd==NULL) return E_FAIL;
  280. }
  281. *retval=(ProcAdd) ((D3DCOLOR) color);
  282. return S_OK;
  283. }
  284. STDMETHODIMP C_dxj_DirectX7Object::colorGetGreen(long color, float *retval)
  285. {
  286. static float (WINAPI *ProcAdd)(long)=NULL;
  287. LoadD3DRMDLL();
  288. if (ProcAdd==NULL){
  289. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  290. ProcAdd = (float (WINAPI*)(long)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMColorGetGreen");
  291. if (ProcAdd==NULL) return E_FAIL;
  292. }
  293. *retval=(ProcAdd) ((D3DCOLOR) color);
  294. return S_OK;
  295. }
  296. STDMETHODIMP C_dxj_DirectX7Object::colorGetBlue(long color, float *retval)
  297. {
  298. static float (WINAPI *ProcAdd)(long)=NULL;
  299. LoadD3DRMDLL();
  300. if (ProcAdd==NULL){
  301. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  302. ProcAdd = (float (WINAPI*)(long)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMColorGetBlue");
  303. if (ProcAdd==NULL) return E_FAIL;
  304. }
  305. *retval=(ProcAdd) ((D3DCOLOR) color);
  306. return S_OK;
  307. }
  308. STDMETHODIMP C_dxj_DirectX7Object::createColorRGB(float r, float g, float b, long *color)
  309. {
  310. static long (WINAPI *ProcAdd)(float,float,float)=NULL;
  311. LoadD3DRMDLL();
  312. if (ProcAdd==NULL){
  313. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  314. ProcAdd = (long (WINAPI*)(float,float,float)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMCreateColorRGB");
  315. if (ProcAdd==NULL) return E_FAIL;
  316. }
  317. *color=(ProcAdd) (r,g,b);
  318. return S_OK;
  319. }
  320. STDMETHODIMP C_dxj_DirectX7Object::createColorRGBA(float r, float g, float b, float a, long *color)
  321. {
  322. static long (WINAPI *ProcAdd)(float,float,float,float)=NULL;
  323. LoadD3DRMDLL();
  324. if (ProcAdd==NULL){
  325. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  326. ProcAdd = (long (WINAPI*)(float,float,float,float)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMCreateColorRGBA");
  327. if (ProcAdd==NULL) return E_FAIL;
  328. }
  329. *color=(ProcAdd) (r,g,b,a);
  330. return S_OK;
  331. }
  332. STDMETHODIMP C_dxj_DirectX7Object::matrixFromQuaternion(D3dMatrix *matrix, D3dRMQuaternion *quat)
  333. {
  334. if ((!matrix) || (!quat)) return E_INVALIDARG;
  335. D3DMATRIX *mat=(D3DMATRIX*)matrix;
  336. FLOAT w=quat->s;
  337. FLOAT x=quat->v.x;
  338. FLOAT y=quat->v.y;
  339. FLOAT z=quat->v.z;
  340. FLOAT xx = x*x, xy = x*y, xz = x*z, xw = x*w;
  341. FLOAT yy = y*y, yz = y*z, yw = y*w;
  342. FLOAT zz = z*z, zw = z*w;
  343. FLOAT ww = w*w;
  344. mat->_11 = xx-yy-zz+ww;
  345. mat->_12 = 2.0f*(xy-zw);
  346. mat->_13 = 2.0f*(xz+yw);
  347. mat->_21 = 2.0f*(xy+zw);
  348. mat->_22 = -xx+yy-zz+ww;
  349. mat->_23 = 2.0f*(yz-xw);
  350. mat->_31 = 2.0f*(xz-yw);
  351. mat->_32 = 2.0f*(yz+xw);
  352. mat->_33 = -xx-yy+zz+ww;
  353. mat->_14 = mat->_41 = 0.0f;
  354. mat->_24 = mat->_42 = 0.0f;
  355. mat->_34 = mat->_43 = 0.0f;
  356. mat->_44 = xx+yy+zz+ww;
  357. return S_OK;
  358. }
  359. STDMETHODIMP C_dxj_DirectX7Object::quaternionRotation(D3dRMQuaternion *quat, D3dVector *axis, float theta)
  360. {
  361. static D3dRMQuaternion* (WINAPI *ProcAdd)(D3dRMQuaternion*,D3dVector*,float)=NULL;
  362. LoadD3DRMDLL();
  363. if (ProcAdd==NULL){
  364. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  365. ProcAdd = (D3dRMQuaternion* (WINAPI*)(D3dRMQuaternion*,D3dVector*,float)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMQuaternionRotation");
  366. if (ProcAdd==NULL) return E_FAIL;
  367. }
  368. if (NULL== (ProcAdd) (quat,axis,theta)) return E_FAIL;
  369. return S_OK;
  370. }
  371. STDMETHODIMP C_dxj_DirectX7Object::quaternionMultiply(D3dRMQuaternion *quatr, D3dRMQuaternion *quat1, D3dRMQuaternion *quat2)
  372. {
  373. static D3dRMQuaternion* (WINAPI *ProcAdd)(D3dRMQuaternion*,D3dRMQuaternion*,D3dRMQuaternion*)=NULL;
  374. LoadD3DRMDLL();
  375. if (ProcAdd==NULL){
  376. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  377. ProcAdd = (D3dRMQuaternion* (WINAPI*)(D3dRMQuaternion*,D3dRMQuaternion*,D3dRMQuaternion*)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMQuaternionMultiply");
  378. if (ProcAdd==NULL) return E_FAIL;
  379. }
  380. if (NULL== (ProcAdd) (quatr,quat1,quat2)) return E_FAIL;
  381. return S_OK;
  382. }
  383. STDMETHODIMP C_dxj_DirectX7Object::quaternionSlerp(D3dRMQuaternion *quatr, D3dRMQuaternion *quat1, D3dRMQuaternion *quat2, float t)
  384. {
  385. static D3dRMQuaternion* (WINAPI *ProcAdd)(D3dRMQuaternion*,D3dRMQuaternion*,D3dRMQuaternion*,float)=NULL;
  386. LoadD3DRMDLL();
  387. if (ProcAdd==NULL){
  388. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  389. ProcAdd = (D3dRMQuaternion* (WINAPI*)(D3dRMQuaternion*,D3dRMQuaternion*,D3dRMQuaternion*,float)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMQuaternionSlerp");
  390. if (ProcAdd==NULL) return E_FAIL;
  391. }
  392. if (NULL== (ProcAdd) (quatr,quat1,quat2,t)) return E_FAIL;
  393. return S_OK;
  394. }
  395. STDMETHODIMP C_dxj_DirectX7Object::vectorAdd(D3dVector *v, D3dVector *a, D3dVector *b)
  396. {
  397. v->x = a->x + b->x;
  398. v->y = a->y + b->y;
  399. v->z = a->z + b->z;
  400. return S_OK;
  401. }
  402. STDMETHODIMP C_dxj_DirectX7Object::vectorCopy(D3dVector *dest, D3dVector *src)
  403. {
  404. memcpy(dest,src,sizeof(D3DVECTOR));
  405. return S_OK;
  406. }
  407. STDMETHODIMP C_dxj_DirectX7Object::vectorCrossProduct(D3dVector *v, D3dVector *a, D3dVector *b)
  408. {
  409. v->x = ((a->y) * (b->z)) - ((a->z) * (b->y));
  410. v->y = ((a->z) * (b->x)) - ((a->x) * (b->z));
  411. v->z = ((a->x) * (b->y)) - ((a->y) * (b->x));
  412. return S_OK;
  413. }
  414. STDMETHODIMP C_dxj_DirectX7Object::vectorDotProduct(D3dVector *a, D3dVector *b, float *ret)
  415. {
  416. //TODO consider overflow?
  417. *ret = (a->x * b->x) + (a->y *b->y) + (a->z * b->z);
  418. return S_OK;
  419. }
  420. STDMETHODIMP C_dxj_DirectX7Object::vectorModulus(D3dVector *a, float *ret)
  421. {
  422. //NOTE: would do it myself but dont want to link in c runtime for Sqrt
  423. static float (WINAPI *ProcAdd)(D3dVector*)=NULL;
  424. LoadD3DRMDLL();
  425. if (ProcAdd==NULL){
  426. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  427. ProcAdd = (float (WINAPI*)(D3dVector*)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMVectorModulus");
  428. if (ProcAdd==NULL) return E_FAIL;
  429. }
  430. *ret=(ProcAdd) (a);
  431. return S_OK;
  432. }
  433. STDMETHODIMP C_dxj_DirectX7Object::vectorNormalize(D3dVector *dest)
  434. {
  435. double l;
  436. l = dest->x * dest->x + dest->y * dest->y + dest->z * dest->z;
  437. l = sqrt(l);
  438. dest->x = dest->x / (float)l;
  439. dest->y = dest->y / (float)l;
  440. dest->z = dest->z / (float)l;
  441. return S_OK;
  442. }
  443. STDMETHODIMP C_dxj_DirectX7Object::vectorRandom(D3dVector *a)
  444. {
  445. static D3dVector* (WINAPI *ProcAdd)(D3dVector*)=NULL;
  446. LoadD3DRMDLL();
  447. if (ProcAdd==NULL){
  448. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  449. ProcAdd = (D3dVector* (WINAPI*)(D3dVector*)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMVectorRandom");
  450. if (ProcAdd==NULL) return E_FAIL;
  451. }
  452. if ((ProcAdd)(a)==NULL) return E_FAIL;
  453. return S_OK;
  454. }
  455. STDMETHODIMP C_dxj_DirectX7Object::vectorReflect(D3dVector *dest,D3dVector *ray,D3dVector *norm)
  456. {
  457. static D3dVector* (WINAPI *ProcAdd)(D3dVector*,D3dVector*,D3dVector*)=NULL;
  458. LoadD3DRMDLL();
  459. if (ProcAdd==NULL){
  460. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  461. ProcAdd = (D3dVector* (WINAPI*)(D3dVector*,D3dVector*,D3dVector*)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMVectorReflect");
  462. if (ProcAdd==NULL) return E_FAIL;
  463. }
  464. if ((ProcAdd)(dest,ray,norm)==NULL) return E_FAIL;
  465. return S_OK;
  466. }
  467. STDMETHODIMP C_dxj_DirectX7Object::vectorRotate(D3dVector *dest,D3dVector *vA,D3dVector *vAxis, float theta)
  468. {
  469. static D3dVector* (WINAPI *ProcAdd)(D3dVector*,D3dVector*,D3dVector*,float)=NULL;
  470. LoadD3DRMDLL();
  471. if (ProcAdd==NULL){
  472. if (g_hInstD3DRMDLL==NULL) return E_NOINTERFACE;
  473. ProcAdd = (D3dVector* (WINAPI*)(D3dVector*,D3dVector*,D3dVector*,float)) GetProcAddress(g_hInstD3DRMDLL, "D3DRMVectorRotate");
  474. if (ProcAdd==NULL) return E_FAIL;
  475. }
  476. if ((ProcAdd)(dest,vA,vAxis,theta)==NULL) return E_FAIL;
  477. return S_OK;
  478. }
  479. STDMETHODIMP C_dxj_DirectX7Object::vectorScale(D3dVector *v, D3dVector *a, float factor)
  480. {
  481. v->x=a->x *factor;
  482. v->y=a->y *factor;
  483. v->z=a->z *factor;
  484. return S_OK;
  485. }
  486. STDMETHODIMP C_dxj_DirectX7Object::vectorSubtract(D3dVector *v, D3dVector *a, D3dVector *b)
  487. {
  488. v->x = a->x - b->x;
  489. v->y = a->y - b->y;
  490. v->z = a->z - b->z;
  491. return S_OK;
  492. }
  493. STDMETHODIMP C_dxj_DirectX7Object::directPlayCreate(BSTR strGuid, I_dxj_DirectPlay4 **directPlay)
  494. {
  495. LPDIRECTPLAY4 dp;
  496. HRESULT hr=E_FAIL;
  497. LPDIRECTPLAY lpDP=NULL;
  498. GUID guid;
  499. LoadDPLAY();
  500. ZeroMemory(&guid,sizeof(GUID));
  501. hr=DPLBSTRtoGUID(&guid,strGuid);
  502. if FAILED(hr) return E_INVALIDARG;
  503. if (m_pDirectPlayCreate == NULL ) return E_FAIL;
  504. // create a DirectPlay1 interface
  505. hr = (m_pDirectPlayCreate)(&guid, &lpDP, NULL);
  506. if FAILED(hr) return hr;
  507. if (!lpDP) return E_FAIL;
  508. // now get Dplay3 interface
  509. hr = lpDP->QueryInterface(IID_IDirectPlay4,(LPVOID *)&dp);
  510. lpDP->Release();
  511. if FAILED(hr) return hr;
  512. if (!dp) return E_FAIL;
  513. *directPlay=NULL;
  514. INTERNAL_CREATE(_dxj_DirectPlay4, dp, directPlay);
  515. if (*directPlay==NULL) return E_FAIL;
  516. return hr;
  517. }
  518. STDMETHODIMP C_dxj_DirectX7Object::directPlayLobbyCreate(I_dxj_DirectPlayLobby3 **ret)
  519. {
  520. LPDIRECTPLAYLOBBY3 dp=NULL;
  521. HRESULT hr=E_FAIL;
  522. LPDIRECTPLAYLOBBY lpDP=NULL;
  523. LoadDPLAY();
  524. if (m_pDirectPlayLobbyCreate == NULL ) return E_FAIL;
  525. // create a DirectPlay1 interface
  526. hr = m_pDirectPlayLobbyCreate(NULL, &lpDP, NULL,NULL,0);
  527. if FAILED(hr) return hr;
  528. if (!lpDP) return E_FAIL;
  529. // no get Dplay3 interface
  530. hr = lpDP->QueryInterface(IID_IDirectPlayLobby3,(LPVOID *)&dp);
  531. lpDP->Release();
  532. if FAILED(hr) return hr;
  533. *ret=NULL;
  534. if (!dp) return E_FAIL;
  535. INTERNAL_CREATE(_dxj_DirectPlayLobby3, dp, ret);
  536. if (*ret==NULL) return E_FAIL;
  537. return hr;
  538. }
  539. /////////////////////////////////////////////////////////////////////////////
  540. STDMETHODIMP C_dxj_DirectX7Object::getDPEnum( I_dxj_DPEnumServiceProviders **retVal)
  541. {
  542. HRESULT hr;
  543. LoadDPLAY();
  544. if (!m_pDirectPlayEnumerate) return E_FAIL;
  545. hr=C_dxj_DPEnumObject::create(m_pDirectPlayEnumerate,retVal);
  546. return hr;
  547. }
  548. ////////////////////////////////////////////////////////////////
  549. STDMETHODIMP C_dxj_DirectX7Object::directInputCreate(I_dxj_DirectInput **ret){
  550. LoadDINPUTDLL();
  551. HRESULT hr;
  552. static HRESULT (WINAPI *ProcAdd)(HINSTANCE,DWORD,LPDIRECTINPUT*,LPUNKNOWN)=NULL;
  553. if (ProcAdd==NULL){
  554. if (g_hInstDINPUTDLL==NULL) return E_NOINTERFACE;
  555. ProcAdd = (HRESULT (WINAPI*)(HINSTANCE,DWORD,LPDIRECTINPUT*,LPUNKNOWN)) GetProcAddress(g_hInstDINPUTDLL, "DirectInputCreateA");
  556. if (ProcAdd==NULL) return E_FAIL;
  557. }
  558. LPDIRECTINPUT lpInput=NULL;
  559. hr= (ProcAdd)(g_hInst,(DWORD)DIRECTINPUT_VERSION,&lpInput,NULL);
  560. if FAILED(hr) return hr;
  561. INTERNAL_CREATE(_dxj_DirectInput,lpInput,ret);
  562. return hr;
  563. }
  564. /////////////////////////////////////////////////////////////////////////////
  565. //
  566. STDMETHODIMP C_dxj_DirectX7Object::tickCount( long *retval)
  567. {
  568. *retval = GetTickCount();
  569. return S_OK;
  570. }
  571. STDMETHODIMP C_dxj_DirectX7Object::systemBpp(long *retval)
  572. {
  573. HDC hdc;
  574. hdc = ::GetDC(NULL);
  575. if (!hdc) return E_OUTOFMEMORY;
  576. *retval = GetDeviceCaps(hdc, BITSPIXEL);
  577. ::ReleaseDC(NULL, hdc);
  578. return S_OK;
  579. }
  580. STDMETHODIMP C_dxj_DirectX7Object::rotateXMatrix(
  581. /* [out][in] */ D3dMatrix __RPC_FAR *mDest,
  582. /* [in] */ float radians){
  583. //id matrix
  584. float fcos;
  585. float fsin;
  586. ZeroMemory(mDest,sizeof(D3dMatrix));
  587. mDest->rc11 = 1;
  588. mDest->rc44 = 1;
  589. fsin=(float)sin(radians);
  590. fcos=(float)cos(radians);
  591. mDest->rc22 = fcos;
  592. mDest->rc33 = fcos;
  593. mDest->rc23 = -fsin;
  594. mDest->rc32 = fsin;
  595. return S_OK;
  596. }
  597. STDMETHODIMP C_dxj_DirectX7Object::rotateYMatrix(
  598. /* [out][in] */ D3dMatrix __RPC_FAR *mDest,
  599. /* [in] */ float radians){
  600. //id matrix
  601. float fcos;
  602. float fsin;
  603. ZeroMemory(mDest,sizeof(D3dMatrix));
  604. mDest->rc22 = 1;
  605. mDest->rc44 = 1;
  606. fsin=(float)sin(radians);
  607. fcos=(float)cos(radians);
  608. mDest->rc11 = fcos;
  609. mDest->rc33 = fcos;
  610. mDest->rc13 = fsin;
  611. mDest->rc31 = -fsin;
  612. return S_OK;
  613. }
  614. STDMETHODIMP C_dxj_DirectX7Object::rotateZMatrix(
  615. /* [out][in] */ D3dMatrix __RPC_FAR *mDest,
  616. /* [in] */ float radians){
  617. //id matrix
  618. float fcos;
  619. float fsin;
  620. ZeroMemory(mDest,sizeof(D3dMatrix));
  621. mDest->rc33 = 1;
  622. mDest->rc44 = 1;
  623. fsin=(float)sin(radians);
  624. fcos=(float)cos(radians);
  625. mDest->rc11 = fcos;
  626. mDest->rc22 = fcos;
  627. mDest->rc12 = -fsin;
  628. mDest->rc21 = fsin;
  629. return S_OK;
  630. }
  631. STDMETHODIMP C_dxj_DirectX7Object::viewMatrix(
  632. /* [out][in] */ D3dMatrix __RPC_FAR *view,
  633. /* [in] */ D3dVector __RPC_FAR *from,
  634. /* [in] */ D3dVector __RPC_FAR *at,
  635. /* [in] */ D3dVector __RPC_FAR *world_up,
  636. /* [in] */ float roll)
  637. {
  638. D3dVector up;
  639. D3dVector right;
  640. D3dVector view_Dir;
  641. ZeroMemory(view,sizeof(D3dMatrix));
  642. view->rc11 = 1;
  643. view->rc22 = 1;
  644. view->rc33 = 1;
  645. view->rc44 = 1;
  646. view_Dir.x=at->x-from->x;
  647. view_Dir.y=at->y-from->y;
  648. view_Dir.z=at->z-from->z;
  649. vectorNormalize(&view_Dir);
  650. //think lefthanded coords
  651. vectorCrossProduct(&right, world_up, &view_Dir);
  652. vectorCrossProduct(&up, &view_Dir, &right);
  653. vectorNormalize(&right);
  654. vectorNormalize(&up);
  655. view->rc11 = right.x;
  656. view->rc21 = right.y;
  657. view->rc31 = right.z;
  658. view->rc12 = up.x ; //AK? should this be negative?
  659. view->rc22 = up.y;
  660. view->rc32 = up.z;
  661. view->rc13 = view_Dir.x;
  662. view->rc23 = view_Dir.y;
  663. view->rc33 = view_Dir.z;
  664. view->rc41 = -((right.x * from->x) + (right.y * from->y) + (right.z * from->z));
  665. view->rc42 = -((up.x * from->x) + (up.y * from->y) + (up.z * from->z));
  666. view->rc43 = -((view_Dir.x * from->x) + (view_Dir.y * from->y) + (view_Dir.z * from->z));
  667. if (roll!=0){
  668. D3dMatrix rotZMat;
  669. rotateZMatrix(&rotZMat,-roll);
  670. matrixMultiply(view,&rotZMat,view);
  671. }
  672. return S_OK;
  673. }
  674. STDMETHODIMP C_dxj_DirectX7Object::matrixMultiply(
  675. /* [out][in] */ D3dMatrix __RPC_FAR *result,
  676. /* [in] */ D3dMatrix __RPC_FAR *a,
  677. /* [in] */ D3dMatrix __RPC_FAR *b)
  678. {
  679. D3dMatrix ret;
  680. ZeroMemory(&ret,sizeof(D3dMatrix));
  681. ret.rc11 = b->rc11 * a->rc11 + b->rc21 * a->rc12 + b->rc31 * a->rc13 + b->rc41 * a->rc14;
  682. ret.rc12 = b->rc12 * a->rc11 + b->rc22 * a->rc12 + b->rc32 * a->rc13 + b->rc42 * a->rc14;
  683. ret.rc13 = b->rc13 * a->rc11 + b->rc23 * a->rc12 + b->rc33 * a->rc13 + b->rc43 * a->rc14;
  684. ret.rc14 = b->rc14 * a->rc11 + b->rc24 * a->rc12 + b->rc34 * a->rc13 + b->rc44 * a->rc14;
  685. ret.rc21 = b->rc11 * a->rc21 + b->rc21 * a->rc22 + b->rc31 * a->rc23 + b->rc41 * a->rc24;
  686. ret.rc22 = b->rc12 * a->rc21 + b->rc22 * a->rc22 + b->rc32 * a->rc23 + b->rc42 * a->rc24;
  687. ret.rc23 = b->rc13 * a->rc21 + b->rc23 * a->rc22 + b->rc33 * a->rc23 + b->rc43 * a->rc24;
  688. ret.rc24 = b->rc14 * a->rc21 + b->rc24 * a->rc22 + b->rc34 * a->rc23 + b->rc44 * a->rc24;
  689. ret.rc31 = b->rc11 * a->rc31 + b->rc21 * a->rc32 + b->rc31 * a->rc33 + b->rc41 * a->rc34;
  690. ret.rc32 = b->rc12 * a->rc31 + b->rc22 * a->rc32 + b->rc32 * a->rc33 + b->rc42 * a->rc34;
  691. ret.rc33 = b->rc13 * a->rc31 + b->rc23 * a->rc32 + b->rc33 * a->rc33 + b->rc43 * a->rc34;
  692. ret.rc34 = b->rc14 * a->rc31 + b->rc24 * a->rc32 + b->rc34 * a->rc33 + b->rc44 * a->rc34;
  693. ret.rc41 = b->rc11 * a->rc41 + b->rc21 * a->rc42 + b->rc31 * a->rc43 + b->rc41 * a->rc44;
  694. ret.rc42 = b->rc12 * a->rc41 + b->rc22 * a->rc42 + b->rc32 * a->rc43 + b->rc42 * a->rc44;
  695. ret.rc43 = b->rc13 * a->rc41 + b->rc23 * a->rc42 + b->rc33 * a->rc43 + b->rc43 * a->rc44;
  696. ret.rc44 = b->rc14 * a->rc41 + b->rc24 * a->rc42 + b->rc34 * a->rc43 + b->rc44 * a->rc44;
  697. memcpy(result,&ret,sizeof(D3dMatrix));
  698. return S_OK;
  699. }
  700. STDMETHODIMP C_dxj_DirectX7Object::projectionMatrix(
  701. /* [out][in] */ D3dMatrix __RPC_FAR *mDest,
  702. /* [in] */ float near_plane,
  703. /* [in] */ float far_plane,
  704. /* [in] */ float fov)
  705. {
  706. float c,s,Q;
  707. c =(float) cos(fov * 0.5);
  708. s = (float)sin(fov * 0.5);
  709. if (far_plane==0) return E_INVALIDARG;
  710. Q = s / (1 - near_plane / far_plane);
  711. ZeroMemory(mDest,sizeof(D3dMatrix));
  712. mDest->rc11 = c;
  713. mDest->rc22 = c;
  714. mDest->rc33 = Q;
  715. mDest->rc43 = -Q * near_plane;
  716. mDest->rc34 = s;
  717. return S_OK;
  718. }
  719. STDMETHODIMP C_dxj_DirectX7Object::copyMatrix(
  720. /* [out][in] */ D3dMatrix __RPC_FAR *mDest,
  721. /* [in] */ D3dMatrix __RPC_FAR *mSrc)
  722. {
  723. memcpy(mDest,mSrc,sizeof(D3dMatrix));
  724. return S_OK;
  725. }
  726. STDMETHODIMP C_dxj_DirectX7Object::identityMatrix(
  727. /* [out][in] */ D3dMatrix __RPC_FAR *mDest)
  728. {
  729. ZeroMemory (mDest,sizeof(D3dMatrix));
  730. mDest->rc11=1;
  731. mDest->rc22=1;
  732. mDest->rc33=1;
  733. mDest->rc44=1;
  734. return S_OK;
  735. }
  736. STDMETHODIMP C_dxj_DirectX7Object::zeroMatrix(
  737. /* [out][in] */ D3dMatrix __RPC_FAR *mDest)
  738. {
  739. ZeroMemory (mDest,sizeof(D3dMatrix));
  740. return S_OK;
  741. }
  742. STDMETHODIMP C_dxj_DirectX7Object::directMusicLoaderCreate (
  743. /* [retval][out] */ I_dxj_DirectMusicLoader __RPC_FAR *__RPC_FAR *ret)
  744. {
  745. HRESULT hr;
  746. IDirectMusicLoader *pLoader=NULL;
  747. hr =CoCreateInstance(
  748. CLSID_DirectMusicLoader,
  749. NULL,
  750. CLSCTX_INPROC,
  751. IID_IDirectMusicLoader,
  752. (void**)&pLoader);
  753. if (FAILED(hr)) return E_NOINTERFACE;
  754. if (!pLoader) return E_FAIL;
  755. INTERNAL_CREATE_NOADDREF(_dxj_DirectMusicLoader,pLoader,ret);
  756. return S_OK;
  757. }
  758. STDMETHODIMP C_dxj_DirectX7Object::directMusicComposerCreate (
  759. /* [retval][out] */ I_dxj_DirectMusicComposer __RPC_FAR *__RPC_FAR *ret)
  760. {
  761. IDirectMusicComposer *pComp=NULL;
  762. if (FAILED(CoCreateInstance(
  763. CLSID_DirectMusicComposer,
  764. NULL,
  765. CLSCTX_INPROC,
  766. IID_IDirectMusicComposer,
  767. (void**)&pComp ))) {
  768. return E_NOINTERFACE;
  769. }
  770. if (!pComp) return E_FAIL;
  771. INTERNAL_CREATE_NOADDREF(_dxj_DirectMusicComposer,pComp,ret);
  772. return S_OK;
  773. }
  774. STDMETHODIMP C_dxj_DirectX7Object::directMusicPerformanceCreate (
  775. /* [retval][out] */ I_dxj_DirectMusicPerformance __RPC_FAR *__RPC_FAR *ret)
  776. {
  777. IDirectMusicPerformance *pPerf=NULL;
  778. if (FAILED(CoCreateInstance(
  779. CLSID_DirectMusicPerformance,
  780. NULL,
  781. CLSCTX_INPROC,
  782. IID_IDirectMusicPerformance,
  783. (void**)&pPerf ))) {
  784. return E_NOINTERFACE;
  785. }
  786. if (!pPerf) return E_FAIL;
  787. INTERNAL_CREATE_NOADDREF(_dxj_DirectMusicPerformance,pPerf,ret);
  788. return S_OK;
  789. }
  790. STDMETHODIMP C_dxj_DirectX7Object::getWindowRect(
  791. /* [in] */ long hwnd,
  792. /* [out][in] */ Rect __RPC_FAR *r)
  793. {
  794. BOOL b=GetWindowRect((HWND)hwnd,(LPRECT)r);
  795. if (!b) return E_FAIL;
  796. return S_OK;
  797. }
  798. STDMETHODIMP C_dxj_DirectX7Object::createEvent(
  799. /* [in] */ I_dxj_DirectXEvent __RPC_FAR *event,
  800. /* [retval][out] */ long __RPC_FAR *h)
  801. {
  802. HRESULT hr;
  803. LPSTREAM pStm=NULL;
  804. IUnknown *pUnk=NULL;
  805. HANDLE hEvent=NULL;
  806. EVENTTHREADINFO *pNewEvent=NULL;
  807. EVENTTHREADINFO *pTemp=NULL;
  808. if (!event) return E_INVALIDARG;
  809. if (!h) return E_INVALIDARG;
  810. pNewEvent=(EVENTTHREADINFO*)malloc(sizeof(EVENTTHREADINFO));
  811. if (!pNewEvent) return E_OUTOFMEMORY;
  812. pNewEvent->pNext=NULL;
  813. pNewEvent->fEnd=FALSE;
  814. pNewEvent->pCallback=event;
  815. pNewEvent->pStream=NULL;
  816. pNewEvent->threadID=0;
  817. event->AddRef();
  818. pNewEvent->hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
  819. if (!pNewEvent->hEvent){
  820. free(pNewEvent);
  821. event->Release();
  822. return E_FAIL;
  823. }
  824. //pNewEvent->hEndEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
  825. hr=event->QueryInterface(IID_IUnknown,(void**)&pUnk);
  826. if FAILED(hr) {
  827. free(pNewEvent);
  828. event->Release();
  829. return E_FAIL;
  830. }
  831. hr=CoMarshalInterThreadInterfaceInStream(IID_IUnknown,pUnk,&pStm);
  832. if (pUnk) pUnk->Release();
  833. if FAILED(hr) {
  834. free(pNewEvent);
  835. event->Release();
  836. return E_FAIL;
  837. }
  838. pNewEvent->pStream=pStm;
  839. pNewEvent->hThread=CreateThread(NULL,0,ThreadFunc,(unsigned long*)pNewEvent,CREATE_SUSPENDED ,&pNewEvent->threadID);
  840. if (!pNewEvent->threadID) {
  841. CloseHandle(pNewEvent->hEvent);
  842. free(pNewEvent);
  843. event->Release();
  844. return E_FAIL;
  845. }
  846. if (!m_pEventList){
  847. m_pEventList=pNewEvent;
  848. }
  849. else{
  850. pTemp=m_pEventList;
  851. m_pEventList=pNewEvent;
  852. pNewEvent->pNext=pTemp;
  853. }
  854. ResumeThread(pNewEvent->hThread);
  855. *h=(long)PtrToLong(pNewEvent->hEvent); //bugbug SUNDOWN -
  856. return S_OK;
  857. }
  858. STDMETHODIMP C_dxj_DirectX7Object::setEvent(
  859. /* [in] */ long eventId)
  860. {
  861. SetEvent((HANDLE)eventId);
  862. return S_OK;
  863. }
  864. STDMETHODIMP C_dxj_DirectX7Object::destroyEvent(
  865. /* [in] */ long eventId)
  866. {
  867. //find the info on the stack
  868. if (!m_pEventList) return E_INVALIDARG;
  869. EVENTTHREADINFO *pTemp=NULL;
  870. EVENTTHREADINFO *pLast=NULL;
  871. //rely on lazy evaluation
  872. for (pTemp=m_pEventList; ((pTemp)&&(pTemp->hEvent!=(HANDLE)eventId));pLast=pTemp,pTemp=pTemp->pNext);
  873. if (!pTemp) return E_INVALIDARG;
  874. //remove it from our Link List
  875. if (!pLast) {
  876. m_pEventList=pTemp->pNext;
  877. }
  878. else {
  879. pLast->pNext=pTemp->pNext;
  880. }
  881. //indicate that we want to kill the thread
  882. pTemp->fEnd=TRUE;
  883. //Fire the event in case we are waiting
  884. if (pTemp->hEvent) SetEvent(pTemp->hEvent);
  885. //Wait for it to finish out
  886. if (pTemp->hThread) WaitForSingleObject(pTemp->hThread,1000);
  887. //wait for the end event to signal
  888. //if (pTemp->hEndEvent) WaitForSingleObject(pTemp->hEndEvent,1000);
  889. //desctroy the event
  890. if (pTemp->hEvent) CloseHandle(pTemp->hEvent);
  891. //if (pTemp->hEndEvent) CloseHandle (pTemp->hEndEvent);
  892. if (pTemp->pCallback) pTemp->pCallback->Release();
  893. //thread is gone..
  894. //free the memory
  895. free(pTemp);
  896. return S_OK;
  897. }
  898. DWORD WINAPI ThreadFunc(LPVOID param){
  899. HRESULT hr;
  900. IUnknown *pUnk=NULL;
  901. EVENTTHREADINFO *pCntrl=(EVENTTHREADINFO *)param;
  902. I_dxj_DirectXEvent *pVBCallback=NULL;
  903. OleInitialize(NULL);
  904. LCID LOCAL_SYSTEM_DEFAULT=GetSystemDefaultLCID();
  905. //note pstrm is released even on failure
  906. hr=CoGetInterfaceAndReleaseStream(pCntrl->pStream,IID_IUnknown,(void**)&pUnk);
  907. pCntrl->pCallback=NULL; //since released to 0
  908. if FAILED(hr) return -1;
  909. if (!pUnk) return -1;
  910. hr=pUnk->QueryInterface(IID_I_dxj_DirectXEvent,(void**)&pVBCallback);
  911. pUnk->Release();
  912. if FAILED(hr) return -1;
  913. while (pCntrl->fEnd==FALSE)
  914. {
  915. WaitForSingleObject(pCntrl->hEvent,INFINITE);
  916. if ((pVBCallback )&&(pCntrl->fEnd==FALSE))
  917. {
  918. pVBCallback->AddRef();
  919. pVBCallback->DXCallback((long)PtrToLong(pCntrl->hEvent)); //bugbug SUNDOWN
  920. pVBCallback->Release();
  921. }
  922. }
  923. if (pVBCallback) pVBCallback->Release();
  924. OleUninitialize();
  925. //we need to syncronize the ending of the thread..
  926. //if (pCntrl->hEndEvent) SetEvent(pCntrl->hEndEvent);
  927. return 0;
  928. }
  929. STDMETHODIMP C_dxj_DirectX7Object::createD3DVertex(float x, float y, float z, float nx, float ny, float nz, float tu, float tv, D3dVertex *v)
  930. {
  931. if (!v) return E_INVALIDARG;
  932. ((D3DVERTEX*)v)->x=x;
  933. ((D3DVERTEX*)v)->y=y;
  934. ((D3DVERTEX*)v)->z=z;
  935. ((D3DVERTEX*)v)->nx=nx;
  936. ((D3DVERTEX*)v)->ny=ny;
  937. ((D3DVERTEX*)v)->nz=nz;
  938. ((D3DVERTEX*)v)->tu=tu;
  939. ((D3DVERTEX*)v)->tv=tv;
  940. return S_OK;
  941. }
  942. STDMETHODIMP C_dxj_DirectX7Object::createD3DLVertex(float x, float y, float z, long color, long specular, float tu, float tv, D3dLVertex *v)
  943. {
  944. if (!v) return E_INVALIDARG;
  945. ((D3DLVERTEX*)v)->x=x;
  946. ((D3DLVERTEX*)v)->y=y;
  947. ((D3DLVERTEX*)v)->z=z;
  948. ((D3DLVERTEX*)v)->color=color;
  949. ((D3DLVERTEX*)v)->specular=specular;
  950. ((D3DLVERTEX*)v)->dwReserved=0;
  951. ((D3DLVERTEX*)v)->tu=tu;
  952. ((D3DLVERTEX*)v)->tv=tv;
  953. return S_OK;
  954. }
  955. STDMETHODIMP C_dxj_DirectX7Object::createD3DTLVertex(float sx, float sy, float sz, float rhw, long color, long specular, float tu, float tv, D3dTLVertex *v)
  956. {
  957. if (!v) return E_INVALIDARG;
  958. ((D3DTLVERTEX*)v)->sx=sx;
  959. ((D3DTLVERTEX*)v)->sy=sy;
  960. ((D3DTLVERTEX*)v)->sz=sz;
  961. ((D3DTLVERTEX*)v)->rhw=rhw;
  962. ((D3DTLVERTEX*)v)->color=color;
  963. ((D3DTLVERTEX*)v)->specular=specular;
  964. ((D3DTLVERTEX*)v)->tu=tu;
  965. ((D3DTLVERTEX*)v)->tv=tv;
  966. return S_OK;
  967. }
  968. STDMETHODIMP C_dxj_DirectX7Object::createNewGuid(BSTR *ret)
  969. {
  970. HRESULT hr;
  971. GUID g=GUID_NULL;
  972. if (!ret) return E_INVALIDARG;
  973. hr=::CoCreateGuid(&g);
  974. if FAILED(hr) return hr;
  975. *ret=GUIDtoBSTR(&g);
  976. return S_OK;
  977. }