Source code of Windows XP (NT5)
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.

879 lines
22 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: ddraw7obj.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // dDrawObj.cpp : Implementation of CDirectApp and DLL registration.
  11. #include "stdafx.h"
  12. #include "Direct.h"
  13. #include "dms.h"
  14. #include "dDraw7Obj.h"
  15. #include "ddClipperObj.h"
  16. #include "ddSurface7Obj.h"
  17. #include "ddPaletteObj.h"
  18. #include "ddEnumModesObj.h"
  19. #include "ddEnumSurfacesObj.h"
  20. #include "d3d7Obj.h"
  21. #include "ddIdentifierObj.h"
  22. extern BOOL is4Bit;
  23. extern HRESULT CopyInDDSurfaceDesc2(DDSURFACEDESC2 *,DDSurfaceDesc2*);
  24. extern HRESULT CopyOutDDSurfaceDesc2(DDSurfaceDesc2*,DDSURFACEDESC2 *);
  25. ///////////////////////////////////////////////////////////////////
  26. // InternalAddRef
  27. ///////////////////////////////////////////////////////////////////
  28. DWORD C_dxj_DirectDraw7Object::InternalAddRef(){
  29. DWORD i;
  30. i=CComObjectRoot::InternalAddRef();
  31. DPF2(1,"DDraw7 [%d] AddRef %d \n",creationid,i);
  32. return i;
  33. }
  34. ///////////////////////////////////////////////////////////////////
  35. // InternalRelease
  36. ///////////////////////////////////////////////////////////////////
  37. DWORD C_dxj_DirectDraw7Object::InternalRelease(){
  38. DWORD i;
  39. i=CComObjectRoot::InternalRelease();
  40. DPF2(1,"DDraw4 [%d] Release %d \n",creationid,i);
  41. return i;
  42. }
  43. ///////////////////////////////////////////////////////////////////
  44. // C_dxj_DirectDraw7Object
  45. ///////////////////////////////////////////////////////////////////
  46. C_dxj_DirectDraw7Object::C_dxj_DirectDraw7Object(){
  47. DPF1(1,"Constructor Creation DirectDraw7Object[%d] \n ",g_creationcount);
  48. m__dxj_DirectDraw7= NULL;
  49. parent = NULL;
  50. pinterface = NULL;
  51. nextobj = g_dxj_DirectDraw7;
  52. creationid = ++g_creationcount;
  53. g_dxj_DirectDraw7 = (void *)this;
  54. m_hwnd=NULL;
  55. }
  56. ///////////////////////////////////////////////////////////////////
  57. // ~C_dxj_DirectDraw7Object
  58. ///////////////////////////////////////////////////////////////////
  59. C_dxj_DirectDraw7Object::~C_dxj_DirectDraw7Object()
  60. {
  61. DPF(1,"Entering ~DirectDraw7Object destructor \n");
  62. C_dxj_DirectDraw7Object *prev=NULL;
  63. for(C_dxj_DirectDraw7Object *ptr=(C_dxj_DirectDraw7Object *)g_dxj_DirectDraw7; ptr; ptr=(C_dxj_DirectDraw7Object *)ptr->nextobj)
  64. {
  65. if(ptr == this)
  66. {
  67. if(prev)
  68. prev->nextobj = ptr->nextobj;
  69. else
  70. g_dxj_DirectDraw7 = (void*)ptr->nextobj;
  71. DPF(1,"DirectDraw7Object found in g_dxj list now removed\n");
  72. break;
  73. }
  74. prev = ptr;
  75. }
  76. if(m__dxj_DirectDraw7){
  77. int count = IUNK(m__dxj_DirectDraw7)->Release();
  78. #ifdef DEBUG
  79. char buffer[256];
  80. wsprintf(buffer,"DirectX IDirectDraw7 Ref count [%d] \n",count);
  81. #endif
  82. if(count==0) m__dxj_DirectDraw7 = NULL;
  83. }
  84. if(parent) IUNK(parent)->Release();
  85. }
  86. ///////////////////////////////////////////////////////////////////
  87. // InternalGetObject
  88. // InternalSetObject
  89. // restoreDisplayMode
  90. // flipToGDISurface
  91. // setDisplayMode
  92. ///////////////////////////////////////////////////////////////////
  93. GETSET_OBJECT(_dxj_DirectDraw7);
  94. PASS_THROUGH_R(_dxj_DirectDraw7, restoreDisplayMode, RestoreDisplayMode)
  95. PASS_THROUGH_R(_dxj_DirectDraw7, flipToGDISurface, FlipToGDISurface)
  96. PASS_THROUGH5_R(_dxj_DirectDraw7, setDisplayMode, SetDisplayMode, long,long,long,long,long)
  97. ///////////////////////////////////////////////////////////////////
  98. // getMonitorFrequency
  99. ///////////////////////////////////////////////////////////////////
  100. STDMETHODIMP C_dxj_DirectDraw7Object::getMonitorFrequency(long *ret)
  101. {
  102. HRESULT hr;
  103. hr=m__dxj_DirectDraw7->GetMonitorFrequency((DWORD*)ret);
  104. return hr;
  105. }
  106. ///////////////////////////////////////////////////////////////////
  107. // getGDISurface
  108. ///////////////////////////////////////////////////////////////////
  109. STDMETHODIMP C_dxj_DirectDraw7Object::getGDISurface(I_dxj_DirectDrawSurface7 **rv)
  110. {
  111. LPDIRECTDRAWSURFACE7 lp4=NULL;
  112. if ( is4Bit )
  113. return E_FAIL;
  114. *rv = NULL;
  115. HRESULT hr = DD_OK;
  116. if( ( hr=m__dxj_DirectDraw7->GetGDISurface(&lp4) ) != DD_OK)
  117. return hr;
  118. INTERNAL_CREATE(_dxj_DirectDrawSurface7, lp4, rv);
  119. return hr;
  120. }
  121. ///////////////////////////////////////////////////////////////////
  122. // getVerticalBlankStatus
  123. ///////////////////////////////////////////////////////////////////
  124. STDMETHODIMP C_dxj_DirectDraw7Object::getVerticalBlankStatus( long *status)
  125. {
  126. if ( is4Bit )
  127. return E_FAIL;
  128. return m__dxj_DirectDraw7->GetVerticalBlankStatus((int *)status);
  129. }
  130. ///////////////////////////////////////////////////////////////////
  131. // setCooperativeLevel
  132. ///////////////////////////////////////////////////////////////////
  133. STDMETHODIMP C_dxj_DirectDraw7Object::setCooperativeLevel( HWnd hwn, long flags)
  134. {
  135. if ( is4Bit )
  136. return E_FAIL;
  137. m_hwnd = (HWND)hwn;
  138. return m__dxj_DirectDraw7->SetCooperativeLevel((HWND)hwn, (DWORD)flags);
  139. }
  140. ///////////////////////////////////////////////////////////////////
  141. // waitForVerticalBlank
  142. ///////////////////////////////////////////////////////////////////
  143. STDMETHODIMP C_dxj_DirectDraw7Object::waitForVerticalBlank(long flags,long handle, long *status)
  144. {
  145. if ( is4Bit )
  146. return E_FAIL;
  147. *status = m__dxj_DirectDraw7->WaitForVerticalBlank(flags, (void *)handle);
  148. return S_OK;
  149. }
  150. ///////////////////////////////////////////////////////////////////
  151. // createClipper
  152. ///////////////////////////////////////////////////////////////////
  153. STDMETHODIMP C_dxj_DirectDraw7Object::createClipper(long flags, I_dxj_DirectDrawClipper **val)
  154. {
  155. if ( is4Bit )
  156. return E_FAIL;
  157. DPF1(1,"enter DDraw4[%d]::createClipper ",creationid);
  158. //
  159. // need to create one of MY surfaces!
  160. //
  161. LPDIRECTDRAWCLIPPER ddc;
  162. HRESULT hr = DD_OK;
  163. if( (hr=m__dxj_DirectDraw7->CreateClipper( flags, &ddc, NULL)) != DD_OK )
  164. return hr;
  165. INTERNAL_CREATE(_dxj_DirectDrawClipper, ddc, val);
  166. DPF1(1,"exit DDraw4[%d]::createClipper ",creationid);
  167. return hr;
  168. }
  169. ///////////////////////////////////////////////////////////////////
  170. // createPalette
  171. ///////////////////////////////////////////////////////////////////
  172. STDMETHODIMP C_dxj_DirectDraw7Object::createPalette(long flags, SAFEARRAY **pe, I_dxj_DirectDrawPalette **val)
  173. {
  174. LPPALETTEENTRY ppe;
  175. if ( is4Bit )
  176. return E_FAIL;
  177. if (!ISSAFEARRAY1D(pe,(DWORD)256)) return E_INVALIDARG;
  178. ppe = (LPPALETTEENTRY)((SAFEARRAY*)*pe)->pvData;
  179. LPDIRECTDRAWPALETTE ddp;
  180. HRESULT hr = DD_OK;
  181. *val = NULL;
  182. if( (hr=m__dxj_DirectDraw7->CreatePalette( flags, (LPPALETTEENTRY)ppe, &ddp, NULL)) == DD_OK )
  183. {
  184. INTERNAL_CREATE( _dxj_DirectDrawPalette, ddp, val);
  185. }
  186. return hr;
  187. }
  188. ///////////////////////////////////////////////////////////////////
  189. // createSurface
  190. ///////////////////////////////////////////////////////////////////
  191. STDMETHODIMP C_dxj_DirectDraw7Object::createSurface(DDSurfaceDesc2 *dd, I_dxj_DirectDrawSurface7 **retval)
  192. {
  193. HRESULT retv;
  194. LPDIRECTDRAWSURFACE7 dds7; // DirectX object pointer
  195. DDSURFACEDESC2 ddsd;
  196. DPF1(1,"enter DDraw7[%d]::createSurface ",creationid);
  197. if ( is4Bit )
  198. return E_FAIL;
  199. if(! (dd && retval) )
  200. return E_POINTER;
  201. CopyInDDSurfaceDesc2(&ddsd,dd);
  202. //docdoc: CreateSurface returns error if 'punk' is anything but NULL
  203. retv = m__dxj_DirectDraw7->CreateSurface( &ddsd, &dds7, NULL);
  204. if FAILED(retv) return retv;
  205. INTERNAL_CREATE(_dxj_DirectDrawSurface7, dds7, retval);
  206. dd->lpSurface = NULL;
  207. DPF1(1,"exit DDraw7[%d]::createSurface ",creationid);
  208. return S_OK;
  209. }
  210. ///////////////////////////////////////////////////////////////////
  211. // duplicateSurface
  212. ///////////////////////////////////////////////////////////////////
  213. STDMETHODIMP C_dxj_DirectDraw7Object::duplicateSurface(I_dxj_DirectDrawSurface7 *ddIn, I_dxj_DirectDrawSurface7 **ddOut)
  214. {
  215. HRESULT retval;
  216. if ( is4Bit )
  217. return E_FAIL;
  218. //
  219. // need to create one of MY surfaces!
  220. //
  221. LPDIRECTDRAWSURFACE7 lpddout7=NULL;
  222. DO_GETOBJECT_NOTNULL( LPDIRECTDRAWSURFACE7, lpddin, ddIn);
  223. if( (retval = m__dxj_DirectDraw7->DuplicateSurface(lpddin, &lpddout7)) != DD_OK )
  224. return retval;
  225. INTERNAL_CREATE( _dxj_DirectDrawSurface7, lpddout7, ddOut);
  226. return S_OK;
  227. }
  228. ///////////////////////////////////////////////////////////////////
  229. // getCaps
  230. ///////////////////////////////////////////////////////////////////
  231. STDMETHODIMP C_dxj_DirectDraw7Object::getCaps(DDCaps *driverCaps, DDCaps *HELcaps)
  232. {
  233. if ( is4Bit )
  234. return E_FAIL;
  235. if (!driverCaps) return E_INVALIDARG;
  236. if (!HELcaps) return E_INVALIDARG;
  237. ((DDCAPS*)driverCaps)->dwSize=sizeof(DDCAPS);
  238. ((DDCAPS*)HELcaps)->dwSize=sizeof(DDCAPS);
  239. HRESULT hr = m__dxj_DirectDraw7->GetCaps((DDCAPS*)driverCaps, (DDCAPS*)HELcaps);
  240. return hr;
  241. }
  242. ///////////////////////////////////////////////////////////////////
  243. // getDisplayMode
  244. ///////////////////////////////////////////////////////////////////
  245. STDMETHODIMP C_dxj_DirectDraw7Object::getDisplayMode(DDSurfaceDesc2 *desc)
  246. {
  247. HRESULT retval;
  248. DDSURFACEDESC2 ddsd;
  249. if (!desc) return E_INVALIDARG;
  250. CopyInDDSurfaceDesc2(&ddsd,desc);
  251. retval = m__dxj_DirectDraw7->GetDisplayMode(&ddsd);
  252. if( retval != S_OK)
  253. return retval;
  254. CopyOutDDSurfaceDesc2(desc,&ddsd);
  255. desc->lpSurface = NULL;
  256. return S_OK;
  257. }
  258. ///////////////////////////////////////////////////////////////////
  259. // getAvailableTotalMem
  260. ///////////////////////////////////////////////////////////////////
  261. STDMETHODIMP C_dxj_DirectDraw7Object::getAvailableTotalMem(DDSCaps2 *ddsCaps, long *m)
  262. {
  263. return m__dxj_DirectDraw7->GetAvailableVidMem((LPDDSCAPS2)ddsCaps, (unsigned long *)m, NULL);
  264. }
  265. ///////////////////////////////////////////////////////////////////
  266. // getFreeMem
  267. ///////////////////////////////////////////////////////////////////
  268. STDMETHODIMP C_dxj_DirectDraw7Object::getFreeMem(DDSCaps2 *ddsCaps, long *m)
  269. {
  270. return m__dxj_DirectDraw7->GetAvailableVidMem((LPDDSCAPS2)ddsCaps, NULL, (unsigned long *)m);
  271. }
  272. ///////////////////////////////////////////////////////////////////
  273. // getDirect3D
  274. ///////////////////////////////////////////////////////////////////
  275. STDMETHODIMP C_dxj_DirectDraw7Object::getDirect3D(I_dxj_Direct3d7 **retval)
  276. {
  277. LPDIRECT3D7 lpD3D;
  278. HRESULT hr = DD_OK;
  279. if ( is4Bit )
  280. return E_FAIL;
  281. if( (hr=m__dxj_DirectDraw7->QueryInterface(IID_IDirect3D7, (void**) &lpD3D)) != DD_OK)
  282. return hr;
  283. INTERNAL_CREATE(_dxj_Direct3d7, lpD3D, retval);
  284. return hr;
  285. }
  286. ///////////////////////////////////////////////////////////////////
  287. // getNumFourCCCodes
  288. ///////////////////////////////////////////////////////////////////
  289. STDMETHODIMP C_dxj_DirectDraw7Object::getNumFourCCCodes(long *retval)
  290. {
  291. return m__dxj_DirectDraw7->GetFourCCCodes((DWORD*)retval, NULL);
  292. }
  293. ///////////////////////////////////////////////////////////////////
  294. // getScanLine
  295. ///////////////////////////////////////////////////////////////////
  296. STDMETHODIMP C_dxj_DirectDraw7Object::getScanLine(long *lines, long *status)
  297. {
  298. *status = (long)m__dxj_DirectDraw7->GetScanLine((DWORD*)lines);
  299. return S_OK;
  300. }
  301. ///////////////////////////////////////////////////////////////////
  302. // loadPaletteFromBitmap
  303. ///////////////////////////////////////////////////////////////////
  304. STDMETHODIMP C_dxj_DirectDraw7Object::loadPaletteFromBitmap(BSTR bName, I_dxj_DirectDrawPalette **retval)
  305. {
  306. USES_CONVERSION;
  307. IDirectDrawPalette* ddpal;
  308. int i;
  309. int n;
  310. int fh;
  311. HRSRC h;
  312. LPBITMAPINFOHEADER lpbi;
  313. PALETTEENTRY ape[256];
  314. RGBQUAD * prgb;
  315. HRESULT hr=S_OK;
  316. if ( is4Bit )
  317. return E_FAIL;
  318. LPCTSTR szBitmap = W2T(bName);
  319. for (i=0; i<256; i++) // build a 332 palette as the default
  320. {
  321. ape[i].peRed = (BYTE)(((i >> 5) & 0x07) * 255 / 7);
  322. ape[i].peGreen = (BYTE)(((i >> 2) & 0x07) * 255 / 7);
  323. ape[i].peBlue = (BYTE)(((i >> 0) & 0x03) * 255 / 3);
  324. ape[i].peFlags = (BYTE)0;
  325. }
  326. //
  327. // get a pointer to the bitmap resource.
  328. //
  329. if (szBitmap && (h = FindResource(NULL, szBitmap, RT_BITMAP)))
  330. {
  331. lpbi = (LPBITMAPINFOHEADER)LockResource(LoadResource(NULL, h));
  332. if (!lpbi){
  333. DPF(1,"lock resource failed\n");
  334. return E_OUTOFMEMORY; // error
  335. }
  336. prgb = (RGBQUAD*)((BYTE*)lpbi + lpbi->biSize);
  337. if (lpbi == NULL || lpbi->biSize < sizeof(BITMAPINFOHEADER))
  338. n = 0;
  339. else if (lpbi->biBitCount > 8)
  340. n = 0;
  341. else if (lpbi->biClrUsed == 0)
  342. n = 1 << lpbi->biBitCount;
  343. else
  344. n = lpbi->biClrUsed;
  345. //
  346. // a DIB color table has its colors stored BGR not RGB
  347. // so flip them around.
  348. //
  349. for(i=0; i<n; i++ )
  350. {
  351. ape[i].peRed = prgb[i].rgbRed;
  352. ape[i].peGreen = prgb[i].rgbGreen;
  353. ape[i].peBlue = prgb[i].rgbBlue;
  354. ape[i].peFlags = 0;
  355. }
  356. }
  357. else if (szBitmap && (fh = _lopen(szBitmap, OF_READ)) != -1)
  358. {
  359. BITMAPFILEHEADER bf;
  360. BITMAPINFOHEADER bi;
  361. _lread(fh, &bf, sizeof(bf));
  362. _lread(fh, &bi, sizeof(bi));
  363. _lread(fh, ape, sizeof(ape));
  364. _lclose(fh);
  365. if (bi.biSize != sizeof(BITMAPINFOHEADER))
  366. n = 0;
  367. else if (bi.biBitCount > 8)
  368. n = 0;
  369. else if (bi.biClrUsed == 0)
  370. n = 1 << bi.biBitCount;
  371. else
  372. n = bi.biClrUsed;
  373. //
  374. // a DIB color table has its colors stored BGR not RGB
  375. // so flip them around.
  376. //
  377. for(i=0; i<n; i++ )
  378. {
  379. BYTE r = ape[i].peRed;
  380. ape[i].peRed = ape[i].peBlue;
  381. ape[i].peBlue = r;
  382. }
  383. }
  384. m__dxj_DirectDraw7->CreatePalette(DDPCAPS_8BIT, ape, &ddpal, NULL);
  385. if( ddpal )
  386. {
  387. INTERNAL_CREATE(_dxj_DirectDrawPalette, ddpal, retval);
  388. }
  389. else
  390. {
  391. //
  392. // no object, set the return value to NULL as well.
  393. //
  394. *retval = NULL;
  395. hr = E_FAIL;
  396. }
  397. return hr;
  398. }
  399. ///////////////////////////////////////////////////////////////////
  400. // createSurfaceFromFile
  401. ///////////////////////////////////////////////////////////////////
  402. STDMETHODIMP C_dxj_DirectDraw7Object::createSurfaceFromFile(BSTR file, DDSurfaceDesc2 *desc, I_dxj_DirectDrawSurface7 **surf)
  403. {
  404. DPF1(1,"enter DDraw7[%d]::createSurfaceFromFile ",creationid);
  405. HDC hdc;
  406. HDC hdcImage;
  407. BITMAP bm;
  408. HRESULT hr;
  409. HBITMAP hbm;
  410. HRESULT retv;
  411. LPDIRECTDRAWSURFACE7 dds7; // DirectX object pointer
  412. LPSTR szFileName=NULL;
  413. int width=0;
  414. int height=0;
  415. if ( is4Bit )
  416. return E_FAIL;
  417. if(! (desc && surf) )
  418. return E_POINTER;
  419. USES_CONVERSION;
  420. szFileName=W2T(file);
  421. //If width and height are zero then we will generate our own width and
  422. //height from the bitmap.
  423. //The LoadImage api however doesnt work propery without size params
  424. //Consider there must be a way to make it work.
  425. if ((desc->lWidth!=0)&&(desc->lHeight!=0)&&(desc->lFlags & DDSD_WIDTH)&&(desc->lFlags & DDSD_HEIGHT))
  426. {
  427. width=desc->lWidth ;
  428. height=desc->lHeight;
  429. }
  430. if (desc->lFlags==0) {
  431. desc->lFlags=DDSD_CAPS;
  432. ((DDSURFACEDESC*)desc)->ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
  433. }
  434. if (!szFileName) return CTL_E_FILENOTFOUND;
  435. hbm = (HBITMAP)LoadImage((HINSTANCE)NULL, szFileName, IMAGE_BITMAP,
  436. width, height,
  437. LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  438. DWORD dwErr=GetLastError();
  439. if (!hbm){
  440. //often users pass in width and height in twips and not pixels
  441. //loadimage compails and returns ERROR_NOT_ENOUGH_MEMORY
  442. if (dwErr==ERROR_NOT_ENOUGH_MEMORY)
  443. {
  444. return E_OUTOFMEMORY;
  445. }
  446. else if (dwErr==ERROR_INVALID_PARAMETER)
  447. {
  448. return E_INVALIDARG;
  449. }
  450. else {
  451. return CTL_E_FILENOTFOUND;
  452. }
  453. }
  454. // get size of the bitmap
  455. //
  456. GetObject(hbm, sizeof(bm), &bm); // get size of bitmap
  457. width=bm.bmWidth;
  458. height=bm.bmHeight;
  459. desc->lFlags = desc->lFlags | DDSD_WIDTH | DDSD_HEIGHT;
  460. if ((desc->lWidth==0)||(desc->lHeight==0))
  461. {
  462. desc->lWidth =width;
  463. desc->lHeight =height;
  464. }
  465. DDSURFACEDESC2 ddsd;
  466. CopyInDDSurfaceDesc2(&ddsd,desc);
  467. if( (retv = m__dxj_DirectDraw7->CreateSurface(&ddsd, &dds7, NULL)) != DD_OK )
  468. return retv;
  469. CopyOutDDSurfaceDesc2(desc,&ddsd);
  470. INTERNAL_CREATE(_dxj_DirectDrawSurface7, dds7, surf);
  471. desc->lpSurface = NULL;
  472. //
  473. // make sure this surface is restored.
  474. //
  475. dds7->Restore();
  476. //
  477. // select bitmap into a memoryDC so we can use it.
  478. //
  479. hdcImage = CreateCompatibleDC(NULL);
  480. SelectObject(hdcImage, hbm);
  481. if (!hdcImage){
  482. DeleteObject(hbm);
  483. return E_FAIL;
  484. }
  485. if ((hr = dds7->GetDC(&hdc)) == DD_OK)
  486. {
  487. StretchBlt(hdc, 0, 0, desc->lWidth , desc->lHeight, hdcImage,
  488. 0, 0, width, height, SRCCOPY);
  489. dds7->ReleaseDC(hdc);
  490. }
  491. DeleteDC(hdcImage);
  492. if (hbm) DeleteObject(hbm);
  493. DPF1(buffer,"exit DDraw7[%d]::createSurfaceFromFile",creationid);
  494. return S_OK;
  495. }
  496. ///////////////////////////////////////////////////////////////////
  497. // createSurfaceFromResource
  498. ///////////////////////////////////////////////////////////////////
  499. STDMETHODIMP C_dxj_DirectDraw7Object::createSurfaceFromResource(BSTR resFile, BSTR resourceName, DDSurfaceDesc2 *desc, I_dxj_DirectDrawSurface7 **surf)
  500. {
  501. DPF1(1,"enter DDraw4[%d]::createSurfaceFromResource ",creationid);
  502. if ( is4Bit )
  503. return E_FAIL;
  504. if(! (desc && surf) )
  505. return E_POINTER;
  506. HRESULT hr;
  507. HRSRC hres=NULL;
  508. HGLOBAL hglob=NULL;
  509. HDC hdc;
  510. HDC hdcImage;
  511. BITMAP bm;
  512. HBITMAP hbm;
  513. HRESULT retv;
  514. LPDIRECTDRAWSURFACE7 dds7; // DirectX object pointer
  515. LPSTR szResName=NULL;
  516. if (!resourceName) return E_INVALIDARG;
  517. if (!surf) return E_INVALIDARG;
  518. HMODULE hMod=NULL;
  519. USES_CONVERSION;
  520. if ((resFile) &&(resFile[0]!=0)){
  521. // NOTE:
  522. // seems that GetModuleHandleW is
  523. // always returning 0 on w98
  524. // so we must convert to ansi first
  525. LPCTSTR pszName = W2T(resFile);
  526. hMod= GetModuleHandle(pszName);
  527. }
  528. else {
  529. hMod= GetModuleHandle(NULL);
  530. }
  531. LPCTSTR pszName2 = W2T(resourceName);
  532. //hbm = (HBITMAP)LoadImageW((HINSTANCE)hMod, resourceName,
  533. // IMAGE_BITMAP,
  534. // 0, 0,
  535. // LR_CREATEDIBSECTION);
  536. hbm = (HBITMAP)LoadImage((HINSTANCE)hMod,
  537. pszName2,
  538. IMAGE_BITMAP,
  539. 0, 0,
  540. LR_CREATEDIBSECTION);
  541. if (!hbm){
  542. //MessageBox(NULL,"FAILED ON LOAD IMAGE","TEST",MB_OK);
  543. return E_FAIL;
  544. }
  545. // get size of the bitmap
  546. //
  547. GetObject(hbm, sizeof(bm), &bm); // get size of bitmap
  548. DWORD width=bm.bmWidth;
  549. DWORD height=bm.bmHeight;
  550. desc->lFlags = desc->lFlags | DDSD_WIDTH | DDSD_HEIGHT;
  551. if ((desc->lWidth==0)||(desc->lHeight==0))
  552. {
  553. desc->lWidth =width;
  554. desc->lHeight =height;
  555. }
  556. DDSURFACEDESC2 ddsd;
  557. CopyInDDSurfaceDesc2(&ddsd,desc);
  558. if( (retv = m__dxj_DirectDraw7->CreateSurface(&ddsd, &dds7, NULL)) != DD_OK ){
  559. if (hbm) DeleteObject(hbm);
  560. return retv;
  561. }
  562. CopyOutDDSurfaceDesc2(desc,&ddsd);
  563. INTERNAL_CREATE(_dxj_DirectDrawSurface7, dds7, surf);
  564. desc->lpSurface = NULL;
  565. //
  566. // make sure this surface is restored.
  567. //
  568. dds7->Restore();
  569. //
  570. // select bitmap into a memoryDC so we can use it.
  571. //
  572. hdcImage = CreateCompatibleDC(NULL);
  573. if (!hdcImage){
  574. DeleteObject(hbm);
  575. return E_OUTOFMEMORY;
  576. }
  577. SelectObject(hdcImage, hbm);
  578. if ((hr = dds7->GetDC(&hdc)) == DD_OK)
  579. {
  580. StretchBlt(hdc, 0, 0, desc->lWidth , desc->lHeight, hdcImage,
  581. 0, 0, width, height, SRCCOPY);
  582. dds7->ReleaseDC(hdc);
  583. }
  584. DeleteDC(hdcImage);
  585. if (hbm) DeleteObject(hbm);
  586. DPF1(1r,"exit DDraw4[%d]::createSurfaceFromFile",creationid);
  587. return S_OK;
  588. }
  589. ///////////////////////////////////////////////////////////////////
  590. // getFourCCCodes
  591. ///////////////////////////////////////////////////////////////////
  592. STDMETHODIMP C_dxj_DirectDraw7Object::getFourCCCodes(SAFEARRAY **ppsa)
  593. {
  594. DWORD count= ((SAFEARRAY*)*ppsa)->rgsabound[0].cElements;
  595. if ( ((SAFEARRAY*)*ppsa)->cDims!=1) return E_INVALIDARG;
  596. return m__dxj_DirectDraw7->GetFourCCCodes(&count,(DWORD*)((SAFEARRAY*)*ppsa)->pvData);
  597. }
  598. ///////////////////////////////////////////////////////////////////
  599. // getDisplayModesEnum
  600. ///////////////////////////////////////////////////////////////////
  601. STDMETHODIMP C_dxj_DirectDraw7Object::getDisplayModesEnum(
  602. /* [in] */ long flags,
  603. /* [in] */ DDSurfaceDesc2 *ddsd,
  604. /* [retval][out] */ I_dxj_DirectDrawEnumModes __RPC_FAR *__RPC_FAR *retval)
  605. {
  606. HRESULT hr;
  607. hr=C_dxj_DirectDrawEnumModesObject::create(m__dxj_DirectDraw7,flags, ddsd, retval);
  608. return hr;
  609. }
  610. ///////////////////////////////////////////////////////////////////
  611. // testCooperativeLevel
  612. ///////////////////////////////////////////////////////////////////
  613. STDMETHODIMP C_dxj_DirectDraw7Object::testCooperativeLevel(
  614. /* [in,out] */ long *status)
  615. {
  616. HRESULT hr;
  617. hr=m__dxj_DirectDraw7->TestCooperativeLevel();
  618. *status=(long)hr;
  619. return S_OK;
  620. }
  621. ///////////////////////////////////////////////////////////////////
  622. // restoreAllSurfaces
  623. ///////////////////////////////////////////////////////////////////
  624. STDMETHODIMP C_dxj_DirectDraw7Object::restoreAllSurfaces()
  625. {
  626. HRESULT hr;
  627. hr=m__dxj_DirectDraw7->RestoreAllSurfaces();
  628. return hr;
  629. }
  630. STDMETHODIMP C_dxj_DirectDraw7Object::getSurfaceFromDC(long hdc, I_dxj_DirectDrawSurface7 **ret)
  631. {
  632. HRESULT hr;
  633. LPDIRECTDRAWSURFACE7 pDDS=NULL;
  634. hr=m__dxj_DirectDraw7->GetSurfaceFromDC((HDC)hdc,&pDDS);
  635. if FAILED(hr) return hr;
  636. INTERNAL_CREATE(_dxj_DirectDrawSurface7,pDDS,ret);
  637. return hr;
  638. }
  639. STDMETHODIMP C_dxj_DirectDraw7Object::getSurfacesEnum(
  640. /* [in] */ long flags,
  641. /* [in] */ DDSurfaceDesc2 __RPC_FAR *desc,
  642. /* [retval][out] */ I_dxj_DirectDrawEnumSurfaces __RPC_FAR *__RPC_FAR *ret)
  643. {
  644. HRESULT hr=C_dxj_DirectDrawEnumSurfacesObject::create((I_dxj_DirectDraw7*)this , flags, desc,ret);
  645. return hr;
  646. }
  647. STDMETHODIMP C_dxj_DirectDraw7Object::getDeviceIdentifier(
  648. long flags,I_dxj_DirectDrawIdentifier **ret)
  649. {
  650. HRESULT hr;
  651. hr=C_dxj_DirectDrawIdentifierObject::Create(m__dxj_DirectDraw7,(DWORD)flags,ret);
  652. return hr;
  653. }