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.

882 lines
23 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 = NULL;
  319. __try { szBitmap = W2T(bName); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  320. for (i=0; i<256; i++) // build a 332 palette as the default
  321. {
  322. ape[i].peRed = (BYTE)(((i >> 5) & 0x07) * 255 / 7);
  323. ape[i].peGreen = (BYTE)(((i >> 2) & 0x07) * 255 / 7);
  324. ape[i].peBlue = (BYTE)(((i >> 0) & 0x03) * 255 / 3);
  325. ape[i].peFlags = (BYTE)0;
  326. }
  327. //
  328. // get a pointer to the bitmap resource.
  329. //
  330. if (szBitmap && (h = FindResource(NULL, szBitmap, RT_BITMAP)))
  331. {
  332. lpbi = (LPBITMAPINFOHEADER)LockResource(LoadResource(NULL, h));
  333. if (!lpbi){
  334. DPF(1,"lock resource failed\n");
  335. return E_OUTOFMEMORY; // error
  336. }
  337. prgb = (RGBQUAD*)((BYTE*)lpbi + lpbi->biSize);
  338. if (lpbi == NULL || lpbi->biSize < sizeof(BITMAPINFOHEADER))
  339. n = 0;
  340. else if (lpbi->biBitCount > 8)
  341. n = 0;
  342. else if (lpbi->biClrUsed == 0)
  343. n = 1 << lpbi->biBitCount;
  344. else
  345. n = lpbi->biClrUsed;
  346. //
  347. // a DIB color table has its colors stored BGR not RGB
  348. // so flip them around.
  349. //
  350. for(i=0; i<n; i++ )
  351. {
  352. ape[i].peRed = prgb[i].rgbRed;
  353. ape[i].peGreen = prgb[i].rgbGreen;
  354. ape[i].peBlue = prgb[i].rgbBlue;
  355. ape[i].peFlags = 0;
  356. }
  357. }
  358. else if (szBitmap && (fh = _lopen(szBitmap, OF_READ)) != -1)
  359. {
  360. BITMAPFILEHEADER bf;
  361. BITMAPINFOHEADER bi;
  362. _lread(fh, &bf, sizeof(bf));
  363. _lread(fh, &bi, sizeof(bi));
  364. _lread(fh, ape, sizeof(ape));
  365. _lclose(fh);
  366. if (bi.biSize != sizeof(BITMAPINFOHEADER))
  367. n = 0;
  368. else if (bi.biBitCount > 8)
  369. n = 0;
  370. else if (bi.biClrUsed == 0)
  371. n = 1 << bi.biBitCount;
  372. else
  373. n = bi.biClrUsed;
  374. //
  375. // a DIB color table has its colors stored BGR not RGB
  376. // so flip them around.
  377. //
  378. for(i=0; i<n; i++ )
  379. {
  380. BYTE r = ape[i].peRed;
  381. ape[i].peRed = ape[i].peBlue;
  382. ape[i].peBlue = r;
  383. }
  384. }
  385. m__dxj_DirectDraw7->CreatePalette(DDPCAPS_8BIT, ape, &ddpal, NULL);
  386. if( ddpal )
  387. {
  388. INTERNAL_CREATE(_dxj_DirectDrawPalette, ddpal, retval);
  389. }
  390. else
  391. {
  392. //
  393. // no object, set the return value to NULL as well.
  394. //
  395. *retval = NULL;
  396. hr = E_FAIL;
  397. }
  398. return hr;
  399. }
  400. ///////////////////////////////////////////////////////////////////
  401. // createSurfaceFromFile
  402. ///////////////////////////////////////////////////////////////////
  403. STDMETHODIMP C_dxj_DirectDraw7Object::createSurfaceFromFile(BSTR file, DDSurfaceDesc2 *desc, I_dxj_DirectDrawSurface7 **surf)
  404. {
  405. DPF1(1,"enter DDraw7[%d]::createSurfaceFromFile ",creationid);
  406. HDC hdc;
  407. HDC hdcImage;
  408. BITMAP bm;
  409. HRESULT hr;
  410. HBITMAP hbm;
  411. HRESULT retv;
  412. LPDIRECTDRAWSURFACE7 dds7; // DirectX object pointer
  413. LPSTR szFileName=NULL;
  414. int width=0;
  415. int height=0;
  416. if ( is4Bit )
  417. return E_FAIL;
  418. if(! (desc && surf) )
  419. return E_POINTER;
  420. USES_CONVERSION;
  421. __try { szFileName=W2T(file); } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  422. //If width and height are zero then we will generate our own width and
  423. //height from the bitmap.
  424. //The LoadImage api however doesnt work propery without size params
  425. //Consider there must be a way to make it work.
  426. if ((desc->lWidth!=0)&&(desc->lHeight!=0)&&(desc->lFlags & DDSD_WIDTH)&&(desc->lFlags & DDSD_HEIGHT))
  427. {
  428. width=desc->lWidth ;
  429. height=desc->lHeight;
  430. }
  431. if (desc->lFlags==0) {
  432. desc->lFlags=DDSD_CAPS;
  433. ((DDSURFACEDESC*)desc)->ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
  434. }
  435. if (!szFileName) return CTL_E_FILENOTFOUND;
  436. hbm = (HBITMAP)LoadImage((HINSTANCE)NULL, szFileName, IMAGE_BITMAP,
  437. width, height,
  438. LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  439. DWORD dwErr=GetLastError();
  440. if (!hbm){
  441. //often users pass in width and height in twips and not pixels
  442. //loadimage compails and returns ERROR_NOT_ENOUGH_MEMORY
  443. if (dwErr==ERROR_NOT_ENOUGH_MEMORY)
  444. {
  445. return E_OUTOFMEMORY;
  446. }
  447. else if (dwErr==ERROR_INVALID_PARAMETER)
  448. {
  449. return E_INVALIDARG;
  450. }
  451. else {
  452. return CTL_E_FILENOTFOUND;
  453. }
  454. }
  455. // get size of the bitmap
  456. //
  457. GetObject(hbm, sizeof(bm), &bm); // get size of bitmap
  458. width=bm.bmWidth;
  459. height=bm.bmHeight;
  460. desc->lFlags = desc->lFlags | DDSD_WIDTH | DDSD_HEIGHT;
  461. if ((desc->lWidth==0)||(desc->lHeight==0))
  462. {
  463. desc->lWidth =width;
  464. desc->lHeight =height;
  465. }
  466. DDSURFACEDESC2 ddsd;
  467. CopyInDDSurfaceDesc2(&ddsd,desc);
  468. if( (retv = m__dxj_DirectDraw7->CreateSurface(&ddsd, &dds7, NULL)) != DD_OK )
  469. return retv;
  470. CopyOutDDSurfaceDesc2(desc,&ddsd);
  471. INTERNAL_CREATE(_dxj_DirectDrawSurface7, dds7, surf);
  472. desc->lpSurface = NULL;
  473. //
  474. // make sure this surface is restored.
  475. //
  476. dds7->Restore();
  477. //
  478. // select bitmap into a memoryDC so we can use it.
  479. //
  480. hdcImage = CreateCompatibleDC(NULL);
  481. SelectObject(hdcImage, hbm);
  482. if (!hdcImage){
  483. DeleteObject(hbm);
  484. return E_FAIL;
  485. }
  486. if ((hr = dds7->GetDC(&hdc)) == DD_OK)
  487. {
  488. StretchBlt(hdc, 0, 0, desc->lWidth , desc->lHeight, hdcImage,
  489. 0, 0, width, height, SRCCOPY);
  490. dds7->ReleaseDC(hdc);
  491. }
  492. DeleteDC(hdcImage);
  493. if (hbm) DeleteObject(hbm);
  494. DPF1(buffer,"exit DDraw7[%d]::createSurfaceFromFile",creationid);
  495. return S_OK;
  496. }
  497. ///////////////////////////////////////////////////////////////////
  498. // createSurfaceFromResource
  499. ///////////////////////////////////////////////////////////////////
  500. STDMETHODIMP C_dxj_DirectDraw7Object::createSurfaceFromResource(BSTR resFile, BSTR resourceName, DDSurfaceDesc2 *desc, I_dxj_DirectDrawSurface7 **surf)
  501. {
  502. DPF1(1,"enter DDraw4[%d]::createSurfaceFromResource ",creationid);
  503. if ( is4Bit )
  504. return E_FAIL;
  505. if(! (desc && surf) )
  506. return E_POINTER;
  507. HRESULT hr;
  508. HRSRC hres=NULL;
  509. HGLOBAL hglob=NULL;
  510. HDC hdc;
  511. HDC hdcImage;
  512. BITMAP bm;
  513. HBITMAP hbm;
  514. HRESULT retv;
  515. LPDIRECTDRAWSURFACE7 dds7; // DirectX object pointer
  516. LPSTR szResName=NULL;
  517. if (!resourceName) return E_INVALIDARG;
  518. if (!surf) return E_INVALIDARG;
  519. HMODULE hMod=NULL;
  520. USES_CONVERSION;
  521. if ((resFile) &&(resFile[0]!=0)){
  522. // NOTE:
  523. // seems that GetModuleHandleW is
  524. // always returning 0 on w98
  525. // so we must convert to ansi first
  526. LPCTSTR pszName = NULL;
  527. __try { pszName = W2T(resFile); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  528. hMod= GetModuleHandle(pszName);
  529. }
  530. else {
  531. hMod= GetModuleHandle(NULL);
  532. }
  533. LPCTSTR pszName2 = NULL;
  534. __try { pszName2 = W2T(resourceName); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  535. //hbm = (HBITMAP)LoadImageW((HINSTANCE)hMod, resourceName,
  536. // IMAGE_BITMAP,
  537. // 0, 0,
  538. // LR_CREATEDIBSECTION);
  539. hbm = (HBITMAP)LoadImage((HINSTANCE)hMod,
  540. pszName2,
  541. IMAGE_BITMAP,
  542. 0, 0,
  543. LR_CREATEDIBSECTION);
  544. if (!hbm){
  545. //MessageBox(NULL,"FAILED ON LOAD IMAGE","TEST",MB_OK);
  546. return E_FAIL;
  547. }
  548. // get size of the bitmap
  549. //
  550. GetObject(hbm, sizeof(bm), &bm); // get size of bitmap
  551. DWORD width=bm.bmWidth;
  552. DWORD height=bm.bmHeight;
  553. desc->lFlags = desc->lFlags | DDSD_WIDTH | DDSD_HEIGHT;
  554. if ((desc->lWidth==0)||(desc->lHeight==0))
  555. {
  556. desc->lWidth =width;
  557. desc->lHeight =height;
  558. }
  559. DDSURFACEDESC2 ddsd;
  560. CopyInDDSurfaceDesc2(&ddsd,desc);
  561. if( (retv = m__dxj_DirectDraw7->CreateSurface(&ddsd, &dds7, NULL)) != DD_OK ){
  562. if (hbm) DeleteObject(hbm);
  563. return retv;
  564. }
  565. CopyOutDDSurfaceDesc2(desc,&ddsd);
  566. INTERNAL_CREATE(_dxj_DirectDrawSurface7, dds7, surf);
  567. desc->lpSurface = NULL;
  568. //
  569. // make sure this surface is restored.
  570. //
  571. dds7->Restore();
  572. //
  573. // select bitmap into a memoryDC so we can use it.
  574. //
  575. hdcImage = CreateCompatibleDC(NULL);
  576. if (!hdcImage){
  577. DeleteObject(hbm);
  578. return E_OUTOFMEMORY;
  579. }
  580. SelectObject(hdcImage, hbm);
  581. if ((hr = dds7->GetDC(&hdc)) == DD_OK)
  582. {
  583. StretchBlt(hdc, 0, 0, desc->lWidth , desc->lHeight, hdcImage,
  584. 0, 0, width, height, SRCCOPY);
  585. dds7->ReleaseDC(hdc);
  586. }
  587. DeleteDC(hdcImage);
  588. if (hbm) DeleteObject(hbm);
  589. DPF1(1r,"exit DDraw4[%d]::createSurfaceFromFile",creationid);
  590. return S_OK;
  591. }
  592. ///////////////////////////////////////////////////////////////////
  593. // getFourCCCodes
  594. ///////////////////////////////////////////////////////////////////
  595. STDMETHODIMP C_dxj_DirectDraw7Object::getFourCCCodes(SAFEARRAY **ppsa)
  596. {
  597. DWORD count= ((SAFEARRAY*)*ppsa)->rgsabound[0].cElements;
  598. if ( ((SAFEARRAY*)*ppsa)->cDims!=1) return E_INVALIDARG;
  599. return m__dxj_DirectDraw7->GetFourCCCodes(&count,(DWORD*)((SAFEARRAY*)*ppsa)->pvData);
  600. }
  601. ///////////////////////////////////////////////////////////////////
  602. // getDisplayModesEnum
  603. ///////////////////////////////////////////////////////////////////
  604. STDMETHODIMP C_dxj_DirectDraw7Object::getDisplayModesEnum(
  605. /* [in] */ long flags,
  606. /* [in] */ DDSurfaceDesc2 *ddsd,
  607. /* [retval][out] */ I_dxj_DirectDrawEnumModes __RPC_FAR *__RPC_FAR *retval)
  608. {
  609. HRESULT hr;
  610. hr=C_dxj_DirectDrawEnumModesObject::create(m__dxj_DirectDraw7,flags, ddsd, retval);
  611. return hr;
  612. }
  613. ///////////////////////////////////////////////////////////////////
  614. // testCooperativeLevel
  615. ///////////////////////////////////////////////////////////////////
  616. STDMETHODIMP C_dxj_DirectDraw7Object::testCooperativeLevel(
  617. /* [in,out] */ long *status)
  618. {
  619. HRESULT hr;
  620. hr=m__dxj_DirectDraw7->TestCooperativeLevel();
  621. *status=(long)hr;
  622. return S_OK;
  623. }
  624. ///////////////////////////////////////////////////////////////////
  625. // restoreAllSurfaces
  626. ///////////////////////////////////////////////////////////////////
  627. STDMETHODIMP C_dxj_DirectDraw7Object::restoreAllSurfaces()
  628. {
  629. HRESULT hr;
  630. hr=m__dxj_DirectDraw7->RestoreAllSurfaces();
  631. return hr;
  632. }
  633. STDMETHODIMP C_dxj_DirectDraw7Object::getSurfaceFromDC(long hdc, I_dxj_DirectDrawSurface7 **ret)
  634. {
  635. HRESULT hr;
  636. LPDIRECTDRAWSURFACE7 pDDS=NULL;
  637. hr=m__dxj_DirectDraw7->GetSurfaceFromDC((HDC)hdc,&pDDS);
  638. if FAILED(hr) return hr;
  639. INTERNAL_CREATE(_dxj_DirectDrawSurface7,pDDS,ret);
  640. return hr;
  641. }
  642. STDMETHODIMP C_dxj_DirectDraw7Object::getSurfacesEnum(
  643. /* [in] */ long flags,
  644. /* [in] */ DDSurfaceDesc2 __RPC_FAR *desc,
  645. /* [retval][out] */ I_dxj_DirectDrawEnumSurfaces __RPC_FAR *__RPC_FAR *ret)
  646. {
  647. HRESULT hr=C_dxj_DirectDrawEnumSurfacesObject::create((I_dxj_DirectDraw7*)this , flags, desc,ret);
  648. return hr;
  649. }
  650. STDMETHODIMP C_dxj_DirectDraw7Object::getDeviceIdentifier(
  651. long flags,I_dxj_DirectDrawIdentifier **ret)
  652. {
  653. HRESULT hr;
  654. hr=C_dxj_DirectDrawIdentifierObject::Create(m__dxj_DirectDraw7,(DWORD)flags,ret);
  655. return hr;
  656. }