#include #include #include #include #include #include #include #include #include using namespace std::literals::string_literals; static constexpr auto WORLD_ROWS = 16; static constexpr auto WORLD_COLUMNS = WORLD_ROWS; 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. 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. static std::string WORLD_AS_STRING[WORLD_ROWS] = { //original world map, which we want to convert to bitmap. "1111111111111111"s, "1000000000000001"s, "1001111100000001"s, "1001000101010101"s, "1001000100000001"s, "1001001100000001"s, "1000000000000001"s, "1000000000000001"s, "1000000000000001"s, "1001100111111001"s, "1001000000001001"s, "1001110000001001"s, "1001000000001001"s, "1001111111101001"s, "1000000000000001"s, "1111111111111111"s }; uint_fast16_t toInt(const std::string& s) { std::bitset bits; std::istringstream is(s); is >> bits; return static_cast(bits.to_ulong()); } void prettyPrintBitMap(){ std::cout << "static constexpr uint_fast16_t WORLD[WORLD_ROWS] = { // world map\n"; for (size_t i = 0; i < WORLD_ROWS; i++) { std::cout << std::dec << "/*[" << std::setfill(' ') << std::setw(2) << i << "] " << WORLD_AS_STRING[i] << "*/\t0x" << std::hex << toInt(WORLD_AS_STRING[i]) << ",\n"; } std::cout << "};\n"; } ////// this is the copy-pasted output from prettyPrintBitMap() ////// pick whatever unsigned type provides enough bits to fit all columns. 16 in my case. static constexpr uint_fast16_t WORLD[WORLD_ROWS] = { 0xffff, // 1111111111111111 0x8001, // 1000000000000001 0x9f01, // 1001111100000001 0x9155, // 1001000101010101 0x9101, // 1001000100000001 0x9301, // 1001001100000001 0x8001, // 1000000000000001 0x8001, // 1000000000000001 0x8001, // 1000000000000001 0x99f9, // 1001100111111001 0x9009, // 1001000000001001 0x9c09, // 1001110000001001 0x9009, // 1001000000001001 0x9fe9, // 1001111111101001 0x8001, // 1000000000000001 0xffff, // 1111111111111111 }; ////// inline constexpr bool isWall(int x, int y) noexcept { if (x < FIRST_VALID_CELL || y < FIRST_VALID_CELL || x > LAST_VALID_CELL || y > LAST_VALID_CELL) { return true; } return (WORLD[y] >> ((WORLD_COLUMNS - 1) - x) & 0x01); } void testBitmapWorld() { assert(!isWall(FIRST_VALID_CELL, FIRST_VALID_CELL)); assert(!isWall(LAST_VALID_CELL, LAST_VALID_CELL)); assert(isWall(3, 3)); //test an arbitrary position that we know should be a wall for (size_t i = 0; i < WORLD_COLUMNS; i++) { assert(isWall(0, i)); //test first row, all wall if (i != 0 && i < WORLD_COLUMNS - 1) { //skip the outer edges (all wall) assert(!isWall(1, i)); //test second row assert(!isWall(2, WORLD_ROWS - 2)); //test second-to-last row } assert(isWall(WORLD_ROWS - 1, i)); //test last row } } int main(){ prettyPrintBitMap(); //convert from string representation to bitmap, and dump the source-code to stdout. testBitmapWorld(); return 0; }