Jump to content
  • Advertisement
Sign in to follow this  
ahayweh

2D Water effect woes...

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, Has anyone ever coded a successful 2D water effect ala :
Ooooo... Pretty Water
Right now, after much ( to much ) effort, my looks like:
Free Image Hosting at www.ImageShack.us
The left side is the background texture ( attempted to be ) modulated by the water calculations. The right is the actual modulating water. The problem w/the tutorials, is that none of them actual show what the modulating water is supposed to look like. Cause mine looks like it is lacking "smoothness". Here is the offending code:
/*
==========================================================================
LVL_UnderWater_CalcNewWater

Calcuates water.
==========================================================================
*/
void LVL_UnderWater_CalcNewWater( void )
{
	static int xLimit = (currentGame.gfxConfig.xDimen/2 - 1);
	static int yLimit = (currentGame.gfxConfig.yDimen - 1);
	static int i=0;
	int average=0;
	
	
	for( int y=1; y<yLimit; y++ )
	{
		for( int x=1;x<xLimit;x++ )
		{
			average = 
                        ( (_getpixel16( oldWaterBuffer , x + 1 , y ) +
 			   _getpixel16( oldWaterBuffer , x - 1 , y ) +
			   _getpixel16( oldWaterBuffer , x , y + 1  ) +
			   _getpixel16( oldWaterBuffer , x , y - 1 )  + 
						 
			   _getpixel16( oldWaterBuffer , x - 1 , y + 1 )  + 
			   _getpixel16( oldWaterBuffer , x - 1 , y - 1 )  + 
			   _getpixel16( oldWaterBuffer , x + 1 , y + 1 )  + 
			   _getpixel16( oldWaterBuffer , x + 1 , y - 1 )  ) >> 2 ) -						  
						 
			   _getpixel16( newWaterBuffer , x , y );
			
			average -= _getpixel16( newWaterBuffer , x , y )>>5;

			if( average < 0 )
			{
				average = 0;
			}
			if( average > 255 )
			{
				average = 255-average;
				average %= average;
			}


			_putpixel16( newWaterBuffer , x , y , average  );
		}

	}

	i++;
}
/*
===========================================================================
LVL_UnderWater_PaintWater

Simulated dynamic water in offscreen buffer. 
===========================================================================
*/
void LVL_UnderWater_PaintWater( float dampening )
{
	int deltaX=0,deltaY=0,indexX=0,indexY=0,color=0,texture=0;

	for( int y = 87;y < 394;y++ )
	{
		for( int x = 12;x < 304;x++ )
		{
			deltaX = _getpixel16( newWaterBuffer , x , y ) - _getpixel( newWaterBuffer , x + 1 , y );
			deltaY = _getpixel16( newWaterBuffer , x , y ) - _getpixel( newWaterBuffer , x , y + 1 );

			//deltaX >>= 3;
			//deltaY >>= 3;

			indexX = deltaX + x;
			indexY = deltaY + y;

			color = indexX;
			texture = _getpixel16( waterLevel , x , y );
			
			if( color < 0 )
			{
				color = 1;
			}
			if( color > 255 )
			{
				color = 254;
			}
	
			_putpixel16( pageTwo , x , y , color + texture );

			
			_putpixel16( pageThree , x , y , _getpixel16(newWaterBuffer , x ,y ) );
			

			// save rendered water for next frame
			_putpixel16( oldWaterBuffer , x , y , _getpixel16( newWaterBuffer , x , y ) );
			
		}
	}

}

Please excuse the code. After a weekends worth of work, it has degenerated to a hacked-compile-n-hopes-it-works-this-time coding terror. Thanks a lot, ahayweh

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!