DDRAW.CPP 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***************************************************************************
  19. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  20. ***************************************************************************
  21. * *
  22. * Project Name : Westwood Win32 Library *
  23. * *
  24. * File Name : DDRAW.CPP *
  25. * *
  26. * Programmer : Philip W. Gorrow *
  27. * *
  28. * Start Date : October 10, 1995 *
  29. * *
  30. * Last Update : October 10, 1995 [] *
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. /*=========================================================================*/
  36. /* The following PRIVATE functions are in this file: */
  37. /*=========================================================================*/
  38. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  39. #include "misc.h"
  40. #include <gbuffer.h>
  41. #include <palette.h>
  42. LPDIRECTDRAW DirectDrawObject=NULL; // Pointer to the direct draw object
  43. LPDIRECTDRAW2 DirectDraw2Interface = NULL; // Pointer to direct draw 2 interface
  44. HWND MainWindow; // Handle to programs main window
  45. // this is passed to SetCooperativeLevel
  46. // so DirectDraw knows which window is ours
  47. PALETTEENTRY PaletteEntries[256]; // 256 windows palette entries
  48. LPDIRECTDRAWPALETTE PalettePtr; // Pointer to direct draw palette object
  49. BOOL FirstPaletteSet=FALSE; // Is this the first time 'Set_Palette' has been called?
  50. LPDIRECTDRAWSURFACE PaletteSurface=NULL;
  51. SurfaceMonitorClass AllSurfaces; //List of all direct draw surfaces
  52. BOOL CanVblankSync = TRUE;
  53. BOOL SystemToVideoBlits =FALSE; // Does hardware support system mem to video mem blits?
  54. BOOL VideoToSystemBlits =FALSE; // Does hardware support video mem to system mem blits?
  55. BOOL SystemToSystemBlits = FALSE; // Does hardware support system mem to system mem blits?
  56. BOOL OverlappedVideoBlits = TRUE; // Can video driver blit overlapped regions?
  57. /*
  58. ** Function to call if we detect focus loss
  59. */
  60. extern void (*Misc_Focus_Loss_Function)(void) = NULL;
  61. extern void (*Misc_Focus_Restore_Function)(void) = NULL;
  62. /***********************************************************************************************
  63. * Process_DD_Result -- Does a message box based on the result of a DD command *
  64. * *
  65. * INPUT: HRESULT result - the result returned from the direct draw command *
  66. * int display_ok_msg - should a message be displayed if command ok * *
  67. * *
  68. * OUTPUT: none *
  69. * *
  70. * HISTORY: *
  71. * 09/27/1995 PWG : Created. *
  72. *=============================================================================================*/
  73. void Process_DD_Result(HRESULT result, int display_ok_msg)
  74. {
  75. switch (result) {
  76. case DD_OK:
  77. if (display_ok_msg) {
  78. MessageBox(MainWindow, "Direct Draw request went ok.", "Note", MB_ICONEXCLAMATION|MB_OK);
  79. }
  80. break;
  81. case DDERR_ALREADYINITIALIZED:
  82. MessageBox(MainWindow, "This object is already initialized ","Note", MB_ICONEXCLAMATION|MB_OK);
  83. break;
  84. case DDERR_BLTFASTCANTCLIP:
  85. MessageBox(MainWindow, "Return if a clipper object is attached to the source surface passed into a BltFast call.","Note", MB_ICONEXCLAMATION|MB_OK);
  86. break;
  87. case DDERR_CANNOTATTACHSURFACE:
  88. MessageBox(MainWindow, "This surface can not be attached to the requested surface. ","Note", MB_ICONEXCLAMATION|MB_OK);
  89. break;
  90. case DDERR_CANNOTDETACHSURFACE:
  91. MessageBox(MainWindow, "This surface can not be detached from the requested surface. ","Note", MB_ICONEXCLAMATION|MB_OK);
  92. break;
  93. case DDERR_CANTCREATEDC:
  94. MessageBox(MainWindow, "Windows can not create any more DCs","Note", MB_ICONEXCLAMATION|MB_OK);
  95. break;
  96. case DDERR_CANTDUPLICATE:
  97. MessageBox(MainWindow, "Can't duplicate primary & 3D surfaces, or surfaces that are implicitly created.","Note", MB_ICONEXCLAMATION|MB_OK);
  98. break;
  99. case DDERR_CANTLOCKSURFACE:
  100. MessageBox(MainWindow, "Unable to lock surface because no driver exists which can supply a pointer to the surface.","Note", MB_ICONEXCLAMATION|MB_OK);
  101. break;
  102. case DDERR_CLIPPERISUSINGHWND:
  103. MessageBox(MainWindow, "An attempt was made to set a cliplist for a clipper object that is already monitoring an hwnd.","Note", MB_ICONEXCLAMATION|MB_OK);
  104. break;
  105. case DDERR_COLORKEYNOTSET:
  106. MessageBox(MainWindow, "No src color key specified for this operation.","Note", MB_ICONEXCLAMATION|MB_OK);
  107. break;
  108. case DDERR_CURRENTLYNOTAVAIL:
  109. MessageBox(MainWindow, "Support is currently not available. ","Note", MB_ICONEXCLAMATION|MB_OK);
  110. break;
  111. case DDERR_DIRECTDRAWALREADYCREATED:
  112. MessageBox(MainWindow, "A DirectDraw object representing this driver has already been created for this process. ","Note", MB_ICONEXCLAMATION|MB_OK);
  113. break;
  114. case DDERR_EXCEPTION:
  115. MessageBox(MainWindow, "An exception was encountered while performing the requested operation. ","Note", MB_ICONEXCLAMATION|MB_OK);
  116. break;
  117. case DDERR_EXCLUSIVEMODEALREADYSET:
  118. MessageBox(MainWindow, "An attempt was made to set the cooperative level when it was already set to exclusive.","Note", MB_ICONEXCLAMATION|MB_OK);
  119. break;
  120. case DDERR_GENERIC:
  121. MessageBox(MainWindow, "Generic failure.","Note", MB_ICONEXCLAMATION|MB_OK);
  122. break;
  123. case DDERR_HEIGHTALIGN:
  124. MessageBox(MainWindow, "Height of rectangle provided is not a multiple of reqd alignment.","Note", MB_ICONEXCLAMATION|MB_OK);
  125. break;
  126. case DDERR_HWNDALREADYSET:
  127. MessageBox(MainWindow, "The CooperativeLevel HWND has already been set. It can not be reset while the process has surfaces or palettes created.","Note", MB_ICONEXCLAMATION|MB_OK);
  128. break;
  129. case DDERR_HWNDSUBCLASSED:
  130. MessageBox(MainWindow, "HWND used by DirectDraw CooperativeLevel has been subclassed, this prevents DirectDraw from restoring state.","Note", MB_ICONEXCLAMATION|MB_OK);
  131. break;
  132. case DDERR_IMPLICITLYCREATED:
  133. MessageBox(MainWindow, "This surface can not be restored because it is an implicitly created surface.","Note", MB_ICONEXCLAMATION|MB_OK);
  134. break;
  135. case DDERR_INCOMPATIBLEPRIMARY:
  136. MessageBox(MainWindow, "Unable to match primary surface creation request with existing primary surface.","Note", MB_ICONEXCLAMATION|MB_OK);
  137. break;
  138. case DDERR_INVALIDCAPS:
  139. MessageBox(MainWindow, "One or more of the caps bits passed to the callback are incorrect.","Note", MB_ICONEXCLAMATION|MB_OK);
  140. break;
  141. case DDERR_INVALIDCLIPLIST:
  142. MessageBox(MainWindow, "DirectDraw does not support the provided cliplist.","Note", MB_ICONEXCLAMATION|MB_OK);
  143. break;
  144. case DDERR_INVALIDDIRECTDRAWGUID:
  145. MessageBox(MainWindow, "The GUID passed to DirectDrawCreate is not a valid DirectDraw driver identifier.","Note", MB_ICONEXCLAMATION|MB_OK);
  146. break;
  147. case DDERR_INVALIDMODE:
  148. MessageBox(MainWindow, "DirectDraw does not support the requested mode.","Note", MB_ICONEXCLAMATION|MB_OK);
  149. break;
  150. case DDERR_INVALIDOBJECT:
  151. MessageBox(MainWindow, "DirectDraw received a pointer that was an invalid DIRECTDRAW object.","Note", MB_ICONEXCLAMATION|MB_OK);
  152. break;
  153. case DDERR_INVALIDPARAMS:
  154. MessageBox(MainWindow, "One or more of the parameters passed to the function are incorrect.","Note", MB_ICONEXCLAMATION|MB_OK);
  155. break;
  156. case DDERR_INVALIDPIXELFORMAT:
  157. MessageBox(MainWindow, "The pixel format was invalid as specified.","Note", MB_ICONEXCLAMATION|MB_OK);
  158. break;
  159. case DDERR_INVALIDPOSITION:
  160. MessageBox(MainWindow, "Returned when the position of the overlay on the destination is no longer legal for that destination.","Note", MB_ICONEXCLAMATION|MB_OK);
  161. break;
  162. case DDERR_INVALIDRECT:
  163. MessageBox(MainWindow, "Rectangle provided was invalid.","Note", MB_ICONEXCLAMATION|MB_OK);
  164. break;
  165. case DDERR_INVALIDSURFACETYPE:
  166. MessageBox(MainWindow, "The requested action could not be performed because the surface was of the wrong type.","Note", MB_ICONEXCLAMATION|MB_OK);
  167. break;
  168. case DDERR_LOCKEDSURFACES:
  169. MessageBox(MainWindow, "Operation could not be carried out because one or more surfaces are locked.","Note", MB_ICONEXCLAMATION|MB_OK);
  170. break;
  171. case DDERR_NO3D:
  172. MessageBox(MainWindow, "There is no 3D present.","Note", MB_ICONEXCLAMATION|MB_OK);
  173. break;
  174. case DDERR_NOALPHAHW:
  175. MessageBox(MainWindow, "Operation could not be carried out because there is no alpha accleration hardware present or available.","Note", MB_ICONEXCLAMATION|MB_OK);
  176. break;
  177. #if(0)
  178. case DDERR_NOANTITEARHW:
  179. MessageBox(MainWindow, "Operation could not be carried out because there is no hardware support for synchronizing blts to avoid tearing. ","Note", MB_ICONEXCLAMATION|MB_OK);
  180. break;
  181. #endif
  182. case DDERR_NOBLTHW:
  183. MessageBox(MainWindow, "No blter hardware present.","Note", MB_ICONEXCLAMATION|MB_OK);
  184. break;
  185. #if(0)
  186. case DDERR_NOBLTQUEUEHW:
  187. MessageBox(MainWindow, "Operation could not be carried out because there is no hardware support for asynchronous blting. ","Note", MB_ICONEXCLAMATION|MB_OK);
  188. break;
  189. #endif
  190. case DDERR_NOCLIPLIST:
  191. MessageBox(MainWindow, "No cliplist available.","Note", MB_ICONEXCLAMATION|MB_OK);
  192. break;
  193. case DDERR_NOCLIPPERATTACHED:
  194. MessageBox(MainWindow, "No clipper object attached to surface object.","Note", MB_ICONEXCLAMATION|MB_OK);
  195. break;
  196. case DDERR_NOCOLORCONVHW:
  197. MessageBox(MainWindow, "Operation could not be carried out because there is no color conversion hardware present or available.","Note", MB_ICONEXCLAMATION|MB_OK);
  198. break;
  199. case DDERR_NOCOLORKEY:
  200. MessageBox(MainWindow, "Surface doesn't currently have a color key","Note", MB_ICONEXCLAMATION|MB_OK);
  201. break;
  202. case DDERR_NOCOLORKEYHW:
  203. MessageBox(MainWindow, "Operation could not be carried out because there is no hardware support of the destination color key.","Note", MB_ICONEXCLAMATION|MB_OK);
  204. break;
  205. case DDERR_NOCOOPERATIVELEVELSET:
  206. MessageBox(MainWindow, "Create function called without DirectDraw object method SetCooperativeLevel being called.","Note", MB_ICONEXCLAMATION|MB_OK);
  207. break;
  208. case DDERR_NODC:
  209. MessageBox(MainWindow, "No DC was ever created for this surface.","Note", MB_ICONEXCLAMATION|MB_OK);
  210. break;
  211. case DDERR_NODDROPSHW:
  212. MessageBox(MainWindow, "No DirectDraw ROP hardware.","Note", MB_ICONEXCLAMATION|MB_OK);
  213. break;
  214. case DDERR_NODIRECTDRAWHW:
  215. MessageBox(MainWindow, "A hardware-only DirectDraw object creation was attempted but the driver did not support any hardware.","Note", MB_ICONEXCLAMATION|MB_OK);
  216. break;
  217. case DDERR_NODIRECTDRAWSUPPORT:
  218. MessageBox(MainWindow, "No DirectDraw support possible with current display driver.","Note", MB_ICONEXCLAMATION|MB_OK);
  219. break;
  220. case DDERR_NOEMULATION:
  221. MessageBox(MainWindow, "Software emulation not available.","Note", MB_ICONEXCLAMATION|MB_OK);
  222. break;
  223. case DDERR_NOEXCLUSIVEMODE:
  224. MessageBox(MainWindow, "Operation requires the application to have exclusive mode but the application does not have exclusive mode.","Note", MB_ICONEXCLAMATION|MB_OK);
  225. break;
  226. case DDERR_NOFLIPHW:
  227. MessageBox(MainWindow, "Flipping visible surfaces is not supported.","Note", MB_ICONEXCLAMATION|MB_OK);
  228. break;
  229. case DDERR_NOGDI:
  230. MessageBox(MainWindow, "There is no GDI present.","Note", MB_ICONEXCLAMATION|MB_OK);
  231. break;
  232. case DDERR_NOHWND:
  233. MessageBox(MainWindow, "Clipper notification requires an HWND or no HWND has previously been set as the CooperativeLevel HWND.","Note", MB_ICONEXCLAMATION|MB_OK);
  234. break;
  235. case DDERR_NOMIRRORHW:
  236. MessageBox(MainWindow, "Operation could not be carried out because there is no hardware present or available.","Note", MB_ICONEXCLAMATION|MB_OK);
  237. break;
  238. case DDERR_NOOVERLAYDEST:
  239. MessageBox(MainWindow, "Returned when GetOverlayPosition is called on an overlay that UpdateOverlay has never been called on to establish a destination.","Note", MB_ICONEXCLAMATION|MB_OK);
  240. break;
  241. case DDERR_NOOVERLAYHW:
  242. MessageBox(MainWindow, "Operation could not be carried out because there is no overlay hardware present or available.","Note", MB_ICONEXCLAMATION|MB_OK);
  243. break;
  244. case DDERR_NOPALETTEATTACHED:
  245. MessageBox(MainWindow, "No palette object attached to this surface. ","Note", MB_ICONEXCLAMATION|MB_OK);
  246. break;
  247. case DDERR_NOPALETTEHW:
  248. MessageBox(MainWindow, "No hardware support for 16 or 256 color palettes.","Note", MB_ICONEXCLAMATION|MB_OK);
  249. break;
  250. case DDERR_NORASTEROPHW:
  251. MessageBox(MainWindow, "Operation could not be carried out because there is no appropriate raster op hardware present or available.","Note", MB_ICONEXCLAMATION|MB_OK);
  252. break;
  253. case DDERR_NOROTATIONHW:
  254. MessageBox(MainWindow, "Operation could not be carried out because there is no rotation hardware present or available.","Note", MB_ICONEXCLAMATION|MB_OK);
  255. break;
  256. case DDERR_NOSTRETCHHW:
  257. MessageBox(MainWindow, "Operation could not be carried out because there is no hardware support for stretching.","Note", MB_ICONEXCLAMATION|MB_OK);
  258. break;
  259. case DDERR_NOT4BITCOLOR:
  260. MessageBox(MainWindow, "DirectDrawSurface is not in 4 bit color palette and the requested operation requires 4 bit color palette.","Note", MB_ICONEXCLAMATION|MB_OK);
  261. break;
  262. case DDERR_NOT4BITCOLORINDEX:
  263. MessageBox(MainWindow, "DirectDrawSurface is not in 4 bit color index palette and the requested operation requires 4 bit color index palette.","Note", MB_ICONEXCLAMATION|MB_OK);
  264. break;
  265. case DDERR_NOT8BITCOLOR:
  266. MessageBox(MainWindow, "DirectDrawSurface is not in 8 bit color mode and the requested operation requires 8 bit color.","Note", MB_ICONEXCLAMATION|MB_OK);
  267. break;
  268. case DDERR_NOTAOVERLAYSURFACE:
  269. MessageBox(MainWindow, "Returned when an overlay member is called for a non-overlay surface.","Note", MB_ICONEXCLAMATION|MB_OK);
  270. break;
  271. case DDERR_NOTEXTUREHW:
  272. MessageBox(MainWindow, "Operation could not be carried out because there is no texture mapping hardware present or available.","Note", MB_ICONEXCLAMATION|MB_OK);
  273. break;
  274. case DDERR_NOTFLIPPABLE:
  275. MessageBox(MainWindow, "An attempt has been made to flip a surface that is not flippable.","Note", MB_ICONEXCLAMATION|MB_OK);
  276. break;
  277. case DDERR_NOTFOUND:
  278. MessageBox(MainWindow, "Requested item was not found.","Note", MB_ICONEXCLAMATION|MB_OK);
  279. break;
  280. case DDERR_NOTLOCKED:
  281. MessageBox(MainWindow, "Surface was not locked. An attempt to unlock a surface that was not locked at all, or by this process, has been attempted.","Note", MB_ICONEXCLAMATION|MB_OK);
  282. break;
  283. case DDERR_NOTPALETTIZED:
  284. MessageBox(MainWindow, "The surface being used is not a palette-based surface.","Note", MB_ICONEXCLAMATION|MB_OK);
  285. break;
  286. case DDERR_NOVSYNCHW:
  287. MessageBox(MainWindow, "Operation could not be carried out because there is no hardware support for vertical blank synchronized operations.","Note", MB_ICONEXCLAMATION|MB_OK);
  288. break;
  289. case DDERR_NOZBUFFERHW:
  290. MessageBox(MainWindow, "Operation could not be carried out because there is no hardware support for zbuffer blting.","Note", MB_ICONEXCLAMATION|MB_OK);
  291. break;
  292. case DDERR_NOZOVERLAYHW:
  293. MessageBox(MainWindow, "Overlay surfaces could not be z layered based on their BltOrder because the hardware does not support z layering of overlays.","Note", MB_ICONEXCLAMATION|MB_OK);
  294. break;
  295. case DDERR_OUTOFCAPS:
  296. MessageBox(MainWindow, "The hardware needed for the requested operation has already been allocated.","Note", MB_ICONEXCLAMATION|MB_OK);
  297. break;
  298. case DDERR_OUTOFMEMORY:
  299. MessageBox(MainWindow, "DirectDraw does not have enough memory to perform the operation.","Note", MB_ICONEXCLAMATION|MB_OK);
  300. break;
  301. case DDERR_OUTOFVIDEOMEMORY:
  302. MessageBox(MainWindow, "DirectDraw does not have enough memory to perform the operation.","Note", MB_ICONEXCLAMATION|MB_OK);
  303. break;
  304. case DDERR_OVERLAYCANTCLIP:
  305. MessageBox(MainWindow, "The hardware does not support clipped overlays.","Note", MB_ICONEXCLAMATION|MB_OK);
  306. break;
  307. case DDERR_OVERLAYCOLORKEYONLYONEACTIVE:
  308. MessageBox(MainWindow, "Can only have ony color key active at one time for overlays.","Note", MB_ICONEXCLAMATION|MB_OK);
  309. break;
  310. case DDERR_OVERLAYNOTVISIBLE:
  311. MessageBox(MainWindow, "Returned when GetOverlayPosition is called on a hidden overlay.","Note", MB_ICONEXCLAMATION|MB_OK);
  312. break;
  313. case DDERR_PALETTEBUSY:
  314. MessageBox(MainWindow, "Access to this palette is being refused because the palette is already locked by another thread.","Note", MB_ICONEXCLAMATION|MB_OK);
  315. break;
  316. case DDERR_PRIMARYSURFACEALREADYEXISTS:
  317. MessageBox(MainWindow, "This process already has created a primary surface.","Note", MB_ICONEXCLAMATION|MB_OK);
  318. break;
  319. case DDERR_REGIONTOOSMALL:
  320. MessageBox(MainWindow, "Region passed to Clipper::GetClipList is too small.","Note", MB_ICONEXCLAMATION|MB_OK);
  321. break;
  322. case DDERR_SURFACEALREADYATTACHED:
  323. MessageBox(MainWindow, "This surface is already attached to the surface it is being attached to.","Note", MB_ICONEXCLAMATION|MB_OK);
  324. break;
  325. case DDERR_SURFACEALREADYDEPENDENT:
  326. MessageBox(MainWindow, "This surface is already a dependency of the surface it is being made a dependency of.","Note", MB_ICONEXCLAMATION|MB_OK);
  327. break;
  328. case DDERR_SURFACEBUSY:
  329. MessageBox(MainWindow, "Access to this surface is being refused because the surface is already locked by another thread.","Note", MB_ICONEXCLAMATION|MB_OK);
  330. break;
  331. case DDERR_SURFACEISOBSCURED:
  332. MessageBox(MainWindow, "Access to surface refused because the surface is obscured.","Note", MB_ICONEXCLAMATION|MB_OK);
  333. break;
  334. case DDERR_SURFACELOST:
  335. MessageBox(MainWindow, "Access to this surface is being refused because the surface memory is gone. The DirectDrawSurface object representing this surface should have Restore called on it.","Note", MB_ICONEXCLAMATION|MB_OK);
  336. break;
  337. case DDERR_SURFACENOTATTACHED:
  338. MessageBox(MainWindow, "The requested surface is not attached.","Note", MB_ICONEXCLAMATION|MB_OK);
  339. break;
  340. case DDERR_TOOBIGHEIGHT:
  341. MessageBox(MainWindow, "Height requested by DirectDraw is too large.","Note", MB_ICONEXCLAMATION|MB_OK);
  342. break;
  343. case DDERR_TOOBIGSIZE:
  344. MessageBox(MainWindow, "Size requested by DirectDraw is too large -- the individual height and width are OK.","Note", MB_ICONEXCLAMATION|MB_OK);
  345. break;
  346. case DDERR_TOOBIGWIDTH:
  347. MessageBox(MainWindow, "Width requested by DirectDraw is too large.","Note", MB_ICONEXCLAMATION|MB_OK);
  348. break;
  349. case DDERR_UNSUPPORTED:
  350. MessageBox(MainWindow, "Action not supported.","Note", MB_ICONEXCLAMATION|MB_OK);
  351. break;
  352. case DDERR_UNSUPPORTEDFORMAT:
  353. MessageBox(MainWindow, "FOURCC format requested is unsupported by DirectDraw.","Note", MB_ICONEXCLAMATION|MB_OK);
  354. break;
  355. case DDERR_UNSUPPORTEDMASK:
  356. MessageBox(MainWindow, "Bitmask in the pixel format requested is unsupported by DirectDraw.","Note", MB_ICONEXCLAMATION|MB_OK);
  357. break;
  358. case DDERR_VERTICALBLANKINPROGRESS:
  359. MessageBox(MainWindow, "Vertical blank is in progress.","Note", MB_ICONEXCLAMATION|MB_OK);
  360. break;
  361. case DDERR_WASSTILLDRAWING:
  362. MessageBox(MainWindow, "Informs DirectDraw that the previous Blt which is transfering information to or from this Surface is incomplete.","Note", MB_ICONEXCLAMATION|MB_OK);
  363. break;
  364. case DDERR_WRONGMODE:
  365. MessageBox(MainWindow, "This surface can not be restored because it was created in a different mode.","Note", MB_ICONEXCLAMATION|MB_OK);
  366. break;
  367. case DDERR_XALIGN:
  368. MessageBox(MainWindow, "Rectangle provided was not horizontally aligned on required boundary.","Note", MB_ICONEXCLAMATION|MB_OK);
  369. break;
  370. default:
  371. char string[256];
  372. sprintf (string, "Unrecognised Direct Draw result code: %d", result & 0xffff);
  373. MessageBox(MainWindow, string,"Note", MB_ICONEXCLAMATION|MB_OK);
  374. break;
  375. }
  376. }
  377. /***********************************************************************************************
  378. * Check_Overlapped_Blit_Capability -- See if video driver supports blitting overlapped regions*
  379. * *
  380. * We will check for this by drawing something to a video page and blitting it over itself. *
  381. * If we end up with the top line repeating then overlapped region blits dont work. *
  382. * *
  383. * INPUT: Nothing *
  384. * *
  385. * OUTPUT: Nothing *
  386. * *
  387. * WARNINGS: None *
  388. * *
  389. * HISTORY: *
  390. * 6/7/96 5:06PM ST : Created *
  391. *=============================================================================================*/
  392. void Check_Overlapped_Blit_Capability(void)
  393. {
  394. /*
  395. ** Assume we can until we find out otherwise
  396. */
  397. OverlappedVideoBlits = TRUE;
  398. GraphicBufferClass test_buffer;
  399. test_buffer.Init (64, 64, NULL, 0, (GBC_Enum)GBC_VIDEOMEM);
  400. test_buffer.Clear();
  401. /*
  402. ** Plot a pixel in the top left corner of the buffer.
  403. */
  404. test_buffer.Put_Pixel(0, 0, 255);
  405. /*
  406. ** Blit the buffer down by one line. If we end up with a vertical strip of pixel 255's then
  407. ** overlapped blits dont work
  408. */
  409. test_buffer.Blit(test_buffer, 0, 0, 0, 1, test_buffer.Get_Width(), test_buffer.Get_Height()-1);
  410. if (test_buffer.Get_Pixel (0 ,5) == 255) OverlappedVideoBlits = FALSE;
  411. }
  412. /***********************************************************************************************
  413. * Set_Video_Mode -- Initializes Direct Draw and sets the required Video Mode *
  414. * *
  415. * INPUT: int width - the width of the video mode in pixels *
  416. * int height - the height of the video mode in pixels *
  417. * int bits_per_pixel - the number of bits per pixel the video mode supports *
  418. * *
  419. * OUTPUT: none *
  420. * *
  421. * HISTORY: *
  422. * 09/26/1995 PWG : Created. *
  423. *=============================================================================================*/
  424. BOOL Set_Video_Mode(HWND hwnd, int w, int h, int bits_per_pixel)
  425. {
  426. HRESULT result;
  427. //
  428. // If there is not currently a direct draw object then we need to define one.
  429. //
  430. if ( DirectDrawObject == NULL ){
  431. //MessageBox(MainWindow, "In Set_Video_Mode. About to call DirectDrawCreate.","Note", MB_ICONEXCLAMATION|MB_OK);
  432. result = DirectDrawCreate(NULL, &DirectDrawObject, NULL);
  433. Process_DD_Result(result, FALSE);
  434. if (result == DD_OK){
  435. if (w==320){
  436. result = DirectDrawObject->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX);
  437. } else {
  438. //MessageBox(MainWindow, "In Set_Video_Mode. About to call SetCooperativeLevel.","Note", MB_ICONEXCLAMATION|MB_OK);
  439. result = DirectDrawObject->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
  440. }
  441. Process_DD_Result(result, FALSE);
  442. }else{
  443. return (FALSE);
  444. }
  445. }
  446. //
  447. // Set the required display mode with 8 bits per pixel
  448. //
  449. //MessageBox(MainWindow, "In Set_Video_Mode. About to call call SetDisplayMode.","Note", MB_ICONEXCLAMATION|MB_OK);
  450. result = DirectDrawObject->SetDisplayMode ( w , h , bits_per_pixel );
  451. if (result != DD_OK){
  452. // Process_DD_Result(result, FALSE);
  453. DirectDrawObject->Release();
  454. DirectDrawObject = NULL;
  455. return(FALSE);
  456. }
  457. //
  458. // Create a direct draw palette object
  459. //
  460. //MessageBox(MainWindow, "In Set_Video_Mode. About to call CreatePalette.","Note", MB_ICONEXCLAMATION|MB_OK);
  461. result = DirectDrawObject->CreatePalette( DDPCAPS_8BIT | DDPCAPS_ALLOW256, &PaletteEntries[0] , &PalettePtr ,NULL);
  462. Process_DD_Result(result, FALSE);
  463. if (result != DD_OK){
  464. return (FALSE);
  465. }
  466. Check_Overlapped_Blit_Capability();
  467. //MessageBox(MainWindow, "In Set_Video_Mode. About to return success.","Note", MB_ICONEXCLAMATION|MB_OK);
  468. #if (0)
  469. /*
  470. ** Find out if DirectX 2 extensions are available
  471. */
  472. result = DirectDrawObject->QueryInterface (IID_IDirectDraw2, (LPVOID*)&DirectDraw2Interface);
  473. SystemToVideoBlits = FALSE;
  474. VideoToSystemBlits = FALSE;
  475. SystemToSystemBlits= FALSE;
  476. if (result != DD_OK){
  477. DirectDraw2Interface = NULL;
  478. }else{
  479. DDCAPS capabilities;
  480. DDCAPS emulated_capabilities;
  481. memset ((char*)&capabilities, 0, sizeof(capabilities));
  482. memset ((char*)&emulated_capabilities, 0, sizeof(emulated_capabilities));
  483. capabilities.dwSize = sizeof (capabilities);
  484. emulated_capabilities.dwSize = sizeof (emulated_capabilities);
  485. DirectDrawObject->GetCaps (&capabilities, &emulated_capabilities);
  486. if (capabilities.dwCaps & DDCAPS_CANBLTSYSMEM){
  487. SystemToVideoBlits = (capabilities.dwSVBCaps & DDCAPS_BLT) ? TRUE : FALSE;
  488. VideoToSystemBlits = (capabilities.dwVSBCaps & DDCAPS_BLT) ? TRUE : FALSE;
  489. SystemToSystemBlits = (capabilities.dwSSBCaps & DDCAPS_BLT) ? TRUE : FALSE;
  490. }
  491. }
  492. #endif //(0)
  493. //MessageBox(MainWindow, "In Set_Video_Mode. About to return success.","Note", MB_ICONEXCLAMATION|MB_OK);
  494. return (TRUE);
  495. }
  496. /***********************************************************************************************
  497. * Reset_Video_Mode -- Resets video mode and deletes Direct Draw Object *
  498. * *
  499. * INPUT: none *
  500. * *
  501. * OUTPUT: none *
  502. * *
  503. * WARNINGS: *
  504. * *
  505. * HISTORY: *
  506. * 09/26/1995 PWG : Created. *
  507. *=============================================================================================*/
  508. void Reset_Video_Mode(void)
  509. {
  510. HRESULT result;
  511. //
  512. // If a direct draw object has been declared and a video mode has been set
  513. // then reset the video mode and release the direct draw object.
  514. //
  515. if ( DirectDrawObject ) {
  516. result = DirectDrawObject->RestoreDisplayMode();
  517. Process_DD_Result(result, FALSE);
  518. result = DirectDrawObject->Release();
  519. Process_DD_Result(result, FALSE);
  520. DirectDrawObject = NULL;
  521. }
  522. }
  523. /***********************************************************************************************
  524. * Get_Free_Video_Memory -- returns amount of free video memory *
  525. * *
  526. * *
  527. * *
  528. * INPUT: Nothing *
  529. * *
  530. * OUTPUT: bytes of available video RAM *
  531. * *
  532. * WARNINGS: None *
  533. * *
  534. * HISTORY: *
  535. * 11/29/95 12:52PM ST : Created *
  536. *=============================================================================================*/
  537. unsigned int Get_Free_Video_Memory(void)
  538. {
  539. DDCAPS video_capabilities;
  540. if (DirectDrawObject){
  541. video_capabilities.dwSize = sizeof (video_capabilities);
  542. //MessageBox(MainWindow, "In Get_Free_Video_Memory. About to call GetCaps","Note", MB_ICONEXCLAMATION|MB_OK);
  543. if (DD_OK == DirectDrawObject->GetCaps (&video_capabilities , NULL)){
  544. char string [256];
  545. sprintf (string, "In Get_Free_Video_Memory. About to return %d bytes",video_capabilities.dwVidMemFree);
  546. //MessageBox(MainWindow, string,"Note", MB_ICONEXCLAMATION|MB_OK);
  547. return (video_capabilities.dwVidMemFree);
  548. }
  549. }
  550. //MessageBox(MainWindow, "In Get_Free_Video_Memory. About to return failure","Note", MB_ICONEXCLAMATION|MB_OK);
  551. return (0);
  552. }
  553. /***********************************************************************************************
  554. * Get_Video_Hardware_Caps -- returns bitmask of direct draw video hardware support *
  555. * *
  556. * *
  557. * *
  558. * INPUT: Nothing *
  559. * *
  560. * OUTPUT: hardware flags *
  561. * *
  562. * WARNINGS: Must call Set_Video_Mode 1st to create the direct draw object *
  563. * *
  564. * HISTORY: *
  565. * 1/12/96 9:14AM ST : Created *
  566. *=============================================================================================*/
  567. unsigned Get_Video_Hardware_Capabilities(void)
  568. {
  569. DDCAPS video_capabilities;
  570. unsigned video;
  571. /*
  572. ** Fail if the direct draw object has not been initialised
  573. */
  574. if (!DirectDrawObject) return (0);
  575. /*
  576. ** Get the capabilities of the direct draw object
  577. */
  578. video_capabilities.dwSize = sizeof(video_capabilities);
  579. //MessageBox(MainWindow, "In Get_Video_Hardware_Capabilities. About to call GetCaps","Note", MB_ICONEXCLAMATION|MB_OK);
  580. HRESULT result = DirectDrawObject->GetCaps (&video_capabilities, NULL);
  581. if (result != DD_OK){
  582. Process_DD_Result(result, FALSE);
  583. return (0);
  584. }
  585. /*
  586. ** Set flags to indicate the presence of the features we are interested in
  587. */
  588. video = 0;
  589. /* Hardware blits supported? */
  590. if (video_capabilities.dwCaps & DDCAPS_BLT) video |= VIDEO_BLITTER;
  591. /* Hardware blits asyncronous? */
  592. if (video_capabilities.dwCaps & DDCAPS_BLTQUEUE) video |= VIDEO_BLITTER_ASYNC;
  593. /* Can palette changes be synced to vertical refresh? */
  594. if (video_capabilities.dwCaps & DDCAPS_PALETTEVSYNC) video |= VIDEO_SYNC_PALETTE;
  595. /* Is the video cards memory bank switched? */
  596. if (video_capabilities.dwCaps & DDCAPS_BANKSWITCHED) video |= VIDEO_BANK_SWITCHED;
  597. /* Can the blitter do filled rectangles? */
  598. if (video_capabilities.dwCaps & DDCAPS_BLTCOLORFILL) video |= VIDEO_COLOR_FILL;
  599. /* Is there no hardware assistance avaailable at all? */
  600. if (video_capabilities.dwCaps & DDCAPS_NOHARDWARE) video |= VIDEO_NO_HARDWARE_ASSIST;
  601. //MessageBox(MainWindow, "In Get_Video_Hardware_Capabilities. About to return success.","Note", MB_ICONEXCLAMATION|MB_OK);
  602. return (video);
  603. }
  604. /***********************************************************************************************
  605. * Wait_Vert_Blank -- Waits for the start (leading edge) of a vertical blank *
  606. * *
  607. * INPUT: *
  608. * *
  609. * OUTPUT: *
  610. * *
  611. * WARNINGS: *
  612. * *
  613. * HISTORY: *
  614. *=============================================================================================*/
  615. extern int ScreenWidth;
  616. void Wait_Vert_Blank(void)
  617. {
  618. if( ScreenWidth!=320 && CanVblankSync){
  619. HRESULT result = DirectDrawObject->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0);
  620. if (result == E_NOTIMPL){
  621. CanVblankSync = FALSE;
  622. return;
  623. }
  624. Process_DD_Result(result, FALSE);
  625. }
  626. }
  627. /***********************************************************************************************
  628. * Set_Palette -- set a direct draw palette *
  629. * *
  630. * *
  631. * *
  632. * INPUT: ptr to 768 rgb palette bytes *
  633. * *
  634. * OUTPUT: Nothing *
  635. * *
  636. * WARNINGS: None *
  637. * *
  638. * HISTORY: *
  639. * 10/11/95 3:33PM ST : Created *
  640. *=============================================================================================*/
  641. void Set_DD_Palette ( void *palette )
  642. {
  643. /*
  644. ** Trap null ptr
  645. */
  646. if (!palette) return;
  647. int j;
  648. int k;
  649. char *palette_get;
  650. if ( DirectDrawObject && PaletteSurface ){
  651. k=0;
  652. palette_get = (char*)palette;
  653. for( j=0 ; j<768 ; j+=3 )
  654. {
  655. PaletteEntries[k].peRed = (unsigned char)((*palette_get++)<<2);
  656. PaletteEntries[k].peGreen = (unsigned char)((*palette_get++)<<2);
  657. PaletteEntries[k].peBlue = (unsigned char)((*palette_get++)<<2);
  658. k++;
  659. }
  660. if ( !FirstPaletteSet ){
  661. //MessageBox(MainWindow, "In Set_DD_Palette. About to call SetPalette","Note", MB_ICONEXCLAMATION|MB_OK);
  662. PaletteSurface->SetPalette( PalettePtr );
  663. FirstPaletteSet=TRUE;
  664. }
  665. //MessageBox(MainWindow, "In Set_DD_Palette. About to call SetEntries","Note", MB_ICONEXCLAMATION|MB_OK);
  666. PalettePtr->SetEntries( 0 , 0 , 256 , &PaletteEntries[0] );
  667. }
  668. //MessageBox(MainWindow, "Leaving Set_DD_Palette","Note", MB_ICONEXCLAMATION|MB_OK);
  669. }
  670. /***********************************************************************************************
  671. * Wait_Blit -- waits for the DirectDraw blitter to become idle *
  672. * *
  673. * *
  674. * *
  675. * INPUT: Nothing *
  676. * *
  677. * OUTPUT: Nothing *
  678. * *
  679. * WARNINGS: None *
  680. * *
  681. * HISTORY: *
  682. * 07-25-95 03:53pm ST : Created *
  683. *=============================================================================================*/
  684. void Wait_Blit (void)
  685. {
  686. HRESULT return_code;
  687. do {
  688. return_code=PaletteSurface->GetBltStatus (DDGBS_ISBLTDONE);
  689. } while (return_code != DD_OK && return_code != DDERR_SURFACELOST);
  690. }
  691. /***********************************************************************************************
  692. * SMC::SurfaceMonitorClass -- constructor for surface monitor class *
  693. * *
  694. * *
  695. * *
  696. * INPUT: Nothing *
  697. * *
  698. * OUTPUT: Nothing *
  699. * *
  700. * WARNINGS: None *
  701. * *
  702. * HISTORY: *
  703. * 11/3/95 3:23PM ST : Created *
  704. *=============================================================================================*/
  705. SurfaceMonitorClass::SurfaceMonitorClass(void)
  706. {
  707. for (int i=0 ; i<MAX_SURFACES ; i++)
  708. {
  709. Surface[i]=NULL;
  710. }
  711. InFocus=FALSE;
  712. SurfacesRestored=FALSE;
  713. }
  714. /***********************************************************************************************
  715. * SMC::Add_DD_Surface -- add a new surface to the list *
  716. * *
  717. * *
  718. * *
  719. * INPUT: ptr to surface *
  720. * *
  721. * OUTPUT: Nothing *
  722. * *
  723. * WARNINGS: None *
  724. * *
  725. * HISTORY: *
  726. * 11/3/95 3:24PM ST : Created *
  727. *=============================================================================================*/
  728. void SurfaceMonitorClass::Add_DD_Surface (LPDIRECTDRAWSURFACE new_surface)
  729. {
  730. if ( !Got_Surface_Already (new_surface) ){
  731. for (int i=0 ; i<MAX_SURFACES ; i++)
  732. {
  733. if ( Surface[i]==NULL ){
  734. Surface[i]=new_surface;
  735. return;
  736. }
  737. }
  738. }
  739. }
  740. /***********************************************************************************************
  741. * SMC::Remove_DD_Surface -- remove a direct draw surface from the list *
  742. * *
  743. * *
  744. * *
  745. * INPUT: ptr to Surface *
  746. * *
  747. * OUTPUT: Nothing *
  748. * *
  749. * WARNINGS: None *
  750. * *
  751. * HISTORY: *
  752. * 11/3/95 3:25PM ST : Created *
  753. *=============================================================================================*/
  754. void SurfaceMonitorClass::Remove_DD_Surface (LPDIRECTDRAWSURFACE old_surface)
  755. {
  756. for (int i=0 ; i<MAX_SURFACES ; i++)
  757. {
  758. if ( Surface[i]==old_surface ){
  759. Surface[i]=NULL;
  760. return;
  761. }
  762. }
  763. }
  764. /***********************************************************************************************
  765. * SMC::Got_Surface_Already -- check if a surface is already in the list *
  766. * *
  767. * *
  768. * *
  769. * INPUT: ptr to surface *
  770. * *
  771. * OUTPUT: True if surface is in list *
  772. * *
  773. * WARNINGS: None *
  774. * *
  775. * HISTORY: *
  776. * 11/3/95 3:25PM ST : Created *
  777. *=============================================================================================*/
  778. BOOL SurfaceMonitorClass::Got_Surface_Already (LPDIRECTDRAWSURFACE test_surface)
  779. {
  780. for (int i=0 ; i<MAX_SURFACES ; i++)
  781. {
  782. if ( Surface[i]==test_surface ){
  783. return(TRUE);
  784. }
  785. }
  786. return (FALSE);
  787. }
  788. /***********************************************************************************************
  789. * SMC::Restore_Surfaces -- restore the direct draw surfaces in the list *
  790. * *
  791. * *
  792. * *
  793. * INPUT: Nothing *
  794. * *
  795. * OUTPUT: Nothing *
  796. * *
  797. * WARNINGS: None *
  798. * *
  799. * HISTORY: *
  800. * 11/3/95 3:26PM ST : Created *
  801. *=============================================================================================*/
  802. void SurfaceMonitorClass::Restore_Surfaces (void)
  803. {
  804. if (InFocus){
  805. /*
  806. ** Call restore for each Direct Draw surface
  807. */
  808. for (int i=0 ; i<MAX_SURFACES ; i++)
  809. {
  810. if ( Surface[i] ){
  811. if (Surface[i]->Restore() != DD_OK){
  812. if (Misc_Focus_Loss_Function){
  813. Misc_Focus_Loss_Function();
  814. }
  815. return;
  816. }
  817. }
  818. }
  819. /*
  820. ** PWG/ST: Now that we know all the surfaces are restored call
  821. ** the function pointer to notify the program that it has
  822. ** happened. This function pointer is used to clear the pages,
  823. ** etc.
  824. */
  825. if (Misc_Focus_Restore_Function){
  826. Misc_Focus_Restore_Function();
  827. }
  828. SurfacesRestored = TRUE;
  829. /*
  830. ** Restore the palette
  831. */
  832. Set_DD_Palette (CurrentPalette);
  833. }
  834. }
  835. /***********************************************************************************************
  836. * SMC::Set_Surface_Focus -- set the InFocus flag to the given state *
  837. * *
  838. * The InFocus flag is used to keep track of whether our application is currently in focus. *
  839. * We dont want to be restoring video surfaces when we are supposed to be running in the *
  840. * background. *
  841. * *
  842. * INPUT: bool in focus *
  843. * *
  844. * OUTPUT: Nothing *
  845. * *
  846. * WARNINGS: None *
  847. * *
  848. * HISTORY: *
  849. * 11/6/95 12:21PM ST : Created *
  850. *=============================================================================================*/
  851. void SurfaceMonitorClass::Set_Surface_Focus ( BOOL in_focus )
  852. {
  853. InFocus=in_focus;
  854. }
  855. /***********************************************************************************************
  856. * SMC::Release -- releases all direct draw surfaces *
  857. * *
  858. * Call this at the end of the game before called RestoreDisplayMode *
  859. * *
  860. * INPUT: Nothing *
  861. * *
  862. * OUTPUT: Nothing *
  863. * *
  864. * WARNINGS: None *
  865. * *
  866. * HISTORY: *
  867. * 6/6/96 12:23PM ST : Created *
  868. *=============================================================================================*/
  869. void SurfaceMonitorClass::Release(void)
  870. {
  871. /*
  872. ** Call release for each Direct Draw surface
  873. */
  874. for (int i=0 ; i<MAX_SURFACES ; i++)
  875. {
  876. if ( Surface[i] ){
  877. Surface[i]->Release();
  878. Surface[i] = 0;
  879. }
  880. }
  881. }