WEDInterface.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #include "StdAfx.h"
  13. extern CGame *_pGame;
  14. extern INDEX gam_iQuickStartDifficulty;
  15. extern INDEX gam_iQuickStartMode;
  16. extern INDEX gam_iStartDifficulty;
  17. extern INDEX gam_iStartMode;
  18. // initialize game and load settings
  19. void CGame::Initialize(const CTFileName &fnGameSettings)
  20. {
  21. gm_fnSaveFileName = fnGameSettings;
  22. InitInternal();
  23. }
  24. // save settings and cleanup
  25. void CGame::End(void)
  26. {
  27. EndInternal();
  28. }
  29. // automaticaly manage input enable/disable toggling
  30. static BOOL _bInputEnabled = FALSE;
  31. void UpdateInputEnabledState(CViewPort *pvp)
  32. {
  33. // input should be enabled if application is active
  34. // and no menu is active and no console is active
  35. BOOL bShouldBeEnabled = _pGame->gm_csConsoleState==CS_OFF && _pGame->gm_csComputerState==CS_OFF;
  36. // if should be turned off
  37. if (!bShouldBeEnabled && _bInputEnabled) {
  38. // disable it
  39. _pInput->DisableInput();
  40. // remember new state
  41. _bInputEnabled = FALSE;
  42. }
  43. // if should be turned on
  44. if (bShouldBeEnabled && !_bInputEnabled) {
  45. // enable it
  46. _pInput->EnableInput(pvp);
  47. // remember new state
  48. _bInputEnabled = TRUE;
  49. }
  50. }
  51. // automaticaly manage pause toggling
  52. void UpdatePauseState(void)
  53. {
  54. BOOL bShouldPause =
  55. _pGame->gm_csConsoleState ==CS_ON || _pGame->gm_csConsoleState ==CS_TURNINGON || _pGame->gm_csConsoleState ==CS_TURNINGOFF ||
  56. _pGame->gm_csComputerState==CS_ON || _pGame->gm_csComputerState==CS_TURNINGON || _pGame->gm_csComputerState==CS_TURNINGOFF;
  57. _pNetwork->SetLocalPause(bShouldPause);
  58. }
  59. // run a quicktest game from within editor
  60. void CGame::QuickTest(const CTFileName &fnMapName,
  61. CDrawPort *pdp, CViewPort *pvp)
  62. {
  63. UINT uiMessengerMsg = RegisterWindowMessageA("Croteam Messenger: Incoming Message");
  64. EnableLoadingHook(pdp);
  65. // quick start game with the world
  66. gm_strNetworkProvider = "Local";
  67. gm_aiStartLocalPlayers[0] = gm_iWEDSinglePlayer;
  68. gm_aiStartLocalPlayers[1] = -1;
  69. gm_aiStartLocalPlayers[2] = -1;
  70. gm_aiStartLocalPlayers[3] = -1;
  71. gm_CurrentSplitScreenCfg = CGame::SSC_PLAY1;
  72. // set properties for a quick start session
  73. CSessionProperties sp;
  74. SetQuickStartSession(sp);
  75. // start the game
  76. if( !NewGame( fnMapName, fnMapName, sp)) {
  77. DisableLoadingHook();
  78. return;
  79. }
  80. // enable input
  81. _pInput->EnableInput(pvp);
  82. // initialy, game is running
  83. BOOL bRunning = TRUE;
  84. // while it is still running
  85. while( bRunning)
  86. {
  87. // while there are any messages in the message queue
  88. MSG msg;
  89. while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE)) {
  90. // if it is not a mouse message
  91. if( !(msg.message>=WM_MOUSEFIRST && msg.message<=WM_MOUSELAST)) {
  92. // if not system key messages
  93. if( !(msg.message==WM_KEYDOWN && msg.wParam==VK_F10
  94. ||msg.message==WM_SYSKEYDOWN)) {
  95. // dispatch it
  96. TranslateMessage(&msg);
  97. }
  98. // if paint message
  99. if( msg.message==WM_PAINT) {
  100. // dispatch it
  101. DispatchMessage(&msg);
  102. }
  103. }
  104. // if should stop
  105. if ((msg.message==WM_QUIT)
  106. ||(msg.message==WM_CLOSE)
  107. ||(msg.message==WM_KEYDOWN && msg.wParam==VK_ESCAPE)
  108. ||(msg.message==WM_ACTIVATE)
  109. ||(msg.message==WM_CANCELMODE)
  110. ||(msg.message==WM_KILLFOCUS)
  111. ||(msg.message==WM_ACTIVATEAPP)) {
  112. // stop running
  113. bRunning = FALSE;
  114. break;
  115. }
  116. if (msg.message==uiMessengerMsg)
  117. {
  118. if(!_pNetwork->IsPaused())
  119. {
  120. // pause it
  121. _pNetwork->TogglePause();
  122. }
  123. char *pachrTemp=getenv("TEMP");
  124. if( pachrTemp!=NULL)
  125. {
  126. FILE *pfileMessage=fopen(CTString(pachrTemp)+"Messenger.msg","r");
  127. if( pfileMessage!=NULL)
  128. {
  129. char achrMessage[1024];
  130. char *pachrMessage=fgets( achrMessage, 1024-1, pfileMessage);
  131. if( pachrMessage!=NULL)
  132. {
  133. CPrintF("%s",pachrMessage);
  134. }
  135. }
  136. }
  137. }
  138. // if pause pressed
  139. if (msg.message==WM_KEYDOWN && msg.wParam==VK_PAUSE &&
  140. _pGame->gm_csConsoleState==CS_OFF && _pGame->gm_csComputerState==CS_OFF) {
  141. // toggle pause
  142. _pNetwork->TogglePause();
  143. }
  144. if(msg.message==WM_KEYDOWN &&
  145. (MapVirtualKey(msg.wParam, 0)==41 // scan code for '~'
  146. ||msg.wParam==VK_F1)) {
  147. if (_pGame->gm_csConsoleState==CS_OFF || _pGame->gm_csConsoleState==CS_TURNINGOFF) {
  148. _pGame->gm_csConsoleState = CS_TURNINGON;
  149. } else {
  150. _pGame->gm_csConsoleState = CS_TURNINGOFF;
  151. }
  152. }
  153. extern INDEX con_bTalk;
  154. if (con_bTalk && _pGame->gm_csConsoleState==CS_OFF) {
  155. con_bTalk = FALSE;
  156. _pGame->gm_csConsoleState = CS_TALK;
  157. }
  158. if (msg.message==WM_KEYDOWN) {
  159. ConsoleKeyDown(msg);
  160. if (_pGame->gm_csConsoleState!=CS_ON) {
  161. ComputerKeyDown(msg);
  162. }
  163. } else if (msg.message==WM_KEYUP) {
  164. // special handler for talk (not to invoke return key bind)
  165. if( msg.wParam==VK_RETURN && _pGame->gm_csConsoleState==CS_TALK) _pGame->gm_csConsoleState = CS_OFF;
  166. } else if (msg.message==WM_CHAR) {
  167. ConsoleChar(msg);
  168. }
  169. if (msg.message==WM_LBUTTONDOWN
  170. ||msg.message==WM_RBUTTONDOWN
  171. ||msg.message==WM_LBUTTONDBLCLK
  172. ||msg.message==WM_RBUTTONDBLCLK
  173. ||msg.message==WM_LBUTTONUP
  174. ||msg.message==WM_RBUTTONUP) {
  175. if (_pGame->gm_csConsoleState!=CS_ON) {
  176. ComputerKeyDown(msg);
  177. }
  178. }
  179. }
  180. // get real cursor position
  181. if (_pGame->gm_csComputerState != CS_OFF) {
  182. POINT pt;
  183. ::GetCursorPos(&pt);
  184. ::ScreenToClient(pvp->vp_hWnd, &pt);
  185. ComputerMouseMove(pt.x, pt.y);
  186. }
  187. UpdatePauseState();
  188. UpdateInputEnabledState(pvp);
  189. // if playing a demo and it is finished
  190. if (_pNetwork->IsDemoPlayFinished()) {
  191. // stop running
  192. bRunning = FALSE;
  193. }
  194. // do the main game loop
  195. GameMainLoop();
  196. // redraw the view
  197. if (pdp->Lock()) {
  198. // if current view preferences will not clear the background, clear it here
  199. if( _wrpWorldRenderPrefs.GetPolygonsFillType() == CWorldRenderPrefs::FT_NONE) {
  200. // clear background
  201. pdp->Fill(C_BLACK| CT_OPAQUE);
  202. pdp->FillZBuffer(ZBUF_BACK);
  203. }
  204. // redraw view
  205. if (_pGame->gm_csComputerState != CS_ON) {
  206. GameRedrawView(pdp, (_pGame->gm_csConsoleState==CS_ON)?0:GRV_SHOWEXTRAS);
  207. }
  208. ComputerRender(pdp);
  209. ConsoleRender(pdp);
  210. pdp->Unlock();
  211. // show it
  212. pvp->SwapBuffers();
  213. }
  214. }
  215. if (_pGame->gm_csConsoleState != CS_OFF) {
  216. _pGame->gm_csConsoleState = CS_TURNINGOFF;
  217. }
  218. if (_pGame->gm_csComputerState != CS_OFF) {
  219. _pGame->gm_csComputerState = CS_TURNINGOFF;
  220. cmp_ppenPlayer = NULL;
  221. }
  222. _pInput->DisableInput();
  223. StopGame();
  224. DisableLoadingHook();
  225. }