SamplesGame.h 4.4 KB

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