gameInterface.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _GAMEINTERFACE_H_
  23. #define _GAMEINTERFACE_H_
  24. #include "platform/event.h"
  25. #include "collection/vector.h"
  26. class FileStream;
  27. class GameInterface
  28. {
  29. private:
  30. enum JournalMode {
  31. JournalOff,
  32. JournalSave,
  33. JournalPlay,
  34. };
  35. JournalMode mJournalMode;
  36. bool mRunning;
  37. bool mJournalBreak;
  38. bool mRequiresRestart;
  39. /// Events are stored here by any thread, for processing by the main thread.
  40. Vector<Event*> eventQueue1, eventQueue2, *eventQueue;
  41. public:
  42. GameInterface();
  43. /// @name Game Loop
  44. /// @{
  45. virtual bool mainInitialize( int argc, const char **argv ) = 0;
  46. virtual void mainLoop( void ) = 0;
  47. virtual void mainShutdown( void ) = 0;
  48. /// @}
  49. virtual void gameDeactivate( const bool noRender ) = 0;
  50. virtual void gameReactivate( void ) = 0;
  51. /// @name Platform Interface
  52. /// The platform calls these functions to control execution of the game.
  53. /// @{
  54. virtual void textureKill( void ) = 0;
  55. virtual void textureResurrect( void ) = 0;
  56. virtual void refreshWindow( void ) = 0;
  57. /// Place an event in Game's event queue.
  58. virtual void postEvent(Event &event);
  59. /// Process all the events in Game's event queue. Only the main thread should call this.
  60. virtual void processEvents();
  61. /// @}
  62. /// @name Event Handlers
  63. /// default event behavior with journaling support
  64. /// default handler forwards events to appropriate routines
  65. /// @{
  66. virtual void processEvent(Event *event);
  67. virtual void processQuitEvent() = 0;
  68. virtual void processTimeEvent(TimeEvent *event) = 0;
  69. virtual void processInputEvent(InputEvent *event) = 0;
  70. virtual void processMouseMoveEvent(MouseMoveEvent *event) = 0;
  71. virtual void processScreenTouchEvent(ScreenTouchEvent *event) = 0;
  72. virtual void processConsoleEvent(ConsoleEvent *event) = 0;
  73. virtual void processPacketReceiveEvent(PacketReceiveEvent *event) = 0;
  74. virtual void processConnectedAcceptEvent(ConnectedAcceptEvent *event) = 0;
  75. virtual void processConnectedReceiveEvent(ConnectedReceiveEvent *event) = 0;
  76. virtual void processConnectedNotifyEvent(ConnectedNotifyEvent *event) = 0;
  77. /// @}
  78. /// @name Running
  79. /// @{
  80. inline void setRunning( const bool running ) { mRunning = running; }
  81. inline bool isRunning( void ) const { return mRunning; }
  82. inline void setRestart( const bool restart ) { mRequiresRestart = restart; }
  83. inline bool requiresRestart( void ) const { return mRequiresRestart; }
  84. /// @}
  85. /// @name Journaling
  86. ///
  87. /// Journaling is used in order to make a "demo" of the actual game. It logs
  88. /// all processes that happen throughout code execution (NOT script). This is
  89. /// very handy for debugging. Say an end user finds a crash in the program.
  90. /// The user can start up the engine with journal recording enabled, reproduce
  91. /// the crash, then send in the journal file to the development team. The
  92. /// development team can then play back the journal file and see exactly what
  93. /// happened to cause the crash. This will result in the ability to run the
  94. /// program through the debugger to easily track what went wrong and easily
  95. /// create a stack trace.
  96. ///
  97. /// Actually enabling journaling may be different in different distributions
  98. /// if the developers decided to change how it works. However, by default,
  99. /// run the program with the "-jSave filename" command argument. The filename
  100. /// does not need an extension, and only requires write access. If the file
  101. /// does not exist, it will be created. In order to play back a journal,
  102. /// use the "-jPlay filename" command argument, and just watch the magic happen.
  103. /// Examples:
  104. /// @code
  105. /// torqueDemo_DEBUG.exe -jSave crash
  106. /// torqueDemo_DEBUG.exe -jPlay crash
  107. /// @endcode
  108. /// @{
  109. /// If we're doing a journal playback, this function is responsible for reading
  110. /// events from the journal file and dispatching them.
  111. void journalProcess();
  112. /// Start loading journal data from the specified file.
  113. void loadJournal(const char *fileName);
  114. /// Start saving journal data to the specified file (must be able to write it).
  115. void saveJournal(const char *fileName);
  116. /// Play back the specified journal.
  117. ///
  118. /// @param fileName Journal file to play back.
  119. /// @param journalBreak Should we break execution after we're done?
  120. void playJournal(const char *fileName, bool journalBreak = false);
  121. JournalMode getJournalMode() { return mJournalMode; };
  122. /// Are we reading back from the journal?
  123. inline bool isJournalReading( void ) const { return mJournalMode == JournalPlay; }
  124. /// Are we writing to the journal?
  125. inline bool isJournalWriting( void ) const { return mJournalMode == JournalSave; }
  126. void journalRead(U32 *val); ///< Read a U32 from the journal.
  127. void journalWrite(U32 val); ///< Write a U32 to the journal.
  128. void journalRead(U32 size, void *buffer); ///< Read a block of data from the journal.
  129. void journalWrite(U32 size, const void *buffer);///< Write a block of data to the journal.
  130. FileStream *getJournalStream( void );
  131. /// @}
  132. };
  133. /// Global game instance.
  134. extern GameInterface* Game;
  135. #endif