StatisticsHistoryTest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // TODO - see PLplot plplot.sourceforge.net/download.phpd for visualization
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <stdlib.h>
  14. #include "GetTime.h"
  15. #include "RakPeerInterface.h"
  16. #include "MessageIdentifiers.h"
  17. #include "StatisticsHistory.h"
  18. #include <math.h>
  19. #include "RakSleep.h"
  20. using namespace RakNet;
  21. enum HistoryObject
  22. {
  23. HO_SIN_WAVE,
  24. HO_COS_WAVE,
  25. };
  26. static const int numGraphColumns=79;
  27. static const int numGraphRows=5;
  28. void PrintGraph(DataStructures::Queue<StatisticsHistory::TimeAndValue> &histogram, SHValueType highest, SHValueType lowest)
  29. {
  30. if (histogram.Size()==0)
  31. return;
  32. SHValueType range = highest - lowest;
  33. bool drawPoint[numGraphRows][numGraphColumns];
  34. memset(drawPoint, 0, sizeof(drawPoint));
  35. unsigned int numR;
  36. for (unsigned int c = 0; c < histogram.Size() && c < numGraphColumns; c++)
  37. {
  38. double r = (double) numGraphRows * ((histogram[c].val-lowest) / range);
  39. if (r - floor(r)>.5)
  40. r=ceil(r);
  41. else
  42. r=floor(r);
  43. numR = (unsigned int) r;
  44. for (unsigned int r = 0; r < numR && r < numGraphRows; r++)
  45. {
  46. drawPoint[r][c]=true;
  47. }
  48. }
  49. for (int r=numGraphRows-1; r >= 0; r--)
  50. {
  51. for (unsigned int c = 0; c < numGraphColumns; c++)
  52. {
  53. if (drawPoint[r][c])
  54. printf("|");
  55. else
  56. printf(" ");
  57. }
  58. printf("\n");
  59. }
  60. for (unsigned int c = 0; c < numGraphColumns; c++)
  61. printf("-");
  62. printf("\n\n");
  63. }
  64. int main(void)
  65. {
  66. printf("Tests StatisticsHistory in single player.\n");
  67. printf("Difficulty: Intermediate\n\n");
  68. DataStructures::Queue<StatisticsHistory::TimeAndValue> histogram;
  69. StatisticsHistory::TimeAndValueQueue *tav;
  70. StatisticsHistory::TimeAndValueQueue tavInst;
  71. StatisticsHistory statisticsHistory;
  72. statisticsHistory.SetDefaultTimeToTrack(10000);
  73. statisticsHistory.AddObject(StatisticsHistory::TrackedObjectData(HO_SIN_WAVE,0,0));
  74. statisticsHistory.AddObject(StatisticsHistory::TrackedObjectData(HO_COS_WAVE,0,0));
  75. double f;
  76. Time nextPrint=0;
  77. while (1)
  78. {
  79. f = (double) ((double)GetTime() / (double)1000);
  80. statisticsHistory.AddValueByObjectID(HO_SIN_WAVE,"Waveform",sin(f),GetTime(), false);
  81. statisticsHistory.AddValueByObjectID(HO_COS_WAVE,"Waveform",cos(f),GetTime(), false);
  82. // Show sin wave
  83. if (GetTime()>nextPrint)
  84. {
  85. system("cls");
  86. Time curTime = GetTime();
  87. statisticsHistory.GetHistoryForKey(HO_SIN_WAVE, "Waveform", &tav, curTime);
  88. tav->ResizeSampleSet(numGraphColumns, histogram, StatisticsHistory::DC_CONTINUOUS);
  89. PrintGraph(histogram, 1, -1);
  90. // Show cos wave
  91. statisticsHistory.GetHistoryForKey(HO_COS_WAVE, "Waveform", &tav, curTime);
  92. tav->ResizeSampleSet(numGraphColumns, histogram, StatisticsHistory::DC_CONTINUOUS);
  93. PrintGraph(histogram, 1, -1);
  94. nextPrint = GetTime() + 500;
  95. // Show sin wave + cos wave
  96. statisticsHistory.MergeAllObjectsOnKey("Waveform", &tavInst, StatisticsHistory::DC_CONTINUOUS);
  97. tavInst.ResizeSampleSet(numGraphColumns, histogram, StatisticsHistory::DC_CONTINUOUS);
  98. PrintGraph(histogram, 2, -2);
  99. }
  100. RakSleep(30);
  101. }
  102. return 1;
  103. }
粤ICP备19079148号