home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / dispdib.h < prev    next >
C/C++ Source or Header  |  1998-04-25  |  11KB  |  277 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*        DISPDIB.H - Include file for DisplayDib() function.               */
  4. /*                                                                          */
  5. /*        Note: You must include WINDOWS.H before including this file.      */
  6. /*                                                                          */
  7. /*        Copyright (c) 1990-1995, Microsoft Corp.  All rights reserved.    */
  8. /*                                                                          */
  9. /****************************************************************************/
  10.  
  11. // DisplayDib() error return codes
  12. #define DISPLAYDIB_NOERROR          0x0000  // success
  13. #define DISPLAYDIB_NOTSUPPORTED     0x0001  // function not supported
  14. #define DISPLAYDIB_INVALIDDIB       0x0002  // null or invalid DIB header
  15. #define DISPLAYDIB_INVALIDFORMAT    0x0003  // invalid DIB format
  16. #define DISPLAYDIB_INVALIDTASK      0x0004  // not called from current task
  17. #define DISPLAYDIB_STOP             0x0005  // stop requested
  18. #define DISPLAYDIB_NOTACTIVE        0x0006  // DisplayDibWindow not foreground
  19. #define DISPLAYDIB_BADSIZE          0x0007  //
  20.  
  21. // flags for <wFlags> parameter of DisplayDib()
  22. #define DISPLAYDIB_NOPALETTE        0x0010  // don't set palette
  23. #define DISPLAYDIB_NOCENTER         0x0020  // don't center image
  24. #define DISPLAYDIB_NOWAIT           0x0040  // don't wait before returning
  25. #define DISPLAYDIB_NOIMAGE          0x0080  // don't draw image
  26. #define DISPLAYDIB_ZOOM2            0x0100  // stretch by 2
  27. #define DISPLAYDIB_DONTLOCKTASK     0x0200  // don't lock current task
  28. #define DISPLAYDIB_TEST             0x0400  // testing the command
  29. #define DISPLAYDIB_NOFLIP           0x0800  // dont page flip
  30. #define DISPLAYDIB_BEGIN            0x8000  // start of multiple calls
  31. #define DISPLAYDIB_END              0x4000  // end of multiple calls
  32.  
  33. #define DISPLAYDIB_MODE             0x000F  // mask for display mode
  34. #define DISPLAYDIB_MODE_DEFAULT     0x0000  // default display mode
  35. #define DISPLAYDIB_MODE_320x200x8   0x0001  // 320-by-200
  36. #define DISPLAYDIB_MODE_320x240x8   0x0005  // 320-by-240
  37.  
  38. //
  39. // a Win32 app must use the window class the function
  40. // versions are not available
  41. //
  42. #ifndef _WIN32
  43.  
  44. // function prototypes
  45. UINT FAR PASCAL DisplayDib(LPBITMAPINFOHEADER lpbi, LPSTR lpBits, WORD wFlags);
  46. UINT FAR PASCAL DisplayDibEx(LPBITMAPINFOHEADER lpbi, int x, int y, LPSTR lpBits, WORD wFlags);
  47.  
  48. #define DisplayDibBegin() DisplayDib(NULL, NULL, DISPLAYDIB_BEGIN|DISPDIB_NOWAIT)
  49. #define DisplayDibEnd()   DisplayDib(NULL, NULL, DISPLAYDIB_END|DISPDIB_NOWAIT)
  50.  
  51. #endif
  52.  
  53. //
  54. //  DisplayDibWindow class.
  55. //
  56. //  simple interface to DISPDIB as a window class.
  57. //  draw images and create a fullscreen window in one easy step.
  58. //
  59. //  advantages over calling the APIs directly.
  60. //
  61. //      if you show the window it will handle enabling/disabling
  62. //      fullscreen mode when it has a activation.
  63. //
  64. //      while in fullscreen mode, window will be sized to
  65. //      cover entire display preventing other apps from getting
  66. //      clicked on. (when visible)
  67. //
  68. //      if window looses activation, fullscreen mode will be disabled
  69. //      DDM_DRAW will return DISPLAYDIB_NOTACTIVE if you try to draw
  70. //
  71. //      forwards all mouse and keyboard events to owner, easy way
  72. //      to take over entire screen.
  73. //
  74. //      alows interop with a Win32 application (via WM_COPYDATA)
  75. //      NOTE WM_COPYDATA does not actualy copy anything if the
  76. //      window belongs to the calling thread.  it will do a copy
  77. //      if the window is owned by another thread....
  78. //
  79. //  you can use a DisplayDibWindow in two ways.....
  80. //
  81. //      hidden window
  82. //
  83. //          if the window is hidden, you must use the
  84. //          DDM_BEGIN and DDM_END message to enable/disable
  85. //          fullscreen mode manualy when your app is activated deactivated.
  86. //
  87. //      visible toplevel window
  88. //
  89. //          if you show the window it will take over the entire screen
  90. //          and forward all mouse/keyboard input to its owner.
  91. //
  92. //          it will enter fullscreen automaticly when it is shown.
  93. //
  94. //          it will leave fullscreen and hide it self it another app
  95. //          grabs the focus.
  96. //
  97. //  class name:     "DisplayDibWindow"
  98. //                  class is registered when DISPDIB.DLL is loaded.
  99. //                  as a global class.
  100. //
  101. //  messages:
  102. //
  103. //      DDM_SETFMT  set new DIB format or program a new palette
  104. //
  105. //            fullscreen mode, will use best mode
  106. //                  for displaying the passed DIB format.
  107. //            defaul is 320x240x8 tripple buffered
  108. //
  109. //                  the palette will be programed with the color
  110. //                  table of the passed BITMAPINFOHEADER.
  111. //
  112. //                  the format is a BITMAPINFOHEADER followed by a color table.
  113. //
  114. //                  you must set a format before doing a begin, end or draw
  115. //                  you can set a 320x200 or a 320x24 mode by selecting
  116. //                  a DIB of the format you want.
  117. //
  118. //                  if you do a setfmt while fullscreen mode is active only the
  119. //                  the palette will be changed the new size (if any) wont
  120. //                  happen until the next begin.
  121. //
  122. //        wParam = 0
  123. //          lParam = LPBITMAPINFOHEADER
  124. //
  125. //          returns 0 if success else DISPLAYDIB_* error code.
  126. //
  127. //      DDM_DRAW    draws DIB data to fullscreen
  128. //                  format is assumed the same as format passed to
  129. //                  DDM_BEGIN or DDM_FMT
  130. //
  131. //          wParam = flags
  132. //          lParam = bits pointer.
  133. //
  134. //          returns 0 if success else DISPLAYDIB_* error code.
  135. //
  136. //      DDM_CLOSE   destroy window *and* free the DLL
  137. //
  138. //      DDM_BEGIN   enter DISPDIB mode.
  139. //          wParam = flags
  140. //          lParam = 0
  141. //
  142. //          returns 0 if success else DISPLAYDIB_* error code.
  143. //
  144. //      DDM_END     leave DISPDIB mode.
  145. //          wParam = flags
  146. //          lParam = 0
  147. //
  148. //          returns 0 if success else DISPLAYDIB_* error code.
  149. //
  150. //      WM_COPYDATA allows a Win32 app to send a DDM_ message, that requires
  151. //      a pointer.
  152. //
  153. //          wParam = hwnd of sender
  154. //          lParam = PCOPYDATASTRUCT
  155. //                  dwData      - LOWORD: DDM_* message value.
  156. //                  dwData      - HIWORD: wParam for message
  157. //                  lpData      - lParam (pointer to a BITMAPINFOHEADER or bits)
  158. //                  cbData      - size of data
  159. //
  160. //          returns   0 if success else DISPLAYDIB_* error code.
  161. //
  162.  
  163. #define DISPLAYDIB_WINDOW_CLASS     "DisplayDibWindow"
  164. #define DISPLAYDIB_DLL              "DISPDIB.DLL"
  165.  
  166. #define DDM_SETFMT      WM_USER+0
  167. #define DDM_DRAW        WM_USER+1
  168. #define DDM_CLOSE       WM_USER+2
  169. #define DDM_BEGIN       WM_USER+3
  170. #define DDM_END         WM_USER+4
  171.  
  172. //
  173. // inline function to send a message to a DisplayDibWindow
  174. //
  175. __inline UINT DisplayDibWindowMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, DWORD cbSize)
  176. {
  177. #if defined(_WIN32) || defined(WIN32)
  178.         COPYDATASTRUCT cds;
  179.         cds.dwData = MAKELONG(msg, wParam);
  180.         cds.cbData = lParam ? cbSize : 0;
  181.         cds.lpData = (LPVOID)lParam;
  182.         return (UINT)SendMessage(hwnd, WM_COPYDATA, (WPARAM)(HWND)NULL, (LPARAM)(LPVOID)&cds);
  183. #else
  184.         return (UINT)SendMessage(hwnd, msg, wParam, lParam);
  185. #endif
  186. }
  187.  
  188. //
  189. // inline function to create a DisplayDibWindow
  190. //
  191. __inline HWND DisplayDibWindowCreateEx(HWND hwndParent, HINSTANCE hInstance, DWORD dwStyle)
  192. {
  193. #if defined(_WIN32) || defined(WIN32)
  194.     DWORD show = 2;
  195.     DWORD zero = 0;
  196.     LPVOID params[4] = {NULL, &zero, &show, 0};
  197.  
  198.     if ((UINT)LoadModule(DISPLAYDIB_DLL, ¶ms) < (UINT)HINSTANCE_ERROR)
  199.         return NULL;    // loading DISPDIB did not work
  200. #else
  201.     if ((UINT)LoadLibrary(DISPLAYDIB_DLL) < (UINT)HINSTANCE_ERROR)
  202.         return NULL;    // loading DISPDIB did not work
  203. #endif
  204.  
  205.     return CreateWindow(DISPLAYDIB_WINDOW_CLASS,"",dwStyle,0, 0,
  206.             GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
  207.             hwndParent, NULL,
  208.             (hInstance ? hInstance : GetWindowInstance(hwndParent)), NULL);
  209. }
  210.  
  211. //
  212. //  helper macros for a DisplayDibWindow
  213. //
  214. //  DisplayDibWindowCreate
  215. //
  216. //      used to create a toplevel WS_POPUP window.
  217. //
  218. //  DisplayDibWindowCreateEx
  219. //
  220. //      used to create a non-toplevel window, of a custom style.
  221. //
  222. //  DisplayDibWindowSetFmt
  223. //
  224. //      macro to send the DDM_SETFMT message.
  225. //
  226. //  DisplayDibWindowDraw
  227. //
  228. //      macro to send the DDM_DRAW message
  229. //
  230. //  DisplayDibWindowBegin
  231. //
  232. //      macro used to show the window
  233. //
  234. //  DisplayDibWindowEnd
  235. //
  236. //      macro used to hide the window
  237. //
  238. //  DisplayDibWindowBeginEx
  239. //
  240. //      macro used to send a DDM_BEGIN message, used with hidden windows
  241. //
  242. //  DisplayDibWindowEndEx
  243. //
  244. //      macro used to send a DDM_END message, used with hidden windows
  245. //
  246. //  DisplayDibWindowClose
  247. //
  248. //      macro used to send a DDM_CLOSE message
  249. //      this will destroy the window and free the DLL.
  250. //
  251. //  NOTES
  252. //      warning DisplayDibWindowBegin/End will show the DisplayDibWindow
  253. //      this will steal actiation away from your app. all mouse keyboard
  254. //      input will go to the dispdib window and it will forward it to
  255. //      its owner (make sure you set the right owner on create)
  256. //
  257. //      this may cause a problem for your app, you can keep the window
  258. //      hidden be using the DDM_BEGIN/END messages in this case.
  259. //
  260. #define DisplayDibWindowCreate(hwndP, hInstance)        DisplayDibWindowCreateEx(hwndP, hInstance, WS_POPUP)
  261. #define DisplayDibWindowSetFmt(hwnd, lpbi)              DisplayDibWindowMessage(hwnd, DDM_SETFMT, 0, (LPARAM)(LPVOID)(lpbi), sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD))
  262. #define DisplayDibWindowDraw(hwnd, flags, bits, size)   DisplayDibWindowMessage(hwnd, DDM_DRAW, (WPARAM)(UINT)(flags), (LPARAM)(LPVOID)(bits), (DWORD)(size))
  263.  
  264. #ifdef __cplusplus
  265. #define DisplayDibWindowBegin(hwnd)                     ::ShowWindow(hwnd, SW_SHOWNORMAL)
  266. #define DisplayDibWindowEnd(hwnd)                       ::ShowWindow(hwnd, SW_HIDE)
  267. #define DisplayDibWindowBeginEx(hwnd, f)                ::SendMessage(hwnd, DDM_BEGIN, (WPARAM)(UINT)(f), 0)
  268. #define DisplayDibWindowEndEx(hwnd)                     ::SendMessage(hwnd, DDM_END, 0, 0)
  269. #define DisplayDibWindowClose(hwnd)                     ::SendMessage(hwnd, DDM_CLOSE, 0, 0)
  270. #else
  271. #define DisplayDibWindowBegin(hwnd)                     ShowWindow(hwnd, SW_SHOWNORMAL)
  272. #define DisplayDibWindowEnd(hwnd)                       ShowWindow(hwnd, SW_HIDE)
  273. #define DisplayDibWindowBeginEx(hwnd)                   SendMessage(hwnd, DDM_BEGIN, 0, 0)
  274. #define DisplayDibWindowEndEx(hwnd)                     SendMessage(hwnd, DDM_END, 0, 0)
  275. #define DisplayDibWindowClose(hwnd)                     SendMessage(hwnd, DDM_CLOSE, 0, 0)
  276. #endif
  277.