Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sainsmart UNO ATmega328 @ 16Mhz
- // A sloppy Benchmark test for the Arduino by Arne.
- // Does some tile maps stuff and enemy-map collision.
- // No graphics naturally. A PPU would help with that.
- // Written sloppily, but that's the test...
- // ...is this Arduino UNO thing fast enough for doing game stuff...
- // ...using compiled C-whatever and not clever asm?
- // RESULTS:
- // Benchmark start...
- // Millisec count: 12
- // X,Y map cells set to value:
- // = 1536
- // Enemies moved with X,Y coordinates using a switch for direction,
- // using bitshifted 32-bit (int is 16-bit here) fixed point coord check against a cactus map,
- // healing or taking damage depending on cell content, with bounds check:
- // = 768
- // Note, printing out to serial monitor and using random() is rally slow.
- // Using uint would probably be faster than long?
- // I should be more careful with truncating / comparing different variables too.
- // Benchmark is triggered by button connected to pin 2 and gnd, no external resistor.
- unsigned long TimerStart = 0;
- unsigned long TimerEnd = 0;
- const unsigned int MAP_TSIZE = 16; // Tile size
- const unsigned int ENEMY_COUNT = 32;
- const unsigned long MAP_USIZE = 8388608ul; // 16 << 19; // FixP Unit size (16+3)
- const unsigned int BENCH_LOOPS = 6;
- byte Map[MAP_TSIZE*MAP_TSIZE]; // Dummy game map
- unsigned long EnemyX[ENEMY_COUNT]; // Dummy enemies
- unsigned long EnemyY[ENEMY_COUNT];
- int EnemyHP[ENEMY_COUNT];
- byte EnemyDir[ENEMY_COUNT];
- void setup()
- {
- // Set up pins.
- pinMode(2, INPUT); // Pin 2 for benchmark button trigger.
- digitalWrite(2, HIGH); // Pull up.
- pinMode(2, INPUT_PULLUP); // Yeees, pull up.
- Serial.begin(9600);
- //Serial.println(MAP_TSIZE);
- //Serial.println(MAP_USIZE);
- // Set up game.
- Serial.println("Initilizing game...");
- FRandomizeEnemies();
- Serial.println("Done!");
- }
- void loop()
- {
- int Pin = digitalRead(2);
- if (Pin == LOW)
- {
- Serial.println("Benchmark start...");
- FBenchmark();
- delay(500);
- } // Endif
- delay(10);
- }
- void FBenchmark()
- {
- TimerStart = millis();
- for (int i = 0; i < BENCH_LOOPS; i++)
- {
- //FRandomizeEnemies();
- FIterateMap();
- FEnemyMapCollide();
- FEnemyMapCollide();
- FEnemyMapCollide();
- FEnemyMapCollide();
- } // Next
- TimerEnd = millis();
- Serial.print("Millisec count: ");
- Serial.println(TimerEnd-TimerStart);
- Serial.println("X,Y map cells set to value: ");
- Serial.println(MAP_TSIZE*MAP_TSIZE*BENCH_LOOPS);
- Serial.println("Enemies moved with X,Y coordinates using a switch for direction, ");
- Serial.println("using bitshifted 32-bit (int is 16-bit here) fixed point coord check against a cactus map,");
- Serial.println("healing or taking damage depending on cell content, with bounds check: ");
- Serial.println(4*ENEMY_COUNT*BENCH_LOOPS);
- }
- // Scatter enemies.
- void FRandomizeEnemies()
- {
- for (int i = 0; i < ENEMY_COUNT; i++)
- {
- EnemyX[i] = random(MAP_USIZE);
- EnemyY[i] = random(MAP_USIZE);
- EnemyHP[i] = random(100);
- EnemyDir[i] = random(4);
- // Serial.print(EnemyX[i]); Serial.print(", "); Serial.println(EnemyY[i]);
- } // Next
- }
- // Update all enemies and collide with map.
- void FEnemyMapCollide()
- {
- unsigned int mcx; // Map cell.
- unsigned int mcy;
- unsigned int px; // Pixel pos.
- unsigned int py;
- for (int i = 0; i < ENEMY_COUNT; i++)
- {
- FEnemyMove(i);
- px = EnemyX[i] >> 16; // Pixel pos.
- py = EnemyY[i] >> 16;
- mcx = px >> 3; // 8px tile pos.
- mcy = py >> 3;
- // Check bounds within tile map.
- if (mcx < 1) { mcx = 1; } else if (mcx >= MAP_TSIZE-1) { mcx = MAP_TSIZE-2; }
- if (mcy < 1) { mcy = 1; } else if (mcy >= MAP_TSIZE-1) { mcy = MAP_TSIZE-2; }
- // Serial.print(mcx); Serial.print(", "); Serial.println(mcy);
- // Collide with tiles.
- if (Map[mcy*MAP_TSIZE + mcx] < 128)
- {
- EnemyHP[i]--;
- if (EnemyHP[i] < 0) { EnemyHP[i] = 100; }
- } else {
- EnemyHP[i]++;
- if (EnemyHP[i] >= 100) { EnemyHP[i] = 0; }
- } // Endif
- } // Next
- }
- // Move a specific enemy in its facing direction.
- void FEnemyMove(int i)
- {
- switch (EnemyDir[i])
- {
- case 0:
- EnemyY[i]+= 100000;
- case 1:
- EnemyY[i]-= 100000;
- case 2:
- EnemyX[i]+= 100000;
- case 3:
- EnemyX[i]-= 100000;
- } // End switch
- }
- // Iterate all x and y tiles in the game map and set to byte value.
- void FIterateMap()
- {
- int x;
- int y;
- int yo;
- int t;
- for (y = 0; y < MAP_TSIZE; y++)
- {
- yo = y*MAP_TSIZE;
- for (x = 0; x < MAP_TSIZE; x++)
- {
- t = yo+x;
- //if (t < MAP_TSIZE*MAP_TSIZE) {
- Map[t] = t;
- //} else { Serial.println("FRandomizeMap bound error!"); }
- } // Next
- } // Next
- }
- // Iterate all x and y tiles in the game map and set to random byte value.
- void FRandomizeMap()
- {
- int x;
- int y;
- int yo;
- int t;
- for (y = 0; y < MAP_TSIZE; y++)
- {
- yo = y*MAP_TSIZE;
- for (x = 0; x < MAP_TSIZE; x++)
- {
- t = yo+x;
- // Random is rather slow. 10 x.
- Map[t] = random(0,256); // Long into byte array, 0-255
- } // Next
- } // Next
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement