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.

199 lines
6.3 KiB

  1. #include "stdafx.h"
  2. #include "DXSvr.h"
  3. extern IDirectDraw7 * g_pDD;
  4. //***************************************************************************************
  5. HRESULT LoadBMPFile(TCHAR* strFilename, HBITMAP* phbm)
  6. {
  7. // Check params
  8. if (NULL==strFilename || NULL==phbm)
  9. return DDERR_INVALIDPARAMS;
  10. // Try to load the bitmap as a resource.
  11. (*phbm) = (HBITMAP)LoadImage(g_hMainInstance, strFilename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  12. // If the bitmap wasn't a resource, try it as a file.
  13. if (NULL == (*phbm))
  14. (*phbm) = (HBITMAP)LoadImage(NULL, strFilename, IMAGE_BITMAP, 0, 0,
  15. LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  16. return (*phbm) ? DD_OK : DDERR_NOTFOUND;
  17. }
  18. //***************************************************************************************
  19. HRESULT LoadBMPFileChain(TCHAR** strFilenames , HBITMAP* phbm , int num_files)
  20. {
  21. for (int i = 0 ; i < num_files ; i++)
  22. {
  23. if (FAILED(LoadBMPFile(strFilenames[i] , phbm+i)))
  24. {
  25. for (int j = 0 ; j < i ; j++)
  26. DeleteObject(phbm[j]);
  27. return E_FAIL;
  28. }
  29. }
  30. return S_OK;
  31. }
  32. //***************************************************************************************
  33. IDirectDrawSurface7* CreateTextureFromBitmap(HBITMAP hBmp , DDPIXELFORMAT& ddpfFormat)
  34. {
  35. IDirectDrawSurface7* pTexture;
  36. IDirectDrawSurface7* pSurface;
  37. DDSURFACEDESC2 desc;
  38. // Figure out the size of the bitmap
  39. BITMAP bm;
  40. GetObject(hBmp, sizeof(BITMAP), &bm);
  41. // Create texture surface. Flag as a managed texture
  42. memset(&desc , 0 , sizeof(desc));
  43. desc.dwSize = sizeof(desc);
  44. desc.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT;
  45. desc.dwHeight = bm.bmHeight;
  46. desc.dwWidth = bm.bmWidth;
  47. desc.ddpfPixelFormat = ddpfFormat;
  48. desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
  49. desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
  50. if (FAILED(g_pDD->CreateSurface(&desc , &pTexture , NULL))) // g_pDDraw
  51. return NULL;
  52. // Now create system surface which we'll put the bitmap data into.
  53. desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE|DDSCAPS_SYSTEMMEMORY;
  54. desc.ddsCaps.dwCaps2 = 0;
  55. if (FAILED(g_pDD->CreateSurface(&desc , &pSurface , NULL)))
  56. {
  57. pTexture->Release();
  58. return NULL;
  59. }
  60. // Copy the bitmap into the system surface
  61. HDC image = CreateCompatibleDC(NULL);
  62. if (image != NULL)
  63. {
  64. SelectObject(image , hBmp);
  65. HDC dc;
  66. if (SUCCEEDED(pSurface->GetDC(&dc)))
  67. {
  68. BitBlt(dc , 0 , 0 , bm.bmWidth , bm.bmHeight , image , 0 , 0 , SRCCOPY);
  69. pSurface->ReleaseDC(dc);
  70. }
  71. DeleteDC(image);
  72. }
  73. // Load the texture
  74. pTexture->Blt(NULL , pSurface , NULL , DDBLT_WAIT , NULL);
  75. // Lose the system copy of the texture
  76. pSurface->Release();
  77. // Done
  78. return pTexture;
  79. }
  80. //***************************************************************************************
  81. IDirectDrawSurface7* CreateMipTextureFromBitmapChain(HBITMAP* hBmp , DDPIXELFORMAT& ddpfFormat , int num_mips)
  82. {
  83. IDirectDrawSurface7* pSurface;
  84. IDirectDrawSurface7* pTexture;
  85. DDSURFACEDESC2 desc;
  86. // Figure out the size of the first bitmap
  87. BITMAP bm;
  88. GetObject(*hBmp, sizeof(BITMAP), &bm);
  89. // Create texture surface. Flag as a managed texture
  90. memset(&desc , 0 , sizeof(desc));
  91. desc.dwSize = sizeof(desc);
  92. desc.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_MIPMAPCOUNT;
  93. desc.dwHeight = bm.bmHeight;
  94. desc.dwWidth = bm.bmWidth;
  95. desc.dwMipMapCount = num_mips;
  96. desc.ddpfPixelFormat = ddpfFormat;
  97. desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE|DDSCAPS_MIPMAP|DDSCAPS_COMPLEX;
  98. desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
  99. if (FAILED(g_pDD->CreateSurface(&desc , &pTexture , NULL)))
  100. return NULL;
  101. // Now create system surface which we'll put the bitmap data into.
  102. desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE|DDSCAPS_SYSTEMMEMORY|DDSCAPS_MIPMAP|DDSCAPS_COMPLEX;
  103. desc.ddsCaps.dwCaps2 = 0;
  104. if (FAILED(g_pDD->CreateSurface(&desc , &pSurface , NULL)))
  105. {
  106. pTexture->Release();
  107. return NULL;
  108. }
  109. // Copy the bitmaps into the system surfaces
  110. IDirectDrawSurface7* pMipLevel = pSurface;
  111. HDC image = CreateCompatibleDC(NULL);
  112. if (image != NULL)
  113. {
  114. for (int i = 0 ; i < num_mips ; i++)
  115. {
  116. GetObject(hBmp[i] , sizeof(BITMAP) , &bm);
  117. SelectObject(image , hBmp[i]);
  118. HDC dc;
  119. if (SUCCEEDED(pMipLevel->GetDC(&dc)))
  120. {
  121. BitBlt(dc , 0 , 0 , bm.bmWidth , bm.bmHeight , image , 0 , 0 , SRCCOPY);
  122. pMipLevel->ReleaseDC(dc);
  123. }
  124. DDSCAPS2 ddsCaps;
  125. ddsCaps.dwCaps = DDSCAPS_TEXTURE|DDSCAPS_MIPMAP;
  126. ddsCaps.dwCaps2 = ddsCaps.dwCaps3 = ddsCaps.dwCaps4 = 0;
  127. if (FAILED(pMipLevel->GetAttachedSurface(&ddsCaps , &pMipLevel)))
  128. break;
  129. }
  130. DeleteDC(image);
  131. }
  132. // Load the texture
  133. pTexture->Blt(NULL , pSurface , NULL , DDBLT_WAIT , NULL);
  134. // Lose the system copy of the texture
  135. pSurface->Release();
  136. // Done
  137. return pTexture;
  138. }
  139. //***************************************************************************************
  140. IDirectDrawSurface7* LoadAndCreateTexture(TCHAR* strFilename , DDPIXELFORMAT& ddpfFormat)
  141. {
  142. HBITMAP hBmp;
  143. if (FAILED(LoadBMPFile(strFilename , &hBmp)))
  144. return NULL;
  145. IDirectDrawSurface7* texture = CreateTextureFromBitmap(hBmp , ddpfFormat);
  146. DeleteObject(hBmp);
  147. return texture;
  148. }
  149. //***************************************************************************************
  150. IDirectDrawSurface7* LoadAndCreateMipTexture(TCHAR** strFilenames , DDPIXELFORMAT& ddpfFormat , int num_mips)
  151. {
  152. if (num_mips > 16 || num_mips < 1)
  153. return NULL;
  154. HBITMAP hBmp[16];
  155. if (FAILED(LoadBMPFileChain(strFilenames , hBmp , num_mips)))
  156. return NULL;
  157. IDirectDrawSurface7* texture = CreateMipTextureFromBitmapChain(hBmp , ddpfFormat , num_mips);
  158. for (int i = 0 ; i < num_mips ; i++)
  159. DeleteObject(hBmp[i]);
  160. return texture;
  161. }