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.

321 lines
16 KiB

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: d3dxsprite.h
  6. // Content: D3DX sprite helper functions
  7. //
  8. // These functions allow you to use sprites with D3DX. A "sprite" is
  9. // loosely defined as a 2D image that you want to transfer to the
  10. // rendering target. The source image can be a texture created
  11. // with the help of the D3DX texture loader; though advanced users may
  12. // want to create their own. A helper function (PrepareDeviceForSprite)
  13. // is provided to make it easy to set up render states on a device.
  14. // (Again, advanced users can use their own created devices.)
  15. //
  16. // There are two general techniques for sprites; the simpler one just
  17. // specifies a destination rectangle and a rotation anlge. A more
  18. // powerful technique supports rendering to non-rectangular quads.
  19. //
  20. // Both techniques support clipping, alpha, and rotation. More
  21. // details are below.
  22. //
  23. ///////////////////////////////////////////////////////////////////////////
  24. #ifndef __D3DXSPRITE_H__
  25. #define __D3DXSPRITE_H__
  26. #include <d3d.h>
  27. #include <limits.h>
  28. #include "d3dxerr.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. //-------------------------------------------------------------------------
  33. // D3DXPrepareDeviceForSprite:
  34. //
  35. // Call this function to set up all the render states necessary for
  36. // BltSprite/WarpSprite to work correctly. (Advanced users may opt to
  37. // not call this function first; in which case Blt/WarpSprite functions
  38. // will use whatever render/texture states were set up on the device when
  39. // they are called.)
  40. //
  41. // Warning: This function modifies render states and may impact performance
  42. // negatively on some 3D hardware if it is called too often per frame.
  43. //
  44. // Warning: If the render state changes (other than through calls to
  45. // BltSprite or WarpSprite), you will need to call this function again before
  46. // calling BltSprite or WarpSprite.
  47. //
  48. // Details: This function modifies the the rendering first texture stage and
  49. // it modifies some renderstates for the entire device. Here is the exact
  50. // list:
  51. //
  52. // SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  53. // SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  54. // SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  55. // SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  56. // SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  57. // SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFN_LINEAR);
  58. // SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
  59. //
  60. // SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
  61. // SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
  62. // SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
  63. //
  64. // Depending on the value of ZEnable parameter, this function will
  65. // will either call
  66. // SetRenderState(D3DRENDERSTATE_ZENABLE, FALSE);
  67. // - or -
  68. // SetRenderState(D3DRENDERSTATE_ZENABLE, TRUE);
  69. //
  70. // Parameters:
  71. // pd3dDevice - a pointer to the d3d device that you wish to prepare
  72. // for use with D3DX Sprite Services
  73. // ZEnable - a flag indicating whether you want the sprites to
  74. // check and update the Z buffer as part of rendering.
  75. // If ZEnable is FALSE, OR you are using
  76. // alpha-blending, then it is necessary to render your
  77. // sprites from back-to-front.
  78. //
  79. //-------------------------------------------------------------------------
  80. #ifdef __cplusplus
  81. HRESULT WINAPI
  82. D3DXPrepareDeviceForSprite( LPDIRECT3DDEVICE7 pd3dDevice,
  83. BOOL ZEnable = FALSE);
  84. #else
  85. HRESULT WINAPI
  86. D3DXPrepareDeviceForSprite( LPDIRECT3DDEVICE7 pd3dDevice,
  87. BOOL ZEnable);
  88. #endif
  89. //-------------------------------------------------------------------------
  90. // The D3DXDrawBasicSprite() function performs blitting of source images onto
  91. // a 3D rendering device. This function only calls SetTexture on the first
  92. // renderstage with the parameter (pd3dTexture) if that parameter is non-null.
  93. // This function assumes that D3DXPrepareDeviceForSprite has been called on
  94. // the device or that caller has in some other way correctly prepared the
  95. // renderstates.
  96. //
  97. // This function supports scaling, rotations, alpha-blending, and choosing
  98. // a source sub-rect.
  99. //
  100. // Rotation angle is specified in radians. Both rotations and scales
  101. // are applied around the center of the sprite; where the center of the
  102. // sprite is half the width/height of the sprite, plus the offset parameter.
  103. //
  104. // Use the offset parameter if you want the sprite's center to be something
  105. // other than the image center.
  106. //
  107. // The destination point indicates where you would like the center of
  108. // the sprite to draw to.
  109. //
  110. // Parameters:
  111. // pd3dTexture - a pointer to the surface containing the texture
  112. // pd3dDevice - a pointer to the d3d device to render to. It is
  113. // assumed that render states are set up. (See
  114. // D3DXPrepareDeviceForSprite)
  115. // ppointDest - a pointer to the target point for the sprite. The
  116. // components of the vector must be in screen
  117. // space.
  118. // alpha - alpha value to apply to sprite. 1.0 means totally
  119. // opaque; and 0.0 means totally transparent.
  120. // WARNING: If you are using alpha, then you should render
  121. // from back to front in order to avoid rendering
  122. // artifacts.
  123. // angleRad - angle of rotation around the 'center' of the rect
  124. // scale - a uniform scale that is applied to the source rect
  125. // to specify the size of the image that is rendered
  126. // pOffset - offset from the center of the source rect to use as the
  127. // center of rotation
  128. // pSourceRect - a rect that indicates what portion of the source
  129. // source texture to use. If NULL is passed, then the
  130. // entire source is used. If the source texture was
  131. // created via D3DX, then the rect should be specified
  132. // in the coordinates of the original image (so that you
  133. // don't have to worry about stretching/scaling that D3DX
  134. // may have done to make the image work with your current
  135. // 3D Device.) Note that horizontal or vertical mirroring
  136. // may be simply accomplished by swapping the left/right
  137. // or top/bottom fields of this RECT.
  138. //-------------------------------------------------------------------------
  139. #ifdef __cplusplus
  140. HRESULT WINAPI
  141. D3DXDrawSpriteSimple(LPDIRECTDRAWSURFACE7 pd3dTexture,
  142. LPDIRECT3DDEVICE7 pd3dDevice,
  143. const D3DXVECTOR3 *ppointDest,
  144. float alpha = 1.0f,
  145. float scale = 1.0f,
  146. float angleRad = 0.0f,
  147. const D3DXVECTOR2 *pOffset = NULL,
  148. const RECT *pSourceRect = NULL);
  149. #else
  150. HRESULT WINAPI
  151. D3DXDrawSpriteSimple(LPDIRECTDRAWSURFACE7 pd3dTexture,
  152. LPDIRECT3DDEVICE7 pd3dDevice,
  153. D3DXVECTOR3 *ppointDest,
  154. float alpha,
  155. float scale,
  156. float angleRad,
  157. D3DXVECTOR2 *pOffset,
  158. RECT *pSourceRect);
  159. #endif
  160. //-------------------------------------------------------------------------
  161. // The D3DXDrawSprite() function transforms source images onto a 3D
  162. // rendering device. It takes a general 4x4 matrix which is use to transform
  163. // the points of a default rect: (left=-.5, top=-.5, right=+.5, bottom=+.5).
  164. // (This default rect was chosen so that it was centered around the origin
  165. // to ease setting up rotations. And it was chosen to have a width/height of one
  166. // to ease setting up scales.)
  167. //
  168. // This function only calls SetTexture on the first
  169. // renderstage with the parameter (pd3dTexture) if that parameter is non-null.
  170. // This function assumes that D3DXPrepareDeviceForSprite has been called on
  171. // the device or that caller has in some other way correctly prepared the
  172. // renderstates.
  173. //
  174. // This function supports alpha-blending, and choosing
  175. // a source sub-rect. (A value of NULL for source sub-rect means the entire
  176. // texture is used.)
  177. //
  178. // Note that if the transformed points have a value for w (the homogenous
  179. // coordinate) that is not 1, then this function will invert it and pass
  180. // that value to D3D as the rhw field of a TLVERTEX. If the value for w is
  181. // zero, then it use 1 as the rhw.
  182. //
  183. // Parameters:
  184. // pd3dTexture - a pointer to the surface containing the texture
  185. // pd3dDevice - a pointer to the d3d device to render to. It is
  186. // assumed that render states are set up. (See
  187. // D3DXPrepareDeviceForSprite)
  188. // pMatrixTransform - 4x4 matrix that specifies the transformation
  189. // that will be applied to the default -.5 to +.5
  190. // rectangle.
  191. // alpha - alpha value to apply to sprite. 1.0 means totally
  192. // opaque; and 0.0 means totally transparent.
  193. // WARNING: If you are using alpha, then you should render
  194. // from back to front in order to avoid rendering
  195. // artifacts.Furthermore, you should avoid scenarios where
  196. // semi-transparent objects intersect.
  197. // pSourceRect - a rect that indicates what portion of the source
  198. // source texture to use. If NULL is passed, then the
  199. // entire source is used. If the source texture was
  200. // created via D3DX, then the rect should be specified
  201. // in the coordinates of the original image (so that you
  202. // don't have to worry about stretching/scaling that D3DX
  203. // may have done to make the image work with your current
  204. // 3D Device.) Note that mirroring may be simply accomplished
  205. // by swapping the left/right or top/bottom fields of
  206. // this RECT.
  207. //
  208. //-------------------------------------------------------------------------
  209. #ifdef __cplusplus
  210. HRESULT WINAPI
  211. D3DXDrawSpriteTransform(LPDIRECTDRAWSURFACE7 pd3dTexture,
  212. LPDIRECT3DDEVICE7 pd3dDevice,
  213. const D3DXMATRIX *pMatrixTransform,
  214. float alpha = 1.0f,
  215. const RECT *pSourceRect = NULL);
  216. #else
  217. HRESULT WINAPI
  218. D3DXDrawSpriteTransform(LPDIRECTDRAWSURFACE7 pd3dTexture,
  219. LPDIRECT3DDEVICE7 pd3dDevice,
  220. D3DXMATRIX *pMatrixTransform,
  221. float alpha,
  222. RECT *pSourceRect);
  223. #endif
  224. //-------------------------------------------------------------------------
  225. // The D3DXBuildSpriteTransform() function is a helper provided which
  226. // creates a matrix corresponding to simple properties. This matrix is
  227. // set up to pass directly to D3DXTransformSprite.
  228. //
  229. // Parameters:
  230. // pMatrix - a pointer to the result matrix
  231. // prectDest - a pointer to the target rectangle for the sprite
  232. // angleRad - angle of rotation around the 'center' of the rect
  233. // pOffset - offset from the center of the source rect to use as the
  234. // center of rotation
  235. //
  236. //-------------------------------------------------------------------------
  237. #ifdef __cplusplus
  238. void WINAPI
  239. D3DXBuildSpriteTransform(D3DXMATRIX *pMatrix,
  240. const RECT *prectDest,
  241. float angleRad = 0.0f,
  242. const D3DXVECTOR2 *pOffset = NULL);
  243. #else
  244. void WINAPI
  245. D3DXBuildSpriteTransform(D3DXMATRIX *pMatrix,
  246. RECT *prectDest,
  247. float angleRad,
  248. D3DXVECTOR2 *pOffset);
  249. #endif
  250. //-------------------------------------------------------------------------
  251. // The D3DXDrawSprite3D() function renders a texture onto a 3D quad. The
  252. // quad ABCD is broken into two triangles ABC and ACD which are rendered
  253. // via DrawPrim.
  254. //
  255. // Parameters:
  256. // pd3dTexture - a pointer to the surface containing the texture
  257. // pd3dDevice - a pointer to the d3d device to render to. It is
  258. // assumed that render states are set up. (See
  259. // D3DXPrepareDeviceForSprite)
  260. // quad - array of 4 points in the following order:
  261. // upper-left, upper-right, lower-right, lower-left.
  262. // If these vectors contain a W, then this function
  263. // will take the reciprocal of that value to pass as
  264. // as the rhw (i.e. reciprocal homogenous w).
  265. // alpha - alpha value to apply to sprite. 1.0 means totally
  266. // opaque; and 0.0 means totally transparent.
  267. // WARNING: If you are using alpha, then you should render
  268. // from back to front in order to avoid rendering
  269. // artifacts.Furthermore, you should avoid scenarios where
  270. // semi-transparent objects intersect.
  271. // pSourceRect - a rect that indicates what portion of the source
  272. // source texture to use. If NULL is passed, then the
  273. // entire source is used. If the source texture was
  274. // created via D3DX, then the rect should be specified
  275. // in the coordinates of the original image (so that you
  276. // don't have to worry about stretching/scaling that D3DX
  277. // may have done to make the image work with your current
  278. // 3D Device.) Note that mirroring may be simply accomplished
  279. // by swapping the left/right or top/bottom fields of
  280. // this RECT.
  281. //-------------------------------------------------------------------------
  282. #ifdef __cplusplus
  283. HRESULT WINAPI
  284. D3DXDrawSprite3D(LPDIRECTDRAWSURFACE7 pd3dTexture,
  285. LPDIRECT3DDEVICE7 pd3dDevice,
  286. const D3DXVECTOR4 quad[4],
  287. float alpha = 1.0f,
  288. const RECT *pSourceRect = NULL);
  289. #else
  290. HRESULT WINAPI
  291. D3DXDrawSprite3D(LPDIRECTDRAWSURFACE7 pd3dTexture,
  292. LPDIRECT3DDEVICE7 pd3dDevice,
  293. D3DXVECTOR4 quad[4],
  294. float alpha,
  295. RECT *pSourceRect);
  296. #endif
  297. #ifdef __cplusplus
  298. } // extern "C"
  299. #endif
  300. #endif // __D3DXSPRITE_H__