SamplesGame.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef SAMPLESGAME_H_
  2. #define SAMPLESGAME_H_
  3. #include "gameplay.h"
  4. #include "Sample.h"
  5. using namespace gameplay;
  6. /**
  7. * Macro for adding a sample. The purpose is to put the code that adds the sample in the class's cpp file.
  8. */
  9. #define ADD_SAMPLE(cateogry, title, className, order) \
  10. static className* _createSample() \
  11. { \
  12. return new className(); \
  13. } \
  14. struct _foo ## className \
  15. { \
  16. _foo ## className() \
  17. { \
  18. SamplesGame::addSample((cateogry), (title), (void*)&_createSample, order); \
  19. } \
  20. }; \
  21. static _foo ## className _f;
  22. /**
  23. * Main game class.
  24. */
  25. class SamplesGame : public Game, Control::Listener
  26. {
  27. public:
  28. /**
  29. * Constructor.
  30. */
  31. SamplesGame();
  32. void keyEvent(Keyboard::KeyEvent evt, int key);
  33. void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  34. bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  35. void menuEvent();
  36. void gestureSwipeEvent(int x, int y, int direction);
  37. void gesturePinchEvent(int x, int y, float scale);
  38. void gestureTapEvent(int x, int y);
  39. void gestureLongTapEvent(int x, int y, float duration);
  40. void gestureDragEvent(int x, int y);
  41. void gestureDropEvent(int x, int y);
  42. void controlEvent(Control* control, EventType evt);
  43. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex = 0);
  44. /**
  45. * Adds a sample.
  46. *
  47. * @param category The cateogry title to group the sample in.
  48. * @param title The title string of the sample.
  49. * @param func The function pointer that is used to create the sample.
  50. * @param order The order of the sample. Samples are sorted lowest order to highest.
  51. */
  52. static void addSample(const char* category, const char* title, void* func, unsigned int order);
  53. static SamplesGame* getInstance();
  54. protected:
  55. /**
  56. * @see Game::initialize
  57. */
  58. void initialize();
  59. /**
  60. * @see Game::finalize
  61. */
  62. void finalize();
  63. /**
  64. * @see Game::update
  65. */
  66. void update(float elapsedTime);
  67. /**
  68. * @see Game::render
  69. */
  70. void render(float elapsedTime);
  71. private:
  72. /**
  73. * Function pointer type that is used to create a SampleGame.
  74. */
  75. typedef void*(*SampleGameCreatePtr)();
  76. /**
  77. * Run the given sample.
  78. *
  79. * @param func The function pointer that creates the SampleGame.
  80. */
  81. void runSample(void* func);
  82. /**
  83. * Handles the activate when the user touches or clicks on a part of the screen.
  84. *
  85. * @param x The x-coordinate in screen space where the user clicked.
  86. * @param y The y-coordinate in screen space where the user clicked.
  87. */
  88. void activate(int x, int y);
  89. /**
  90. * Exits the active sample and returns to the main menu.
  91. */
  92. void exitActiveSample();
  93. /**
  94. * Draws the main menu, which is a list of all the available samples.
  95. */
  96. void drawTextMenu();
  97. private:
  98. struct SampleRecord
  99. {
  100. std::string title;
  101. void* funcPtr;
  102. unsigned int order;
  103. SampleRecord() : funcPtr(NULL), order(0) { }
  104. SampleRecord(std::string title, void* funcPtr, unsigned int order) : title(title), funcPtr(funcPtr), order(order) { }
  105. SampleRecord& operator = (const SampleRecord& copy)
  106. {
  107. title = copy.title;
  108. funcPtr = copy.funcPtr;
  109. order = copy.order;
  110. return *this;
  111. }
  112. bool operator<(const SampleRecord& v) const
  113. {
  114. return order < v.order;
  115. }
  116. };
  117. /**
  118. * The list of category title strings.
  119. */
  120. static std::vector<std::string>* _categories;
  121. /**
  122. * The collection of sample titles and the function pointers that create the samples.
  123. * The pair represents the string title of the sample and the function pointer that is used to create the sample.
  124. * The inner vector is a list of those pairs in the order that they were added.
  125. * The outer vector represents the list of categories that contain the list of samples in the category.
  126. * The index of _categories maps to the index of the outer vector. (Therefore their size should always be the same).
  127. */
  128. typedef std::vector<SampleRecord> SampleRecordList;
  129. static std::vector<SampleRecordList>* _samples;
  130. Sample* _activeSample;
  131. Font* _font;
  132. Form* _sampleSelectForm;
  133. };
  134. #endif