In a recent program of mine I make a fairly high number of over-lap checks at once. The checks take place between two 2D vectors of integers (well, a vector of vectors of ints). At the moment, it simply loops through every tile in the smaller one, and then check if it overlaps with certain numbers on the other, given an offset. The project is a rogue-like, and I use this to check if a room can be placed on the map.
In order to speed up map generation, I'd like to have bit-fields that I can just bitwise-or together to check for an overlap. I've written a class that dynamically allocates an array of enough chars (although I could replace it with just about any other type by changing a single typedef) that add up to enough bits for the array. (The class is passed a width and height in the constructor, then it generates enough "chars" to hold width*height bits)
I've gotten that working well enough, I can set, reset and check the individual bits using member functions I've written to do so, but I still need a bit more functionality. I need to be able to bitwise-or two of the class objects together, so I can add a room's mask to the map after it is placed, and bitwise-and two of the class objects together, so I can check for an overlap.
I've attached the class header file as it currently stands. I've tried the following, but it doesn't seem to work. Any ideas what I've messed up?
Code:
bool CheckOverlap (BitField& otherField)
{
int w = width;
int h = height;
if(w>otherField.width) {w = otherField.width;}
if(h>otherField.height) {h = otherField.height;}
int size = w*h;
for(unsigned int i=0; i<(size/data_size)+1; i++)
{
if( (data[i] & otherField.data[i]) > 0 )
{
return 1;
}
}
return 0;
}
void Combine(BitField& otherField, int x, int y)
{
int w = otherField.width;
int h = otherField.height;
int x2 = x+w-1;
int y2 = y+h-1;
int startBit = x+(y*width);
int endBit = x2+(y2*width);
int startData = startBit/data_size;
int endData = endBit/data_size;
for(int i=0; i<=endData-startData; i++)
{
data[startBit+i] |= otherField.data[i];
}
}
Thank you for your time!
Timothy Sassone