SHARE
TWEET

Bitmap LevelData -generation

ulfben Mar 9th, 2020 (edited) 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <bitset>
  3. #include <string>
  4. #include <sstream>
  5. #include <climits>
  6. #include <cstdint>
  7. #include <string>
  8. #include <cassert>
  9. #include <iomanip>
  10. using namespace std::literals::string_literals;
  11. static constexpr auto WORLD_ROWS = 16;
  12. static constexpr auto WORLD_COLUMNS = WORLD_ROWS;
  13. static constexpr auto FIRST_VALID_CELL = 1; //to shortcut collision testing we can check against the map boundary walls. Helps with mouse interaction and for Q&D respawn when stuck in a wall.
  14. static constexpr auto LAST_VALID_CELL = WORLD_ROWS - 2; //0 == wall, 15 == wall. so if our position is below or above the valid spaces, we can bail without performing lookup.
  15. static std::string WORLD_AS_STRING[WORLD_ROWS] = { //original world map, which we want to convert to bitmap.
  16.     "1111111111111111"s,
  17.     "1000000000000001"s,
  18.     "1001111100000001"s,
  19.     "1001000101010101"s,
  20.     "1001000100000001"s,
  21.     "1001001100000001"s,
  22.     "1000000000000001"s,
  23.     "1000000000000001"s,
  24.     "1000000000000001"s,
  25.     "1001100111111001"s,
  26.     "1001000000001001"s,
  27.     "1001110000001001"s,
  28.     "1001000000001001"s,
  29.     "1001111111101001"s,
  30.     "1000000000000001"s,
  31.     "1111111111111111"s
  32. };
  33.  
  34. uint_fast16_t toInt(const std::string& s) {
  35.     std::bitset<WORLD_COLUMNS> bits;
  36.     std::istringstream is(s);
  37.     is >> bits;
  38.     return static_cast<uint_fast16_t>(bits.to_ulong());
  39. }
  40.  
  41. void prettyPrintBitMap(){
  42.     std::cout << "static constexpr uint_fast16_t  WORLD[WORLD_ROWS] = { // world map\n";
  43.     for (size_t i = 0; i < WORLD_ROWS; i++) {  
  44.         std::cout << std::dec << "/*[" << std::setfill(' ') << std::setw(2) << i << "] " << WORLD_AS_STRING[i] << "*/\t0x" << std::hex << toInt(WORLD_AS_STRING[i]) << ",\n";
  45.     }
  46.     std::cout << "};\n";
  47. }
  48.  
  49. ////// this is the copy-pasted output from prettyPrintBitMap()
  50. ////// pick whatever unsigned type provides enough bits to fit all columns. 16 in my case.
  51. static constexpr uint_fast16_t WORLD[WORLD_ROWS] = {
  52.         0xffff, // 1111111111111111
  53.         0x8001, // 1000000000000001
  54.         0x9f01, // 1001111100000001
  55.         0x9155, // 1001000101010101
  56.         0x9101, // 1001000100000001
  57.         0x9301, // 1001001100000001
  58.         0x8001, // 1000000000000001
  59.         0x8001, // 1000000000000001
  60.         0x8001, // 1000000000000001
  61.         0x99f9, // 1001100111111001
  62.         0x9009, // 1001000000001001
  63.         0x9c09, // 1001110000001001
  64.         0x9009, // 1001000000001001
  65.         0x9fe9, // 1001111111101001
  66.         0x8001, // 1000000000000001
  67.         0xffff, // 1111111111111111
  68. };
  69. //////
  70.  
  71. inline constexpr bool isWall(int x, int y) noexcept {
  72.     if (x < FIRST_VALID_CELL || y < FIRST_VALID_CELL
  73.         || x > LAST_VALID_CELL || y > LAST_VALID_CELL) {
  74.         return true;
  75.     }
  76.     return (WORLD[y] >> ((WORLD_COLUMNS - 1) - x) & 0x01);
  77. }
  78. void testBitmapWorld() {
  79.     assert(!isWall(FIRST_VALID_CELL, FIRST_VALID_CELL));
  80.     assert(!isWall(LAST_VALID_CELL, LAST_VALID_CELL));
  81.     assert(isWall(3, 3)); //test an arbitrary position that we know should be a wall
  82.     for (size_t i = 0; i < WORLD_COLUMNS; i++) {
  83.         assert(isWall(0, i)); //test first row, all wall
  84.         if (i != 0 && i < WORLD_COLUMNS - 1) { //skip the outer edges (all wall)
  85.             assert(!isWall(1, i)); //test second row
  86.             assert(!isWall(2, WORLD_ROWS - 2)); //test second-to-last row
  87.         }
  88.         assert(isWall(WORLD_ROWS - 1, i)); //test last row
  89.     }    
  90. }
  91.  
  92. int main(){    
  93.     prettyPrintBitMap(); //convert from string representation to bitmap, and dump the source-code to stdout.
  94.     testBitmapWorld();
  95.     return 0;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top