   Optimizing a Programm
  HelloI'am new in game programming, please help.I have a map with three layers:Layer 1: contains 8 types of tiles for the floor.Layer 2: contains 16 types of decorationsLayer 3: Determines if that tile has collision or not.Have any idea of the code to write/read this map where the information of the three layers occupies only ONE byte for each tile ??For example, if the map is 32x32, its size with this method will be 1024 bytes.   
  8 types = 3 bits, 16 decoration types = 4 bits, collision = 1 bit. Just shift them all into a 8-bit byte.   ;  Have you determined that the size of your maps is a problem? If not, why are you worrying about this? To put things into perspective, one frame of animation for my protagonist, saved in a PNG format, is about 15KB. I have over 250 frames of animation for him so far -- over 4MB of images. For your map to be a comparable size using your desired 1-byte-per-tile system, it'd need to be over 4 million tiles in size. And even then, that's just 4MB; not a big deal. You'd be better-served by compressing your game using ZIP or something similar and distributing that, than by wrestling with a complicated and brittle binary format. Don't optimize until you know it'll be a problem.   ;Quote:Have any idea of the code to write/read this map where the information of the three layers occupies only ONE byte for each tile ??Ok, so you have 3 layers (arrays perhaps), each of which uses some type (integer) in memory, but you want to write this to disk in optimal manner?const int n = 32 * 32;int floor[n];int decoration[n];int collidable[n];// to storefor (int i = 0; i < n; i++) {  // encode 3 arrays into single byte  unsigned char value;  value = floor;  value += decoration << 3;  value += collidable << 7;  // write the value}// to readfor (int i = 0; i < n; i++) {  unsigned char value = // read it from somewhere  floor = value & 0x7;  // modulo 8  decoration = (value >> 3) & 15; // modulo 16  collidable = (value >> 7) & 1; // modulo 2};While it would be possible to use bitfields to represent the data in memory, those aren't guaranteed to serialize identically between different platforms/compilers (IIRC).   ;  Yeah, Derakon is right.However it depends if this is something you want to learn on, or if it's something you want to release.If you want to do an un-necesary optomization just to get the practice, that's one thing (ie learn how to do bitpacking), but you should realize too that doing this isn't going to really win you any preformance or significant memory (:   ;Quote:Original post by Atrix256If you want to do an un-necesary optomization just to get the practice, that's one thing (ie learn how to do bitpacking), but you should realize too that doing this isn't going to really win you any preformance or significant memory (:With that logic, why use PNGs to store images. XML is the best format:<xml>  <image widrh="500" height="500">    <scanline> <pixel>   <channel type="RED"><value>17</value>   </channel >   <channel type="BLUE"><value>55</value>   </channel >   <channel type="GREEN"><value>255</value>   </channel >   <channel type="ALPHA"><value>255</value>   </channel > </pixel>  // and so on...Choice of encoding is a perfectly valid concern which doesn't have much to do with optimization. And choosing bit packed format in this case, especially since data seems to be chosen with that in mind makes it perfect match.In some cases, choosing data representation in such a way that it can be stored compactly is perfectly valid choice - and also seems to be in this case.   ;Quote:Original post by AntheusIn some cases, choosing data representation in such a way that it can be stored compactly is perfectly valid choice - and also seems to be in this case.It's for that reason that this seems like a homework question to me. Why would you choose numbers that conveniently happen fit into an 8-bit word, but then you don't actually know how to pack those bits in?Anyway, the answer has already been given, so it's a bit late now...   ;  Bah Antheus you know there's a difference :PA widely used image format, vs the format of a map in a game someone is making to learn how to make games.Knowing when to optomize and when it doesn't matter is an important part of the process (:   ;  The 'answer' is long and complicated, and ignores features C and C++ already have to do just this thing.struct Cell{unsigned char ground : 3;unsigned char decoration : 4;unsigned char collide : 1; //Note that I use char, not bool, //because sizeof(bool) is not guaranteed to be 1, and is actually probably 4.//To see why this is important; // http://cplus.about.com/od/learningc/ss/lowlevel_10.htm};;Quote:Original post by DeyjaThe 'answer' is long and complicated, and ignores features C and C++ already have to do just this thing.It is also portable and language-agnostic, even if expressed with C-like syntax, taking into consideration that C or C++ were not specifically mentioned.I personally avoid bitfields since there is no end to special cases and platform-specific limitations with them.   