SamplesGame.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 controlEvent(Control* control, EventType evt);
  40. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex = 0);
  41. /**
  42. * Adds a sample.
  43. *
  44. * @param category The cateogry title to group the sample in.
  45. * @param title The title string of the sample.
  46. * @param func The function pointer that is used to create the sample.
  47. * @param order The order of the sample. Samples are sorted lowest order to highest.
  48. */
  49. static void addSample(const char* category, const char* title, void* func, unsigned int order);
  50. static SamplesGame* getInstance();
  51. protected:
  52. /**
  53. * @see Game::initialize
  54. */
  55. void initialize();
  56. /**
  57. * @see Game::finalize
  58. */
  59. void finalize();
  60. /**
  61. * @see Game::update
  62. */
  63. void update(float elapsedTime);
  64. /**
  65. * @see Game::render
  66. */
  67. void render(float elapsedTime);
  68. private:
  69. /**
  70. * Function pointer type that is used to create a SampleGame.
  71. */
  72. typedef void*(*SampleGameCreatePtr)();
  73. /**
  74. * Run the given sample.
  75. *
  76. * @param func The function pointer that creates the SampleGame.
  77. */
  78. void runSample(void* func);
  79. /**
  80. * Handles the activate when the user touches or clicks on a part of the screen.
  81. *
  82. * @param x The x-coordinate in screen space where the user clicked.
  83. * @param y The y-coordinate in screen space where the user clicked.
  84. */
  85. void activate(int x, int y);
  86. /**
  87. * Exits the active sample and returns to the main menu.
  88. */
  89. void exitActiveSample();
  90. /**
  91. * Draws the main menu, which is a list of all the available samples.
  92. */
  93. void drawTextMenu();
  94. private:
  95. struct SampleRecord
  96. {
  97. std::string title;
  98. void* funcPtr;
  99. unsigned int order;
  100. SampleRecord() : funcPtr(NULL), order(0) { }
  101. SampleRecord(std::string title, void* funcPtr, unsigned int order) : title(title), funcPtr(funcPtr), order(order) { }
  102. SampleRecord& operator = (const SampleRecord& copy)
  103. {
  104. title = copy.title;
  105. funcPtr = copy.funcPtr;
  106. order = copy.order;
  107. return *this;
  108. }
  109. bool operator<(const SampleRecord& v) const
  110. {
  111. return order < v.order;
  112. }
  113. };
  114. /**
  115. * The list of category title strings.
  116. */
  117. static std::vector<std::string>* _categories;
  118. /**
  119. * The collection of sample titles and the function pointers that create the samples.
  120. * The pair represents the string title of the sample and the function pointer that is used to create the sample.
  121. * The inner vector is a list of those pairs in the order that they were added.
  122. * The outer vector represents the list of categories that contain the list of samples in the category.
  123. * The index of _categories maps to the index of the outer vector. (Therefore their size should always be the same).
  124. */
  125. typedef std::vector<SampleRecord> SampleRecordList;
  126. static std::vector<SampleRecordList>* _samples;
  127. Sample* _activeSample;
  128. Font* _font;
  129. Form* _sampleSelectForm;
  130. };
  131. #endif