Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include "RandomApp.h"
  2. #include "Font.h"
  3. #include "Input.h"
  4. #include <imgui_glfw3.h>
  5. #include <random>
  6.  
  7. const int GALAXY_WIDTH = 100;
  8. const int GALAXY_HEIGHT = 100;
  9.  
  10. RandomApp::RandomApp() {
  11.  
  12. }
  13.  
  14. RandomApp::~RandomApp() {
  15.  
  16. }
  17.  
  18. bool RandomApp::startup() {
  19.  
  20. m_2dRenderer = new aie::Renderer2D();
  21.  
  22. m_font = new aie::Font("./font/consolas.ttf", 32);
  23.  
  24. m_seed = 1;
  25. m_galaxy = nullptr;
  26.  
  27. aie::ImGui_Init(getWindowPtr(), true);
  28.  
  29. return true;
  30. }
  31.  
  32. void RandomApp::shutdown() {
  33.  
  34. delete m_font;
  35. delete m_2dRenderer;
  36. }
  37.  
  38. void RandomApp::fillGalaxy()
  39. {
  40. if (m_galaxy == nullptr) {
  41. m_galaxy = new char[GALAXY_WIDTH * GALAXY_HEIGHT];
  42. }
  43.  
  44. memset(m_galaxy, 0, GALAXY_WIDTH * GALAXY_HEIGHT);
  45.  
  46. srand(m_seed);
  47. for (int y = 0; y < GALAXY_HEIGHT; y++) {
  48. for (int x = 0; x < GALAXY_WIDTH; x++) {
  49. int probability = rand() % 100;
  50. if (probability > 70) {
  51. m_galaxy[y * GALAXY_WIDTH + x] = 0x01;
  52. }
  53. }
  54. }
  55. }
  56.  
  57. int RandomApp::starAt(int galaxy, int nX, int nY)
  58. {
  59. int nReturn = 0;
  60.  
  61. srand(galaxy);
  62. for (int y = 0; y <= nY; y++) {
  63. for (int x = 0; x <= nX; x++) {
  64. nReturn = rand() % 2;
  65. }
  66. }
  67. return nReturn;
  68. }
  69.  
  70. int RandomApp::starAt2(int galaxy, int nX, int nY)
  71. {
  72. int nReturn = 0;
  73.  
  74. srand(galaxy * (nX + (GALAXY_WIDTH*nY)));
  75. int probability = rand() % 100;
  76. if (probability > 70)
  77. return 0x01;
  78.  
  79. return 0;
  80. }
  81.  
  82.  
  83. int RandomApp::starAt3(int galaxy, int nX, int nY)
  84. {
  85. int nReturn = 0;
  86.  
  87. srand(galaxy * (nX + (GALAXY_WIDTH*nY)));
  88. int probability = rand() % 100;
  89. if (probability > 70)
  90. return 0x01;
  91.  
  92. return 0;
  93. }
  94.  
  95. void RandomApp::update(float deltaTime) {
  96.  
  97. // input example
  98. aie::Input* input = aie::Input::getInstance();
  99.  
  100. ImGui::InputInt("Seed", &m_seed);
  101.  
  102. if (ImGui::Button("Big Fat Array")) {
  103. m_sample = Samples::technique1;
  104. fillGalaxy();
  105. }
  106. if (ImGui::Button("Generate As Needed")) {
  107. m_sample = Samples::technique2;
  108. }
  109. if (ImGui::Button("Generate As Needed 2")) {
  110. m_sample = Samples::technique3;
  111. }
  112. if (ImGui::Button("Generate As Needed 3")) {
  113. m_sample = Samples::technique4;
  114. }
  115.  
  116.  
  117. // exit the application
  118. if (input->isKeyDown(aie::INPUT_KEY_ESCAPE))
  119. quit();
  120. }
  121.  
  122. void RandomApp::draw() {
  123.  
  124. // wipe the screen to the background colour
  125. clearScreen();
  126.  
  127. // begin drawing sprites
  128. m_2dRenderer->begin();
  129.  
  130. // draw your stuff here!
  131. switch (m_sample) {
  132. case Samples::technique1:
  133. {
  134. if (m_galaxy == nullptr)
  135. break;
  136. for (int y = 0; y < GALAXY_HEIGHT; y++) {
  137. for (int x = 0; x < GALAXY_WIDTH; x++) {
  138. if (m_galaxy[y * GALAXY_WIDTH + x] != 0) {
  139. m_2dRenderer->drawCircle(x << 4, y << 4, 4);
  140. }
  141. }
  142. }
  143. break;
  144. }
  145. case Samples::technique2:
  146. {
  147. for (int y = 0; y < GALAXY_HEIGHT>>1; y++) {
  148. for (int x = 0; x < GALAXY_WIDTH>>1; x++) {
  149. if (starAt(m_seed, x, y) != 0) {
  150. m_2dRenderer->drawCircle(x << 4, y << 4, 4);
  151. }
  152. }
  153. }
  154. break;
  155. }
  156. case Samples::technique3:
  157. {
  158. for (int y = 0; y < GALAXY_HEIGHT; y++) {
  159. for (int x = 0; x < GALAXY_WIDTH; x++) {
  160. if (starAt2(m_seed, x, y) != 0) {
  161. m_2dRenderer->drawCircle(x << 4, y << 4, 4);
  162. }
  163. }
  164. }
  165. break;
  166. }
  167. case Samples::technique4:
  168. {
  169. for (int y = 0; y < GALAXY_HEIGHT; y++) {
  170. for (int x = 0; x < GALAXY_WIDTH; x++) {
  171. if (starAt3(m_seed, x, y) != 0) {
  172. m_2dRenderer->drawCircle(x << 4, y << 4, 4);
  173. }
  174. }
  175. }
  176. break;
  177. }
  178. }
  179.  
  180.  
  181. // output some text
  182. m_2dRenderer->drawText(m_font, "Press ESC to quit", 0, 0);
  183.  
  184. // done drawing sprites
  185. m_2dRenderer->end();
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement