Last active
April 14, 2021 20:03
-
-
Save cppcooper/e99bca81fab15d2518b0 to your computer and use it in GitHub Desktop.
[C++] bit packing map data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using uint32 = unsigned __int32; | |
#define regionWidth 4 | |
#define regionHeight 4 | |
class Region_Tile | |
{ | |
private: | |
uint32* Region; | |
uint32 bitmask; | |
__int8 bit_position; | |
public: | |
Region_Tile( uint32* Region, __int8 index ) | |
{ | |
this->Region = Region; | |
bit_position = index * 2; | |
bitmask = 3 << bit_position; | |
} | |
//First two bits of assigned value are kept | |
Region_Tile& operator=( __int8 tile_value ) | |
{ | |
*Region &= ~bitmask; | |
*Region |= ( 3 & tile_value ) << bit_position; | |
return *this; | |
} | |
__int8 Get_Tile_Value() | |
{ | |
return ( ( *Region & bitmask ) >> bit_position ); | |
} | |
bool canMove() | |
{ | |
bitmask = 1 << ( bit_position + 1 ); | |
return bitmask & *Region; | |
} | |
bool haveBeenTo() | |
{ | |
if ( Get_Tile_Value() == 3 ) | |
{ | |
return true; | |
} | |
return false; | |
} | |
}; | |
class Region | |
{ | |
private: | |
uint32 region_tiles = 2861864066; | |
public: | |
Region_Tile Get_Tile( ushort x, ushort y ) | |
{ | |
if ( x < regionWidth && y < regionHeight ) | |
{ | |
__int8 index = ( y * regionWidth ) + x; | |
return Region_Tile( ®ion_tiles, index ); | |
} | |
else | |
{ | |
assert( 0 ); | |
} | |
} | |
uint32& Region_Tiles() | |
{ | |
return region_tiles; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment