Simple Question...

I'm looking at making a bitmask function for collision detection in my game, the function would need to take a bitmap and store one value if each pixel is transparent, and another if it is not. Is using a two-dimensional array of bools a good way to do this? I'm currently doing it using the actual bitmaps, but the library I'm using only allows you to read from a single bitmap at a time, and reading from bitmaps is slow, relatively speaking, so I'm looking for a better way to do it. As an aside: is a bool stored in memory as a single bit? Since it only has two possible values, I assume so, but I don't know for sure. Thanks for the help!
Last edited on
The answer is no, a bool isn't stored as a single bit. You can check this by doing:
 
cout << sizeof(bool) << endl;

You will get the size in number of bytes (I think probably one byte, i.e. 8 bits).

If you are conserned with memory usage, you should use something like unsigned char to store 8 bits, and you have to do quite a few low-level bit operations. I think you should have a look at std::bitset:

http://cplusplus.com/reference/stl/bitset/
Thanks for the response. I was actually more worried about performance than memory, but one of my prrofessors informed me that the size of the variables being used won't have any noticeable impact on the performance of my program, so an array of bools works fine. That's an interesting class, though, thanks for pointing it out to me. My degree program only has two courses of C++ and one of C, so I'm trying to self-teach mysef what I haven't already learned in school so I can use C++ libraries for game design.
Topic archived. No new replies allowed.