make my own bitmask??

Started by
0 comments, last by Oluseyi 18 years, 7 months ago
hi all, I try to make my own mask for a bitmap (it's to use pixel perfect collision) And I have no idea if my algo is exact (how to print only one bit??) so there my

// before (in the class constructor)
	m_mask = new unsigned[(m_picture->w*m_picture->h)/32 + 2]; 
	for (int i = 0; i < (m_picture->w*m_picture->h)/32 + 2 ; i++)m_mask = 0;


void CPicture::CreateMask(){
	// creer le mask de bit.
	//attention, superbe arithmetique de pointeur.
	unsigned* p = m_mask;
	(*p) += 1;
	int h  = m_picture->h;
	int w  = m_picture->w;
	unsigned int colorkey = m_picture->format->colorkey;
	unsigned int Pixel;
	int cpt = 0;
	
	SDL_LockSurface(m_picture);
	for (int j = 0; j<h; j++){
		for (int i = 0; i<w; i++){
			Pixel = GetPixel(i,j);// recupere le pixel i,j
			switch (cpt){
				case 0 : if(Pixel != colorkey) (*p) |= 0x8; break;
				case 1 : if(Pixel != colorkey) (*p) |= 0x4; break;
				case 2 : if(Pixel != colorkey) (*p) |= 0x2; break;
				case 3 : if(Pixel != colorkey) (*p) |= 0x1; break;
			}
			cpt++;	
			if(cpt == 4){(*p)+=1; cpt = 0;}		
		}
	SDL_UnlockSurface(m_picture);
	}
}

[Edit: Use forward slash - / - to close tags, but I'm replacing your [code] with [source] because of the amount of code you posted. - Oluseyi] So, first, I create a board where his lenght equals (m_picture->w*m_picture->h)/32 + 2 m_picture is ùmy picture (SDL_Surface*) and +2 is for not have memory prob create mask must fill m_mask with 1 or 0 (0 if it's a transparency pixel, 1 if there are color) is that code seems to you correct??? thanks a+++ [Edited by - Oluseyi on September 30, 2005 11:50:33 AM]
Advertisement
(*p) += 1 increments the value at the address p by 1. It does not move to the next address in a pointer sequence/array. You want ++p (if you're incrementing by one, use the increment operators; if you don't need side effects, use pre-increment as in this example).

This topic is closed to new replies.

Advertisement