Getting desperate, sweaty.

Started by
3 comments, last by The Pale Hunter 22 years, 1 month ago
Okay, here are my two fade functions. The first, Fade2Black(), works perfectly. The second, FadeFromBlack(), does not. They are identical in every way, except for what they do with the alpha channel. So what is the problem exactly? FadeFromBlack(), after bringing up the alpha channel to make the fullscreen black quad, BlackBox, transparent, sort of starts over. I can make it stop if I do not reset the values before I return 1 and exit, but then it won't run twice as those values must be reset before it runs again. I've been working on this stupid fade for days and days, and I'm really getting ANGRY. If anyone has an idea why FadeFromBlack() isn't working, or simply has a better fade function, please oh please won't you help a poor boy not go insane? FADE2BLACK():
      
//Fades screen to black.  Pass "fast" or "slow".

bool Fade2Black(char* speed)
{


//First draw a transparent black quad on the screen	

BlackBox.Draw(blackBoxBuffer,NULL);

static DWORD currentColor = 0x00000000; //Transparent Black


//Assign the quad's vertex colors

BlackBoxVertices[0].color = currentColor;
BlackBoxVertices[1].color = currentColor;
BlackBoxVertices[2].color = currentColor;
BlackBoxVertices[3].color = currentColor;

//Grab the first byte of the color, which is the alpha value

static DWORD dwAlpha = (currentColor>>24)&0x00;

//Increment the alpha value if it isn't fully opaque

while (dwAlpha != 0xFF)
{
	
	if (speed == "fast")
	{
		dwAlpha += 0x11;
	}

	else if (speed == "slow")
	{
		dwAlpha += 0x01;
	}

	else
	{
		//error

	}
	
//Store the alpha value back into the color

currentColor = (currentColor&0x00000000)|(dwAlpha<<24);

//This is the D3D equivalent to DDraw's flip surface command

hr=blackBoxBuffer->Lock(0,0, &vb_vertices,0);   
memcpy(vb_vertices, BlackBoxVertices, sizeof(T_VERTEX)*4 );  
blackBoxBuffer->Unlock();

return 0;

}

//If we are where we should be

if (currentColor == 0xFF000000)
{
        //Reset the color and return

	currentColor = 0x00000000;
	dwAlpha = 0x00;
	return 1;
}

else
	return 0;



}//end Fade2Black()

  
  
FADEFROMBLACK():
  
      
//Fades screen from black.  Pass "fast" or "slow".

bool FadeFromBlack(char* speed)
{


//First draw a transparent black quad on the screen	

BlackBox.Draw(blackBoxBuffer,NULL);

static DWORD currentColor = 0xFF000000; //Opaque Black




//Grab the first byte of the color, which is the alpha value

static DWORD dwAlpha = (currentColor>>24)&0xFF;


//Assign the quad's vertex colors

BlackBoxVertices[0].color = currentColor;
BlackBoxVertices[1].color = currentColor;
BlackBoxVertices[2].color = currentColor;
BlackBoxVertices[3].color = currentColor;


//Increment the alpha value if it isn't fully opaque

while (dwAlpha != 0x00)
{
	
	if (speed == "fast")
	{
		dwAlpha -= 0x11;
	}

	else if (speed == "slow")
	{
		dwAlpha -= 0x01;
	}

	else
	{
		//error

	}
	
//Store the alpha value back into the color

currentColor = (currentColor&0x00000000)|(dwAlpha<<24);


//This is the D3D equivalent to DDraw's flip surface command

hr=blackBoxBuffer->Lock(0,0, &vb_vertices,0);   
memcpy(vb_vertices, BlackBoxVertices, sizeof(T_VERTEX)*4 );  
blackBoxBuffer->Unlock();

return 0;

}

//If we are where we should be

if (currentColor == 0x00000000)
{
        //Reset the color and return

        //This is causing a problem here

	currentColor = 0xFF000000;
	dwAlpha = 0xFF;
	return 1;
}

else
	return 0;


}//end FadeFromBlack()

      
Edited by - The Pale Hunter on February 19, 2002 9:06:55 PM Edited by - The Pale Hunter on February 19, 2002 9:08:08 PM
Advertisement
A lot of code duplicated!

I suggest that you should make only one Fade function with an extra bool parameter: bBlackFrom.

Starting from the existing one that works will make things simpler.



Laurent - http://www.lafaqmfc.com/
My little game: http://www.lafaqmfc.com/starshooter.htm
But that is just it...I don''t know why Fade2Black() works and FadeFromBlack() does not. When I put them both into the same function, then the portion that brings the screen back from black does the same odd thing that FadeFromBlack() does.
Perhaps if you didn''t do the string=="fast" thing with character pointers. They don''t have a clue as to what''s going on there with that. Use stricmp or one of the other string compare routines.
Quickly scanning the code I see that the following line does nothing:

static DWORD dwAlpha = (currentColor>>24)&0x00;

ANDing with 0 equals 0 no matter what.

Same here:
currentColor = (currentColor&0x00000000)|(dwAlpha<<24);

Use:
currentColor = (dwAlpha & 0xFF) << 24;
or you can trash the AND operations since the box is always black and just stick with the alpha component. Use the D3DCOLOR_RGBA macro for ease:

currentColor = D3DCOLOR_RGBA(0,0,0,dwAlpha);

I would also suggest wrapping the function into one, specifying a flag to determine direction.

This topic is closed to new replies.

Advertisement