game.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/platformInput.h"
  24. #include "app/game.h"
  25. #include "math/mMath.h"
  26. #include "core/dnet.h"
  27. #include "core/stream/fileStream.h"
  28. #include "core/frameAllocator.h"
  29. #include "core/iTickable.h"
  30. #include "core/strings/findMatch.h"
  31. #include "console/simBase.h"
  32. #include "console/console.h"
  33. #include "console/consoleTypes.h"
  34. #include "console/engineAPI.h"
  35. #include "gui/controls/guiMLTextCtrl.h"
  36. #ifdef TORQUE_TGB_ONLY
  37. #include "T2D/oldModel/networking/t2dGameConnection.h"
  38. #include "T2D/oldModel/networking/t2dNetworkServerSceneProcess.h"
  39. #include "T2D/oldModel/networking/t2dNetworkClientSceneProcess.h"
  40. #else
  41. #include "T3D/gameBase/gameConnection.h"
  42. #include "T3D/gameFunctions.h"
  43. #include "T3D/gameBase/gameProcess.h"
  44. #endif
  45. #include "platform/profiler.h"
  46. #include "gfx/gfxCubemap.h"
  47. #include "gfx/gfxTextureManager.h"
  48. #include "sfx/sfxSystem.h"
  49. #ifdef TORQUE_PLAYER
  50. // See matching #ifdef in editor/editor.cpp
  51. bool gEditingMission = false;
  52. #endif
  53. //--------------------------------------------------------------------------
  54. ConsoleFunctionGroupBegin( InputManagement, "Functions that let you deal with input from scripts" );
  55. DefineConsoleFunction( deactivateDirectInput, void, (), ,
  56. "()"
  57. "@brief Disables DirectInput.\n\n"
  58. "Also deactivates any connected joysticks.\n\n"
  59. "@ingroup Input" )
  60. {
  61. if ( Input::isActive() )
  62. Input::deactivate();
  63. }
  64. DefineConsoleFunction( activateDirectInput, void, (), ,
  65. "()"
  66. "@brief Activates DirectInput.\n\n"
  67. "Also activates any connected joysticks."
  68. "@ingroup Input")
  69. {
  70. if ( !Input::isActive() )
  71. Input::activate();
  72. }
  73. ConsoleFunctionGroupEnd( InputManagement );
  74. //--------------------------------------------------------------------------
  75. static const U32 MaxPlayerNameLength = 16;
  76. DefineConsoleFunction( strToPlayerName, const char*, (const char* ptr ), , "strToPlayerName(string);" )
  77. {
  78. // Strip leading spaces and underscores:
  79. while ( *ptr == ' ' || *ptr == '_' )
  80. ptr++;
  81. U32 len = dStrlen( ptr );
  82. if ( len )
  83. {
  84. char* ret = Con::getReturnBuffer( MaxPlayerNameLength + 1 );
  85. char* rptr = ret;
  86. ret[MaxPlayerNameLength - 1] = '\0';
  87. ret[MaxPlayerNameLength] = '\0';
  88. bool space = false;
  89. U8 ch;
  90. while ( *ptr && dStrlen( ret ) < MaxPlayerNameLength )
  91. {
  92. ch = (U8) *ptr;
  93. // Strip all illegal characters:
  94. if ( ch < 32 || ch == ',' || ch == '.' || ch == '\'' || ch == '`' )
  95. {
  96. ptr++;
  97. continue;
  98. }
  99. // Don't allow double spaces or space-underline combinations:
  100. if ( ch == ' ' || ch == '_' )
  101. {
  102. if ( space )
  103. {
  104. ptr++;
  105. continue;
  106. }
  107. else
  108. space = true;
  109. }
  110. else
  111. space = false;
  112. *rptr++ = *ptr;
  113. ptr++;
  114. }
  115. *rptr = '\0';
  116. //finally, strip out the ML text control chars...
  117. return GuiMLTextCtrl::stripControlChars(ret);
  118. }
  119. return( "" );
  120. }
  121. ConsoleFunctionGroupBegin( Platform , "General platform functions.");
  122. DefineConsoleFunction( lockMouse, void, (bool isLocked ), , "(bool isLocked)"
  123. "@brief Lock or unlock the mouse to the window.\n\n"
  124. "When true, prevents the mouse from leaving the bounds of the game window.\n\n"
  125. "@ingroup Input")
  126. {
  127. Platform::setWindowLocked(isLocked);
  128. }
  129. DefineConsoleFunction( setNetPort, bool, (int port, bool bind), (true), "(int port, bool bind=true)"
  130. "@brief Set the network port for the game to use.\n\n"
  131. "@param port The port to use.\n"
  132. "@param bind True if bind() should be called on the port.\n"
  133. "@returns True if the port was successfully opened.\n"
  134. "This will trigger a windows firewall prompt. "
  135. "If you don't have firewall tunneling tech you can set this to false to avoid the prompt.\n\n"
  136. "@ingroup Networking")
  137. {
  138. return Net::openPort((S32)port, bind);
  139. }
  140. DefineConsoleFunction( closeNetPort, void, (), , "()"
  141. "@brief Closes the current network port\n\n"
  142. "@ingroup Networking")
  143. {
  144. Net::closePort();
  145. }
  146. DefineConsoleFunction( saveJournal, void, (const char * filename), , "(string filename)"
  147. "Save the journal to the specified file.\n\n"
  148. "@ingroup Platform")
  149. {
  150. Journal::Record(filename);
  151. }
  152. DefineConsoleFunction( playJournal, void, (const char * filename), , "(string filename)"
  153. "@brief Begin playback of a journal from a specified field.\n\n"
  154. "@param filename Name and path of file journal file\n"
  155. "@ingroup Platform")
  156. {
  157. // CodeReview - BJG 4/24/2007 - The break flag needs to be wired back in.
  158. // bool jBreak = (argc > 2)? dAtob(argv[2]): false;
  159. Journal::Play(filename);
  160. }
  161. DefineConsoleFunction( getSimTime, S32, (), , "()"
  162. "Return the current sim time in milliseconds.\n\n"
  163. "@brief Sim time is time since the game started.\n\n"
  164. "@ingroup Platform")
  165. {
  166. return Sim::getCurrentTime();
  167. }
  168. DefineConsoleFunction( getRealTime, S32, (), , "()"
  169. "@brief Return the current real time in milliseconds.\n\n"
  170. "Real time is platform defined; typically time since the computer booted.\n\n"
  171. "@ingroup Platform")
  172. {
  173. return Platform::getRealMilliseconds();
  174. }
  175. ConsoleFunctionGroupEnd(Platform);
  176. //-----------------------------------------------------------------------------
  177. bool clientProcess(U32 timeDelta)
  178. {
  179. bool ret = true;
  180. #ifndef TORQUE_TGB_ONLY
  181. ret = ClientProcessList::get()->advanceTime(timeDelta);
  182. #else
  183. ret = gt2dNetworkClientProcess.advanceTime( timeDelta );
  184. #endif
  185. ITickable::advanceTime(timeDelta);
  186. #ifndef TORQUE_TGB_ONLY
  187. // Determine if we're lagging
  188. GameConnection* connection = GameConnection::getConnectionToServer();
  189. if(connection)
  190. {
  191. connection->detectLag();
  192. }
  193. #else
  194. // Determine if we're lagging
  195. t2dGameConnection* connection = t2dGameConnection::getConnectionToServer();
  196. if(connection)
  197. {
  198. connection->detectLag();
  199. }
  200. #endif
  201. // Let SFX process.
  202. SFX->_update();
  203. return ret;
  204. }
  205. bool serverProcess(U32 timeDelta)
  206. {
  207. bool ret = true;
  208. #ifndef TORQUE_TGB_ONLY
  209. ret = ServerProcessList::get()->advanceTime(timeDelta);
  210. #else
  211. ret = gt2dNetworkServerProcess.advanceTime( timeDelta );
  212. #endif
  213. return ret;
  214. }