AppInterface.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "AppInterface.h"
  11. #include "RakAssert.h"
  12. #include "FSM.h"
  13. #include "RunnableState.h"
  14. #ifdef _CONSOLE
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #if defined(__GNUC__)
  19. #define _vsnprintf vsnprintf
  20. #endif
  21. #endif
  22. AppInterface::AppInterface()
  23. {
  24. hasFocus=false;
  25. primaryFSM=0;
  26. primaryStates=0;
  27. quit=false;
  28. primaryStatesLength=0;
  29. lastElapsedTimeMS=0;
  30. lastCurTimeMS=0;
  31. }
  32. AppInterface::~AppInterface()
  33. {
  34. }
  35. void AppInterface::PreConfigure(void)
  36. {
  37. primaryFSM = new FSM;
  38. }
  39. void AppInterface::PostConfigure(const char *defaultResourceConfigurationPath)
  40. {
  41. }
  42. void AppInterface::Update(AppTime curTimeMS,AppTime elapsedTimeMS)
  43. {
  44. lastCurTimeMS=curTimeMS;
  45. lastElapsedTimeMS=elapsedTimeMS;
  46. }
  47. void AppInterface::OnAppShutdown(void)
  48. {
  49. delete primaryFSM;
  50. if (primaryStates)
  51. {
  52. int i;
  53. for (i=0; i < primaryStatesLength; i++)
  54. delete primaryStates[i];
  55. delete [] primaryStates;
  56. }
  57. }
  58. void AppInterface::DebugOut(unsigned int lifetimeMS, const char *format, ...)
  59. {
  60. #ifdef _CONSOLE
  61. char text[8096];
  62. va_list ap;
  63. va_start(ap, format);
  64. _vsnprintf(text, 8096-1, format, ap);
  65. va_end(ap);
  66. strcat(text, "\n");
  67. text[8096-1]=0;
  68. printf(text);
  69. #else
  70. // Don't call this without an implementation. Perhaps you meant to use MainApp() instead
  71. RakAssert(0);
  72. #endif
  73. }
  74. bool AppInterface::HasFocus(void) const
  75. {
  76. return hasFocus;
  77. }
  78. void AppInterface::SetFocus(bool hasFocus)
  79. {
  80. if (this->hasFocus!=hasFocus)
  81. {
  82. if (primaryFSM->CurrentState())
  83. ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(hasFocus);
  84. this->hasFocus=hasFocus;
  85. }
  86. }
  87. void AppInterface::PushState(RunnableState* state)
  88. {
  89. if (hasFocus && primaryFSM->CurrentState())
  90. ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(false);
  91. primaryFSM->AddState(state);
  92. if (hasFocus)
  93. ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(true);
  94. else
  95. ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(false);
  96. }
  97. void AppInterface::PushState(int stateType)
  98. {
  99. PushState(primaryStates[stateType]);
  100. }
  101. void AppInterface::PopState(int popCount)
  102. {
  103. RakAssert(popCount>=1);
  104. RakAssert(primaryFSM->GetStateHistorySize()>=1+popCount);
  105. if (hasFocus && primaryFSM->CurrentState())
  106. ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(false);
  107. primaryFSM->SetPriorState(primaryFSM->GetStateHistorySize()-1-popCount);
  108. if (hasFocus)
  109. ((RunnableState*)(primaryFSM->CurrentState()))->SetFocus(true);
  110. }
  111. RunnableState* AppInterface::GetCurrentState(void) const
  112. {
  113. return (RunnableState*) primaryFSM->CurrentState();
  114. }
  115. int AppInterface::GetStateHistorySize(void) const
  116. {
  117. return primaryFSM->GetStateHistorySize();
  118. }
  119. void AppInterface::AllocateStates(int numStates)
  120. {
  121. RakAssert(primaryStates==0);
  122. primaryStates = new RunnableState*[numStates];
  123. primaryStatesLength=numStates;
  124. }
  125. void AppInterface::SetState(int stateType, RunnableState* state)
  126. {
  127. primaryStates[stateType] = state;
  128. state->SetParentApp(this);
  129. state->OnStateReady();
  130. }
  131. RunnableState* AppInterface::GetState(int stateType) const
  132. {
  133. return primaryStates[stateType];
  134. }
  135. bool AppInterface::ShouldQuit(void) const
  136. {
  137. return quit;
  138. }
  139. void AppInterface::Quit(void)
  140. {
  141. quit=true;
  142. }
  143. AppTime AppInterface::GetLastCurrentTime(void) const
  144. {
  145. return lastCurTimeMS;
  146. }
  147. AppTime AppInterface::GetLastElapsedTime(void) const
  148. {
  149. return lastElapsedTimeMS;
  150. }
粤ICP备19079148号