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.

818 lines
21 KiB

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