ddraw.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*=============================================================================
  2. Name : ddraw.cpp
  3. Purpose : non-DirectDraw methods for setting display modes
  4. Created 7/23/1999 by khent
  5. Copyright Relic Entertainment, Inc. All rights reserved.
  6. =============================================================================*/
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <windows.h>
  9. extern "C" unsigned int fullScreen;
  10. extern "C" unsigned int mainDirectDraw;
  11. static BOOL hwCreated = FALSE;
  12. static int hwWidth, hwHeight;
  13. static int hwDepth = 0;
  14. extern "C" unsigned int ddSetRes(int, int, int);
  15. extern "C" unsigned int ddCreateWindow(int, int, int, int);
  16. extern "C" unsigned int ddDeleteWindow(void);
  17. extern "C" unsigned int ddGetDepth(void);
  18. extern "C" unsigned int ddActivate(int activate);
  19. /*-----------------------------------------------------------------------------
  20. Name : hwGetDepth
  21. Description : returns screen bitdepth
  22. Inputs :
  23. Outputs :
  24. Return : bitdepth
  25. ----------------------------------------------------------------------------*/
  26. extern "C" unsigned int hwGetDepth(void)
  27. {
  28. return hwDepth;
  29. }
  30. /*-----------------------------------------------------------------------------
  31. Name : hwSetRes
  32. Description : attempt to set specified display mode, or restore previous
  33. Inputs : width, height, depth - display mode characteristics
  34. Outputs :
  35. Return : >0 success
  36. ----------------------------------------------------------------------------*/
  37. extern "C" unsigned int hwSetRes(int width, int height, int depth)
  38. {
  39. DEVMODE devMode;
  40. int modeExist, modeSwitch, closeMode = 0;
  41. int i;
  42. if (width == -1)
  43. {
  44. width = hwWidth;
  45. height = hwHeight;
  46. depth = hwDepth;
  47. }
  48. if (width == 0)
  49. {
  50. return (ChangeDisplaySettings(NULL, 0) == DISP_CHANGE_SUCCESSFUL);
  51. }
  52. else
  53. {
  54. for (i = 0;; i++)
  55. {
  56. modeExist = EnumDisplaySettings(NULL, i, &devMode);
  57. if (!modeExist)
  58. {
  59. break;
  60. }
  61. if ((devMode.dmBitsPerPel == depth) &&
  62. (devMode.dmPelsWidth == width) &&
  63. (devMode.dmPelsHeight == height))
  64. {
  65. modeSwitch = ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
  66. if (modeSwitch == DISP_CHANGE_SUCCESSFUL)
  67. {
  68. hwWidth = width;
  69. hwHeight = height;
  70. hwDepth = depth;
  71. return 1;
  72. }
  73. if (!closeMode)
  74. {
  75. closeMode = i;
  76. }
  77. }
  78. }
  79. EnumDisplaySettings(NULL, closeMode, &devMode);
  80. devMode.dmBitsPerPel = depth;
  81. devMode.dmPelsWidth = width;
  82. devMode.dmPelsHeight = height;
  83. devMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  84. modeSwitch = ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
  85. if (modeSwitch == DISP_CHANGE_SUCCESSFUL)
  86. {
  87. hwWidth = width;
  88. hwHeight = height;
  89. hwDepth = depth;
  90. return 1;
  91. }
  92. devMode.dmFields = DM_BITSPERPEL;
  93. modeSwitch = ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
  94. if (modeSwitch == DISP_CHANGE_SUCCESSFUL)
  95. {
  96. devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
  97. modeSwitch = ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
  98. if (modeSwitch == DISP_CHANGE_SUCCESSFUL)
  99. {
  100. hwWidth = width;
  101. hwHeight = height;
  102. hwDepth = depth;
  103. return 1;
  104. }
  105. ChangeDisplaySettings(NULL, 0);
  106. }
  107. #if 0
  108. if (modeSwitch == DISP_CHANGE_RESTART)
  109. {
  110. //...
  111. }
  112. else if (modeSwitch == DISP_CHANGE_BADMODE)
  113. {
  114. //...
  115. }
  116. else if (modeSwitch == DISP_CHANGE_FAILED)
  117. {
  118. //...
  119. }
  120. else
  121. {
  122. //...
  123. }
  124. #endif
  125. hwDepth = 0;
  126. return 0;
  127. }
  128. }
  129. /*-----------------------------------------------------------------------------
  130. Name : hwDeleteWindow
  131. Description : restore previous display mode
  132. Inputs :
  133. Outputs :
  134. Return : >0 success
  135. ----------------------------------------------------------------------------*/
  136. extern "C" unsigned int hwDeleteWindow(void)
  137. {
  138. if (mainDirectDraw)
  139. {
  140. return ddDeleteWindow();
  141. }
  142. if (hwCreated)
  143. {
  144. hwCreated = FALSE;
  145. return hwSetRes(0, 0, 0);
  146. }
  147. else
  148. {
  149. return TRUE;
  150. }
  151. }
  152. /*-----------------------------------------------------------------------------
  153. Name : hwCreateWindow
  154. Description : set display mode
  155. Inputs : ihwnd - window handle
  156. width, height, depth - display mode characteristics
  157. Outputs :
  158. Return : >0 success
  159. ----------------------------------------------------------------------------*/
  160. extern "C" unsigned int hwCreateWindow(int ihwnd, int width, int height, int depth)
  161. {
  162. unsigned int rval;
  163. if (mainDirectDraw)
  164. {
  165. return ddCreateWindow(ihwnd, width, height, depth);
  166. }
  167. if (!fullScreen)
  168. {
  169. hwCreated = FALSE;
  170. hwDepth = 0;
  171. return TRUE;
  172. }
  173. rval = hwSetRes(width, height, depth);
  174. hwCreated = rval;
  175. return rval;
  176. }
  177. /*-----------------------------------------------------------------------------
  178. Name : hwActivate
  179. Description : restore / set the display mode on minimize / restore
  180. Inputs : activate - 0 minimize, 1 restore
  181. Outputs :
  182. Return : >0 success
  183. ----------------------------------------------------------------------------*/
  184. extern "C" unsigned int opReloading;
  185. extern "C" unsigned int hwActivate(int activate)
  186. {
  187. if (mainDirectDraw)
  188. {
  189. return ddActivate(activate);
  190. }
  191. if (opReloading)
  192. {
  193. return 1;
  194. }
  195. if (activate)
  196. {
  197. //restore
  198. return hwSetRes(hwWidth, hwHeight, hwDepth);
  199. }
  200. else
  201. {
  202. //minimize
  203. return hwSetRes(0, 0, 0);
  204. }
  205. }