Guest User

Arduino Game Benchmark

a guest
Sep 13th, 2014
868
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Sainsmart UNO ATmega328 @ 16Mhz
  2.  
  3. // A sloppy Benchmark test for the Arduino by Arne.
  4. // Does some tile maps stuff and enemy-map collision.
  5. // No graphics naturally. A PPU would help with that.
  6. // Written sloppily, but that's the test...
  7. // ...is this Arduino UNO thing fast enough for doing game stuff...
  8. // ...using compiled C-whatever and not clever asm?
  9.  
  10. // RESULTS:
  11.  
  12. // Benchmark start...
  13. // Millisec count: 12
  14.  
  15. // X,Y map cells set to value:
  16. // = 1536
  17.  
  18. // Enemies moved with X,Y coordinates using a switch for direction,
  19. // using bitshifted 32-bit (int is 16-bit here) fixed point coord check against a cactus map,
  20. // healing or taking damage depending on cell content, with bounds check:
  21. // = 768
  22.  
  23. // Note, printing out to serial monitor and using random() is rally slow.
  24. // Using uint would probably be faster than long?
  25. // I should be more careful with truncating / comparing different variables too.
  26. // Benchmark is triggered by button connected to pin 2 and gnd, no external resistor.
  27.  
  28. unsigned long TimerStart = 0;
  29. unsigned long TimerEnd = 0;
  30.  
  31. const unsigned int MAP_TSIZE = 16;        // Tile size
  32. const unsigned int ENEMY_COUNT = 32;
  33. const unsigned long MAP_USIZE = 8388608ul; // 16 << 19;  // FixP Unit size (16+3)
  34. const unsigned int BENCH_LOOPS = 6;
  35.  
  36. byte Map[MAP_TSIZE*MAP_TSIZE];            // Dummy game map
  37.  
  38. unsigned long EnemyX[ENEMY_COUNT];         // Dummy enemies
  39. unsigned long EnemyY[ENEMY_COUNT];
  40. int EnemyHP[ENEMY_COUNT];
  41. byte EnemyDir[ENEMY_COUNT];
  42.  
  43. void setup()
  44. {
  45.   // Set up pins.
  46.   pinMode(2, INPUT);        // Pin 2 for benchmark button trigger.
  47.   digitalWrite(2, HIGH);    // Pull up.
  48.   pinMode(2, INPUT_PULLUP); // Yeees, pull up.
  49.  
  50.   Serial.begin(9600);
  51.  
  52.   //Serial.println(MAP_TSIZE);
  53.   //Serial.println(MAP_USIZE);
  54.  
  55.   // Set up game.
  56.   Serial.println("Initilizing game...");
  57.   FRandomizeEnemies();
  58.   Serial.println("Done!");
  59.  
  60. }
  61.  
  62. void loop()
  63. {
  64.   int Pin = digitalRead(2);
  65.   if (Pin == LOW)
  66.   {
  67.     Serial.println("Benchmark start...");
  68.     FBenchmark();
  69.     delay(500);
  70.   } // Endif
  71.  
  72.   delay(10);
  73. }
  74.  
  75.  
  76. void FBenchmark()
  77. {
  78.     TimerStart = millis();
  79.  
  80.     for (int i = 0; i < BENCH_LOOPS; i++)
  81.     {
  82.       //FRandomizeEnemies();
  83.       FIterateMap();
  84.       FEnemyMapCollide();
  85.       FEnemyMapCollide();
  86.       FEnemyMapCollide();
  87.       FEnemyMapCollide();
  88.     } // Next
  89.  
  90.     TimerEnd = millis();
  91.     Serial.print("Millisec count: ");
  92.     Serial.println(TimerEnd-TimerStart);
  93.    
  94.     Serial.println("X,Y map cells set to value: ");
  95.     Serial.println(MAP_TSIZE*MAP_TSIZE*BENCH_LOOPS);
  96.    
  97.     Serial.println("Enemies moved with X,Y coordinates using a switch for direction, ");
  98.     Serial.println("using bitshifted 32-bit (int is 16-bit here) fixed point coord check against a cactus map,");
  99.     Serial.println("healing or taking damage depending on cell content, with bounds check: ");
  100.     Serial.println(4*ENEMY_COUNT*BENCH_LOOPS);
  101.  
  102. }
  103.  
  104.  
  105. // Scatter enemies.
  106. void FRandomizeEnemies()
  107. {
  108.   for (int i = 0; i < ENEMY_COUNT; i++)
  109.   {
  110.     EnemyX[i] = random(MAP_USIZE);
  111.     EnemyY[i] = random(MAP_USIZE);
  112.     EnemyHP[i] = random(100);
  113.     EnemyDir[i] = random(4);
  114.     // Serial.print(EnemyX[i]); Serial.print(", "); Serial.println(EnemyY[i]);
  115.  
  116.   } // Next
  117. }
  118.  
  119.  
  120. // Update all enemies and collide with map.
  121. void FEnemyMapCollide()
  122. {
  123.   unsigned int mcx;   // Map cell.
  124.   unsigned int mcy;
  125.   unsigned int px;    // Pixel pos.
  126.   unsigned int py;
  127.  
  128.   for (int i = 0; i < ENEMY_COUNT; i++)
  129.   {
  130.     FEnemyMove(i);
  131.    
  132.     px = EnemyX[i] >> 16;  // Pixel pos.
  133.     py = EnemyY[i] >> 16;
  134.     mcx = px >> 3;         // 8px tile pos.
  135.     mcy = py >> 3;
  136.     // Check bounds within tile map.
  137.     if (mcx < 1) { mcx = 1; } else if (mcx >= MAP_TSIZE-1) { mcx = MAP_TSIZE-2; }
  138.     if (mcy < 1) { mcy = 1; } else if (mcy >= MAP_TSIZE-1) { mcy = MAP_TSIZE-2; }
  139.     // Serial.print(mcx); Serial.print(", "); Serial.println(mcy);
  140.    
  141.     // Collide with tiles.
  142.     if (Map[mcy*MAP_TSIZE + mcx] < 128)
  143.     {
  144.       EnemyHP[i]--;
  145.       if (EnemyHP[i] < 0) { EnemyHP[i] = 100; }
  146.     } else {
  147.       EnemyHP[i]++;
  148.       if (EnemyHP[i] >= 100) { EnemyHP[i] = 0; }
  149.     } // Endif
  150.   } // Next
  151.  
  152. }
  153.  
  154.  
  155. // Move a specific enemy in its facing direction.
  156. void FEnemyMove(int i)
  157. {
  158.   switch (EnemyDir[i])
  159.   {
  160.   case 0:
  161.     EnemyY[i]+= 100000;
  162.   case 1:
  163.     EnemyY[i]-= 100000;
  164.   case 2:
  165.     EnemyX[i]+= 100000;
  166.   case 3:
  167.     EnemyX[i]-= 100000;
  168.   } // End switch
  169. }
  170.  
  171. // Iterate all x and y tiles in the game map and set to byte value.
  172. void FIterateMap()
  173. {
  174.   int x;
  175.   int y;
  176.   int yo;
  177.   int t;
  178.  
  179.   for (y = 0; y < MAP_TSIZE; y++)
  180.   {
  181.     yo = y*MAP_TSIZE;
  182.     for (x = 0; x < MAP_TSIZE; x++)
  183.     {
  184.       t = yo+x;
  185.       //if (t < MAP_TSIZE*MAP_TSIZE) {
  186.       Map[t] = t;
  187.       //} else { Serial.println("FRandomizeMap bound error!"); }
  188.     } // Next
  189.   } // Next
  190. }
  191.  
  192.  
  193. // Iterate all x and y tiles in the game map and set to random byte value.
  194. void FRandomizeMap()
  195. {
  196.   int x;
  197.   int y;
  198.   int yo;
  199.   int t;
  200.  
  201.   for (y = 0; y < MAP_TSIZE; y++)
  202.   {
  203.     yo = y*MAP_TSIZE;
  204.     for (x = 0; x < MAP_TSIZE; x++)
  205.     {
  206.       t = yo+x;
  207.       // Random is rather slow. 10 x.
  208.       Map[t] = random(0,256); // Long into byte array, 0-255
  209.     } // Next
  210.   } // Next
  211. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×